Various refactorings around bignum arithmetic

This commit is contained in:
Simon Brooke 2019-01-17 17:04:14 +00:00
parent d624c671cd
commit 7f93b04b72
6 changed files with 134 additions and 85 deletions

View file

@ -36,7 +36,7 @@
/**
* hexadecimal digits for printing numbers.
*/
const wchar_t hex_digits[16] = L"0123456789ABCDEF";
const char * hex_digits = "0123456789ABCDEF";
/*
* Doctrine from here on in is that ALL integers are bignums, it's just
@ -133,13 +133,24 @@ struct cons_pointer operate_on_integers( struct cons_pointer a,
switch (op) {
case '*':
rv = ( av * bv ) + carry;
rv = av * bv * ((carry == 0) ? 1 : carry);
break;
case '+':
rv = av + bv + carry;
break;
}
debug_printf( DEBUG_ARITH, L"operate_on_integers: op = '%c'; av = ", op);
debug_print_128bit( av, DEBUG_ARITH);
debug_print( L"; bv = ", DEBUG_ARITH);
debug_print_128bit( bv, DEBUG_ARITH);
debug_print( L"; carry = ", DEBUG_ARITH);
debug_print_128bit( carry, DEBUG_ARITH);
debug_print( L"; rv = ", DEBUG_ARITH);
debug_print_128bit( rv, DEBUG_ARITH);
debug_print( L"\n", DEBUG_ARITH);
if ( MAX_INTEGER >= rv ) {
carry = 0;
} else {
@ -206,7 +217,7 @@ struct cons_pointer multiply_integers( struct cons_pointer a,
struct cons_pointer integer_to_string_add_digit( int digit, int digits,
struct cons_pointer tail ) {
digits++;
wint_t character = ( wint_t ) hex_digits[digit];
wint_t character = btowc(hex_digits[digit]);
return ( digits % 3 == 0 ) ?
make_string( L',', make_string( character,
tail ) ) :
@ -247,13 +258,14 @@ struct cons_pointer integer_to_string( struct cons_pointer int_pointer,
L"integer_to_string: accumulator is %ld\n:",
accumulator );
do {
int offset = (int)(accumulator % base);
debug_printf( DEBUG_IO,
L"integer_to_string: digit is %ld, hexadecimal is %C\n:",
accumulator % base,
btowc(hex_digits[accumulator % base] ));
L"integer_to_string: digit is %ld, hexadecimal is %c\n:",
offset,
hex_digits[offset] );
result =
integer_to_string_add_digit( accumulator % base, digits++,
integer_to_string_add_digit( offset, digits++,
result );
accumulator = accumulator / base;
} while ( accumulator > base );

View file

@ -41,7 +41,8 @@ bool zerop( struct cons_pointer arg ) {
switch ( cell.tag.value ) {
case INTEGERTV:
result = cell.payload.integer.value == 0;
result = cell.payload.integer.value == 0 &&
nilp(cell.payload.integer.more);
break;
case RATIOTV:
result = zerop( cell.payload.ratio.dividend );
@ -134,9 +135,9 @@ struct cons_pointer add_2( struct stack_frame *frame,
struct cons_space_object cell2 = pointer2cell( arg2 );
debug_print( L"add_2( arg1 = ", DEBUG_ARITH );
debug_print_object( arg1, DEBUG_ARITH );
debug_dump_object( arg1, DEBUG_ARITH );
debug_print( L"; arg2 = ", DEBUG_ARITH );
debug_print_object( arg2, DEBUG_ARITH );
debug_dump_object( arg2, DEBUG_ARITH );
debug_print( L"\n", DEBUG_ARITH );
if ( zerop( arg1 ) ) {