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;
|
|
|
|
|
|
|
|
bignum_neg_one = bignum_allocate(1,0);
|
|
|
|
(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:
|
|
|
|
return fixnum_to_bignum(tagged);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
BIGNUM_2 gcd_bignum(BIGNUM_2 x, BIGNUM_2 y)
|
|
|
|
{
|
|
|
|
BIGNUM_2 t;
|
|
|
|
|
|
|
|
if(x < 0)
|
|
|
|
x = -x;
|
|
|
|
if(y < 0)
|
|
|
|
y = -y;
|
|
|
|
|
|
|
|
if(x > y)
|
|
|
|
{
|
|
|
|
t = x;
|
|
|
|
x = y;
|
|
|
|
y = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if(x == 0)
|
|
|
|
return y;
|
|
|
|
|
|
|
|
t = y % x;
|
|
|
|
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 02:00:52 -04:00
|
|
|
/* BIGNUM_2 _x = x->n;
|
2004-08-25 00:26:49 -04:00
|
|
|
BIGNUM_2 _y = y->n;
|
2004-08-05 15:18:31 -04:00
|
|
|
BIGNUM_2 gcd;
|
|
|
|
|
|
|
|
if(_y == 0)
|
|
|
|
{
|
2004-08-25 02:00:52 -04:00
|
|
|
/* FIXME
|
2004-08-05 15:18:31 -04:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
else if(_y < 0)
|
|
|
|
{
|
|
|
|
_x = -_x;
|
|
|
|
_y = -_y;
|
|
|
|
}
|
|
|
|
|
|
|
|
gcd = gcd_bignum(_x,_y);
|
|
|
|
if(gcd != 1)
|
|
|
|
{
|
|
|
|
_x /= gcd;
|
|
|
|
_y /= gcd;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_y == 1)
|
|
|
|
return tag_object(bignum(_x));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return tag_ratio(ratio(
|
2004-08-05 16:49:55 -04:00
|
|
|
tag_object(bignum(_x)),
|
|
|
|
tag_object(bignum(_y))));
|
2004-08-25 02:00:52 -04:00
|
|
|
} */
|
|
|
|
return F;
|
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-25 02:00:52 -04:00
|
|
|
ARRAY* q = s48_bignum_quotient(x,y);
|
|
|
|
if(q == NULL)
|
|
|
|
raise(SIGFPE);
|
|
|
|
return tag_object(q);
|
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-25 02:00:52 -04:00
|
|
|
ARRAY* q;
|
|
|
|
ARRAY* r;
|
|
|
|
if(s48_bignum_divide(x,y,&q,&r))
|
|
|
|
raise(SIGFPE);
|
|
|
|
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-25 02:00:52 -04:00
|
|
|
ARRAY* r = s48_bignum_remainder(x,y);
|
|
|
|
if(r == NULL)
|
|
|
|
raise(SIGFPE);
|
|
|
|
return tag_object(r);
|
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-25 02:00:52 -04:00
|
|
|
CELL shiftleft_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_arithmetic_shift(x,
|
|
|
|
s48_bignum_to_long(y)));
|
2004-08-05 15:18:31 -04:00
|
|
|
}
|
|
|
|
|
2004-08-25 02:00:52 -04:00
|
|
|
CELL shiftright_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_arithmetic_shift(x,
|
|
|
|
-s48_bignum_to_long(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-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-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
|
|
|
}
|