factor/vm/math.cpp

861 lines
15 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-08-17 16:37:07 -04:00
inline void factorvm::vmprim_bignum_to_fixnum()
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
drepl(tag_fixnum(bignum_to_fixnum(untag<bignum>(dpeek()))));
2009-05-02 05:04:19 -04:00
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(bignum_to_fixnum)
{
PRIMITIVE_GETVM()->vmprim_bignum_to_fixnum();
}
inline void factorvm::vmprim_float_to_fixnum()
2009-05-02 05:04:19 -04:00
{
drepl(tag_fixnum(float_to_fixnum(dpeek())));
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(float_to_fixnum)
{
PRIMITIVE_GETVM()->vmprim_float_to_fixnum();
}
2009-05-02 05:04:19 -04:00
/* Division can only overflow when we are dividing the most negative fixnum
by -1. */
2009-08-17 16:37:07 -04:00
inline void factorvm::vmprim_fixnum_divint()
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
fixnum y = untag_fixnum(dpop()); \
fixnum x = untag_fixnum(dpeek());
fixnum result = x / y;
if(result == -fixnum_min)
drepl(allot_integer(-fixnum_min));
2009-05-02 05:04:19 -04:00
else
drepl(tag_fixnum(result));
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(fixnum_divint)
{
PRIMITIVE_GETVM()->vmprim_fixnum_divint();
}
inline void factorvm::vmprim_fixnum_divmod()
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
cell y = ((cell *)ds)[0];
cell x = ((cell *)ds)[-1];
if(y == tag_fixnum(-1) && x == tag_fixnum(fixnum_min))
2009-05-02 05:04:19 -04:00
{
((cell *)ds)[-1] = allot_integer(-fixnum_min);
2009-05-04 05:50:24 -04:00
((cell *)ds)[0] = tag_fixnum(0);
2009-05-02 05:04:19 -04:00
}
else
{
2009-05-04 05:50:24 -04:00
((cell *)ds)[-1] = tag_fixnum(untag_fixnum(x) / untag_fixnum(y));
((cell *)ds)[0] = (fixnum)x % (fixnum)y;
2009-05-02 05:04:19 -04:00
}
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(fixnum_divmod)
{
PRIMITIVE_GETVM()->vmprim_fixnum_divmod();
}
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.
*/
2009-08-17 16:37:07 -04:00
inline fixnum factorvm::sign_mask(fixnum x)
{
return x >> (WORD_SIZE - 1);
}
2009-08-17 16:37:07 -04:00
inline fixnum factorvm::branchless_max(fixnum x, fixnum y)
{
return (x - ((x - y) & sign_mask(x - y)));
}
2009-08-17 16:37:07 -04:00
inline fixnum factorvm::branchless_abs(fixnum x)
{
return (x ^ sign_mask(x)) - sign_mask(x);
}
2009-05-02 05:04:19 -04:00
2009-08-17 16:37:07 -04:00
inline void factorvm::vmprim_fixnum_shift()
2009-05-02 05:04:19 -04:00
{
fixnum y = untag_fixnum(dpop());
2009-05-04 05:50:24 -04:00
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);
2009-05-02 05:04:19 -04:00
drepl(tag_fixnum(x >> -y));
return;
}
else if(y < WORD_SIZE - TAG_BITS)
{
2009-05-04 05:50:24 -04:00
fixnum mask = -((fixnum)1 << (WORD_SIZE - 1 - TAG_BITS - y));
if(!(branchless_abs(x) & mask))
2009-05-02 05:04:19 -04:00
{
drepl(tag_fixnum(x << y));
return;
}
}
2009-05-04 05:50:24 -04:00
drepl(tag<bignum>(bignum_arithmetic_shift(
2009-05-02 05:04:19 -04:00
fixnum_to_bignum(x),y)));
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(fixnum_shift)
{
PRIMITIVE_GETVM()->vmprim_fixnum_shift();
}
inline void factorvm::vmprim_fixnum_to_bignum()
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
drepl(tag<bignum>(fixnum_to_bignum(untag_fixnum(dpeek()))));
2009-05-02 05:04:19 -04:00
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(fixnum_to_bignum)
{
PRIMITIVE_GETVM()->vmprim_fixnum_to_bignum();
}
inline void factorvm::vmprim_float_to_bignum()
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
drepl(tag<bignum>(float_to_bignum(dpeek())));
2009-05-02 05:04:19 -04:00
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(float_to_bignum)
{
PRIMITIVE_GETVM()->vmprim_float_to_bignum();
}
2009-05-02 05:04:19 -04:00
#define POP_BIGNUMS(x,y) \
2009-05-04 05:50:24 -04:00
bignum * y = untag<bignum>(dpop()); \
bignum * x = untag<bignum>(dpop());
2009-05-02 05:04:19 -04:00
2009-08-17 16:37:07 -04:00
inline void factorvm::vmprim_bignum_eq()
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
box_boolean(bignum_equal_p(x,y));
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(bignum_eq)
{
PRIMITIVE_GETVM()->vmprim_bignum_eq();
}
inline void factorvm::vmprim_bignum_add()
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
2009-05-04 05:50:24 -04:00
dpush(tag<bignum>(bignum_add(x,y)));
2009-05-02 05:04:19 -04:00
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(bignum_add)
{
PRIMITIVE_GETVM()->vmprim_bignum_add();
}
inline void factorvm::vmprim_bignum_subtract()
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
2009-05-04 05:50:24 -04:00
dpush(tag<bignum>(bignum_subtract(x,y)));
2009-05-02 05:04:19 -04:00
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(bignum_subtract)
{
PRIMITIVE_GETVM()->vmprim_bignum_subtract();
}
inline void factorvm::vmprim_bignum_multiply()
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
2009-05-04 05:50:24 -04:00
dpush(tag<bignum>(bignum_multiply(x,y)));
2009-05-02 05:04:19 -04:00
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(bignum_multiply)
{
PRIMITIVE_GETVM()->vmprim_bignum_multiply();
}
inline void factorvm::vmprim_bignum_divint()
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
2009-05-04 05:50:24 -04:00
dpush(tag<bignum>(bignum_quotient(x,y)));
2009-05-02 05:04:19 -04:00
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(bignum_divint)
{
PRIMITIVE_GETVM()->vmprim_bignum_divint();
}
inline void factorvm::vmprim_bignum_divmod()
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
bignum *q, *r;
2009-05-02 05:04:19 -04:00
POP_BIGNUMS(x,y);
bignum_divide(x,y,&q,&r);
2009-05-04 05:50:24 -04:00
dpush(tag<bignum>(q));
dpush(tag<bignum>(r));
2009-05-02 05:04:19 -04:00
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(bignum_divmod)
{
PRIMITIVE_GETVM()->vmprim_bignum_divmod();
}
inline void factorvm::vmprim_bignum_mod()
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
2009-05-04 05:50:24 -04:00
dpush(tag<bignum>(bignum_remainder(x,y)));
2009-05-02 05:04:19 -04:00
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(bignum_mod)
{
PRIMITIVE_GETVM()->vmprim_bignum_mod();
}
inline void factorvm::vmprim_bignum_and()
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
2009-05-04 05:50:24 -04:00
dpush(tag<bignum>(bignum_bitwise_and(x,y)));
2009-05-02 05:04:19 -04:00
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(bignum_and)
{
PRIMITIVE_GETVM()->vmprim_bignum_and();
}
inline void factorvm::vmprim_bignum_or()
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
2009-05-04 05:50:24 -04:00
dpush(tag<bignum>(bignum_bitwise_ior(x,y)));
2009-05-02 05:04:19 -04:00
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(bignum_or)
{
PRIMITIVE_GETVM()->vmprim_bignum_or();
}
inline void factorvm::vmprim_bignum_xor()
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
2009-05-04 05:50:24 -04:00
dpush(tag<bignum>(bignum_bitwise_xor(x,y)));
2009-05-02 05:04:19 -04:00
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(bignum_xor)
{
PRIMITIVE_GETVM()->vmprim_bignum_xor();
}
inline void factorvm::vmprim_bignum_shift()
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
fixnum y = untag_fixnum(dpop());
bignum* x = untag<bignum>(dpop());
dpush(tag<bignum>(bignum_arithmetic_shift(x,y)));
2009-05-02 05:04:19 -04:00
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(bignum_shift)
{
PRIMITIVE_GETVM()->vmprim_bignum_shift();
}
inline void factorvm::vmprim_bignum_less()
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
box_boolean(bignum_compare(x,y) == bignum_comparison_less);
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(bignum_less)
{
PRIMITIVE_GETVM()->vmprim_bignum_less();
}
inline void factorvm::vmprim_bignum_lesseq()
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
box_boolean(bignum_compare(x,y) != bignum_comparison_greater);
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(bignum_lesseq)
{
PRIMITIVE_GETVM()->vmprim_bignum_lesseq();
}
inline void factorvm::vmprim_bignum_greater()
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
box_boolean(bignum_compare(x,y) == bignum_comparison_greater);
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(bignum_greater)
{
PRIMITIVE_GETVM()->vmprim_bignum_greater();
}
inline void factorvm::vmprim_bignum_greatereq()
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
box_boolean(bignum_compare(x,y) != bignum_comparison_less);
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(bignum_greatereq)
{
PRIMITIVE_GETVM()->vmprim_bignum_greatereq();
}
inline void factorvm::vmprim_bignum_not()
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
drepl(tag<bignum>(bignum_bitwise_not(untag<bignum>(dpeek()))));
2009-05-02 05:04:19 -04:00
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(bignum_not)
{
PRIMITIVE_GETVM()->vmprim_bignum_not();
}
inline void factorvm::vmprim_bignum_bitp()
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
fixnum bit = to_fixnum(dpop());
bignum *x = untag<bignum>(dpop());
2009-05-02 05:04:19 -04:00
box_boolean(bignum_logbitp(bit,x));
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(bignum_bitp)
{
PRIMITIVE_GETVM()->vmprim_bignum_bitp();
}
inline void factorvm::vmprim_bignum_log2()
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
drepl(tag<bignum>(bignum_integer_length(untag<bignum>(dpeek()))));
2009-05-02 05:04:19 -04:00
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(bignum_log2)
{
PRIMITIVE_GETVM()->vmprim_bignum_log2();
}
unsigned int factorvm::bignum_producer(unsigned int digit)
2009-05-02 05:04:19 -04:00
{
unsigned char *ptr = (unsigned char *)alien_offset(dpeek());
return *(ptr + digit);
}
unsigned int bignum_producer(unsigned int digit, factorvm *myvm)
2009-08-17 16:37:07 -04:00
{
return myvm->bignum_producer(digit);
2009-08-17 16:37:07 -04:00
}
inline void factorvm::vmprim_byte_array_to_bignum()
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
cell n_digits = array_capacity(untag_check<byte_array>(dpeek()));
2009-08-17 16:37:14 -04:00
// bignum * result = factor::digit_stream_to_bignum(n_digits,factor::bignum_producer,0x100,0);
bignum * result = digit_stream_to_bignum(n_digits,factor::bignum_producer,0x100,0);
2009-05-04 05:50:24 -04:00
drepl(tag<bignum>(result));
2009-05-02 05:04:19 -04:00
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(byte_array_to_bignum)
{
PRIMITIVE_GETVM()->vmprim_byte_array_to_bignum();
}
2009-08-17 16:37:07 -04:00
cell factorvm::unbox_array_size()
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
switch(tagged<object>(dpeek()).type())
2009-05-02 05:04:19 -04:00
{
case FIXNUM_TYPE:
{
2009-05-04 05:50:24 -04:00
fixnum n = untag_fixnum(dpeek());
if(n >= 0 && n < (fixnum)array_size_max)
2009-05-02 05:04:19 -04:00
{
dpop();
return n;
}
break;
}
case BIGNUM_TYPE:
{
2009-05-04 05:50:24 -04:00
bignum * zero = untag<bignum>(bignum_zero);
bignum * max = cell_to_bignum(array_size_max);
2009-05-04 05:50:24 -04:00
bignum * n = untag<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);
2009-05-02 05:04:19 -04:00
return 0; /* can't happen */
}
2009-08-17 16:37:07 -04:00
inline void factorvm::vmprim_fixnum_to_float()
2009-05-02 05:04:19 -04:00
{
drepl(allot_float(fixnum_to_float(dpeek())));
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(fixnum_to_float)
{
PRIMITIVE_GETVM()->vmprim_fixnum_to_float();
}
inline void factorvm::vmprim_bignum_to_float()
2009-05-02 05:04:19 -04:00
{
drepl(allot_float(bignum_to_float(dpeek())));
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(bignum_to_float)
{
PRIMITIVE_GETVM()->vmprim_bignum_to_float();
}
inline void factorvm::vmprim_str_to_float()
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
byte_array *bytes = untag_check<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
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(str_to_float)
{
PRIMITIVE_GETVM()->vmprim_str_to_float();
}
inline void factorvm::vmprim_float_to_str()
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
byte_array *array = allot_byte_array(33);
snprintf((char *)(array + 1),32,"%.16g",untag_float_check(dpop()));
2009-05-04 05:50:24 -04:00
dpush(tag<byte_array>(array));
2009-05-02 05:04:19 -04:00
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(float_to_str)
{
PRIMITIVE_GETVM()->vmprim_float_to_str();
}
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
2009-08-17 16:37:07 -04:00
inline void factorvm::vmprim_float_eq()
2009-05-02 05:04:19 -04:00
{
POP_FLOATS(x,y);
box_boolean(x == y);
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(float_eq)
{
PRIMITIVE_GETVM()->vmprim_float_eq();
}
inline void factorvm::vmprim_float_add()
2009-05-02 05:04:19 -04:00
{
POP_FLOATS(x,y);
box_double(x + y);
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(float_add)
{
PRIMITIVE_GETVM()->vmprim_float_add();
}
inline void factorvm::vmprim_float_subtract()
2009-05-02 05:04:19 -04:00
{
POP_FLOATS(x,y);
box_double(x - y);
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(float_subtract)
{
PRIMITIVE_GETVM()->vmprim_float_subtract();
}
inline void factorvm::vmprim_float_multiply()
2009-05-02 05:04:19 -04:00
{
POP_FLOATS(x,y);
box_double(x * y);
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(float_multiply)
{
PRIMITIVE_GETVM()->vmprim_float_multiply();
}
inline void factorvm::vmprim_float_divfloat()
2009-05-02 05:04:19 -04:00
{
POP_FLOATS(x,y);
box_double(x / y);
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(float_divfloat)
{
PRIMITIVE_GETVM()->vmprim_float_divfloat();
}
inline void factorvm::vmprim_float_mod()
2009-05-02 05:04:19 -04:00
{
POP_FLOATS(x,y);
box_double(fmod(x,y));
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(float_mod)
{
PRIMITIVE_GETVM()->vmprim_float_mod();
}
inline void factorvm::vmprim_float_less()
2009-05-02 05:04:19 -04:00
{
POP_FLOATS(x,y);
box_boolean(x < y);
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(float_less)
{
PRIMITIVE_GETVM()->vmprim_float_less();
}
inline void factorvm::vmprim_float_lesseq()
2009-05-02 05:04:19 -04:00
{
POP_FLOATS(x,y);
box_boolean(x <= y);
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(float_lesseq)
{
PRIMITIVE_GETVM()->vmprim_float_lesseq();
}
inline void factorvm::vmprim_float_greater()
2009-05-02 05:04:19 -04:00
{
POP_FLOATS(x,y);
box_boolean(x > y);
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(float_greater)
{
PRIMITIVE_GETVM()->vmprim_float_greater();
}
inline void factorvm::vmprim_float_greatereq()
2009-05-02 05:04:19 -04:00
{
POP_FLOATS(x,y);
box_boolean(x >= y);
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(float_greatereq)
{
PRIMITIVE_GETVM()->vmprim_float_greatereq();
}
inline void factorvm::vmprim_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
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(float_bits)
{
PRIMITIVE_GETVM()->vmprim_float_bits();
}
inline void factorvm::vmprim_bits_float()
2009-05-02 05:04:19 -04:00
{
box_float(bits_float(to_cell(dpop())));
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(bits_float)
{
PRIMITIVE_GETVM()->vmprim_bits_float();
}
inline void factorvm::vmprim_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
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(double_bits)
{
PRIMITIVE_GETVM()->vmprim_double_bits();
}
inline void factorvm::vmprim_bits_double()
2009-05-02 05:04:19 -04:00
{
box_double(bits_double(to_unsigned_8(dpop())));
}
2009-08-17 16:37:07 -04:00
PRIMITIVE(bits_double)
{
PRIMITIVE_GETVM()->vmprim_bits_double();
}
fixnum factorvm::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:
2009-05-04 05:50:24 -04:00
return bignum_to_fixnum(untag<bignum>(tagged));
default:
type_error(FIXNUM_TYPE,tagged);
return 0; /* can't happen */
}
2009-05-02 05:04:19 -04:00
}
VM_C_API fixnum to_fixnum(cell tagged,factorvm *myvm)
2009-08-17 16:37:07 -04:00
{
return VM_PTR->to_fixnum(tagged);
2009-08-17 16:37:07 -04:00
}
cell factorvm::to_cell(cell tagged)
2009-05-02 05:04:19 -04:00
{
2009-05-04 05:50:24 -04:00
return (cell)to_fixnum(tagged);
}
VM_C_API cell to_cell(cell tagged, factorvm *myvm)
2009-08-17 16:37:07 -04:00
{
return VM_PTR->to_cell(tagged);
2009-08-17 16:37:07 -04:00
}
void factorvm::box_signed_1(s8 n)
{
dpush(tag_fixnum(n));
}
VM_C_API void box_signed_1(s8 n)
2009-08-17 16:37:07 -04:00
{
return vm->box_signed_1(n);
}
void factorvm::box_unsigned_1(u8 n)
{
dpush(tag_fixnum(n));
}
VM_C_API void box_unsigned_1(u8 n)
2009-08-17 16:37:07 -04:00
{
return vm->box_unsigned_1(n);
}
void factorvm::box_signed_2(s16 n)
{
dpush(tag_fixnum(n));
}
VM_C_API void box_signed_2(s16 n)
2009-08-17 16:37:07 -04:00
{
return vm->box_signed_2(n);
}
void factorvm::box_unsigned_2(u16 n)
{
dpush(tag_fixnum(n));
2009-05-02 05:04:19 -04:00
}
VM_C_API void box_unsigned_2(u16 n)
{
2009-08-17 16:37:07 -04:00
return vm->box_unsigned_2(n);
}
2009-08-17 16:37:07 -04:00
void factorvm::box_signed_4(s32 n)
{
dpush(allot_integer(n));
}
2009-08-17 16:37:07 -04:00
VM_C_API void box_signed_4(s32 n)
{
return vm->box_signed_4(n);
}
void factorvm::box_unsigned_4(u32 n)
{
dpush(allot_cell(n));
}
2009-08-17 16:37:07 -04:00
VM_C_API void box_unsigned_4(u32 n)
{
return vm->box_unsigned_4(n);
}
void factorvm::box_signed_cell(fixnum integer)
{
dpush(allot_integer(integer));
}
2009-08-17 16:37:07 -04:00
VM_C_API void box_signed_cell(fixnum integer)
{
return vm->box_signed_cell(integer);
}
void factorvm::box_unsigned_cell(cell cell)
{
dpush(allot_cell(cell));
}
2009-08-17 16:37:07 -04:00
VM_C_API void box_unsigned_cell(cell cell)
{
return vm->box_unsigned_cell(cell);
}
void factorvm::box_signed_8(s64 n)
{
if(n < fixnum_min || n > fixnum_max)
2009-05-04 05:50:24 -04:00
dpush(tag<bignum>(long_long_to_bignum(n)));
else
dpush(tag_fixnum(n));
}
2009-08-17 16:37:07 -04:00
VM_C_API void box_signed_8(s64 n)
{
return vm->box_signed_8(n);
}
s64 factorvm::to_signed_8(cell obj)
{
2009-05-04 05:50:24 -04:00
switch(tagged<object>(obj).type())
{
case FIXNUM_TYPE:
return untag_fixnum(obj);
case BIGNUM_TYPE:
2009-05-04 05:50:24 -04:00
return bignum_to_long_long(untag<bignum>(obj));
default:
type_error(BIGNUM_TYPE,obj);
return 0;
}
}
VM_C_API s64 to_signed_8(cell obj,factorvm *myvm)
2009-08-17 16:37:07 -04:00
{
ASSERTVM();
return VM_PTR->to_signed_8(obj);
2009-08-17 16:37:07 -04:00
}
void factorvm::box_unsigned_8(u64 n)
{
2009-05-08 16:23:44 -04:00
if(n > (u64)fixnum_max)
2009-05-04 05:50:24 -04:00
dpush(tag<bignum>(ulong_long_to_bignum(n)));
else
dpush(tag_fixnum(n));
}
2009-08-17 16:37:07 -04:00
VM_C_API void box_unsigned_8(u64 n)
{
return vm->box_unsigned_8(n);
}
u64 factorvm::to_unsigned_8(cell obj)
{
2009-05-04 05:50:24 -04:00
switch(tagged<object>(obj).type())
{
case FIXNUM_TYPE:
return untag_fixnum(obj);
case BIGNUM_TYPE:
2009-05-04 05:50:24 -04:00
return bignum_to_ulong_long(untag<bignum>(obj));
default:
type_error(BIGNUM_TYPE,obj);
return 0;
}
}
VM_C_API u64 to_unsigned_8(cell obj,factorvm *myvm)
2009-08-17 16:37:07 -04:00
{
ASSERTVM();
return VM_PTR->to_unsigned_8(obj);
2009-08-17 16:37:07 -04:00
}
void factorvm::box_float(float flo)
2009-05-02 05:04:19 -04:00
{
dpush(allot_float(flo));
}
2009-08-17 16:37:07 -04:00
VM_C_API void box_float(float flo)
{
return vm->box_float(flo);
}
float factorvm::to_float(cell value)
{
return untag_float_check(value);
}
VM_C_API float to_float(cell value,factorvm *myvm)
2009-08-17 16:37:07 -04:00
{
return VM_PTR->to_float(value);
2009-08-17 16:37:07 -04:00
}
void factorvm::box_double(double flo)
2009-05-02 05:04:19 -04:00
{
dpush(allot_float(flo));
}
2009-08-17 16:37:07 -04:00
VM_C_API void box_double(double flo)
{
return vm->box_double(flo);
}
double factorvm::to_double(cell value)
{
return untag_float_check(value);
}
VM_C_API double to_double(cell value,factorvm *myvm)
2009-08-17 16:37:07 -04:00
{
return VM_PTR->to_double(value);
2009-08-17 16:37:07 -04:00
}
/* The fixnum+, fixnum- and fixnum* primitives are defined in cpu_*.S. On
overflow, they call these functions. */
2009-08-17 16:37:07 -04:00
void factorvm::overflow_fixnum_add(fixnum x, fixnum y)
{
2009-05-04 05:50:24 -04:00
drepl(tag<bignum>(fixnum_to_bignum(
untag_fixnum(x) + untag_fixnum(y))));
}
2009-08-17 16:37:07 -04:00
VM_ASM_API void overflow_fixnum_add(fixnum x, fixnum y)
{
return vm->overflow_fixnum_add(x,y);
}
void factorvm::overflow_fixnum_subtract(fixnum x, fixnum y)
{
2009-05-04 05:50:24 -04:00
drepl(tag<bignum>(fixnum_to_bignum(
untag_fixnum(x) - untag_fixnum(y))));
}
2009-08-17 16:37:07 -04:00
VM_ASM_API void overflow_fixnum_subtract(fixnum x, fixnum y)
{
return vm->overflow_fixnum_subtract(x,y);
}
void factorvm::overflow_fixnum_multiply(fixnum x, fixnum y)
{
2009-05-04 05:50:24 -04:00
bignum *bx = fixnum_to_bignum(x);
2009-08-17 16:37:10 -04:00
GC_BIGNUM(bx,this);
2009-05-04 05:50:24 -04:00
bignum *by = fixnum_to_bignum(y);
2009-08-17 16:37:10 -04:00
GC_BIGNUM(by,this);
2009-05-04 05:50:24 -04:00
drepl(tag<bignum>(bignum_multiply(bx,by)));
}
2009-05-04 02:46:13 -04:00
2009-08-17 16:37:07 -04:00
VM_ASM_API void overflow_fixnum_multiply(fixnum x, fixnum y)
{
return vm->overflow_fixnum_multiply(x,y);
}
2009-05-04 02:46:13 -04:00
}