factor/native/bignum.c

229 lines
4.2 KiB
C
Raw Normal View History

#include "factor.h"
F_FIXNUM to_integer(CELL x)
2004-09-19 17:39:28 -04:00
{
switch(type_of(x))
{
case FIXNUM_TYPE:
return untag_fixnum_fast(x);
case BIGNUM_TYPE:
return s48_bignum_to_long(untag_bignum(x));
default:
type_error(INTEGER_TYPE,x);
return 0;
}
}
/* FFI calls this */
void box_integer(F_FIXNUM integer)
2004-09-19 17:39:28 -04:00
{
dpush(tag_integer(integer));
}
/* FFI calls this */
void box_cell(CELL cell)
{
dpush(tag_cell(cell));
}
2004-09-19 17:39:28 -04:00
/* FFI calls this */
F_FIXNUM unbox_integer(void)
2004-09-19 17:39:28 -04:00
{
return to_integer(dpop());
}
/* FFI calls this */
CELL unbox_cell(void)
{
return to_integer(dpop());
}
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_object(to_bignum(dpeek())));
2004-08-05 15:18:31 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_bignum_eq(void)
2004-08-05 15:18:31 -04:00
{
F_ARRAY* y = to_bignum(dpop());
F_ARRAY* x = to_bignum(dpop());
2004-11-08 22:36:51 -05:00
box_boolean(s48_bignum_equal_p(x,y));
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());
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);
2004-09-19 00:33:40 -04:00
dpush(tag_object(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);
2004-09-19 00:33:40 -04:00
dpush(tag_object(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);
2004-09-19 00:33:40 -04:00
dpush(tag_object(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);
2004-09-19 00:33:40 -04:00
dpush(tag_object(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);
2004-09-19 00:33:40 -04:00
dpush(tag_object(make_float(
2004-08-25 02:00:52 -04:00
s48_bignum_to_double(x) /
2004-09-19 00:33:40 -04:00
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);
2004-08-25 02:00:52 -04:00
dpush(tag_object(q));
2004-09-19 00:33:40 -04:00
dpush(tag_object(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);
2004-09-19 00:33:40 -04:00
dpush(tag_object(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);
2004-09-19 00:33:40 -04:00
dpush(tag_object(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);
2004-09-19 00:33:40 -04:00
dpush(tag_object(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);
2004-09-19 00:33:40 -04:00
dpush(tag_object(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());
2004-09-19 00:33:40 -04:00
dpush(tag_object(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
{
F_ARRAY* y = to_bignum(dpop());
F_ARRAY* x = to_bignum(dpop());
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
{
F_ARRAY* y = to_bignum(dpop());
F_ARRAY* x = to_bignum(dpop());
2004-09-19 00:33:40 -04:00
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
{
F_ARRAY* y = to_bignum(dpop());
F_ARRAY* x = to_bignum(dpop());
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
{
F_ARRAY* y = to_bignum(dpop());
F_ARRAY* x = to_bignum(dpop());
2004-09-19 00:33:40 -04:00
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();
2004-09-19 00:33:40 -04:00
drepl(tag_object(s48_bignum_bitwise_not(
untag_bignum(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
{
2004-08-29 01:04:42 -04:00
copy_object(&bignum_zero);
copy_object(&bignum_pos_one);
copy_object(&bignum_neg_one);
2004-08-17 23:42:10 -04:00
}