WIP - Cyc_sqrt

Working on an alternate implementation that supports complex numbers.
This commit is contained in:
Justin Ethier 2024-03-10 20:01:56 -07:00
parent 37b39693ed
commit bcb0f86e8b
3 changed files with 36 additions and 2 deletions

View file

@ -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
*/

View file

@ -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;
}

View file

@ -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) &&