Skip to content

Commit

Permalink
WIP - Cyc_sqrt
Browse files Browse the repository at this point in the history
Working on an alternate implementation that supports complex numbers.
  • Loading branch information
justinethier committed Mar 11, 2024
1 parent 37b3969 commit bcb0f86
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
2 changes: 2 additions & 0 deletions include/cyclone/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,8 @@ double round_to_nearest_even(double);
void Cyc_exact(void *data, object cont, object z);
object Cyc_exact_no_cps(void *data, object ptr, object z);

object Cyc_sqrt(void *data, object ptr, object z);

/**
* Take Scheme object that is a number and return the number as a C type
*/
Expand Down
27 changes: 27 additions & 0 deletions runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -8891,3 +8891,30 @@ object Cyc_exact_no_cps(void *data, object ptr, object z)
}
return obj_int2obj(i);
}

object Cyc_sqrt(void *data, object ptr, object z)
{
double d;
Cyc_check_num(data, z);
if (obj_is_int(z)) {
d = (obj_obj2int(z));
} else if (type_of(z) == integer_tag) {
d = (((integer_type *)z)->value);
} else if (type_of(z) == bignum_tag) {
d = (mp_get_double(&bignum_value(z)));
} else {
d = (((double_type *)z)->value);
}

if (d >= 0) {
d = sqrt(d);
assign_double(ptr, d);
} else {
double dreal = 0.0;
double dimag = sqrt(fabs(d));
double complex c = dreal + (dimag * I);
assign_complex_num(ptr, c);
}

return ptr;
}
9 changes: 7 additions & 2 deletions scheme/base.sld
Original file line number Diff line number Diff line change
Expand Up @@ -1442,9 +1442,14 @@
(values s r)))
(define-c sqrt
"(void *data, int argc, closure _, object k, object z)"
" return_inexact_double_op(data, k, sqrt, z);"
"
common_type buffer;
// TODO: can common type be brought forward into CPS as result like this? Need to double-check
object result = Cyc_sqrt(data, &buffer, z);
return_closcall1(data, k, result);
"
"(void *data, object ptr, object z)"
" return_inexact_double_op_no_cps(data, ptr, sqrt, z);")
" return Cyc_sqrt(data, ptr, z); ")
(define-c exact-integer?
"(void *data, int argc, closure _, object k, object num)"
" if (obj_is_int(num) || (num != NULL && !is_value_type(num) &&
Expand Down

0 comments on commit bcb0f86

Please sign in to comment.