diff --git a/include/cyclone/types.h b/include/cyclone/types.h index f2c7a814..2b3eb176 100644 --- a/include/cyclone/types.h +++ b/include/cyclone/types.h @@ -851,7 +851,7 @@ typedef struct { // TODO: digits (implicit, after object??) } bignum2_type; -#define C_bignum_digits(n) ((((bignum2_type *)n)->sign) + 1) +#define C_bignum_digits(n) (&(((bignum2_type *)n)->sign) + 1) // TODO: covert applicable definitions below - // #ifdef C_SIXTY_FOUR diff --git a/runtime.c b/runtime.c index 4b61360e..314db984 100644 --- a/runtime.c +++ b/runtime.c @@ -2427,7 +2427,7 @@ int bignum2_num_digits(bignum2_type *bn, int radix) int len = bn->num_digits - 1; //C_bignum_size(num)-1; int nbits = (size_t)len * 32; //C_BIGNUM_DIGIT_LENGTH; - nbits += nlz(C_bignum_digits(bn)+len); // TODO: ? + nbits += nlz(C_bignum_digits(bn)[len]); // TODO: ? len = nlz(radix)-1; len = (nbits + len - 1) / len; @@ -2463,8 +2463,24 @@ static uint32_t bignum_digits_destructive_scale_down(uint32_t *start, uint32_t * void bignum2string(void *data, object cont, bignum2_type *bn, int radix) { static char *characters = "0123456789abcdef"; - // buf int negp = bn->sign, radix_shift = nlz(radix) - 1; + int str_length = bignum2_num_digits(bn, radix); + int heap_grown; + string_type s = gc_alloc(((gc_thread_data *)data)->heap, + sizeof(string_type) + str_length + 1, + boolean_f, // OK to populate manually over here + (gc_thread_data *)data, + &heap_grown); + ((string_type *) s)->hdr.mark = ((gc_thread_data *)data)->gc_alloc_color; + ((string_type *) s)->hdr.grayed = 0; + ((string_type *) s)->hdr.immutable = 0; + ((string_type *) s)->tag = string_tag; + ((string_type *) s)->str_length = str_length; + ((string_type *) s)->num_cp = str_length; + ((string_type *) s)->str = (((char *)s) + sizeof(string_type)); + char *buf = string_str(s), + *index = buf; TODO: should be at other end of buf, I think?? + printf("DEBUG string length %d\n", bignum2_num_digits(bn, radix)); printf("DEBUG radix=%d, nlz = %d\n", radix, radix_shift); printf(" DEBUG power of 2 %d\n", ((uint32_t)1 << radix_shift)); @@ -2524,6 +2540,8 @@ void bignum2string(void *data, object cont, bignum2_type *bn, int radix) // } } // TODO: call into cont with string + printf("string is %s\n", string_str(s)); + return_closcall1(data, k, s); } object Cyc_symbol2string(void *data, object cont, object sym) diff --git a/test-bn.scm b/test-bn.scm index 96387bbc..6c023692 100644 --- a/test-bn.scm +++ b/test-bn.scm @@ -1,12 +1,13 @@ -; /sync.sh runtime.c gc.c include/cyclone/*.h test-bn.scm && cd ../cyclone-bootstrap && rm -f cyclone libcyclone.a ; make cyclone && ./cyclone -L. -I. test-bn.scm && ./test-bn && cd ../cyclone +; ./sync.sh runtime.c gc.c include/cyclone/*.h test-bn.scm && cd ../cyclone-bootstrap && rm -f cyclone libcyclone.a ; ./install.sh && ./cyclone -L. -I. test-bn.scm && ./test-bn && cd ../cyclone (import (scheme base) (scheme write) (scheme repl)) (define-c test-bn "(void *data, int argc, closure _, object k, object fx)" " object bn = Cyc_int2bignum2(data, obj_obj2int(fx)); bignum2string(data, k, bn, 10); - return_closcall1(data, k, bn); ") + ") (test-bn 10) -(test-bn 16) +(test-bn 163264) +(test-bn 16326) ;(repl)