factor/native/arithmetic.c

149 lines
2.4 KiB
C
Raw Normal View History

2004-07-28 19:02:24 -04:00
#include "factor.h"
2004-08-29 15:56:30 -04:00
CELL tag_integer(FIXNUM x)
2004-08-26 22:21:17 -04:00
{
if(x < FIXNUM_MIN || x > FIXNUM_MAX)
return tag_object(s48_long_to_bignum(x));
else
return tag_fixnum(x);
}
2004-09-06 02:32:04 -04:00
CELL tag_cell(CELL x)
2004-08-29 15:56:30 -04:00
{
if(x > FIXNUM_MAX)
return tag_object(s48_ulong_to_bignum(x));
else
return tag_fixnum(x);
}
2004-09-06 02:32:04 -04:00
CELL to_cell(CELL x)
{
switch(type_of(x))
{
case FIXNUM_TYPE:
return untag_fixnum_fast(x);
case BIGNUM_TYPE:
/* really need bignum_to_ulong! */
return s48_bignum_to_long(untag_bignum(x));
default:
type_error(INTEGER_TYPE,x);
return 0;
2004-09-06 02:32:04 -04:00
}
}
CELL upgraded_arithmetic_type(CELL type1, CELL type2)
{
switch(type1)
{
case FIXNUM_TYPE:
return type2;
case BIGNUM_TYPE:
switch(type2)
{
case FIXNUM_TYPE:
return type1;
default:
return type2;
}
case RATIO_TYPE:
switch(type2)
{
case FIXNUM_TYPE:
case BIGNUM_TYPE:
return type1;
default:
return type2;
}
case FLOAT_TYPE:
switch(type2)
{
case FIXNUM_TYPE:
case BIGNUM_TYPE:
case RATIO_TYPE:
return type1;
default:
return type2;
}
case COMPLEX_TYPE:
switch(type2)
{
case FIXNUM_TYPE:
case BIGNUM_TYPE:
case RATIO_TYPE:
case FLOAT_TYPE:
return type1;
default:
return type2;
}
default:
return type1;
}
}
2004-09-19 00:33:40 -04:00
void primitive_arithmetic_type(void)
{
CELL type2 = type_of(dpop());
CELL type1 = type_of(dpop());
dpush(tag_fixnum(upgraded_arithmetic_type(type1,type2)));
}
2004-08-05 20:29:52 -04:00
bool realp(CELL tagged)
{
2004-08-05 20:29:52 -04:00
switch(type_of(tagged))
{
case FIXNUM_TYPE:
case BIGNUM_TYPE:
2004-08-04 22:43:58 -04:00
case RATIO_TYPE:
2004-08-05 17:33:02 -04:00
case FLOAT_TYPE:
2004-08-05 20:29:52 -04:00
return true;
2004-08-05 15:18:31 -04:00
break;
default:
2004-08-05 20:29:52 -04:00
return false;
2004-08-05 15:18:31 -04:00
break;
}
}
2004-08-05 20:29:52 -04:00
void primitive_numberp(void)
{
2004-08-27 02:09:24 -04:00
CELL tagged = dpeek();
drepl(tag_boolean(realp(tagged) || type_of(tagged) == COMPLEX_TYPE));
2004-08-05 20:29:52 -04:00
}
bool zerop(CELL tagged)
{
switch(type_of(tagged))
{
case FIXNUM_TYPE:
return tagged == 0;
case BIGNUM_TYPE:
2004-08-25 02:00:52 -04:00
return BIGNUM_ZERO_P((ARRAY*)UNTAG(tagged));
2004-08-05 20:29:52 -04:00
case FLOAT_TYPE:
return ((FLOAT*)UNTAG(tagged))->n == 0.0;
case RATIO_TYPE:
2004-09-18 22:29:29 -04:00
case COMPLEX_TYPE:
return false;
default:
type_error(NUMBER_TYPE,tagged);
return false; /* Can't happen */
}
}
bool onep(CELL tagged)
{
switch(type_of(tagged))
{
case FIXNUM_TYPE:
2004-09-19 00:33:40 -04:00
return tagged == tag_fixnum(1);
2004-09-18 22:29:29 -04:00
case BIGNUM_TYPE:
return BIGNUM_ONE_P((ARRAY*)UNTAG(tagged),0);
case FLOAT_TYPE:
return ((FLOAT*)UNTAG(tagged))->n == 1.0;
case RATIO_TYPE:
case COMPLEX_TYPE:
2004-08-05 20:29:52 -04:00
return false;
default:
2004-09-18 22:29:29 -04:00
type_error(NUMBER_TYPE,tagged);
2004-08-05 20:29:52 -04:00
return false; /* Can't happen */
}
}