factor/vm/math.cpp

502 lines
10 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
{
void factor_vm::primitive_bignum_to_fixnum()
2009-05-02 05:04:19 -04:00
{
ctx->replace(tag_fixnum(bignum_to_fixnum(untag<bignum>(ctx->peek()))));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_float_to_fixnum()
2009-05-02 05:04:19 -04:00
{
ctx->replace(tag_fixnum(float_to_fixnum(ctx->peek())));
2009-05-02 05:04:19 -04:00
}
/* Division can only overflow when we are dividing the most negative fixnum
by -1. */
void factor_vm::primitive_fixnum_divint()
2009-05-02 05:04:19 -04:00
{
fixnum y = untag_fixnum(ctx->pop()); \
fixnum x = untag_fixnum(ctx->peek());
2009-05-04 05:50:24 -04:00
fixnum result = x / y;
if(result == -fixnum_min)
ctx->replace(from_signed_cell(-fixnum_min));
2009-05-02 05:04:19 -04:00
else
ctx->replace(tag_fixnum(result));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_fixnum_divmod()
2009-05-02 05:04:19 -04:00
{
cell y = ((cell *)ctx->datastack)[0];
cell x = ((cell *)ctx->datastack)[-1];
if(y == tag_fixnum(-1) && x == tag_fixnum(fixnum_min))
2009-05-02 05:04:19 -04:00
{
((cell *)ctx->datastack)[-1] = from_signed_cell(-fixnum_min);
((cell *)ctx->datastack)[0] = tag_fixnum(0);
2009-05-02 05:04:19 -04:00
}
else
{
((cell *)ctx->datastack)[-1] = tag_fixnum(untag_fixnum(x) / untag_fixnum(y));
((cell *)ctx->datastack)[0] = (fixnum)x % (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.
*/
2009-09-23 14:05:46 -04:00
inline fixnum factor_vm::sign_mask(fixnum x)
{
return x >> (WORD_SIZE - 1);
}
2009-09-23 14:05:46 -04:00
inline fixnum factor_vm::branchless_max(fixnum x, fixnum y)
{
return (x - ((x - y) & sign_mask(x - y)));
}
2009-09-23 14:05:46 -04:00
inline fixnum factor_vm::branchless_abs(fixnum x)
{
return (x ^ sign_mask(x)) - sign_mask(x);
}
2009-05-02 05:04:19 -04:00
void factor_vm::primitive_fixnum_shift()
2009-05-02 05:04:19 -04:00
{
fixnum y = untag_fixnum(ctx->pop());
fixnum x = untag_fixnum(ctx->peek());
2009-05-02 05:04:19 -04:00
if(x == 0)
return;
else if(y < 0)
{
y = branchless_max(y,-WORD_SIZE + 1);
ctx->replace(tag_fixnum(x >> -y));
2009-05-02 05:04:19 -04:00
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
{
ctx->replace(tag_fixnum(x << y));
2009-05-02 05:04:19 -04:00
return;
}
}
ctx->replace(tag<bignum>(bignum_arithmetic_shift(
2009-05-02 05:04:19 -04:00
fixnum_to_bignum(x),y)));
}
void factor_vm::primitive_fixnum_to_bignum()
2009-05-02 05:04:19 -04:00
{
ctx->replace(tag<bignum>(fixnum_to_bignum(untag_fixnum(ctx->peek()))));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_float_to_bignum()
2009-05-02 05:04:19 -04:00
{
ctx->replace(tag<bignum>(float_to_bignum(ctx->peek())));
2009-05-02 05:04:19 -04:00
}
#define POP_BIGNUMS(x,y) \
bignum * y = untag<bignum>(ctx->pop()); \
bignum * x = untag<bignum>(ctx->pop());
2009-05-02 05:04:19 -04:00
void factor_vm::primitive_bignum_eq()
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
ctx->push(tag_boolean(bignum_equal_p(x,y)));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_bignum_add()
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
ctx->push(tag<bignum>(bignum_add(x,y)));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_bignum_subtract()
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
ctx->push(tag<bignum>(bignum_subtract(x,y)));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_bignum_multiply()
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
ctx->push(tag<bignum>(bignum_multiply(x,y)));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_bignum_divint()
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
ctx->push(tag<bignum>(bignum_quotient(x,y)));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_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);
ctx->push(tag<bignum>(q));
ctx->push(tag<bignum>(r));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_bignum_mod()
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
ctx->push(tag<bignum>(bignum_remainder(x,y)));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_bignum_and()
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
ctx->push(tag<bignum>(bignum_bitwise_and(x,y)));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_bignum_or()
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
ctx->push(tag<bignum>(bignum_bitwise_ior(x,y)));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_bignum_xor()
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
ctx->push(tag<bignum>(bignum_bitwise_xor(x,y)));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_bignum_shift()
2009-05-02 05:04:19 -04:00
{
fixnum y = untag_fixnum(ctx->pop());
bignum* x = untag<bignum>(ctx->pop());
ctx->push(tag<bignum>(bignum_arithmetic_shift(x,y)));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_bignum_less()
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
ctx->push(tag_boolean(bignum_compare(x,y) == bignum_comparison_less));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_bignum_lesseq()
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
ctx->push(tag_boolean(bignum_compare(x,y) != bignum_comparison_greater));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_bignum_greater()
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
ctx->push(tag_boolean(bignum_compare(x,y) == bignum_comparison_greater));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_bignum_greatereq()
2009-05-02 05:04:19 -04:00
{
POP_BIGNUMS(x,y);
ctx->push(tag_boolean(bignum_compare(x,y) != bignum_comparison_less));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_bignum_not()
2009-05-02 05:04:19 -04:00
{
ctx->replace(tag<bignum>(bignum_bitwise_not(untag<bignum>(ctx->peek()))));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_bignum_bitp()
2009-05-02 05:04:19 -04:00
{
2010-01-24 08:17:18 -05:00
int bit = (int)to_fixnum(ctx->pop());
bignum *x = untag<bignum>(ctx->pop());
ctx->push(tag_boolean(bignum_logbitp(bit,x)));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_bignum_log2()
2009-05-02 05:04:19 -04:00
{
ctx->replace(tag<bignum>(bignum_integer_length(untag<bignum>(ctx->peek()))));
2009-05-02 05:04:19 -04:00
}
2009-09-23 14:05:46 -04:00
unsigned int factor_vm::bignum_producer(unsigned int digit)
2009-05-02 05:04:19 -04:00
{
unsigned char *ptr = (unsigned char *)alien_offset(ctx->peek());
2009-05-02 05:04:19 -04:00
return *(ptr + digit);
}
unsigned int bignum_producer(unsigned int digit, factor_vm *parent)
2009-08-17 16:37:07 -04:00
{
return parent->bignum_producer(digit);
2009-08-17 16:37:07 -04:00
}
void factor_vm::primitive_byte_array_to_bignum()
2009-05-02 05:04:19 -04:00
{
2010-01-24 08:17:18 -05:00
unsigned int n_digits = (unsigned int)array_capacity(untag_check<byte_array>(ctx->peek()));
2009-08-17 16:37:14 -04:00
bignum * result = digit_stream_to_bignum(n_digits,factor::bignum_producer,0x100,0);
ctx->replace(tag<bignum>(result));
2009-05-02 05:04:19 -04:00
}
cell factor_vm::unbox_array_size_slow()
2009-05-02 05:04:19 -04:00
{
if(tagged<object>(ctx->peek()).type() == BIGNUM_TYPE)
2009-05-02 05:04:19 -04:00
{
bignum *zero = untag<bignum>(bignum_zero);
bignum *max = cell_to_bignum(array_size_max);
bignum *n = untag<bignum>(ctx->peek());
if(bignum_compare(n,zero) != bignum_comparison_less
&& bignum_compare(n,max) == bignum_comparison_less)
2009-05-02 05:04:19 -04:00
{
ctx->pop();
return bignum_to_cell(n);
2009-05-02 05:04:19 -04:00
}
}
2010-03-27 07:33:28 -04:00
general_error(ERROR_ARRAY_SIZE,ctx->pop(),tag_fixnum(array_size_max));
2009-05-02 05:04:19 -04:00
return 0; /* can't happen */
}
void factor_vm::primitive_fixnum_to_float()
2009-05-02 05:04:19 -04:00
{
ctx->replace(allot_float(fixnum_to_float(ctx->peek())));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_format_float()
2009-05-02 05:04:19 -04:00
{
byte_array *array = allot_byte_array(100);
char *format = alien_offset(ctx->pop());
double value = untag_float_check(ctx->pop());
SNPRINTF(array->data<char>(),99,format,value);
ctx->push(tag<byte_array>(array));
2009-05-02 05:04:19 -04:00
}
#define POP_FLOATS(x,y) \
double y = untag_float(ctx->pop()); \
double x = untag_float(ctx->pop());
2009-05-02 05:04:19 -04:00
void factor_vm::primitive_float_eq()
2009-05-02 05:04:19 -04:00
{
POP_FLOATS(x,y);
ctx->push(tag_boolean(x == y));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_float_add()
2009-05-02 05:04:19 -04:00
{
POP_FLOATS(x,y);
ctx->push(allot_float(x + y));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_float_subtract()
2009-05-02 05:04:19 -04:00
{
POP_FLOATS(x,y);
ctx->push(allot_float(x - y));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_float_multiply()
2009-05-02 05:04:19 -04:00
{
POP_FLOATS(x,y);
ctx->push(allot_float(x * y));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_float_divfloat()
2009-05-02 05:04:19 -04:00
{
POP_FLOATS(x,y);
ctx->push(allot_float(x / y));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_float_less()
2009-05-02 05:04:19 -04:00
{
POP_FLOATS(x,y);
ctx->push(tag_boolean(x < y));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_float_lesseq()
2009-05-02 05:04:19 -04:00
{
POP_FLOATS(x,y);
ctx->push(tag_boolean(x <= y));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_float_greater()
2009-05-02 05:04:19 -04:00
{
POP_FLOATS(x,y);
ctx->push(tag_boolean(x > y));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_float_greatereq()
2009-05-02 05:04:19 -04:00
{
POP_FLOATS(x,y);
ctx->push(tag_boolean(x >= y));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_float_bits()
2009-05-02 05:04:19 -04:00
{
ctx->push(from_unsigned_cell(float_bits((float)untag_float_check(ctx->pop()))));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_bits_float()
2009-05-02 05:04:19 -04:00
{
2010-01-24 08:17:18 -05:00
ctx->push(allot_float(bits_float((u32)to_cell(ctx->pop()))));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_double_bits()
2009-05-02 05:04:19 -04:00
{
ctx->push(from_unsigned_8(double_bits(untag_float_check(ctx->pop()))));
2009-05-02 05:04:19 -04:00
}
void factor_vm::primitive_bits_double()
2009-05-02 05:04:19 -04:00
{
ctx->push(allot_float(bits_double(to_unsigned_8(ctx->pop()))));
2009-05-02 05:04:19 -04:00
}
/* Cannot allocate */
2009-09-23 14:05:46 -04:00
fixnum factor_vm::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
}
2009-11-03 05:56:58 -05:00
VM_C_API fixnum to_fixnum(cell tagged, factor_vm *parent)
2009-08-17 16:37:07 -04:00
{
return parent->to_fixnum(tagged);
2009-08-17 16:37:07 -04:00
}
2009-09-23 14:05:46 -04:00
cell factor_vm::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, factor_vm *parent)
2009-08-17 16:37:07 -04:00
{
return parent->to_cell(tagged);
2009-08-17 16:37:07 -04:00
}
VM_C_API cell from_signed_cell(fixnum integer, factor_vm *parent)
{
return parent->from_signed_cell(integer);
}
VM_C_API cell from_unsigned_cell(cell integer, factor_vm *parent)
2009-08-17 16:37:07 -04:00
{
return parent->from_unsigned_cell(integer);
2009-08-17 16:37:07 -04:00
}
cell factor_vm::from_signed_8(s64 n)
{
if(n < fixnum_min || n > fixnum_max)
return tag<bignum>(long_long_to_bignum(n));
else
return tag_fixnum((fixnum)n);
}
VM_C_API cell from_signed_8(s64 n, factor_vm *parent)
2009-08-17 16:37:07 -04:00
{
return parent->from_signed_8(n);
2009-08-17 16:37:07 -04:00
}
/* Cannot allocate */
2009-09-23 14:05:46 -04:00
s64 factor_vm::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, factor_vm *parent)
2009-08-17 16:37:07 -04:00
{
return parent->to_signed_8(obj);
2009-08-17 16:37:07 -04:00
}
cell factor_vm::from_unsigned_8(u64 n)
{
2009-05-08 16:23:44 -04:00
if(n > (u64)fixnum_max)
return tag<bignum>(ulong_long_to_bignum(n));
else
return tag_fixnum((fixnum)n);
}
VM_C_API cell from_unsigned_8(u64 n, factor_vm *parent)
2009-08-17 16:37:07 -04:00
{
return parent->from_unsigned_8(n);
2009-08-17 16:37:07 -04:00
}
/* Cannot allocate */
2009-09-23 14:05:46 -04:00
u64 factor_vm::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, factor_vm *parent)
2009-08-17 16:37:07 -04:00
{
return parent->to_unsigned_8(obj);
2009-08-17 16:37:07 -04:00
}
/* Cannot allocate */
2009-09-23 14:05:46 -04:00
float factor_vm::to_float(cell value)
{
return (float)untag_float_check(value);
}
/* Cannot allocate */
2009-09-23 14:05:46 -04:00
double factor_vm::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. */
2009-09-23 14:05:46 -04:00
inline void factor_vm::overflow_fixnum_add(fixnum x, fixnum y)
{
ctx->replace(tag<bignum>(fixnum_to_bignum(
untag_fixnum(x) + untag_fixnum(y))));
}
2009-12-23 07:37:24 -05:00
VM_C_API void overflow_fixnum_add(fixnum x, fixnum y, factor_vm *parent)
2009-08-17 16:37:07 -04:00
{
parent->overflow_fixnum_add(x,y);
2009-08-17 16:37:07 -04:00
}
2009-09-23 14:05:46 -04:00
inline void factor_vm::overflow_fixnum_subtract(fixnum x, fixnum y)
{
ctx->replace(tag<bignum>(fixnum_to_bignum(
untag_fixnum(x) - untag_fixnum(y))));
}
2009-12-23 07:37:24 -05:00
VM_C_API void overflow_fixnum_subtract(fixnum x, fixnum y, factor_vm *parent)
2009-08-17 16:37:07 -04:00
{
parent->overflow_fixnum_subtract(x,y);
2009-08-17 16:37:07 -04:00
}
2009-09-23 14:05:46 -04:00
inline void factor_vm::overflow_fixnum_multiply(fixnum x, fixnum y)
{
2009-05-04 05:50:24 -04:00
bignum *bx = fixnum_to_bignum(x);
2009-09-23 14:03:48 -04:00
GC_BIGNUM(bx);
2009-05-04 05:50:24 -04:00
bignum *by = fixnum_to_bignum(y);
2009-09-23 14:03:48 -04:00
GC_BIGNUM(by);
ctx->replace(tag<bignum>(bignum_multiply(bx,by)));
}
2009-05-04 02:46:13 -04:00
2009-12-23 07:37:24 -05:00
VM_C_API void overflow_fixnum_multiply(fixnum x, fixnum y, factor_vm *parent)
2009-08-17 16:37:07 -04:00
{
parent->overflow_fixnum_multiply(x,y);
2009-08-17 16:37:07 -04:00
}
2009-05-04 02:46:13 -04:00
}