factor/native/fixnum.c

205 lines
3.8 KiB
C
Raw Normal View History

2004-07-16 02:26:21 -04:00
#include "factor.h"
2004-08-05 15:18:31 -04:00
FIXNUM to_fixnum(CELL tagged)
{
RATIO* r;
2004-09-19 00:33:40 -04:00
ARRAY* x;
ARRAY* y;
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 untag_fixnum_fast(tagged);
case BIGNUM_TYPE:
2004-08-27 02:09:24 -04:00
return (FIXNUM)s48_bignum_to_long((ARRAY*)UNTAG(tagged));
2004-08-05 15:18:31 -04:00
case RATIO_TYPE:
r = (RATIO*)UNTAG(tagged);
2004-09-19 00:33:40 -04:00
x = to_bignum(r->numerator);
y = to_bignum(r->denominator);
return to_fixnum(tag_object(s48_bignum_quotient(x,y)));
2004-08-05 16:49:55 -04:00
case FLOAT_TYPE:
f = (FLOAT*)UNTAG(tagged);
return (FIXNUM)f->n;
2004-08-05 15:18:31 -04:00
default:
type_error(FIXNUM_TYPE,tagged);
return -1; /* can't happen */
}
}
void primitive_to_fixnum(void)
{
drepl(tag_fixnum(to_fixnum(dpeek())));
2004-08-05 15:18:31 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_fixnum_eq(void)
2004-08-05 15:18:31 -04:00
{
2004-09-19 00:33:40 -04:00
FIXNUM y = to_fixnum(dpop());
FIXNUM x = to_fixnum(dpop());
dpush(tag_boolean(x == y));
2004-08-05 15:18:31 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_fixnum_add(void)
2004-08-05 15:18:31 -04:00
{
2004-09-19 00:33:40 -04:00
FIXNUM y = to_fixnum(dpop());
FIXNUM x = to_fixnum(dpop());
dpush(tag_integer(x + y));
2004-08-05 15:18:31 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_fixnum_subtract(void)
2004-08-05 15:18:31 -04:00
{
2004-09-19 00:33:40 -04:00
FIXNUM y = to_fixnum(dpop());
FIXNUM x = to_fixnum(dpop());
dpush(tag_integer(x - y));
2004-08-05 15:18:31 -04:00
}
/**
* Multiply two integers, and trap overflow.
* Thanks to David Blaikie (The_Vulture from freenode #java) for the hint.
*/
2004-09-19 00:33:40 -04:00
void primitive_fixnum_multiply(void)
2004-08-05 15:18:31 -04:00
{
2004-09-19 00:33:40 -04:00
FIXNUM y = to_fixnum(dpop());
FIXNUM x = to_fixnum(dpop());
if(x == 0 || y == 0)
2004-09-19 00:33:40 -04:00
dpush(tag_fixnum(0));
else
{
FIXNUM prod = x * y;
/* if this is not equal, we have overflow */
if(prod / x == y)
dpush(tag_integer(prod));
else
{
dpush(tag_object(
s48_bignum_multiply(
s48_long_to_bignum(x),
s48_long_to_bignum(y))));
}
}
2004-08-05 15:18:31 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_fixnum_divint(void)
2004-08-05 15:18:31 -04:00
{
2004-09-19 00:33:40 -04:00
FIXNUM y = to_fixnum(dpop());
FIXNUM x = to_fixnum(dpop());
2004-08-29 15:56:30 -04:00
dpush(tag_integer(x / y));
2004-08-05 15:18:31 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_fixnum_divfloat(void)
2004-08-05 15:18:31 -04:00
{
2004-09-19 00:33:40 -04:00
FIXNUM y = to_fixnum(dpop());
FIXNUM x = to_fixnum(dpop());
dpush(tag_object(make_float((double)x / (double)y)));
2004-08-05 15:18:31 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_fixnum_divmod(void)
2004-08-05 15:18:31 -04:00
{
2004-09-19 00:33:40 -04:00
FIXNUM y = to_fixnum(dpop());
FIXNUM x = to_fixnum(dpop());
dpush(tag_integer(x / y));
dpush(tag_integer(x % y));
2004-08-05 15:18:31 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_fixnum_mod(void)
2004-08-05 15:18:31 -04:00
{
2004-09-19 00:33:40 -04:00
FIXNUM y = to_fixnum(dpop());
FIXNUM x = to_fixnum(dpop());
dpush(tag_fixnum(x % y));
2004-08-05 15:18:31 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_fixnum_and(void)
2004-08-05 15:18:31 -04:00
{
2004-09-19 00:33:40 -04:00
FIXNUM y = to_fixnum(dpop());
FIXNUM x = to_fixnum(dpop());
dpush(tag_fixnum(x & y));
2004-08-05 15:18:31 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_fixnum_or(void)
2004-08-05 15:18:31 -04:00
{
2004-09-19 00:33:40 -04:00
FIXNUM y = to_fixnum(dpop());
FIXNUM x = to_fixnum(dpop());
dpush(tag_fixnum(x | y));
2004-08-05 15:18:31 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_fixnum_xor(void)
2004-08-05 15:18:31 -04:00
{
2004-09-19 00:33:40 -04:00
FIXNUM y = to_fixnum(dpop());
FIXNUM x = to_fixnum(dpop());
dpush(tag_fixnum(x ^ y));
2004-08-05 15:18:31 -04:00
}
/*
* Note the hairy overflow check.
* If we're shifting right by n bits, we won't overflow as long as none of the
* high WORD_SIZE-TAG_BITS-n bits are set.
*/
2004-09-19 00:33:40 -04:00
void primitive_fixnum_shift(void)
2004-08-05 15:18:31 -04:00
{
2004-09-19 00:33:40 -04:00
FIXNUM y = to_fixnum(dpop());
FIXNUM x = to_fixnum(dpop());
if(y < 0)
{
if(y <= -WORD_SIZE)
2004-09-19 00:33:40 -04:00
dpush(x < 0 ? tag_fixnum(-1) : tag_fixnum(0));
else
2004-09-19 00:33:40 -04:00
dpush(tag_fixnum(x >> -y));
return;
}
else if(y == 0)
2004-09-19 00:33:40 -04:00
{
dpush(tag_fixnum(x));
return;
}
else if(y < WORD_SIZE - TAG_BITS)
2004-08-26 19:37:22 -04:00
{
FIXNUM mask = (1 << (WORD_SIZE - 1 - TAG_BITS - y));
if(x > 0)
mask = -mask;
2004-08-05 15:18:31 -04:00
if((x & mask) == 0)
2004-09-19 00:33:40 -04:00
{
dpush(tag_fixnum(x << y));
return;
}
2004-08-26 19:37:22 -04:00
}
2004-09-19 00:33:40 -04:00
dpush(tag_object(s48_bignum_arithmetic_shift(
s48_long_to_bignum(x),y)));
2004-08-05 15:18:31 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_fixnum_less(void)
2004-08-05 15:18:31 -04:00
{
2004-09-19 00:33:40 -04:00
FIXNUM y = to_fixnum(dpop());
FIXNUM x = to_fixnum(dpop());
dpush(tag_boolean(x < y));
2004-08-05 15:18:31 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_fixnum_lesseq(void)
2004-08-05 15:18:31 -04:00
{
2004-09-19 00:33:40 -04:00
FIXNUM y = to_fixnum(dpop());
FIXNUM x = to_fixnum(dpop());
dpush(tag_boolean(x <= y));
2004-08-05 15:18:31 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_fixnum_greater(void)
2004-08-05 15:18:31 -04:00
{
2004-09-19 00:33:40 -04:00
FIXNUM y = to_fixnum(dpop());
FIXNUM x = to_fixnum(dpop());
dpush(tag_boolean(x > y));
2004-08-05 15:18:31 -04:00
}
2004-09-19 00:33:40 -04:00
void primitive_fixnum_greatereq(void)
2004-08-05 15:18:31 -04:00
{
2004-09-19 00:33:40 -04:00
FIXNUM y = to_fixnum(dpop());
FIXNUM x = to_fixnum(dpop());
dpush(tag_boolean(x >= y));
2004-08-05 15:18:31 -04:00
}
2004-08-17 23:42:10 -04:00
2004-09-19 00:33:40 -04:00
void primitive_fixnum_not(void)
2004-08-17 23:42:10 -04:00
{
2004-09-19 00:33:40 -04:00
drepl(tag_fixnum(~to_fixnum(dpeek())));
2004-08-17 23:42:10 -04:00
}