| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | #include "master.hpp"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | namespace factor { | 
					
						
							| 
									
										
										
										
											2009-05-04 02:46:13 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_bignum_to_fixnum() { | 
					
						
							|  |  |  |   ctx->replace(tag_fixnum(bignum_to_fixnum(untag<bignum>(ctx->peek())))); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_float_to_fixnum() { | 
					
						
							|  |  |  |   ctx->replace(tag_fixnum(float_to_fixnum(ctx->peek()))); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 00:30:55 -04:00
										 |  |  | /* does not allocate, even though from_signed_cell can allocate */ | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | /* Division can only overflow when we are dividing the most negative fixnum
 | 
					
						
							|  |  |  | by -1. */ | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_fixnum_divint() { | 
					
						
							|  |  |  |   fixnum y = untag_fixnum(ctx->pop()); | 
					
						
							|  |  |  |   fixnum x = untag_fixnum(ctx->peek()); | 
					
						
							|  |  |  |   fixnum result = x / y; | 
					
						
							|  |  |  |   if (result == -fixnum_min) | 
					
						
							|  |  |  |     /* Does not allocate */ | 
					
						
							|  |  |  |     ctx->replace(from_signed_cell(-fixnum_min)); | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     ctx->replace(tag_fixnum(result)); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 00:30:55 -04:00
										 |  |  | /* does not allocate, even though from_signed_cell can allocate */ | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_fixnum_divmod() { | 
					
						
							|  |  |  |   cell* s0 = (cell*)(ctx->datastack); | 
					
						
							|  |  |  |   cell* s1 = (cell*)(ctx->datastack - sizeof(cell)); | 
					
						
							|  |  |  |   fixnum y = untag_fixnum(*s0); | 
					
						
							|  |  |  |   fixnum x = untag_fixnum(*s1); | 
					
						
							|  |  |  |   if (y == -1 && x == fixnum_min) { | 
					
						
							|  |  |  |     /* Does not allocate */ | 
					
						
							|  |  |  |     *s1 = from_signed_cell(-fixnum_min); | 
					
						
							|  |  |  |     *s0 = tag_fixnum(0); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     *s1 = tag_fixnum(x / y); | 
					
						
							|  |  |  |     *s0 = tag_fixnum(x % 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. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | inline fixnum factor_vm::sign_mask(fixnum x) { return x >> (WORD_SIZE - 1); } | 
					
						
							| 
									
										
										
										
											2009-05-08 16:05:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | inline fixnum factor_vm::branchless_max(fixnum x, fixnum y) { | 
					
						
							|  |  |  |   return (x - ((x - y) & sign_mask(x - y))); | 
					
						
							| 
									
										
										
										
											2009-05-08 16:05:55 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | inline fixnum factor_vm::branchless_abs(fixnum x) { | 
					
						
							|  |  |  |   return (x ^ sign_mask(x)) - sign_mask(x); | 
					
						
							| 
									
										
										
										
											2009-05-08 16:05:55 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_fixnum_shift() { | 
					
						
							|  |  |  |   fixnum y = untag_fixnum(ctx->pop()); | 
					
						
							|  |  |  |   fixnum x = untag_fixnum(ctx->peek()); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  |   if (x == 0) | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   else if (y < 0) { | 
					
						
							|  |  |  |     y = branchless_max(y, -WORD_SIZE + 1); | 
					
						
							|  |  |  |     ctx->replace(tag_fixnum(x >> -y)); | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } else if (y < WORD_SIZE - TAG_BITS) { | 
					
						
							| 
									
										
										
										
											2013-05-13 00:53:47 -04:00
										 |  |  |     fixnum mask = -((fixnum)1 << (WORD_SIZE - 1 - TAG_BITS - y)); | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  |     if (!(branchless_abs(x) & mask)) { | 
					
						
							|  |  |  |       ctx->replace(tag_fixnum(x << y)); | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  |   ctx->replace(tag<bignum>(bignum_arithmetic_shift(fixnum_to_bignum(x), y))); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_fixnum_to_bignum() { | 
					
						
							|  |  |  |   ctx->replace(tag<bignum>(fixnum_to_bignum(untag_fixnum(ctx->peek())))); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_float_to_bignum() { | 
					
						
							|  |  |  |   ctx->replace(tag<bignum>(float_to_bignum(ctx->peek()))); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | #define POP_BIGNUMS(x, y)                \
 | 
					
						
							|  |  |  |   bignum* y = untag<bignum>(ctx->pop()); \ | 
					
						
							|  |  |  |   bignum* x = untag<bignum>(ctx->peek()); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_bignum_eq() { | 
					
						
							|  |  |  |   POP_BIGNUMS(x, y); | 
					
						
							|  |  |  |   ctx->replace(tag_boolean(bignum_equal_p(x, y))); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_bignum_add() { | 
					
						
							|  |  |  |   POP_BIGNUMS(x, y); | 
					
						
							|  |  |  |   ctx->replace(tag<bignum>(bignum_add(x, y))); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_bignum_subtract() { | 
					
						
							|  |  |  |   POP_BIGNUMS(x, y); | 
					
						
							|  |  |  |   ctx->replace(tag<bignum>(bignum_subtract(x, y))); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_bignum_multiply() { | 
					
						
							|  |  |  |   POP_BIGNUMS(x, y); | 
					
						
							|  |  |  |   ctx->replace(tag<bignum>(bignum_multiply(x, y))); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_bignum_divint() { | 
					
						
							|  |  |  |   POP_BIGNUMS(x, y); | 
					
						
							|  |  |  |   ctx->replace(tag<bignum>(bignum_quotient(x, y))); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_bignum_divmod() { | 
					
						
							|  |  |  |   cell* s0 = (cell*)(ctx->datastack); | 
					
						
							|  |  |  |   cell* s1 = (cell*)(ctx->datastack - sizeof(cell)); | 
					
						
							|  |  |  |   bignum* y = untag<bignum>(*s0); | 
					
						
							|  |  |  |   bignum* x = untag<bignum>(*s1); | 
					
						
							|  |  |  |   bignum* q, *r; | 
					
						
							|  |  |  |   bignum_divide(x, y, &q, &r); | 
					
						
							|  |  |  |   *s1 = tag<bignum>(q); | 
					
						
							|  |  |  |   *s0 = tag<bignum>(r); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_bignum_mod() { | 
					
						
							|  |  |  |   POP_BIGNUMS(x, y); | 
					
						
							|  |  |  |   ctx->replace(tag<bignum>(bignum_remainder(x, y))); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_bignum_gcd() { | 
					
						
							|  |  |  |   POP_BIGNUMS(x, y); | 
					
						
							|  |  |  |   ctx->replace(tag<bignum>(bignum_gcd(x, y))); | 
					
						
							| 
									
										
										
										
											2012-04-05 12:17:35 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_bignum_and() { | 
					
						
							|  |  |  |   POP_BIGNUMS(x, y); | 
					
						
							|  |  |  |   ctx->replace(tag<bignum>(bignum_bitwise_and(x, y))); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_bignum_or() { | 
					
						
							|  |  |  |   POP_BIGNUMS(x, y); | 
					
						
							|  |  |  |   ctx->replace(tag<bignum>(bignum_bitwise_ior(x, y))); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_bignum_xor() { | 
					
						
							|  |  |  |   POP_BIGNUMS(x, y); | 
					
						
							|  |  |  |   ctx->replace(tag<bignum>(bignum_bitwise_xor(x, y))); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_bignum_shift() { | 
					
						
							|  |  |  |   fixnum y = untag_fixnum(ctx->pop()); | 
					
						
							|  |  |  |   bignum* x = untag<bignum>(ctx->peek()); | 
					
						
							|  |  |  |   ctx->replace(tag<bignum>(bignum_arithmetic_shift(x, y))); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_bignum_less() { | 
					
						
							|  |  |  |   POP_BIGNUMS(x, y); | 
					
						
							|  |  |  |   ctx->replace(tag_boolean(bignum_compare(x, y) == bignum_comparison_less)); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_bignum_lesseq() { | 
					
						
							|  |  |  |   POP_BIGNUMS(x, y); | 
					
						
							|  |  |  |   ctx->replace(tag_boolean(bignum_compare(x, y) != bignum_comparison_greater)); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_bignum_greater() { | 
					
						
							|  |  |  |   POP_BIGNUMS(x, y); | 
					
						
							|  |  |  |   ctx->replace(tag_boolean(bignum_compare(x, y) == bignum_comparison_greater)); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_bignum_greatereq() { | 
					
						
							|  |  |  |   POP_BIGNUMS(x, y); | 
					
						
							|  |  |  |   ctx->replace(tag_boolean(bignum_compare(x, y) != bignum_comparison_less)); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_bignum_not() { | 
					
						
							|  |  |  |   ctx->replace(tag<bignum>(bignum_bitwise_not(untag<bignum>(ctx->peek())))); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_bignum_bitp() { | 
					
						
							| 
									
										
										
										
											2013-05-13 00:53:47 -04:00
										 |  |  |   int bit = (int)to_fixnum(ctx->pop()); | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  |   bignum* x = untag<bignum>(ctx->peek()); | 
					
						
							|  |  |  |   ctx->replace(tag_boolean(bignum_logbitp(bit, x))); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_bignum_log2() { | 
					
						
							|  |  |  |   ctx->replace(tag<bignum>(bignum_integer_length(untag<bignum>(ctx->peek())))); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-25 14:59:33 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | cell factor_vm::unbox_array_size_slow() { | 
					
						
							|  |  |  |   if (tagged<object>(ctx->peek()).type() == BIGNUM_TYPE) { | 
					
						
							|  |  |  |     bignum* zero = untag<bignum>(bignum_zero); | 
					
						
							|  |  |  |     GC_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) { | 
					
						
							|  |  |  |       ctx->pop(); | 
					
						
							|  |  |  |       return bignum_to_cell(n); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   general_error(ERROR_ARRAY_SIZE, ctx->pop(), tag_fixnum(array_size_max)); | 
					
						
							|  |  |  |   return 0; /* can't happen */ | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-25 14:59:33 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_fixnum_to_float() { | 
					
						
							|  |  |  |   ctx->replace(allot_float(fixnum_to_float(ctx->peek()))); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-25 14:59:33 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_format_float() { | 
					
						
							|  |  |  |   byte_array* array = allot_byte_array(100); | 
					
						
							|  |  |  |   char* format = alien_offset(ctx->pop()); | 
					
						
							|  |  |  |   double value = untag_float_check(ctx->peek()); | 
					
						
							|  |  |  |   SNPRINTF(array->data<char>(), 99, format, value); | 
					
						
							|  |  |  |   ctx->replace(tag<byte_array>(array)); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | #define POP_FLOATS(x, y)              \
 | 
					
						
							|  |  |  |   double y = untag_float(ctx->pop()); \ | 
					
						
							|  |  |  |   double x = untag_float(ctx->peek()); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_float_eq() { | 
					
						
							|  |  |  |   POP_FLOATS(x, y); | 
					
						
							|  |  |  |   ctx->replace(tag_boolean(x == y)); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-25 14:59:33 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_float_add() { | 
					
						
							|  |  |  |   POP_FLOATS(x, y); | 
					
						
							|  |  |  |   ctx->replace(allot_float(x + y)); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-25 14:59:33 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_float_subtract() { | 
					
						
							|  |  |  |   POP_FLOATS(x, y); | 
					
						
							|  |  |  |   ctx->replace(allot_float(x - y)); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-25 14:59:33 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_float_multiply() { | 
					
						
							|  |  |  |   POP_FLOATS(x, y); | 
					
						
							|  |  |  |   ctx->replace(allot_float(x * y)); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-25 14:59:33 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_float_divfloat() { | 
					
						
							|  |  |  |   POP_FLOATS(x, y); | 
					
						
							|  |  |  |   ctx->replace(allot_float(x / y)); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_float_less() { | 
					
						
							|  |  |  |   POP_FLOATS(x, y); | 
					
						
							|  |  |  |   ctx->replace(tag_boolean(x < y)); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_float_lesseq() { | 
					
						
							|  |  |  |   POP_FLOATS(x, y); | 
					
						
							|  |  |  |   ctx->replace(tag_boolean(x <= y)); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_float_greater() { | 
					
						
							|  |  |  |   POP_FLOATS(x, y); | 
					
						
							|  |  |  |   ctx->replace(tag_boolean(x > y)); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_float_greatereq() { | 
					
						
							|  |  |  |   POP_FLOATS(x, y); | 
					
						
							|  |  |  |   ctx->replace(tag_boolean(x >= y)); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 00:30:55 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_float_bits() { | 
					
						
							|  |  |  |   ctx->push( | 
					
						
							| 
									
										
										
										
											2013-05-13 00:53:47 -04:00
										 |  |  |       from_unsigned_cell(float_bits((float)untag_float_check(ctx->pop())))); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 00:30:55 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_bits_float() { | 
					
						
							| 
									
										
										
										
											2013-05-13 00:53:47 -04:00
										 |  |  |   ctx->push(allot_float(bits_float((uint32_t)to_cell(ctx->pop())))); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_double_bits() { | 
					
						
							|  |  |  |   ctx->push(from_unsigned_8(double_bits(untag_float_check(ctx->pop())))); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-25 14:59:33 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | void factor_vm::primitive_bits_double() { | 
					
						
							|  |  |  |   ctx->push(allot_float(bits_double(to_unsigned_8(ctx->pop())))); | 
					
						
							| 
									
										
										
										
											2009-05-02 05:04:19 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-18 16:59:56 -05:00
										 |  |  | /* Cannot allocate */ | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | fixnum factor_vm::to_fixnum(cell tagged) { | 
					
						
							|  |  |  |   switch (TAG(tagged)) { | 
					
						
							|  |  |  |     case FIXNUM_TYPE: | 
					
						
							|  |  |  |       return untag_fixnum(tagged); | 
					
						
							|  |  |  |     case BIGNUM_TYPE: | 
					
						
							|  |  |  |       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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | VM_C_API fixnum to_fixnum(cell tagged, factor_vm* parent) { | 
					
						
							|  |  |  |   return parent->to_fixnum(tagged); | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:07 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-13 00:53:47 -04:00
										 |  |  | cell factor_vm::to_cell(cell tagged) { return (cell)to_fixnum(tagged); } | 
					
						
							| 
									
										
										
										
											2009-05-04 02:00:30 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | VM_C_API cell to_cell(cell tagged, factor_vm* parent) { | 
					
						
							|  |  |  |   return parent->to_cell(tagged); | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:07 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 00:30:55 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | VM_C_API cell from_signed_cell(fixnum integer, factor_vm* parent) { | 
					
						
							|  |  |  |   return parent->from_signed_cell(integer); | 
					
						
							| 
									
										
										
										
											2009-05-04 02:00:30 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 00:30:55 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | VM_C_API cell from_unsigned_cell(cell integer, factor_vm* parent) { | 
					
						
							|  |  |  |   return parent->from_unsigned_cell(integer); | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:07 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 00:30:55 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-13 00:28:25 -04:00
										 |  |  | cell factor_vm::from_signed_8(int64_t n) { | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  |   if (n < fixnum_min || n > fixnum_max) | 
					
						
							|  |  |  |     return tag<bignum>(long_long_to_bignum(n)); | 
					
						
							|  |  |  |   else | 
					
						
							| 
									
										
										
										
											2013-05-13 00:53:47 -04:00
										 |  |  |     return tag_fixnum((fixnum)n); | 
					
						
							| 
									
										
										
										
											2009-05-04 02:00:30 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-13 00:28:25 -04:00
										 |  |  | VM_C_API cell from_signed_8(int64_t n, factor_vm* parent) { | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  |   return parent->from_signed_8(n); | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:07 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-18 16:59:56 -05:00
										 |  |  | /* Cannot allocate */ | 
					
						
							| 
									
										
										
										
											2013-05-13 00:28:25 -04:00
										 |  |  | int64_t factor_vm::to_signed_8(cell obj) { | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  |   switch (tagged<object>(obj).type()) { | 
					
						
							|  |  |  |     case FIXNUM_TYPE: | 
					
						
							|  |  |  |       return untag_fixnum(obj); | 
					
						
							|  |  |  |     case BIGNUM_TYPE: | 
					
						
							|  |  |  |       return bignum_to_long_long(untag<bignum>(obj)); | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |       type_error(BIGNUM_TYPE, obj); | 
					
						
							|  |  |  |       return 0; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2009-05-04 02:00:30 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-13 00:28:25 -04:00
										 |  |  | VM_C_API int64_t to_signed_8(cell obj, factor_vm* parent) { | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  |   return parent->to_signed_8(obj); | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:07 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-25 14:59:33 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-13 00:28:25 -04:00
										 |  |  | cell factor_vm::from_unsigned_8(uint64_t n) { | 
					
						
							| 
									
										
										
										
											2013-05-13 00:53:47 -04:00
										 |  |  |   if (n > (uint64_t)fixnum_max) | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  |     return tag<bignum>(ulong_long_to_bignum(n)); | 
					
						
							|  |  |  |   else | 
					
						
							| 
									
										
										
										
											2013-05-13 00:53:47 -04:00
										 |  |  |     return tag_fixnum((fixnum)n); | 
					
						
							| 
									
										
										
										
											2009-05-04 02:00:30 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-13 00:28:25 -04:00
										 |  |  | VM_C_API cell from_unsigned_8(uint64_t n, factor_vm* parent) { | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  |   return parent->from_unsigned_8(n); | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:07 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-18 16:59:56 -05:00
										 |  |  | /* Cannot allocate */ | 
					
						
							| 
									
										
										
										
											2013-05-13 00:28:25 -04:00
										 |  |  | uint64_t factor_vm::to_unsigned_8(cell obj) { | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  |   switch (tagged<object>(obj).type()) { | 
					
						
							|  |  |  |     case FIXNUM_TYPE: | 
					
						
							|  |  |  |       return untag_fixnum(obj); | 
					
						
							|  |  |  |     case BIGNUM_TYPE: | 
					
						
							|  |  |  |       return bignum_to_ulong_long(untag<bignum>(obj)); | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |       type_error(BIGNUM_TYPE, obj); | 
					
						
							|  |  |  |       return 0; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-13 00:28:25 -04:00
										 |  |  | VM_C_API uint64_t to_unsigned_8(cell obj, factor_vm* parent) { | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  |   return parent->to_unsigned_8(obj); | 
					
						
							| 
									
										
										
										
											2009-05-04 02:00:30 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-18 16:59:56 -05:00
										 |  |  | /* Cannot allocate */ | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | float factor_vm::to_float(cell value) { | 
					
						
							| 
									
										
										
										
											2013-05-13 00:53:47 -04:00
										 |  |  |   return (float)untag_float_check(value); | 
					
						
							| 
									
										
										
										
											2009-05-04 02:00:30 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | /* Cannot allocate */ | 
					
						
							|  |  |  | double factor_vm::to_double(cell value) { return untag_float_check(value); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-04 02:00:30 -04:00
										 |  |  | /* The fixnum+, fixnum- and fixnum* primitives are defined in cpu_*.S. On
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  |    overflow, they call these functions. */ | 
					
						
							| 
									
										
										
										
											2013-03-25 17:05:05 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -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-05-04 02:00:30 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | VM_C_API void overflow_fixnum_add(fixnum x, fixnum y, factor_vm* parent) { | 
					
						
							|  |  |  |   parent->overflow_fixnum_add(x, y); | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:07 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-25 17:05:05 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -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-05-04 02:00:30 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | VM_C_API void overflow_fixnum_subtract(fixnum x, fixnum y, factor_vm* parent) { | 
					
						
							|  |  |  |   parent->overflow_fixnum_subtract(x, y); | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:07 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-25 17:05:05 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | inline void factor_vm::overflow_fixnum_multiply(fixnum x, fixnum y) { | 
					
						
							|  |  |  |   bignum* bx = fixnum_to_bignum(x); | 
					
						
							|  |  |  |   GC_BIGNUM(bx); | 
					
						
							|  |  |  |   bignum* by = fixnum_to_bignum(y); | 
					
						
							|  |  |  |   GC_BIGNUM(by); | 
					
						
							|  |  |  |   ctx->replace(tag<bignum>(bignum_multiply(bx, by))); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | VM_C_API void overflow_fixnum_multiply(fixnum x, fixnum y, factor_vm* parent) { | 
					
						
							|  |  |  |   parent->overflow_fixnum_multiply(x, y); | 
					
						
							| 
									
										
										
										
											2009-08-17 16:37:07 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-04 02:46:13 -04:00
										 |  |  | } |