factor/native/fixnum.c

214 lines
3.2 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-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);
return to_fixnum(divint(r->numerator,r->denominator));
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-08-27 02:09:24 -04:00
CELL number_eq_fixnum(FIXNUM x, FIXNUM y)
2004-08-05 15:18:31 -04:00
{
return tag_boolean(x == y);
}
2004-08-27 02:09:24 -04:00
CELL add_fixnum(FIXNUM x, FIXNUM y)
2004-08-05 15:18:31 -04:00
{
2004-08-29 15:56:30 -04:00
return tag_integer(x + y);
2004-08-05 15:18:31 -04:00
}
2004-08-27 02:09:24 -04:00
CELL subtract_fixnum(FIXNUM x, FIXNUM y)
2004-08-05 15:18:31 -04:00
{
2004-08-29 15:56:30 -04:00
return 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-08-27 02:09:24 -04:00
CELL multiply_fixnum(FIXNUM x, FIXNUM y)
2004-08-05 15:18:31 -04:00
{
FIXNUM prod;
if(x == 0 || y == 0)
return tag_fixnum(0);
prod = x * y;
/* if this is not equal, we have overflow */
if(prod / x == y)
return tag_integer(prod);
return tag_object(
s48_bignum_multiply(
s48_long_to_bignum(x),
s48_long_to_bignum(y)));
2004-08-05 15:18:31 -04:00
}
2004-08-27 02:09:24 -04:00
CELL divint_fixnum(FIXNUM x, FIXNUM y)
2004-08-05 15:18:31 -04:00
{
2004-08-29 15:56:30 -04:00
return tag_integer(x / y);
2004-08-05 16:49:55 -04:00
}
2004-08-27 02:09:24 -04:00
CELL divfloat_fixnum(FIXNUM x, FIXNUM y)
2004-08-05 16:49:55 -04:00
{
2004-08-27 02:09:24 -04:00
return tag_object(make_float((double)x / (double)y));
2004-08-05 15:18:31 -04:00
}
2004-08-27 02:09:24 -04:00
CELL divmod_fixnum(FIXNUM x, FIXNUM y)
2004-08-05 15:18:31 -04:00
{
2004-08-29 15:56:30 -04:00
dpush(tag_integer(x / y));
return tag_integer(x % y);
2004-08-05 15:18:31 -04:00
}
2004-08-27 02:09:24 -04:00
CELL mod_fixnum(FIXNUM x, FIXNUM y)
2004-08-05 15:18:31 -04:00
{
2004-08-26 22:21:17 -04:00
return tag_fixnum(x % y);
2004-08-05 15:18:31 -04:00
}
FIXNUM gcd_fixnum(FIXNUM x, FIXNUM y)
{
FIXNUM 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-27 02:09:24 -04:00
CELL divide_fixnum(FIXNUM x, FIXNUM y)
2004-08-05 15:18:31 -04:00
{
2004-08-05 16:49:55 -04:00
FIXNUM gcd;
2004-08-05 15:18:31 -04:00
2004-08-27 02:09:24 -04:00
if(y == 0)
2004-08-25 20:51:19 -04:00
raise(SIGFPE);
2004-08-27 02:09:24 -04:00
else if(y < 0)
2004-08-05 15:18:31 -04:00
{
2004-08-27 02:09:24 -04:00
x = -x;
y = -y;
2004-08-05 15:18:31 -04:00
}
2004-08-27 02:09:24 -04:00
gcd = gcd_fixnum(x,y);
2004-08-05 15:18:31 -04:00
if(gcd != 1)
{
2004-08-27 02:09:24 -04:00
x /= gcd;
y /= gcd;
2004-08-05 15:18:31 -04:00
}
2004-08-27 02:09:24 -04:00
if(y == 1)
2004-08-29 15:56:30 -04:00
return tag_integer(x);
2004-08-05 15:18:31 -04:00
else
2004-08-26 22:21:17 -04:00
{
return tag_ratio(ratio(
2004-08-29 15:56:30 -04:00
tag_integer(x),
tag_integer(y)));
2004-08-26 22:21:17 -04:00
}
2004-08-05 15:18:31 -04:00
}
2004-08-27 02:09:24 -04:00
CELL and_fixnum(FIXNUM x, FIXNUM y)
2004-08-05 15:18:31 -04:00
{
2004-08-27 02:35:26 -04:00
return tag_fixnum(x & y);
2004-08-05 15:18:31 -04:00
}
2004-08-27 02:09:24 -04:00
CELL or_fixnum(FIXNUM x, FIXNUM y)
2004-08-05 15:18:31 -04:00
{
2004-08-27 02:35:26 -04:00
return tag_fixnum(x | y);
2004-08-05 15:18:31 -04:00
}
2004-08-27 02:09:24 -04:00
CELL xor_fixnum(FIXNUM x, FIXNUM y)
2004-08-05 15:18:31 -04:00
{
2004-08-27 02:35:26 -04:00
return 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-08-27 02:09:24 -04:00
CELL shift_fixnum(FIXNUM x, FIXNUM y)
2004-08-05 15:18:31 -04:00
{
if(y < 0)
{
if(y <= -WORD_SIZE)
return (x < 0 ? tag_fixnum(-1) : tag_fixnum(0));
else
return tag_fixnum(x >> -y);
}
else if(y == 0)
return tag_fixnum(x);
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)
return tag_fixnum(x << y);
2004-08-26 19:37:22 -04:00
}
return tag_object(s48_bignum_arithmetic_shift(
s48_long_to_bignum(x),y));
2004-08-05 15:18:31 -04:00
}
2004-08-27 02:09:24 -04:00
CELL less_fixnum(FIXNUM x, FIXNUM y)
2004-08-05 15:18:31 -04:00
{
2004-08-27 02:09:24 -04:00
return tag_boolean(x < y);
2004-08-05 15:18:31 -04:00
}
2004-08-27 02:09:24 -04:00
CELL lesseq_fixnum(FIXNUM x, FIXNUM y)
2004-08-05 15:18:31 -04:00
{
2004-08-27 02:09:24 -04:00
return tag_boolean(x <= y);
2004-08-05 15:18:31 -04:00
}
2004-08-27 02:09:24 -04:00
CELL greater_fixnum(FIXNUM x, FIXNUM y)
2004-08-05 15:18:31 -04:00
{
2004-08-27 02:09:24 -04:00
return tag_boolean(x > y);
2004-08-05 15:18:31 -04:00
}
2004-08-27 02:09:24 -04:00
CELL greatereq_fixnum(FIXNUM x, FIXNUM y)
2004-08-05 15:18:31 -04:00
{
2004-08-27 02:09:24 -04:00
return tag_boolean(x >= y);
2004-08-05 15:18:31 -04:00
}
2004-08-17 23:42:10 -04:00
2004-08-27 02:09:24 -04:00
CELL not_fixnum(FIXNUM x)
2004-08-17 23:42:10 -04:00
{
2004-08-27 02:09:24 -04:00
return tag_fixnum(~x);
2004-08-17 23:42:10 -04:00
}