Restandardised formatting.
This commit is contained in:
parent
93d4bd14a0
commit
b0a49fb71d
29 changed files with 1861 additions and 1604 deletions
|
|
@ -218,18 +218,19 @@ struct cons_pointer base_partial( int depth ) {
|
|||
/**
|
||||
* destructively modify this `partial` by appending this `digit`.
|
||||
*/
|
||||
struct cons_pointer append_digit( struct cons_pointer partial, struct cons_pointer digit) {
|
||||
struct cons_pointer append_digit( struct cons_pointer partial,
|
||||
struct cons_pointer digit ) {
|
||||
struct cons_pointer c = partial;
|
||||
struct cons_pointer result = partial;
|
||||
|
||||
if (nilp( partial)) {
|
||||
if ( nilp( partial ) ) {
|
||||
result = digit;
|
||||
} else {
|
||||
while ( !nilp( pointer2cell(c).payload.integer.more)) {
|
||||
c = pointer2cell(c).payload.integer.more;
|
||||
while ( !nilp( pointer2cell( c ).payload.integer.more ) ) {
|
||||
c = pointer2cell( c ).payload.integer.more;
|
||||
}
|
||||
|
||||
(&pointer2cell(c))->payload.integer.more = digit;
|
||||
( &pointer2cell( c ) )->payload.integer.more = digit;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
@ -248,8 +249,8 @@ struct cons_pointer append_digit( struct cons_pointer partial, struct cons_point
|
|||
* @param b an integer.
|
||||
*/
|
||||
struct cons_pointer multiply_integers( struct cons_pointer a,
|
||||
struct cons_pointer b ) {
|
||||
struct cons_pointer result = make_integer( 0, NIL);
|
||||
struct cons_pointer b ) {
|
||||
struct cons_pointer result = make_integer( 0, NIL );
|
||||
bool neg = is_negative( a ) != is_negative( b );
|
||||
bool is_first_b = true;
|
||||
int i = 0;
|
||||
|
|
@ -264,7 +265,7 @@ struct cons_pointer multiply_integers( struct cons_pointer a,
|
|||
/* for each digit in a, starting with the least significant (ai) */
|
||||
|
||||
for ( struct cons_pointer ai = a; !nilp( ai );
|
||||
ai = pointer2cell(ai).payload.integer.more) {
|
||||
ai = pointer2cell( ai ).payload.integer.more ) {
|
||||
/* set carry to 0 */
|
||||
__int128_t carry = 0;
|
||||
|
||||
|
|
@ -274,41 +275,41 @@ struct cons_pointer multiply_integers( struct cons_pointer a,
|
|||
|
||||
/* for each digit in b, starting with the least significant (bj) */
|
||||
for ( struct cons_pointer bj = b; !nilp( bj );
|
||||
bj = pointer2cell(bj).payload.integer.more) {
|
||||
bj = pointer2cell( bj ).payload.integer.more ) {
|
||||
|
||||
debug_printf( DEBUG_ARITH,
|
||||
L"multiply_integers: a[i] = %Ld, b[j] = %Ld, i = %d\n",
|
||||
pointer2cell(ai).payload.integer.value,
|
||||
pointer2cell(bj).payload.integer.value, i);
|
||||
L"multiply_integers: a[i] = %Ld, b[j] = %Ld, i = %d\n",
|
||||
pointer2cell( ai ).payload.integer.value,
|
||||
pointer2cell( bj ).payload.integer.value, i );
|
||||
|
||||
/* multiply ai with bj and add the carry, resulting in a
|
||||
* value xj which may exceed one digit */
|
||||
__int128_t xj = pointer2cell(ai).payload.integer.value *
|
||||
pointer2cell(bj).payload.integer.value;
|
||||
__int128_t xj = pointer2cell( ai ).payload.integer.value *
|
||||
pointer2cell( bj ).payload.integer.value;
|
||||
xj += carry;
|
||||
|
||||
/* if xj exceeds one digit, break it into the digit dj and
|
||||
* the carry */
|
||||
carry = xj >> 60;
|
||||
struct cons_pointer dj = make_integer( xj & MAX_INTEGER, NIL);
|
||||
struct cons_pointer dj = make_integer( xj & MAX_INTEGER, NIL );
|
||||
|
||||
/* destructively modify ri by appending dj */
|
||||
ri = append_digit( ri, dj);
|
||||
} /* end for bj */
|
||||
ri = append_digit( ri, dj );
|
||||
} /* end for bj */
|
||||
|
||||
/* if carry is not equal to zero, append it as a final digit
|
||||
* to ri */
|
||||
if (carry != 0) {
|
||||
ri = append_digit( ri, make_integer( carry, NIL));
|
||||
if ( carry != 0 ) {
|
||||
ri = append_digit( ri, make_integer( carry, NIL ) );
|
||||
}
|
||||
|
||||
/* add ri to result */
|
||||
result = add_integers( result, ri);
|
||||
result = add_integers( result, ri );
|
||||
|
||||
debug_print( L"multiply_integers: result is ", DEBUG_ARITH );
|
||||
debug_print_object( result, DEBUG_ARITH );
|
||||
debug_println( DEBUG_ARITH );
|
||||
} /* end for ai */
|
||||
} /* end for ai */
|
||||
}
|
||||
|
||||
debug_print( L"multiply_integers returning: ", DEBUG_ARITH );
|
||||
|
|
@ -342,13 +343,16 @@ struct cons_pointer integer_to_string_add_digit( int digit, int digits,
|
|||
* to be looking to the next. H'mmmm.
|
||||
*/
|
||||
struct cons_pointer integer_to_string( struct cons_pointer int_pointer,
|
||||
int base ) {
|
||||
int base ) {
|
||||
struct cons_pointer result = NIL;
|
||||
|
||||
if ( integerp( int_pointer ) ) {
|
||||
struct cons_pointer next = pointer2cell( int_pointer ).payload.integer.more;
|
||||
__int128_t accumulator = llabs( pointer2cell( int_pointer ).payload.integer.value );
|
||||
bool is_negative = pointer2cell( int_pointer ).payload.integer.value < 0;
|
||||
struct cons_pointer next =
|
||||
pointer2cell( int_pointer ).payload.integer.more;
|
||||
__int128_t accumulator =
|
||||
llabs( pointer2cell( int_pointer ).payload.integer.value );
|
||||
bool is_negative =
|
||||
pointer2cell( int_pointer ).payload.integer.value < 0;
|
||||
int digits = 0;
|
||||
|
||||
if ( accumulator == 0 && nilp( next ) ) {
|
||||
|
|
@ -356,13 +360,14 @@ struct cons_pointer integer_to_string( struct cons_pointer int_pointer,
|
|||
} else {
|
||||
while ( accumulator > 0 || !nilp( next ) ) {
|
||||
if ( accumulator < MAX_INTEGER && !nilp( next ) ) {
|
||||
accumulator += (pointer2cell(next).payload.integer.value << 60);
|
||||
next = pointer2cell(next).payload.integer.more;
|
||||
accumulator +=
|
||||
( pointer2cell( next ).payload.integer.value << 60 );
|
||||
next = pointer2cell( next ).payload.integer.more;
|
||||
}
|
||||
int offset = ( int ) ( accumulator % base );
|
||||
debug_printf( DEBUG_IO,
|
||||
L"integer_to_string: digit is %ld, hexadecimal is %c, accumulator is: ",
|
||||
offset, hex_digits[offset] );
|
||||
L"integer_to_string: digit is %ld, hexadecimal is %c, accumulator is: ",
|
||||
offset, hex_digits[offset] );
|
||||
debug_print_128bit( accumulator, DEBUG_IO );
|
||||
debug_print( L"; result is: ", DEBUG_IO );
|
||||
debug_print_object( result, DEBUG_IO );
|
||||
|
|
@ -374,7 +379,7 @@ struct cons_pointer integer_to_string( struct cons_pointer int_pointer,
|
|||
}
|
||||
|
||||
if ( stringp( result )
|
||||
&& pointer2cell( result ).payload.string.character == L',' ) {
|
||||
&& pointer2cell( result ).payload.string.character == L',' ) {
|
||||
/* if the number of digits in the string is divisible by 3, there will be
|
||||
* an unwanted comma on the front. */
|
||||
result = pointer2cell( result ).payload.string.cdr;
|
||||
|
|
@ -393,14 +398,15 @@ struct cons_pointer integer_to_string( struct cons_pointer int_pointer,
|
|||
/**
|
||||
* true if a and be are both integers whose value is the same value.
|
||||
*/
|
||||
bool equal_integer_integer(struct cons_pointer a, struct cons_pointer b) {
|
||||
bool equal_integer_integer( struct cons_pointer a, struct cons_pointer b ) {
|
||||
bool result = false;
|
||||
|
||||
if (integerp(a) && integerp(b)){
|
||||
if ( integerp( a ) && integerp( b ) ) {
|
||||
struct cons_space_object *cell_a = &pointer2cell( a );
|
||||
struct cons_space_object *cell_b = &pointer2cell( b );
|
||||
|
||||
result = cell_a->payload.integer.value == cell_b->payload.integer.value;
|
||||
result =
|
||||
cell_a->payload.integer.value == cell_b->payload.integer.value;
|
||||
}
|
||||
|
||||
return result;
|
||||
|
|
@ -410,17 +416,16 @@ bool equal_integer_integer(struct cons_pointer a, struct cons_pointer b) {
|
|||
* true if `a` is an integer, and `b` is a real number whose value is the
|
||||
* value of that integer.
|
||||
*/
|
||||
bool equal_integer_real(struct cons_pointer a, struct cons_pointer b) {
|
||||
bool equal_integer_real( struct cons_pointer a, struct cons_pointer b ) {
|
||||
bool result = false;
|
||||
|
||||
if (integerp(a) && realp(b))
|
||||
{
|
||||
long double bv = pointer2cell(b).payload.real.value;
|
||||
if ( integerp( a ) && realp( b ) ) {
|
||||
long double bv = pointer2cell( b ).payload.real.value;
|
||||
|
||||
if (floor(bv) == bv) {
|
||||
result = pointer2cell(a).payload.integer.value == (int64_t)bv;
|
||||
if ( floor( bv ) == bv ) {
|
||||
result = pointer2cell( a ).payload.integer.value == ( int64_t ) bv;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,19 +14,19 @@
|
|||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
struct cons_pointer make_integer(int64_t value, struct cons_pointer more);
|
||||
struct cons_pointer make_integer( int64_t value, struct cons_pointer more );
|
||||
|
||||
struct cons_pointer add_integers(struct cons_pointer a,
|
||||
struct cons_pointer b);
|
||||
struct cons_pointer add_integers( struct cons_pointer a,
|
||||
struct cons_pointer b );
|
||||
|
||||
struct cons_pointer multiply_integers(struct cons_pointer a,
|
||||
struct cons_pointer b);
|
||||
struct cons_pointer multiply_integers( struct cons_pointer a,
|
||||
struct cons_pointer b );
|
||||
|
||||
struct cons_pointer integer_to_string(struct cons_pointer int_pointer,
|
||||
int base);
|
||||
struct cons_pointer integer_to_string( struct cons_pointer int_pointer,
|
||||
int base );
|
||||
|
||||
bool equal_integer_integer(struct cons_pointer a, struct cons_pointer b);
|
||||
bool equal_integer_integer( struct cons_pointer a, struct cons_pointer b );
|
||||
|
||||
bool equal_integer_real(struct cons_pointer a, struct cons_pointer b);
|
||||
bool equal_integer_real( struct cons_pointer a, struct cons_pointer b );
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -247,8 +247,7 @@ struct cons_pointer add_2( struct stack_frame *frame,
|
|||
result = add_integers( arg1, arg2 );
|
||||
break;
|
||||
case RATIOTV:
|
||||
result =
|
||||
add_integer_ratio( arg1, arg2 );
|
||||
result = add_integer_ratio( arg1, arg2 );
|
||||
break;
|
||||
case REALTV:
|
||||
result =
|
||||
|
|
@ -268,8 +267,7 @@ struct cons_pointer add_2( struct stack_frame *frame,
|
|||
result = arg2;
|
||||
break;
|
||||
case INTEGERTV:
|
||||
result =
|
||||
add_integer_ratio( arg2, arg1 );
|
||||
result = add_integer_ratio( arg2, arg1 );
|
||||
break;
|
||||
case RATIOTV:
|
||||
result = add_ratio_ratio( arg1, arg2 );
|
||||
|
|
@ -380,9 +378,7 @@ struct cons_pointer multiply_2( struct stack_frame *frame,
|
|||
result = multiply_integers( arg1, arg2 );
|
||||
break;
|
||||
case RATIOTV:
|
||||
result =
|
||||
multiply_integer_ratio( arg1,
|
||||
arg2 );
|
||||
result = multiply_integer_ratio( arg1, arg2 );
|
||||
break;
|
||||
case REALTV:
|
||||
result =
|
||||
|
|
@ -405,13 +401,10 @@ struct cons_pointer multiply_2( struct stack_frame *frame,
|
|||
result = arg2;
|
||||
break;
|
||||
case INTEGERTV:
|
||||
result =
|
||||
multiply_integer_ratio( arg2,
|
||||
arg1 );
|
||||
result = multiply_integer_ratio( arg2, arg1 );
|
||||
break;
|
||||
case RATIOTV:
|
||||
result =
|
||||
multiply_ratio_ratio( arg1, arg2 );
|
||||
result = multiply_ratio_ratio( arg1, arg2 );
|
||||
break;
|
||||
case REALTV:
|
||||
result =
|
||||
|
|
@ -564,20 +557,18 @@ struct cons_pointer subtract_2( struct stack_frame *frame,
|
|||
result = arg2;
|
||||
break;
|
||||
case INTEGERTV:{
|
||||
struct cons_pointer i =
|
||||
negative( arg2 );
|
||||
struct cons_pointer i = negative( arg2 );
|
||||
inc_ref( i );
|
||||
result = add_integers( arg1, i );
|
||||
dec_ref( i );
|
||||
}
|
||||
break;
|
||||
case RATIOTV:{
|
||||
struct cons_pointer tmp =
|
||||
make_ratio( arg1,
|
||||
make_integer( 1, NIL ) );
|
||||
struct cons_pointer tmp = make_ratio( arg1,
|
||||
make_integer( 1,
|
||||
NIL ) );
|
||||
inc_ref( tmp );
|
||||
result =
|
||||
subtract_ratio_ratio( tmp, arg2 );
|
||||
result = subtract_ratio_ratio( tmp, arg2 );
|
||||
dec_ref( tmp );
|
||||
}
|
||||
break;
|
||||
|
|
@ -599,12 +590,11 @@ struct cons_pointer subtract_2( struct stack_frame *frame,
|
|||
result = arg2;
|
||||
break;
|
||||
case INTEGERTV:{
|
||||
struct cons_pointer tmp =
|
||||
make_ratio( arg2,
|
||||
make_integer( 1, NIL ) );
|
||||
struct cons_pointer tmp = make_ratio( arg2,
|
||||
make_integer( 1,
|
||||
NIL ) );
|
||||
inc_ref( tmp );
|
||||
result =
|
||||
subtract_ratio_ratio( arg1, tmp );
|
||||
result = subtract_ratio_ratio( arg1, tmp );
|
||||
dec_ref( tmp );
|
||||
}
|
||||
break;
|
||||
|
|
@ -696,9 +686,7 @@ struct cons_pointer lisp_divide( struct
|
|||
struct cons_pointer ratio =
|
||||
make_ratio( frame->arg[0], one );
|
||||
inc_ref( ratio );
|
||||
result =
|
||||
divide_ratio_ratio( ratio,
|
||||
frame->arg[1] );
|
||||
result = divide_ratio_ratio( ratio, frame->arg[1] );
|
||||
dec_ref( ratio );
|
||||
}
|
||||
break;
|
||||
|
|
@ -725,17 +713,14 @@ struct cons_pointer lisp_divide( struct
|
|||
struct cons_pointer ratio =
|
||||
make_ratio( frame->arg[1], one );
|
||||
inc_ref( ratio );
|
||||
result =
|
||||
divide_ratio_ratio( frame->arg[0],
|
||||
ratio );
|
||||
result = divide_ratio_ratio( frame->arg[0], ratio );
|
||||
dec_ref( ratio );
|
||||
dec_ref( one );
|
||||
}
|
||||
break;
|
||||
case RATIOTV:
|
||||
result =
|
||||
divide_ratio_ratio( frame->arg[0],
|
||||
frame->arg[1] );
|
||||
divide_ratio_ratio( frame->arg[0], frame->arg[1] );
|
||||
break;
|
||||
case REALTV:
|
||||
result =
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ struct cons_pointer absolute( struct cons_pointer arg );
|
|||
|
||||
long double to_long_double( struct cons_pointer arg );
|
||||
|
||||
int64_t to_long_int( struct cons_pointer arg ) ;
|
||||
int64_t to_long_int( struct cons_pointer arg );
|
||||
|
||||
struct cons_pointer lisp_absolute( struct stack_frame
|
||||
*frame, struct cons_pointer frame_pointer, struct
|
||||
|
|
|
|||
|
|
@ -43,42 +43,36 @@ int64_t least_common_multiple( int64_t m, int64_t n ) {
|
|||
return m / greatest_common_divisor( m, n ) * n;
|
||||
}
|
||||
|
||||
struct cons_pointer simplify_ratio( struct cons_pointer pointer) {
|
||||
struct cons_pointer simplify_ratio( struct cons_pointer pointer ) {
|
||||
struct cons_pointer result = pointer;
|
||||
struct cons_space_object cell = pointer2cell(pointer);
|
||||
struct cons_space_object dividend = pointer2cell(cell.payload.ratio.dividend);
|
||||
struct cons_space_object divisor = pointer2cell(cell.payload.ratio.divisor);
|
||||
struct cons_space_object cell = pointer2cell( pointer );
|
||||
struct cons_space_object dividend =
|
||||
pointer2cell( cell.payload.ratio.dividend );
|
||||
struct cons_space_object divisor =
|
||||
pointer2cell( cell.payload.ratio.divisor );
|
||||
|
||||
if (divisor.payload.integer.value == 1)
|
||||
{
|
||||
result = pointer2cell(pointer).payload.ratio.dividend;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ratiop(pointer))
|
||||
{
|
||||
if ( divisor.payload.integer.value == 1 ) {
|
||||
result = pointer2cell( pointer ).payload.ratio.dividend;
|
||||
} else {
|
||||
if ( ratiop( pointer ) ) {
|
||||
int64_t ddrv = dividend.payload.integer.value,
|
||||
drrv = divisor.payload.integer.value,
|
||||
gcd = greatest_common_divisor(ddrv, drrv);
|
||||
drrv = divisor.payload.integer.value,
|
||||
gcd = greatest_common_divisor( ddrv, drrv );
|
||||
|
||||
if (gcd > 1)
|
||||
{
|
||||
if (drrv / gcd == 1)
|
||||
{
|
||||
result = make_integer(ddrv / gcd, NIL);
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( gcd > 1 ) {
|
||||
if ( drrv / gcd == 1 ) {
|
||||
result = make_integer( ddrv / gcd, NIL );
|
||||
} else {
|
||||
result =
|
||||
make_ratio(make_integer(ddrv / gcd, NIL),
|
||||
make_integer(drrv / gcd, NIL));
|
||||
make_ratio( make_integer( ddrv / gcd, NIL ),
|
||||
make_integer( drrv / gcd, NIL ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -181,8 +175,7 @@ struct cons_pointer add_integer_ratio( struct cons_pointer intarg,
|
|||
( L"Shouldn't happen: bad arg to add_integer_ratio" ),
|
||||
make_cons( intarg,
|
||||
make_cons( ratarg,
|
||||
NIL ) ) ),
|
||||
NIL );
|
||||
NIL ) ) ), NIL );
|
||||
}
|
||||
|
||||
return result;
|
||||
|
|
@ -196,11 +189,10 @@ struct cons_pointer add_integer_ratio( struct cons_pointer intarg,
|
|||
*/
|
||||
struct cons_pointer divide_ratio_ratio( struct cons_pointer arg1,
|
||||
struct cons_pointer arg2 ) {
|
||||
// TODO: this now has to work if `arg1` is an integer
|
||||
struct cons_pointer i = make_ratio( pointer2cell( arg2 ).payload.
|
||||
ratio.divisor,
|
||||
pointer2cell( arg2 ).payload.
|
||||
ratio.dividend ), result =
|
||||
// TODO: this now has to work if `arg1` is an integer
|
||||
struct cons_pointer i =
|
||||
make_ratio( pointer2cell( arg2 ).payload.ratio.divisor,
|
||||
pointer2cell( arg2 ).payload.ratio.dividend ), result =
|
||||
multiply_ratio_ratio( arg1, i );
|
||||
|
||||
dec_ref( i );
|
||||
|
|
@ -217,7 +209,7 @@ struct cons_pointer divide_ratio_ratio( struct cons_pointer arg1,
|
|||
struct cons_pointer multiply_ratio_ratio( struct
|
||||
cons_pointer arg1, struct
|
||||
cons_pointer arg2 ) {
|
||||
// TODO: this now has to work if arg1 is an integer
|
||||
// TODO: this now has to work if arg1 is an integer
|
||||
struct cons_pointer result;
|
||||
|
||||
debug_print( L"multiply_ratio_ratio( arg1 = ", DEBUG_ARITH );
|
||||
|
|
@ -294,7 +286,7 @@ struct cons_pointer multiply_integer_ratio( struct cons_pointer intarg,
|
|||
*/
|
||||
struct cons_pointer subtract_ratio_ratio( struct cons_pointer arg1,
|
||||
struct cons_pointer arg2 ) {
|
||||
struct cons_pointer i = negative( arg2),
|
||||
struct cons_pointer i = negative( arg2 ),
|
||||
result = add_ratio_ratio( arg1, i );
|
||||
|
||||
dec_ref( i );
|
||||
|
|
@ -333,20 +325,18 @@ struct cons_pointer make_ratio( struct cons_pointer dividend,
|
|||
/**
|
||||
* True if a and be are identical ratios, else false.
|
||||
*/
|
||||
bool equal_ratio_ratio(struct cons_pointer a, struct cons_pointer b)
|
||||
{
|
||||
bool equal_ratio_ratio( struct cons_pointer a, struct cons_pointer b ) {
|
||||
bool result = false;
|
||||
|
||||
if (ratiop(a) && ratiop(b))
|
||||
{
|
||||
struct cons_space_object *cell_a = &pointer2cell(a);
|
||||
struct cons_space_object *cell_b = &pointer2cell(b);
|
||||
if ( ratiop( a ) && ratiop( b ) ) {
|
||||
struct cons_space_object *cell_a = &pointer2cell( a );
|
||||
struct cons_space_object *cell_b = &pointer2cell( b );
|
||||
|
||||
result = equal_integer_integer(cell_a->payload.ratio.dividend,
|
||||
cell_b->payload.ratio.dividend) &&
|
||||
equal_integer_integer(cell_a->payload.ratio.divisor,
|
||||
cell_b->payload.ratio.divisor);
|
||||
result = equal_integer_integer( cell_a->payload.ratio.dividend,
|
||||
cell_b->payload.ratio.dividend ) &&
|
||||
equal_integer_integer( cell_a->payload.ratio.divisor,
|
||||
cell_b->payload.ratio.divisor );
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,6 +34,6 @@ struct cons_pointer subtract_ratio_ratio( struct cons_pointer arg1,
|
|||
struct cons_pointer make_ratio( struct cons_pointer dividend,
|
||||
struct cons_pointer divisor );
|
||||
|
||||
bool equal_ratio_ratio(struct cons_pointer a, struct cons_pointer b);
|
||||
bool equal_ratio_ratio( struct cons_pointer a, struct cons_pointer b );
|
||||
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue