factor/vm/math.cpp

517 lines
9.1 KiB
C++
Raw Normal View History

2009-05-02 05:04:19 -04:00
#include "master.hpp"
2009-05-04 02:46:13 -04:00
namespace factor
{
2009-05-02 05:04:19 -04:00
CELL bignum_zero;
CELL bignum_pos_one;
CELL bignum_neg_one;
PRIMITIVE(bignum_to_fixnum)
2009-05-02 05:04:19 -04:00
{
drepl(tag_fixnum(bignum_to_fixnum(untag<F_BIGNUM>(dpeek()))));
2009-05-02 05:04:19 -04:00
}
PRIMITIVE(float_to_fixnum)
2009-05-02 05:04:19 -04:00
{
drepl(tag_fixnum(float_to_fixnum(dpeek())));
}
/* Division can only overflow when we are dividing the most negative fixnum
by -1. */
PRIMITIVE(fixnum_divint)
2009-05-02 05:04:19 -04:00
{
F_FIXNUM y = untag_fixnum(dpop()); \
F_FIXNUM x = untag_fixnum(dpeek());
2009-05-02 05:04:19 -04:00
F_FIXNUM result = x / y;
if(result == -FIXNUM_MIN)
drepl(allot_integer(-FIXNUM_MIN));
else
drepl(tag_fixnum(result));
}
PRIMITIVE(fixnum_divmod)
2009-05-02 05:04:19 -04:00
{
CELL y = ((CELL *)ds)[0];
CELL x = ((CELL *)ds)[-1];
2009-05-02 05:04:19 -04:00
if(y == tag_fixnum(-1) && x == tag_fixnum(FIXNUM_MIN))
{
((CELL *)ds)[-1] = allot_integer(-FIXNUM_MIN);
((CELL *)ds)[0] = tag_fixnum(0);
2009-05-02 05:04:19 -04:00
}
else
{
((CELL *)ds)[-1] = tag_fixnum(untag_fixnum(x) / untag_fixnum(y));
((CELL *)ds)[0] = (F_FIXNUM)x % (F_FIXNUM)y;
2009-05-02 05:04:19 -04:00
}
}
/*
* 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.
*/
#define SIGN_MASK(x) ((x) >> (WORD_SIZE - 1))
#define BRANCHLESS_MAX(x,y) ((x) - (((x) - (y)) & SIGN_MASK((x) - (y))))
#define BRANCHLESS_ABS(x) ((x ^ SIGN_MASK(x)) - SIGN_MASK(x))
PRIMITIVE(fixnum_shift)
2009-05-02 05:04:19 -04:00
{
F_FIXNUM y = untag_fixnum(dpop()); \
F_FIXNUM x = untag_fixnum(dpeek());
2009-05-02 05:04:19 -04:00
if(x == 0)
return;
else if(y < 0)
{
y = BRANCHLESS_MAX(y,-WORD_SIZE + 1);
drepl(tag_fixnum(x >> -y));
return;
}
else if(y < WORD_SIZE - TAG_BITS)
{
F_FIXNUM mask = -((F_FIXNUM)1 << (WORD_SIZE - 1 - TAG_BITS - y));
if(!(BRANCHLESS_ABS(x) & mask))
{
drepl(tag_fixnum(x << y));
return;
}
}
drepl(tag<F_BIGNUM>(bignum_arithmetic_shift(
2009-05-02 05:04:19 -04:00
fixnum_to_bignum(x),y)));
}
PRIMITIVE(fixnum_to_bignum)
2009-05-02 05:04:19 -04:00
{
drepl(tag<F_BIGNUM>(fixnum_to_bignum(untag_fixnum(dpeek()))));
2009-05-02 05:04:19 -04:00
}
PRIMITIVE(float_to_bignum)
2009-05-02 05:04:19 -04:00
{
drepl(tag<F_BIGNUM>(float_to_bignum(dpeek())));
2009-05-02 05:04:19 -04:00
}
#define POP_BIGNUMS(x,y) \
F_BIGNUM * y = untag<F_BIGNUM>(dpop()); \
F_BIGNUM * x = untag<F_BIGNUM>(dpop());
2009-05-02 05:04:19 -04:00
PRIMITIVE(bignum_eq)
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
box_boolean(bignum_equal_p(x,y));
}
PRIMITIVE(bignum_add)
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
dpush(tag<F_BIGNUM>(bignum_add(x,y)));
2009-05-02 05:04:19 -04:00
}
PRIMITIVE(bignum_subtract)
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
dpush(tag<F_BIGNUM>(bignum_subtract(x,y)));
2009-05-02 05:04:19 -04:00
}
PRIMITIVE(bignum_multiply)
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
dpush(tag<F_BIGNUM>(bignum_multiply(x,y)));
2009-05-02 05:04:19 -04:00
}
PRIMITIVE(bignum_divint)
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
dpush(tag<F_BIGNUM>(bignum_quotient(x,y)));
2009-05-02 05:04:19 -04:00
}
PRIMITIVE(bignum_divmod)
2009-05-02 05:04:19 -04:00
{
2009-05-02 05:43:58 -04:00
F_BIGNUM *q, *r;
2009-05-02 05:04:19 -04:00
POP_BIGNUMS(x,y);
bignum_divide(x,y,&q,&r);
dpush(tag<F_BIGNUM>(q));
dpush(tag<F_BIGNUM>(r));
2009-05-02 05:04:19 -04:00
}
PRIMITIVE(bignum_mod)
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
dpush(tag<F_BIGNUM>(bignum_remainder(x,y)));
2009-05-02 05:04:19 -04:00
}
PRIMITIVE(bignum_and)
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
dpush(tag<F_BIGNUM>(bignum_bitwise_and(x,y)));
2009-05-02 05:04:19 -04:00
}
PRIMITIVE(bignum_or)
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
dpush(tag<F_BIGNUM>(bignum_bitwise_ior(x,y)));
2009-05-02 05:04:19 -04:00
}
PRIMITIVE(bignum_xor)
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
dpush(tag<F_BIGNUM>(bignum_bitwise_xor(x,y)));
2009-05-02 05:04:19 -04:00
}
PRIMITIVE(bignum_shift)
2009-05-02 05:04:19 -04:00
{
F_FIXNUM y = untag_fixnum(dpop());
F_BIGNUM* x = untag<F_BIGNUM>(dpop());
dpush(tag<F_BIGNUM>(bignum_arithmetic_shift(x,y)));
2009-05-02 05:04:19 -04:00
}
PRIMITIVE(bignum_less)
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
box_boolean(bignum_compare(x,y) == bignum_comparison_less);
}
PRIMITIVE(bignum_lesseq)
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
box_boolean(bignum_compare(x,y) != bignum_comparison_greater);
}
PRIMITIVE(bignum_greater)
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
box_boolean(bignum_compare(x,y) == bignum_comparison_greater);
}
PRIMITIVE(bignum_greatereq)
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
box_boolean(bignum_compare(x,y) != bignum_comparison_less);
}
PRIMITIVE(bignum_not)
2009-05-02 05:04:19 -04:00
{
drepl(tag<F_BIGNUM>(bignum_bitwise_not(untag<F_BIGNUM>(dpeek()))));
2009-05-02 05:04:19 -04:00
}
PRIMITIVE(bignum_bitp)
2009-05-02 05:04:19 -04:00
{
F_FIXNUM bit = to_fixnum(dpop());
F_BIGNUM *x = untag<F_BIGNUM>(dpop());
2009-05-02 05:04:19 -04:00
box_boolean(bignum_logbitp(bit,x));
}
PRIMITIVE(bignum_log2)
2009-05-02 05:04:19 -04:00
{
drepl(tag<F_BIGNUM>(bignum_integer_length(untag<F_BIGNUM>(dpeek()))));
2009-05-02 05:04:19 -04:00
}
unsigned int bignum_producer(unsigned int digit)
{
unsigned char *ptr = (unsigned char *)alien_offset(dpeek());
return *(ptr + digit);
}
PRIMITIVE(byte_array_to_bignum)
2009-05-02 05:04:19 -04:00
{
CELL n_digits = array_capacity(untag_check<F_BYTE_ARRAY>(dpeek()));
2009-05-02 21:01:54 -04:00
F_BIGNUM * bignum = digit_stream_to_bignum(n_digits,bignum_producer,0x100,0);
drepl(tag<F_BIGNUM>(bignum));
2009-05-02 05:04:19 -04:00
}
CELL unbox_array_size(void)
{
switch(tagged<F_OBJECT>(dpeek()).type())
2009-05-02 05:04:19 -04:00
{
case FIXNUM_TYPE:
{
F_FIXNUM n = untag_fixnum(dpeek());
2009-05-02 05:04:19 -04:00
if(n >= 0 && n < (F_FIXNUM)ARRAY_SIZE_MAX)
{
dpop();
return n;
}
break;
}
case BIGNUM_TYPE:
{
F_BIGNUM * zero = untag<F_BIGNUM>(bignum_zero);
2009-05-02 05:43:58 -04:00
F_BIGNUM * max = cell_to_bignum(ARRAY_SIZE_MAX);
F_BIGNUM * n = untag<F_BIGNUM>(dpeek());
2009-05-02 05:04:19 -04:00
if(bignum_compare(n,zero) != bignum_comparison_less
&& bignum_compare(n,max) == bignum_comparison_less)
{
dpop();
return bignum_to_cell(n);
}
break;
}
}
general_error(ERROR_ARRAY_SIZE,dpop(),tag_fixnum(ARRAY_SIZE_MAX),NULL);
return 0; /* can't happen */
}
PRIMITIVE(fixnum_to_float)
2009-05-02 05:04:19 -04:00
{
drepl(allot_float(fixnum_to_float(dpeek())));
}
PRIMITIVE(bignum_to_float)
2009-05-02 05:04:19 -04:00
{
drepl(allot_float(bignum_to_float(dpeek())));
}
PRIMITIVE(str_to_float)
2009-05-02 05:04:19 -04:00
{
F_BYTE_ARRAY *bytes = untag_check<F_BYTE_ARRAY>(dpeek());
CELL capacity = array_capacity(bytes);
char *c_str = (char *)(bytes + 1);
char *end = c_str;
double f = strtod(c_str,&end);
if(end == c_str + capacity - 1)
2009-05-02 05:04:19 -04:00
drepl(allot_float(f));
else
drepl(F);
2009-05-02 05:04:19 -04:00
}
PRIMITIVE(float_to_str)
2009-05-02 05:04:19 -04:00
{
F_BYTE_ARRAY *array = allot_byte_array(33);
snprintf((char *)(array + 1),32,"%.16g",untag_float_check(dpop()));
dpush(tag<F_BYTE_ARRAY>(array));
2009-05-02 05:04:19 -04:00
}
#define POP_FLOATS(x,y) \
double y = untag_float(dpop()); \
double x = untag_float(dpop());
2009-05-02 05:04:19 -04:00
PRIMITIVE(float_eq)
2009-05-02 05:04:19 -04:00
{
POP_FLOATS(x,y);
box_boolean(x == y);
}
PRIMITIVE(float_add)
2009-05-02 05:04:19 -04:00
{
POP_FLOATS(x,y);
box_double(x + y);
}
PRIMITIVE(float_subtract)
2009-05-02 05:04:19 -04:00
{
POP_FLOATS(x,y);
box_double(x - y);
}
PRIMITIVE(float_multiply)
2009-05-02 05:04:19 -04:00
{
POP_FLOATS(x,y);
box_double(x * y);
}
PRIMITIVE(float_divfloat)
2009-05-02 05:04:19 -04:00
{
POP_FLOATS(x,y);
box_double(x / y);
}
PRIMITIVE(float_mod)
2009-05-02 05:04:19 -04:00
{
POP_FLOATS(x,y);
box_double(fmod(x,y));
}
PRIMITIVE(float_less)
2009-05-02 05:04:19 -04:00
{
POP_FLOATS(x,y);
box_boolean(x < y);
}
PRIMITIVE(float_lesseq)
2009-05-02 05:04:19 -04:00
{
POP_FLOATS(x,y);
box_boolean(x <= y);
}
PRIMITIVE(float_greater)
2009-05-02 05:04:19 -04:00
{
POP_FLOATS(x,y);
box_boolean(x > y);
}
PRIMITIVE(float_greatereq)
2009-05-02 05:04:19 -04:00
{
POP_FLOATS(x,y);
box_boolean(x >= y);
}
PRIMITIVE(float_bits)
2009-05-02 05:04:19 -04:00
{
box_unsigned_4(float_bits(untag_float_check(dpop())));
2009-05-02 05:04:19 -04:00
}
PRIMITIVE(bits_float)
2009-05-02 05:04:19 -04:00
{
box_float(bits_float(to_cell(dpop())));
}
PRIMITIVE(double_bits)
2009-05-02 05:04:19 -04:00
{
box_unsigned_8(double_bits(untag_float_check(dpop())));
2009-05-02 05:04:19 -04:00
}
PRIMITIVE(bits_double)
2009-05-02 05:04:19 -04:00
{
box_double(bits_double(to_unsigned_8(dpop())));
}
VM_C_API F_FIXNUM to_fixnum(CELL tagged)
2009-05-02 05:04:19 -04:00
{
switch(TAG(tagged))
{
case FIXNUM_TYPE:
return untag_fixnum(tagged);
case BIGNUM_TYPE:
return bignum_to_fixnum(untag<F_BIGNUM>(tagged));
default:
type_error(FIXNUM_TYPE,tagged);
return -1; /* can't happen */
}
2009-05-02 05:04:19 -04:00
}
VM_C_API CELL to_cell(CELL tagged)
2009-05-02 05:04:19 -04:00
{
return (CELL)to_fixnum(tagged);
}
VM_C_API void box_signed_1(s8 n)
{
dpush(tag_fixnum(n));
}
VM_C_API void box_unsigned_1(u8 n)
{
dpush(tag_fixnum(n));
}
VM_C_API void box_signed_2(s16 n)
{
dpush(tag_fixnum(n));
2009-05-02 05:04:19 -04:00
}
VM_C_API void box_unsigned_2(u16 n)
{
dpush(tag_fixnum(n));
}
VM_C_API void box_signed_4(s32 n)
{
dpush(allot_integer(n));
}
VM_C_API void box_unsigned_4(u32 n)
{
dpush(allot_cell(n));
}
VM_C_API void box_signed_cell(F_FIXNUM integer)
{
dpush(allot_integer(integer));
}
VM_C_API void box_unsigned_cell(CELL cell)
{
dpush(allot_cell(cell));
}
VM_C_API void box_signed_8(s64 n)
{
if(n < FIXNUM_MIN || n > FIXNUM_MAX)
dpush(tag<F_BIGNUM>(long_long_to_bignum(n)));
else
dpush(tag_fixnum(n));
}
VM_C_API s64 to_signed_8(CELL obj)
{
switch(tagged<F_OBJECT>(obj).type())
{
case FIXNUM_TYPE:
return untag_fixnum(obj);
case BIGNUM_TYPE:
return bignum_to_long_long(untag<F_BIGNUM>(obj));
default:
type_error(BIGNUM_TYPE,obj);
return -1;
}
}
VM_C_API void box_unsigned_8(u64 n)
{
if(n > FIXNUM_MAX)
dpush(tag<F_BIGNUM>(ulong_long_to_bignum(n)));
else
dpush(tag_fixnum(n));
}
VM_C_API u64 to_unsigned_8(CELL obj)
{
switch(tagged<F_OBJECT>(obj).type())
{
case FIXNUM_TYPE:
return untag_fixnum(obj);
case BIGNUM_TYPE:
return bignum_to_ulong_long(untag<F_BIGNUM>(obj));
default:
type_error(BIGNUM_TYPE,obj);
return -1;
}
}
VM_C_API void box_float(float flo)
2009-05-02 05:04:19 -04:00
{
dpush(allot_float(flo));
}
VM_C_API float to_float(CELL value)
{
return untag_float_check(value);
}
VM_C_API void box_double(double flo)
2009-05-02 05:04:19 -04:00
{
dpush(allot_float(flo));
}
VM_C_API double to_double(CELL value)
{
return untag_float_check(value);
}
/* The fixnum+, fixnum- and fixnum* primitives are defined in cpu_*.S. On
overflow, they call these functions. */
VM_ASM_API void overflow_fixnum_add(F_FIXNUM x, F_FIXNUM y)
{
drepl(tag<F_BIGNUM>(fixnum_to_bignum(
untag_fixnum(x) + untag_fixnum(y))));
}
VM_ASM_API void overflow_fixnum_subtract(F_FIXNUM x, F_FIXNUM y)
{
drepl(tag<F_BIGNUM>(fixnum_to_bignum(
untag_fixnum(x) - untag_fixnum(y))));
}
VM_ASM_API void overflow_fixnum_multiply(F_FIXNUM x, F_FIXNUM y)
{
F_BIGNUM *bx = fixnum_to_bignum(x);
GC_BIGNUM(bx);
F_BIGNUM *by = fixnum_to_bignum(y);
GC_BIGNUM(by);
drepl(tag<F_BIGNUM>(bignum_multiply(bx,by)));
}
2009-05-04 02:46:13 -04:00
}