diff --git a/runtime.c b/runtime.c index 1e752d48..168a276a 100644 --- a/runtime.c +++ b/runtime.c @@ -2464,21 +2464,21 @@ object C_bignum_simplify(object big) switch(length) { case 0: - //if (C_in_scratchspacep(C_internal_bignum_vector(big))) - // C_mutate_scratch_slot(NULL, C_internal_bignum_vector(big)); return obj_int2obj(0); case 1: tmp = *start; - if (C_bignum_negativep(big) ? - !(tmp & C_INT_SIGN_BIT) && C_fitsinfixnump(-(C_word)tmp) : - C_ufitsinfixnump(tmp)) { - if (C_in_scratchspacep(C_internal_bignum_vector(big))) - C_mutate_scratch_slot(NULL, C_internal_bignum_vector(big)); - return C_bignum_negativep(big) ? C_fix(-(C_word)tmp) : C_fix(tmp); + if (((uint32_t)tmp) <= CYC_FIXNUM_MAX) { + if (C_bignum_negativep(big)) { + return obj_int2obj(-(int32_t)tmp); + } else { + return obj_int2obj(tmp); + } } /* FALLTHROUGH */ default: - if (scan < last_digit) C_bignum_mutate_size(big, length); + if (scan < last_digit) { + C_bignum_size(big) = length; + } return big; } } @@ -2761,8 +2761,7 @@ object bignum2_plus_unsigned(void *data, bignum2_type *x, bignum2_type *y, int n } assert(scan_r <= end_r); - // TODO: return C_bignum_simplify(result); - return result; // TODO: no, could be a fixnum. need to simplify using above! + return C_bignum_simplify(result); } object Cyc_symbol2string(void *data, object cont, object sym) diff --git a/test-bn.scm b/test-bn.scm index caf2e4e0..183bfacd 100644 --- a/test-bn.scm +++ b/test-bn.scm @@ -6,6 +6,11 @@ " object bn1 = Cyc_int2bignum2(data, obj_obj2int(fx1)); object bn2 = Cyc_int2bignum2(data, obj_obj2int(fx2)); object result = bignum2_plus_unsigned(data, bn1, bn2, 0); // TODO: int negp); +if(is_value_type(result)) { + printf(\"fixnum result\\n\"); +} else if (type_of(result) == bignum2_tag) { + printf(\"bignum result\\n\"); +} return_closcall1(data, k, result); ") (define-c test-bn @@ -32,10 +37,11 @@ (write row) (newline)) (list + (test-plus #x0FFFffff #x0FFFffff) + (test-plus #x2FFFffff #x2FFFffff) (test-plus 1 2) ;; TODO: (fixnum? (test-plus 1 2)) (test-plus -1 2) - (test-plus #x0FFFffff #x0FFFffff) (test-bn 123456789 ) (test-bn 123456789 ) (test-larger-bn 0 #x0FFF0001 )