2004-07-27 22:52:35 -04:00
|
|
|
#include "factor.h"
|
|
|
|
|
2004-08-25 02:00:52 -04:00
|
|
|
void init_bignum(void)
|
|
|
|
{
|
|
|
|
bignum_zero = bignum_allocate(0,0);
|
|
|
|
|
|
|
|
bignum_pos_one = bignum_allocate(1,0);
|
|
|
|
(BIGNUM_REF (bignum_pos_one, 0)) = 1;
|
|
|
|
|
2004-08-25 20:51:19 -04:00
|
|
|
bignum_neg_one = bignum_allocate(1,1);
|
2004-08-25 02:00:52 -04:00
|
|
|
(BIGNUM_REF (bignum_neg_one, 0)) = 1;
|
|
|
|
}
|
|
|
|
|
2004-07-27 22:52:35 -04:00
|
|
|
void primitive_bignump(void)
|
|
|
|
{
|
2004-08-12 17:36:36 -04:00
|
|
|
drepl(tag_boolean(typep(BIGNUM_TYPE,dpeek())));
|
2004-07-27 22:52:35 -04:00
|
|
|
}
|
2004-08-05 15:18:31 -04:00
|
|
|
|
2004-08-25 02:00:52 -04:00
|
|
|
ARRAY* to_bignum(CELL tagged)
|
2004-08-05 15:18:31 -04:00
|
|
|
{
|
|
|
|
RATIO* r;
|
2004-08-05 16:49:55 -04:00
|
|
|
FLOAT* f;
|
2004-08-05 15:18:31 -04:00
|
|
|
|
|
|
|
switch(type_of(tagged))
|
|
|
|
{
|
|
|
|
case FIXNUM_TYPE:
|
2004-08-27 02:09:24 -04:00
|
|
|
return s48_long_to_bignum(untag_fixnum_fast(tagged));
|
2004-08-05 15:18:31 -04:00
|
|
|
case BIGNUM_TYPE:
|
2004-08-25 02:00:52 -04:00
|
|
|
return (ARRAY*)UNTAG(tagged);
|
2004-08-05 15:18:31 -04:00
|
|
|
case RATIO_TYPE:
|
|
|
|
r = (RATIO*)UNTAG(tagged);
|
|
|
|
return to_bignum(divint(r->numerator,r->denominator));
|
2004-08-05 16:49:55 -04:00
|
|
|
case FLOAT_TYPE:
|
|
|
|
f = (FLOAT*)UNTAG(tagged);
|
2004-08-25 02:00:52 -04:00
|
|
|
return s48_double_to_bignum(f->n);
|
2004-08-05 15:18:31 -04:00
|
|
|
default:
|
|
|
|
type_error(BIGNUM_TYPE,tagged);
|
|
|
|
return NULL; /* can't happen */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_to_bignum(void)
|
|
|
|
{
|
2004-08-12 17:36:36 -04:00
|
|
|
drepl(tag_object(to_bignum(dpeek())));
|
2004-08-05 15:18:31 -04:00
|
|
|
}
|
|
|
|
|
2004-08-25 02:00:52 -04:00
|
|
|
CELL number_eq_bignum(ARRAY* x, ARRAY* y)
|
2004-08-05 15:18:31 -04:00
|
|
|
{
|
2004-08-25 02:00:52 -04:00
|
|
|
return tag_boolean(s48_bignum_equal_p(x,y));
|
2004-08-05 15:18:31 -04:00
|
|
|
}
|
|
|
|
|
2004-08-25 02:00:52 -04:00
|
|
|
CELL add_bignum(ARRAY* x, ARRAY* y)
|
2004-08-05 15:18:31 -04:00
|
|
|
{
|
2004-08-25 02:00:52 -04:00
|
|
|
return tag_object(s48_bignum_add(x,y));
|
2004-08-05 15:18:31 -04:00
|
|
|
}
|
|
|
|
|
2004-08-25 02:00:52 -04:00
|
|
|
CELL subtract_bignum(ARRAY* x, ARRAY* y)
|
2004-08-05 15:18:31 -04:00
|
|
|
{
|
2004-08-25 02:00:52 -04:00
|
|
|
return tag_object(s48_bignum_subtract(x,y));
|
2004-08-05 15:18:31 -04:00
|
|
|
}
|
|
|
|
|
2004-08-25 02:00:52 -04:00
|
|
|
CELL multiply_bignum(ARRAY* x, ARRAY* y)
|
2004-08-05 15:18:31 -04:00
|
|
|
{
|
2004-08-25 02:00:52 -04:00
|
|
|
return tag_object(s48_bignum_multiply(x,y));
|
2004-08-05 15:18:31 -04:00
|
|
|
}
|
|
|
|
|
2004-08-25 20:51:19 -04:00
|
|
|
CELL gcd_bignum(ARRAY* x, ARRAY* y)
|
2004-08-05 15:18:31 -04:00
|
|
|
{
|
2004-08-25 20:51:19 -04:00
|
|
|
ARRAY* t;
|
2004-08-05 15:18:31 -04:00
|
|
|
|
2004-08-25 20:51:19 -04:00
|
|
|
if(BIGNUM_NEGATIVE_P(x))
|
|
|
|
x = s48_bignum_negate(x);
|
|
|
|
if(BIGNUM_NEGATIVE_P(y))
|
|
|
|
y = s48_bignum_negate(y);
|
2004-08-05 15:18:31 -04:00
|
|
|
|
2004-08-25 20:51:19 -04:00
|
|
|
if(s48_bignum_compare(x,y) == bignum_comparison_greater)
|
2004-08-05 15:18:31 -04:00
|
|
|
{
|
|
|
|
t = x;
|
|
|
|
x = y;
|
|
|
|
y = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
2004-08-25 20:51:19 -04:00
|
|
|
if(BIGNUM_ZERO_P(x))
|
|
|
|
return tag_object(y);
|
2004-08-05 15:18:31 -04:00
|
|
|
|
2004-08-25 20:51:19 -04:00
|
|
|
t = s48_bignum_remainder(y,x);
|
2004-08-05 15:18:31 -04:00
|
|
|
y = x;
|
|
|
|
x = t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-25 02:00:52 -04:00
|
|
|
CELL divide_bignum(ARRAY* x, ARRAY* y)
|
2004-08-05 15:18:31 -04:00
|
|
|
{
|
2004-08-25 20:51:19 -04:00
|
|
|
ARRAY* gcd;
|
2004-08-05 15:18:31 -04:00
|
|
|
|
2004-08-25 20:51:19 -04:00
|
|
|
if(BIGNUM_ZERO_P(y))
|
|
|
|
raise(SIGFPE);
|
2004-08-05 15:18:31 -04:00
|
|
|
|
2004-08-25 20:51:19 -04:00
|
|
|
if(BIGNUM_NEGATIVE_P(y))
|
2004-08-05 15:18:31 -04:00
|
|
|
{
|
2004-08-25 20:51:19 -04:00
|
|
|
x = s48_bignum_negate(x);
|
|
|
|
y = s48_bignum_negate(y);
|
2004-08-05 15:18:31 -04:00
|
|
|
}
|
|
|
|
|
2004-08-25 20:51:19 -04:00
|
|
|
gcd = (ARRAY*)UNTAG(gcd_bignum(x,y));
|
|
|
|
x = s48_bignum_quotient(x,gcd);
|
|
|
|
y = s48_bignum_quotient(y,gcd);
|
|
|
|
|
|
|
|
if(BIGNUM_ONE_P(y,0))
|
|
|
|
return tag_object(x);
|
2004-08-05 15:18:31 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return tag_ratio(ratio(
|
2004-08-25 20:51:19 -04:00
|
|
|
tag_object(x),
|
|
|
|
tag_object(y)));
|
|
|
|
}
|
2004-08-05 15:18:31 -04:00
|
|
|
}
|
|
|
|
|
2004-08-25 02:00:52 -04:00
|
|
|
CELL divint_bignum(ARRAY* x, ARRAY* y)
|
2004-08-05 15:18:31 -04:00
|
|
|
{
|
2004-08-26 23:05:48 -04:00
|
|
|
return tag_object(s48_bignum_quotient(x,y));
|
2004-08-05 15:18:31 -04:00
|
|
|
}
|
|
|
|
|
2004-08-25 02:00:52 -04:00
|
|
|
CELL divfloat_bignum(ARRAY* x, ARRAY* y)
|
2004-08-05 16:49:55 -04:00
|
|
|
{
|
2004-08-25 02:00:52 -04:00
|
|
|
return tag_object(make_float(
|
|
|
|
s48_bignum_to_double(x) /
|
|
|
|
s48_bignum_to_double(y)));
|
2004-08-05 16:49:55 -04:00
|
|
|
}
|
|
|
|
|
2004-08-25 02:00:52 -04:00
|
|
|
CELL divmod_bignum(ARRAY* x, ARRAY* y)
|
2004-08-05 15:18:31 -04:00
|
|
|
{
|
2004-08-26 23:05:48 -04:00
|
|
|
ARRAY *q, *r;
|
|
|
|
s48_bignum_divide(x,y,&q,&r);
|
2004-08-25 02:00:52 -04:00
|
|
|
dpush(tag_object(q));
|
|
|
|
return tag_object(r);
|
2004-08-05 15:18:31 -04:00
|
|
|
}
|
|
|
|
|
2004-08-25 02:00:52 -04:00
|
|
|
CELL mod_bignum(ARRAY* x, ARRAY* y)
|
2004-08-05 15:18:31 -04:00
|
|
|
{
|
2004-08-26 23:05:48 -04:00
|
|
|
return tag_object(s48_bignum_remainder(x,y));
|
2004-08-05 15:18:31 -04:00
|
|
|
}
|
|
|
|
|
2004-08-25 02:00:52 -04:00
|
|
|
CELL and_bignum(ARRAY* x, ARRAY* y)
|
2004-08-05 15:18:31 -04:00
|
|
|
{
|
2004-08-25 02:00:52 -04:00
|
|
|
return tag_object(s48_bignum_bitwise_and(x,y));
|
2004-08-05 15:18:31 -04:00
|
|
|
}
|
|
|
|
|
2004-08-25 02:00:52 -04:00
|
|
|
CELL or_bignum(ARRAY* x, ARRAY* y)
|
2004-08-05 15:18:31 -04:00
|
|
|
{
|
2004-08-25 02:00:52 -04:00
|
|
|
return tag_object(s48_bignum_bitwise_ior(x,y));
|
2004-08-05 15:18:31 -04:00
|
|
|
}
|
|
|
|
|
2004-08-25 02:00:52 -04:00
|
|
|
CELL xor_bignum(ARRAY* x, ARRAY* y)
|
2004-08-05 15:18:31 -04:00
|
|
|
{
|
2004-08-25 02:00:52 -04:00
|
|
|
return tag_object(s48_bignum_bitwise_xor(x,y));
|
2004-08-05 15:18:31 -04:00
|
|
|
}
|
|
|
|
|
2004-08-26 19:37:22 -04:00
|
|
|
CELL shift_bignum(ARRAY* x, FIXNUM y)
|
2004-08-05 15:18:31 -04:00
|
|
|
{
|
2004-08-26 19:37:22 -04:00
|
|
|
return tag_object(s48_bignum_arithmetic_shift(x,y));
|
2004-08-05 15:18:31 -04:00
|
|
|
}
|
|
|
|
|
2004-08-25 02:00:52 -04:00
|
|
|
CELL less_bignum(ARRAY* x, ARRAY* y)
|
2004-08-05 15:18:31 -04:00
|
|
|
{
|
2004-08-25 02:00:52 -04:00
|
|
|
return tag_boolean(
|
|
|
|
s48_bignum_compare(x,y)
|
|
|
|
== bignum_comparison_less);
|
2004-08-05 15:18:31 -04:00
|
|
|
}
|
|
|
|
|
2004-08-25 02:00:52 -04:00
|
|
|
CELL lesseq_bignum(ARRAY* x, ARRAY* y)
|
2004-08-05 15:18:31 -04:00
|
|
|
{
|
2004-08-25 02:00:52 -04:00
|
|
|
switch(s48_bignum_compare(x,y))
|
|
|
|
{
|
|
|
|
case bignum_comparison_less:
|
|
|
|
case bignum_comparison_equal:
|
|
|
|
return T;
|
|
|
|
case bignum_comparison_greater:
|
|
|
|
return F;
|
2004-08-27 23:20:10 -04:00
|
|
|
default:
|
|
|
|
critical_error("s48_bignum_compare returns bogus value",0);
|
|
|
|
return F;
|
2004-08-25 02:00:52 -04:00
|
|
|
}
|
2004-08-05 15:18:31 -04:00
|
|
|
}
|
|
|
|
|
2004-08-25 02:00:52 -04:00
|
|
|
CELL greater_bignum(ARRAY* x, ARRAY* y)
|
2004-08-05 15:18:31 -04:00
|
|
|
{
|
2004-08-25 02:00:52 -04:00
|
|
|
return tag_boolean(
|
|
|
|
s48_bignum_compare(x,y)
|
|
|
|
== bignum_comparison_greater);
|
|
|
|
}
|
|
|
|
|
|
|
|
CELL greatereq_bignum(ARRAY* x, ARRAY* y)
|
|
|
|
{
|
|
|
|
switch(s48_bignum_compare(x,y))
|
|
|
|
{
|
|
|
|
case bignum_comparison_less:
|
|
|
|
return F;
|
|
|
|
case bignum_comparison_equal:
|
|
|
|
case bignum_comparison_greater:
|
|
|
|
return T;
|
2004-08-27 23:20:10 -04:00
|
|
|
default:
|
|
|
|
critical_error("s48_bignum_compare returns bogus value",0);
|
|
|
|
return F;
|
2004-08-25 02:00:52 -04:00
|
|
|
}
|
2004-08-05 15:18:31 -04:00
|
|
|
}
|
|
|
|
|
2004-08-25 02:00:52 -04:00
|
|
|
CELL not_bignum(ARRAY* x)
|
2004-08-05 15:18:31 -04:00
|
|
|
{
|
2004-08-25 02:00:52 -04:00
|
|
|
return tag_object(s48_bignum_bitwise_not(x));
|
2004-08-05 15:18:31 -04:00
|
|
|
}
|
2004-08-17 23:42:10 -04:00
|
|
|
|
2004-08-25 02:00:52 -04:00
|
|
|
void copy_bignum_constants(void)
|
2004-08-17 23:42:10 -04:00
|
|
|
{
|
2004-08-25 02:00:52 -04:00
|
|
|
bignum_zero = copy_array(bignum_zero);
|
|
|
|
bignum_pos_one = copy_array(bignum_pos_one);
|
|
|
|
bignum_neg_one = copy_array(bignum_neg_one);
|
2004-08-17 23:42:10 -04:00
|
|
|
}
|