Cleanup, add bignum2->str support to runtime

This commit is contained in:
Justin Ethier 2022-06-08 16:02:37 -04:00
parent eb78f50c66
commit 9472304576
3 changed files with 36 additions and 22 deletions

View file

@ -1604,7 +1604,7 @@ int Cyc_bignum_cmp(bn_cmp_type type, object x, int tx, object y, int ty);
void Cyc_int2bignum(int n, mp_int *bn); void Cyc_int2bignum(int n, mp_int *bn);
object Cyc_int2bignum2(gc_thread_data *data, int n); object Cyc_int2bignum2(gc_thread_data *data, int n);
// TODO: debug only, remove this function from here! // TODO: debug only, remove this function from here!
void bignum2string(void *data, object cont, bignum2_type *bn, int base); string_type *bignum2string(void *data, bignum2_type *bn, int base);
/* Remaining GC prototypes that require objects to be defined */ /* Remaining GC prototypes that require objects to be defined */
void *gc_alloc_from_bignum(gc_thread_data *data, bignum_type *src); void *gc_alloc_from_bignum(gc_thread_data *data, bignum_type *src);

View file

@ -1180,6 +1180,11 @@ object _Cyc_display(void *data, object x, FILE * port, int depth)
} }
fprintf(port, ")"); fprintf(port, ")");
break; break;
case bignum2_tag: {
string_type *s = bignum2string(data, x, 10);
return _Cyc_display(data, s, port, depth);
break;
}
case bignum_tag: { case bignum_tag: {
int bufsz; int bufsz;
char *buf; char *buf;
@ -1961,6 +1966,7 @@ object Cyc_is_number(object o)
if ((o != NULL) && (obj_is_int(o) || (!is_value_type(o) if ((o != NULL) && (obj_is_int(o) || (!is_value_type(o)
&& (type_of(o) == integer_tag && (type_of(o) == integer_tag
|| type_of(o) == bignum_tag || type_of(o) == bignum_tag
|| type_of(o) == bignum2_tag
|| type_of(o) == double_tag || type_of(o) == double_tag
|| type_of(o) == complex_num_tag)))) || type_of(o) == complex_num_tag))))
return boolean_t; return boolean_t;
@ -2350,6 +2356,9 @@ object Cyc_number2string2(void *data, object cont, int argc, object n, ...)
Cyc_rt_raise2(data, "number->string - bignum is too large to convert", n); Cyc_rt_raise2(data, "number->string - bignum is too large to convert", n);
} }
BIGNUM_CALL(mp_to_radix(&bignum_value(n), buffer, 1024, &written, base_num)); BIGNUM_CALL(mp_to_radix(&bignum_value(n), buffer, 1024, &written, base_num));
} else if (is_object_type(n) && type_of(n) == bignum2_tag) {
string_type *s = bignum2string(data, n, base_num);
_return_closcall1(data, cont, s);
} else { } else {
if (base_num == 2) { if (base_num == 2) {
val = obj_is_int(n) ? val = obj_is_int(n) ?
@ -2474,7 +2483,7 @@ inline static void bignum_digits_destructive_copy(bignum2_type *target, bignum2_
} }
//TODO: static //TODO: static
void bignum2string(void *data, object cont, bignum2_type *bn, int radix) string_type *bignum2string(void *data, bignum2_type *bn, int radix)
{ {
static char *characters = "0123456789abcdef"; static char *characters = "0123456789abcdef";
int negp = bn->sign, radix_shift = nlz(radix) - 1; int negp = bn->sign, radix_shift = nlz(radix) - 1;
@ -2565,7 +2574,7 @@ void bignum2string(void *data, object cont, bignum2_type *bn, int radix)
for(i = 0; i < steps && index >= buf; ++i) { for(i = 0; i < steps && index >= buf; ++i) {
uint32_t tmp = big_digit / radix; uint32_t tmp = big_digit / radix;
printf("%c", characters[big_digit - (tmp*radix)]); //printf("%c", characters[big_digit - (tmp*radix)]);
*index-- = characters[big_digit - (tmp*radix)]; /* big_digit % radix */ *index-- = characters[big_digit - (tmp*radix)]; /* big_digit % radix */
big_digit = tmp; big_digit = tmp;
} }
@ -2587,7 +2596,7 @@ void bignum2string(void *data, object cont, bignum2_type *bn, int radix)
s->str[i] = '\0'; s->str[i] = '\0';
} }
} }
return_closcall1(data, cont, s); return s;
} }
object Cyc_symbol2string(void *data, object cont, object sym) object Cyc_symbol2string(void *data, object cont, object sym)

View file

@ -2,41 +2,46 @@
(import (scheme base) (scheme write) (scheme repl)) (import (scheme base) (scheme write) (scheme repl))
(define-c test-bn (define-c test-bn
"(void *data, int argc, closure _, object k, object fx, object radix)" "(void *data, int argc, closure _, object k, object fx)"
" object bn = Cyc_int2bignum2(data, obj_obj2int(fx)); " object bn = Cyc_int2bignum2(data, obj_obj2int(fx));
bignum2string(data, k, bn, obj_obj2int(radix)); return_closcall1(data, k, bn);
") ")
(define-c test-larger-bn (define-c test-larger-bn
"(void *data, int argc, closure _, object k, object fx, object fx2, object radix)" "(void *data, int argc, closure _, object k, object fx, object fx2)"
" bignum2_type *bn = gc_alloc_bignum2(data, 2); " bignum2_type *bn = gc_alloc_bignum2(data, 2);
C_bignum_digits(bn)[0] = obj_obj2int(fx2); C_bignum_digits(bn)[0] = obj_obj2int(fx2);
C_bignum_digits(bn)[1] = obj_obj2int(fx); C_bignum_digits(bn)[1] = obj_obj2int(fx);
bn->num_digits = 2; bn->num_digits = 2;
bn->sign = 0; bn->sign = 0;
bignum2string(data, k, bn, obj_obj2int(radix)); return_closcall1(data, k, bn);
") ")
(write (test-bn 123456789))
(newline)
(map (map
(lambda (row) (lambda (row)
(write row) (write row)
(newline)) (newline))
(list (list
(test-larger-bn 0 #x0FFF0001 10) (test-bn 123456789 )
(test-bn #x0FFF0001 10) (test-bn 123456789 )
(test-larger-bn 0 #x0FFF0001 )
(test-bn #x0FFF0001 )
(test-bn -10 10) (test-bn -10 )
(test-bn 163264 10) (test-bn 163264 )
(test-bn 16326 10) (test-bn 16326 )
(test-bn -16326000 10) (test-bn -16326000 )
(test-bn #x0FFFffff 10) (number->string (test-bn #x0FFFffff) 10)
(test-bn #x0FFFffff 16) (number->string (test-bn #x0FFFffff) 16)
(test-bn #x0eadbeef 16) (number->string (test-bn #x0eadbeef) 16)
(test-bn #x0eadbeef 12) (number->string (test-bn #x0eadbeef) 12)
(test-bn #x0eadbeef 8) (test-bn #x0eadbeef)
(test-bn #x0eadbeef 2) (test-bn #x0eadbeef)
(test-bn #x3FFFffff 10) (test-bn #x3FFFffff)
(test-larger-bn #x3FFF0000 #x0FFF0001 10) (test-larger-bn #x3FFF0000 #x0FFF0001 )
)) ))
(newline) (newline)
;(repl) ;(repl)