Begun work on bignums; changed integer size to 64 bits
I'm fairly sure the size of a long int on my machines is 64 bit anyway, but for portability it needs to be explicit.
This commit is contained in:
parent
ad9b1cd7f8
commit
6ee9f9b59a
13 changed files with 109 additions and 25 deletions
14
src/arith/bignum.c
Normal file
14
src/arith/bignum.c
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
/*
|
||||
* bignum.c
|
||||
*
|
||||
* Allocation of and operations on arbitrary precision integers.
|
||||
*
|
||||
* (c) 2018 Simon Brooke <simon@journeyman.cc>
|
||||
* Licensed under GPL version 2.0, or, at your option, any later version.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Bignums generally follow Knuth, vol 2, 4.3. The word size is 64 bits,
|
||||
* and words are stored in individual cons-space objects, comprising the
|
||||
* word itself and a pointer to the next word in the number.
|
||||
*/
|
||||
16
src/arith/bignum.h
Normal file
16
src/arith/bignum.h
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
/**
|
||||
* bignum.h
|
||||
*
|
||||
* functions for bignum cells.
|
||||
*
|
||||
*
|
||||
* (c) 2017 Simon Brooke <simon@journeyman.cc>
|
||||
* Licensed under GPL version 2.0, or, at your option, any later version.
|
||||
*/
|
||||
|
||||
#ifndef __bignum_h
|
||||
#define __bignum_h
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -35,7 +35,7 @@ long double numeric_value( struct cons_pointer pointer ) {
|
|||
/**
|
||||
* Allocate an integer cell representing this value and return a cons pointer to it.
|
||||
*/
|
||||
struct cons_pointer make_integer( long int value ) {
|
||||
struct cons_pointer make_integer( int64_t value ) {
|
||||
struct cons_pointer result = allocate_cell( INTEGERTAG );
|
||||
struct cons_space_object *cell = &pointer2cell( result );
|
||||
cell->payload.integer.value = value;
|
||||
|
|
|
|||
|
|
@ -16,6 +16,6 @@ long double numeric_value( struct cons_pointer pointer );
|
|||
/**
|
||||
* Allocate an integer cell representing this value and return a cons pointer to it.
|
||||
*/
|
||||
struct cons_pointer make_integer( long int value );
|
||||
struct cons_pointer make_integer( int64_t value );
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@
|
|||
#include "stack.h"
|
||||
|
||||
long double to_long_double( struct cons_pointer arg );
|
||||
long int to_long_int( struct cons_pointer arg );
|
||||
int64_t to_long_int( struct cons_pointer arg );
|
||||
struct cons_pointer add_2( struct stack_frame *frame, struct cons_pointer arg1,
|
||||
struct cons_pointer arg2 );
|
||||
|
||||
|
|
@ -97,8 +97,8 @@ long double to_long_double( struct cons_pointer arg ) {
|
|||
* if a ratio may legally have zero as a divisor, or something which is
|
||||
* not a number (or is a big number) is passed in.
|
||||
*/
|
||||
long int to_long_int( struct cons_pointer arg ) {
|
||||
long int result = 0;
|
||||
int64_t to_long_int( struct cons_pointer arg ) {
|
||||
int64_t result = 0;
|
||||
struct cons_space_object cell = pointer2cell( arg );
|
||||
switch ( cell.tag.value ) {
|
||||
case INTEGERTV:
|
||||
|
|
@ -125,10 +125,13 @@ struct cons_pointer add_2( struct stack_frame *frame, struct cons_pointer arg1,
|
|||
struct cons_space_object cell1 = pointer2cell( arg1 );
|
||||
struct cons_space_object cell2 = pointer2cell( arg2 );
|
||||
|
||||
#ifdef DEBUG
|
||||
fputws( L"add_2( arg1 = ", stderr );
|
||||
print( stderr, arg1 );
|
||||
fputws( L"; arg2 = ", stderr );
|
||||
print( stderr, arg2 );
|
||||
fputws( L")\n", stderr);
|
||||
#endif
|
||||
|
||||
if ( zerop( arg1 ) ) {
|
||||
result = arg2;
|
||||
|
|
@ -199,9 +202,11 @@ struct cons_pointer add_2( struct stack_frame *frame, struct cons_pointer arg1,
|
|||
}
|
||||
}
|
||||
|
||||
#ifdef DEBUG
|
||||
fputws( L"}; => ", stderr );
|
||||
print( stderr, arg2 );
|
||||
fputws( L"\n", stderr );
|
||||
#endif
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
@ -254,17 +259,19 @@ struct cons_pointer multiply_2( struct stack_frame *frame,
|
|||
struct cons_space_object cell1 = pointer2cell( arg1 );
|
||||
struct cons_space_object cell2 = pointer2cell( arg2 );
|
||||
|
||||
#ifdef DEBUG
|
||||
fputws( L"multiply_2( arg1 = ", stderr );
|
||||
print( stderr, arg1 );
|
||||
fputws( L"; arg2 = ", stderr );
|
||||
print( stderr, arg2 );
|
||||
fputws( L")\n", stderr);
|
||||
#endif
|
||||
|
||||
if ( zerop( arg1 ) ) {
|
||||
result = arg2;
|
||||
} else if ( zerop( arg2 ) ) {
|
||||
result = arg1;
|
||||
} else {
|
||||
|
||||
switch ( cell1.tag.value ) {
|
||||
case EXCEPTIONTV:
|
||||
result = arg1;
|
||||
|
|
@ -328,9 +335,11 @@ struct cons_pointer multiply_2( struct stack_frame *frame,
|
|||
}
|
||||
}
|
||||
|
||||
fputws( L"}; => ", stderr );
|
||||
#ifdef DEBUG
|
||||
fputws( L" => ", stderr );
|
||||
print( stderr, arg2 );
|
||||
fputws( L"\n", stderr );
|
||||
#endif
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,9 +28,9 @@ struct cons_pointer inverse( struct stack_frame *frame,
|
|||
struct cons_pointer arg );
|
||||
|
||||
/**
|
||||
* return, as a long int, the greatest common divisor of `m` and `n`,
|
||||
* return, as a int64_t, the greatest common divisor of `m` and `n`,
|
||||
*/
|
||||
long int greatest_common_divisor( long int m, long int n ) {
|
||||
int64_t greatest_common_divisor( int64_t m, int64_t n ) {
|
||||
int o;
|
||||
while ( m ) {
|
||||
o = m;
|
||||
|
|
@ -42,9 +42,9 @@ long int greatest_common_divisor( long int m, long int n ) {
|
|||
}
|
||||
|
||||
/**
|
||||
* return, as a long int, the least common multiple of `m` and `n`,
|
||||
* return, as a int64_t, the least common multiple of `m` and `n`,
|
||||
*/
|
||||
long int least_common_multiple( long int m, long int n ) {
|
||||
int64_t least_common_multiple( int64_t m, int64_t n ) {
|
||||
return m / greatest_common_divisor( m, n ) * n;
|
||||
}
|
||||
|
||||
|
|
@ -59,7 +59,7 @@ struct cons_pointer simplify_ratio( struct stack_frame *frame,
|
|||
struct cons_pointer result = arg;
|
||||
|
||||
if (ratiop(arg)) {
|
||||
long int ddrv =
|
||||
int64_t ddrv =
|
||||
pointer2cell( pointer2cell( arg ).payload.ratio.dividend ).
|
||||
payload.integer.value, drrv =
|
||||
pointer2cell( pointer2cell( arg ).payload.ratio.divisor ).
|
||||
|
|
@ -106,7 +106,7 @@ struct cons_pointer add_ratio_ratio( struct stack_frame *frame,
|
|||
if ( ratiop(arg1) && ratiop(arg2)) {
|
||||
struct cons_space_object cell1 = pointer2cell( arg1 );
|
||||
struct cons_space_object cell2 = pointer2cell( arg2 );
|
||||
long int dd1v =
|
||||
int64_t dd1v =
|
||||
pointer2cell( cell1.payload.ratio.dividend ).payload.integer.value,
|
||||
dd2v =
|
||||
pointer2cell( cell2.payload.ratio.dividend ).payload.integer.value,
|
||||
|
|
@ -231,7 +231,7 @@ struct cons_pointer multiply_ratio_ratio( struct
|
|||
if ( ratiop(arg1) && ratiop(arg2)) {
|
||||
struct cons_space_object cell1 = pointer2cell( arg1 );
|
||||
struct cons_space_object cell2 = pointer2cell( arg2 );
|
||||
long int dd1v =
|
||||
int64_t dd1v =
|
||||
pointer2cell( cell1.payload.ratio.dividend ).payload.integer.value,
|
||||
dd2v =
|
||||
pointer2cell( cell2.payload.ratio.dividend ).payload.integer.value,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue