Bug fixes and cleanup

This commit is contained in:
Justin Ethier 2022-06-07 21:29:40 -04:00
parent 524d1b12a8
commit 0a366a8aca
2 changed files with 20 additions and 17 deletions

View file

@ -1782,10 +1782,11 @@ object Cyc_bignum2(bignum2_type *bn, int sign, int n)
object Cyc_int2bignum2(gc_thread_data *data, int n) object Cyc_int2bignum2(gc_thread_data *data, int n)
{ {
bignum2_type *bn = gc_alloc_bignum2(data, n);
if (n < 0) { if (n < 0) {
bignum2_type *bn = gc_alloc_bignum2(data, 1);
return Cyc_bignum2(bn, 1, -n); return Cyc_bignum2(bn, 1, -n);
} else { } else {
bignum2_type *bn = gc_alloc_bignum2(data, 1);
return Cyc_bignum2(bn, 0, n); 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 negp = bn->sign, radix_shift = nlz(radix) - 1;
int str_length = bignum2_num_digits(bn, radix); int str_length = bignum2_num_digits(bn, radix);
int heap_grown; 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, string_type *s = gc_alloc(((gc_thread_data *)data)->heap,
sizeof(string_type) + str_length + 1, sizeof(string_type) + str_length + 1,
boolean_f, // OK to populate manually over here 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), char *buf = string_str(s),
*index = buf + str_length - 1; *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? */ if (((uint32_t)1 << radix_shift) == radix) { /* Power of two? */
uint32_t *scan, *end; uint32_t *scan, *end;
printf("radix power of two\n"); 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 */ 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 && index >= buf; ++i) {
//for(i = 0; i < steps ; ++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 */
@ -2532,19 +2533,13 @@ void bignum2string(void *data, object cont, bignum2_type *bn, int radix)
if (negp) *--index = '-'; if (negp) *--index = '-';
/* Shorten with distance between start and index. */ /* Shorten with distance between start and index. */
// TODO:
if (buf != index) { if (buf != index) {
printf("TODO buf != index\n");
i = str_length - (index - buf); i = str_length - (index - buf);
s->len = s->num_cp = i; s->len = s->num_cp = i;
memmove(buf, index, i); memmove(buf, index, i);
// i = C_header_size(string) - (index - buf); buf[str_length-1] = '\0';
// C_memmove(buf, index, i); /* Move start of number to beginning. */
// C_block_header(string) = C_STRING_TYPE | i; /* Mutate strlength. */
} }
} }
// TODO: call into cont with string
printf("string is %s\n", string_str(s));
return_closcall1(data, cont, s); return_closcall1(data, cont, s);
} }

View file

@ -2,12 +2,20 @@
(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)" "(void *data, int argc, closure _, object k, object fx, object radix)"
" object bn = Cyc_int2bignum2(data, obj_obj2int(fx)); " object bn = Cyc_int2bignum2(data, obj_obj2int(fx));
bignum2string(data, k, bn, 10); bignum2string(data, k, bn, obj_obj2int(radix));
") ")
(test-bn 10) (write
(test-bn 163264) (list
(test-bn 16326) (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) ;(repl)