~ chicken-core (chicken-5) 33cea4e5fb2a44a25d868b90665859dee700f5b7


commit 33cea4e5fb2a44a25d868b90665859dee700f5b7
Author:     Peter Bex <peter@more-magic.net>
AuthorDate: Mon Jan 25 20:45:22 2016 +0100
Commit:     Peter Bex <peter@more-magic.net>
CommitDate: Mon Jan 25 20:45:22 2016 +0100

    Fix number->string "overlap" radix handling.
    
    For power of two radices which had a bit length that's not an exact
    multiple of the word size, this would sometimes result in leading zeroes
    or assertion failures (direct port of the corresponding fix in the
    CHICKEN 4 numbers egg).

diff --git a/runtime.c b/runtime.c
index c09f6c9e..bbd24524 100644
--- a/runtime.c
+++ b/runtime.c
@@ -11545,8 +11545,9 @@ static void bignum_to_str_2(C_word c, C_word *av)
 	radix_digit = big_digit;
         big_digit = *scan++;
 	radix_digit |= ((unsigned int)big_digit << big_digit_len) & radix_mask;
+        *index-- = characters[radix_digit];
 	big_digit >>= (radix_shift - big_digit_len);
-        big_digit_len = C_BIGNUM_DIGIT_LENGTH - big_digit_len;
+        big_digit_len = C_BIGNUM_DIGIT_LENGTH - (radix_shift - big_digit_len);
       }
 
       while(big_digit_len >= radix_shift && index >= buf) {
diff --git a/tests/numbers-test.scm b/tests/numbers-test.scm
index fb556295..dcee51fe 100644
--- a/tests/numbers-test.scm
+++ b/tests/numbers-test.scm
@@ -972,6 +972,12 @@
  (test-equal "edge case printing"
        (number->string (expt 2 256) 16)
        "10000000000000000000000000000000000000000000000000000000000000000")
+ (test-equal "non-exact multiple of 64 length edge case printing"
+       "4000000000000000000000" (number->string (expt 2 65) 8))
+ (test-equal "another non-exact multiple of 64 length edge case printing"
+       "200000000000000000000000" (number->string (expt 2 70) 8))
+ (test-equal "edge case length calculation"
+       "10000000000000000000000000000000000000000000000000000000000000000000000" (number->string (expt 2 210) 8))
  (test-equal "positive hexdigit invariance"
        (number->string
         (string->number "123456789abcdef123456789abcdef123456789abcdef" 16)
Trap