diff --git a/runtime.c b/runtime.c index c7d417ac..0a93ead5 100644 --- a/runtime.c +++ b/runtime.c @@ -1782,10 +1782,11 @@ object Cyc_bignum2(bignum2_type *bn, int sign, int n) object Cyc_int2bignum2(gc_thread_data *data, int n) { - bignum2_type *bn = gc_alloc_bignum2(data, n); if (n < 0) { + bignum2_type *bn = gc_alloc_bignum2(data, 1); return Cyc_bignum2(bn, 1, -n); } else { + bignum2_type *bn = gc_alloc_bignum2(data, 1); return Cyc_bignum2(bn, 0, n); } } @@ -2467,6 +2468,10 @@ void bignum2string(void *data, object cont, bignum2_type *bn, int radix) int negp = bn->sign, radix_shift = nlz(radix) - 1; int str_length = bignum2_num_digits(bn, radix); int heap_grown; + + //printf("DEBUG string length %d\n", bignum2_num_digits(bn, radix)); + //printf("DEBUG radix=%d, nlz = %d\n", radix, radix_shift); + string_type *s = gc_alloc(((gc_thread_data *)data)->heap, sizeof(string_type) + str_length + 1, boolean_f, // OK to populate manually over here @@ -2482,9 +2487,6 @@ void bignum2string(void *data, object cont, bignum2_type *bn, int radix) char *buf = string_str(s), *index = buf + str_length - 1; - 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)); if (((uint32_t)1 << radix_shift) == radix) { /* Power of two? */ uint32_t *scan, *end; printf("radix power of two\n"); @@ -2515,7 +2517,6 @@ void bignum2string(void *data, object cont, bignum2_type *bn, int radix) if (*(scan-1) == 0) scan--; /* Adjust if we exhausted the highest digit */ for(i = 0; i < steps && index >= buf; ++i) { - //for(i = 0; i < steps ; ++i) { uint32_t tmp = big_digit / radix; printf("%c", characters[big_digit - (tmp*radix)]); *index-- = characters[big_digit - (tmp*radix)]; /* big_digit % radix */ @@ -2532,19 +2533,13 @@ void bignum2string(void *data, object cont, bignum2_type *bn, int radix) if (negp) *--index = '-'; /* Shorten with distance between start and index. */ -// TODO: if (buf != index) { - printf("TODO buf != index\n"); i = str_length - (index - buf); s->len = s->num_cp = i; memmove(buf, index, i); -// i = C_header_size(string) - (index - buf); -// C_memmove(buf, index, i); /* Move start of number to beginning. */ -// C_block_header(string) = C_STRING_TYPE | i; /* Mutate strlength. */ + buf[str_length-1] = '\0'; } } - // TODO: call into cont with string - printf("string is %s\n", string_str(s)); return_closcall1(data, cont, s); } diff --git a/test-bn.scm b/test-bn.scm index 6c023692..c32c1fc9 100644 --- a/test-bn.scm +++ b/test-bn.scm @@ -2,12 +2,20 @@ (import (scheme base) (scheme write) (scheme repl)) (define-c test-bn - "(void *data, int argc, closure _, object k, object fx)" + "(void *data, int argc, closure _, object k, object fx, object radix)" " object bn = Cyc_int2bignum2(data, obj_obj2int(fx)); - bignum2string(data, k, bn, 10); + bignum2string(data, k, bn, obj_obj2int(radix)); ") -(test-bn 10) -(test-bn 163264) -(test-bn 16326) +(write + (list + (test-bn -10 10) + (test-bn 163264 10) + (test-bn 16326 10) + ;(test-bn -16326000 10) + (test-bn #x0FFFffff 10) + (test-bn #x0FFFffff 16) + (test-bn #x3FFFffff 10) + )) +(newline) ;(repl)