factor/native/bignum.c

276 lines
4.8 KiB
C
Raw Normal View History

#include "factor.h"
CELL to_cell(CELL x)
{
F_FIXNUM fixnum;
F_ARRAY* bignum;
switch(type_of(x))
{
case FIXNUM_TYPE:
fixnum = untag_fixnum_fast(x);
if(fixnum < 0)
{
range_error(F,0,tag_fixnum(fixnum),FIXNUM_MAX);
return -1;
}
else
return (CELL)fixnum;
break;
case BIGNUM_TYPE:
2004-12-25 18:08:20 -05:00
bignum = to_bignum(x);
if(BIGNUM_NEGATIVE_P(bignum))
{
range_error(F,0,tag_bignum(bignum),FIXNUM_MAX);
return -1;
}
else
2005-03-29 20:34:29 -05:00
return s48_bignum_to_long(untag_bignum_fast(x));
default:
2004-12-27 22:58:43 -05:00
type_error(BIGNUM_TYPE,x);
return 0;
}
}
2004-12-25 18:08:20 -05:00
F_ARRAY* to_bignum(CELL tagged)
2004-08-05 15:18:31 -04:00
{
F_RATIO* r;
F_ARRAY* x;
F_ARRAY* y;
F_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:
return (F_ARRAY*)UNTAG(tagged);
2004-08-05 15:18:31 -04:00
case RATIO_TYPE:
r = (F_RATIO*)UNTAG(tagged);
2004-09-19 00:33:40 -04:00
x = to_bignum(r->numerator);
y = to_bignum(r->denominator);
return s48_bignum_quotient(x,y);
2004-08-05 16:49:55 -04:00
case FLOAT_TYPE:
f = (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)
{
maybe_garbage_collection();
drepl(tag_bignum(to_bignum(dpeek())));
2004-08-05 15:18:31 -04:00
}
#define GC_AND_POP_BIGNUMS(x,y) \
F_ARRAY *x, *y; \
maybe_garbage_collection(); \
2004-12-18 20:24:46 -05:00
y = untag_bignum_fast(dpop()); \
x = untag_bignum_fast(dpop());
2005-03-29 20:34:29 -05:00
void primitive_bignum_eq(void)
{
GC_AND_POP_BIGNUMS(x,y);
box_boolean(s48_bignum_equal_p(x,y));
}
2004-09-19 00:33:40 -04:00
void primitive_bignum_add(void)
2004-08-05 15:18:31 -04:00
{
GC_AND_POP_BIGNUMS(x,y);
dpush(tag_bignum(s48_bignum_add(x,y)));
2004-08-05 15:18:31 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_bignum_subtract(void)
2004-08-05 15:18:31 -04:00
{
GC_AND_POP_BIGNUMS(x,y);
dpush(tag_bignum(s48_bignum_subtract(x,y)));
2004-08-05 15:18:31 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_bignum_multiply(void)
2004-08-05 15:18:31 -04:00
{
GC_AND_POP_BIGNUMS(x,y);
dpush(tag_bignum(s48_bignum_multiply(x,y)));
2004-08-05 15:18:31 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_bignum_divint(void)
2004-08-05 15:18:31 -04:00
{
GC_AND_POP_BIGNUMS(x,y);
dpush(tag_bignum(s48_bignum_quotient(x,y)));
2004-08-05 15:18:31 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_bignum_divfloat(void)
2004-08-05 16:49:55 -04:00
{
GC_AND_POP_BIGNUMS(x,y);
dpush(tag_float(
2004-08-25 02:00:52 -04:00
s48_bignum_to_double(x) /
s48_bignum_to_double(y)));
2004-08-05 16:49:55 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_bignum_divmod(void)
2004-08-05 15:18:31 -04:00
{
F_ARRAY *q, *r;
GC_AND_POP_BIGNUMS(x,y);
s48_bignum_divide(x,y,&q,&r);
dpush(tag_bignum(q));
dpush(tag_bignum(r));
2004-08-05 15:18:31 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_bignum_mod(void)
2004-08-05 15:18:31 -04:00
{
GC_AND_POP_BIGNUMS(x,y);
dpush(tag_bignum(s48_bignum_remainder(x,y)));
2004-08-05 15:18:31 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_bignum_and(void)
2004-08-05 15:18:31 -04:00
{
GC_AND_POP_BIGNUMS(x,y);
dpush(tag_bignum(s48_bignum_bitwise_and(x,y)));
2004-08-05 15:18:31 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_bignum_or(void)
2004-08-05 15:18:31 -04:00
{
GC_AND_POP_BIGNUMS(x,y);
dpush(tag_bignum(s48_bignum_bitwise_ior(x,y)));
2004-08-05 15:18:31 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_bignum_xor(void)
2004-08-05 15:18:31 -04:00
{
GC_AND_POP_BIGNUMS(x,y);
dpush(tag_bignum(s48_bignum_bitwise_xor(x,y)));
2004-08-05 15:18:31 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_bignum_shift(void)
2004-08-05 15:18:31 -04:00
{
F_FIXNUM y;
F_ARRAY* x;
maybe_garbage_collection();
y = to_fixnum(dpop());
x = to_bignum(dpop());
dpush(tag_bignum(s48_bignum_arithmetic_shift(x,y)));
2004-08-05 15:18:31 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_bignum_less(void)
2004-08-05 15:18:31 -04:00
{
GC_AND_POP_BIGNUMS(x,y);
2004-11-08 22:36:51 -05:00
box_boolean(s48_bignum_compare(x,y) == bignum_comparison_less);
2004-08-05 15:18:31 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_bignum_lesseq(void)
2004-08-05 15:18:31 -04:00
{
GC_AND_POP_BIGNUMS(x,y);
2004-08-25 02:00:52 -04:00
switch(s48_bignum_compare(x,y))
{
case bignum_comparison_less:
case bignum_comparison_equal:
2004-09-19 00:33:40 -04:00
dpush(T);
break;
2004-08-25 02:00:52 -04:00
case bignum_comparison_greater:
2004-09-19 00:33:40 -04:00
dpush(F);
break;
2004-08-27 23:20:10 -04:00
default:
critical_error("s48_bignum_compare returns bogus value",0);
2004-09-19 00:33:40 -04:00
break;
2004-08-25 02:00:52 -04:00
}
2004-08-05 15:18:31 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_bignum_greater(void)
2004-08-05 15:18:31 -04:00
{
GC_AND_POP_BIGNUMS(x,y);
2004-11-08 22:36:51 -05:00
box_boolean(s48_bignum_compare(x,y) == bignum_comparison_greater);
2004-08-25 02:00:52 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_bignum_greatereq(void)
2004-08-25 02:00:52 -04:00
{
GC_AND_POP_BIGNUMS(x,y);
2004-08-25 02:00:52 -04:00
switch(s48_bignum_compare(x,y))
{
case bignum_comparison_less:
2004-09-19 00:33:40 -04:00
dpush(F);
break;
2004-08-25 02:00:52 -04:00
case bignum_comparison_equal:
case bignum_comparison_greater:
2004-09-19 00:33:40 -04:00
dpush(T);
break;
2004-08-27 23:20:10 -04:00
default:
critical_error("s48_bignum_compare returns bogus value",0);
2004-09-19 00:33:40 -04:00
break;
2004-08-25 02:00:52 -04:00
}
2004-08-05 15:18:31 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_bignum_not(void)
2004-08-05 15:18:31 -04:00
{
maybe_garbage_collection();
drepl(tag_bignum(s48_bignum_bitwise_not(
2005-03-29 20:34:29 -05:00
untag_bignum_fast(dpeek()))));
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
{
2005-02-19 23:25:21 -05:00
COPY_OBJECT(bignum_zero);
COPY_OBJECT(bignum_pos_one);
COPY_OBJECT(bignum_neg_one);
2004-08-17 23:42:10 -04:00
}
void box_signed_cell(F_FIXNUM integer)
{
dpush(tag_integer(integer));
}
F_FIXNUM unbox_signed_cell(void)
{
return to_fixnum(dpop());
}
void box_unsigned_cell(CELL cell)
{
dpush(tag_cell(cell));
}
F_FIXNUM unbox_unsigned_cell(void)
{
return to_cell(dpop());
}
void box_signed_4(s32 n)
{
dpush(tag_bignum(s48_long_to_bignum(n)));
}
s32 unbox_signed_4(void)
{
2005-04-07 18:54:02 -04:00
return to_fixnum(dpop());
}
void box_unsigned_4(u32 n)
{
dpush(tag_bignum(s48_ulong_to_bignum(n)));
}
u32 unbox_unsigned_4(void)
{
2005-04-07 18:54:02 -04:00
return to_cell(dpop());
}
void box_signed_8(s64 n)
{
dpush(tag_bignum(s48_long_long_to_bignum(n)));
}
s64 unbox_signed_8(void)
{
return 0; /* s48_bignum_to_long_long(to_bignum(dpop())); */
}
void box_unsigned_8(u64 n)
{
dpush(tag_bignum(s48_long_long_to_bignum(n)));
}
u64 unbox_unsigned_8(void)
{
return 0; /* s48_bignum_to_long_long(to_bignum(dpop())); */
}