Changed from using bit-shifts to using arithmetic operators. More tests fail, but...

This commit is contained in:
Simon Brooke 2025-03-16 09:38:00 +00:00
parent bef9be4914
commit 7c84cb433a
3 changed files with 10 additions and 8 deletions

View file

@ -1,6 +1,6 @@
(set! fact (set! fact
(lambda (n) (lambda (n)
"Compute the factorial of `n`, expected to be an integer." "Compute the factorial of `n`, expected to be a natural number."
(cond ((= n 1) 1) (cond ((= n 1) 1)
(t (* n (fact (- n 1))))))) (t (* n (fact (- n 1)))))))

View file

@ -71,7 +71,7 @@ struct cons_pointer make_integer( int64_t value, struct cons_pointer more ) {
__int128_t cell_value( struct cons_pointer c, char op, bool is_first_cell ) { __int128_t cell_value( struct cons_pointer c, char op, bool is_first_cell ) {
long int val = nilp( c ) ? 0 : pointer2cell( c ).payload.integer.value; long int val = nilp( c ) ? 0 : pointer2cell( c ).payload.integer.value;
long int carry = is_first_cell ? 0 : ( MAX_INTEGER + 1 ); long int carry = is_first_cell ? 0 : ( INT_CELL_BASE );
__int128_t result = ( __int128_t ) integerp( c ) ? __int128_t result = ( __int128_t ) integerp( c ) ?
( val == 0 ) ? carry : val : op == '*' ? 1 : 0; ( val == 0 ) ? carry : val : op == '*' ? 1 : 0;
@ -106,15 +106,15 @@ __int128_t int128_to_integer( __int128_t val,
if ( MAX_INTEGER >= val ) { if ( MAX_INTEGER >= val ) {
carry = 0; carry = 0;
} else { } else {
carry = val >> INTEGER_BIT_SHIFT; carry = val % INT_CELL_BASE;
debug_printf( DEBUG_ARITH, debug_printf( DEBUG_ARITH,
L"int128_to_integer: 64 bit overflow; setting carry to %ld\n", L"int128_to_integer: 64 bit overflow; setting carry to %ld\n",
( int64_t ) carry ); ( int64_t ) carry );
val &= MAX_INTEGER; val /= INT_CELL_BASE;
} }
struct cons_space_object *newc = &pointer2cell( new ); struct cons_space_object *newc = &pointer2cell( new );
newc->payload.integer.value = val; newc->payload.integer.value = (int64_t)val;
if ( integerp( less_significant ) ) { if ( integerp( less_significant ) ) {
struct cons_space_object *lsc = &pointer2cell( less_significant ); struct cons_space_object *lsc = &pointer2cell( less_significant );
@ -136,7 +136,7 @@ struct cons_pointer make_integer_128( __int128_t val,
less_significant = less_significant =
make_integer( ( long int ) val & MAX_INTEGER, make_integer( ( long int ) val & MAX_INTEGER,
less_significant ); less_significant );
val = val >> INTEGER_BIT_SHIFT; val = val * INT_CELL_BASE;
} }
} while ( nilp( result ) ); } while ( nilp( result ) );
@ -361,7 +361,7 @@ struct cons_pointer integer_to_string( struct cons_pointer int_pointer,
while ( accumulator > 0 || !nilp( next ) ) { while ( accumulator > 0 || !nilp( next ) ) {
if ( accumulator < MAX_INTEGER && !nilp( next ) ) { if ( accumulator < MAX_INTEGER && !nilp( next ) ) {
accumulator += accumulator +=
( pointer2cell( next ).payload.integer.value << INTEGER_BIT_SHIFT ); ( pointer2cell( next ).payload.integer.value % INT_CELL_BASE );
next = pointer2cell( next ).payload.integer.more; next = pointer2cell( next ).payload.integer.more;
} }
int offset = ( int ) ( accumulator % base ); int offset = ( int ) ( accumulator % base );

View file

@ -22,6 +22,8 @@
* So left shifting and right shifting by 60 bits is correct. * So left shifting and right shifting by 60 bits is correct.
*/ */
#define MAX_INTEGER ((__int128_t)0x0fffffffffffffffL) #define MAX_INTEGER ((__int128_t)0x0fffffffffffffffL)
#define INT_CELL_BASE ((__int128_t)MAX_INTEGER + 1) // ((__int128_t)0x1000000000000000L)
/** /**
* @brief Number of value bits in an integer cell * @brief Number of value bits in an integer cell
* *