| 
									
										
										
										
											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
										 |  |  | static const fixnum fixnum_max = | 
					
						
							| 
									
										
										
										
											2013-05-13 00:53:47 -04:00
										 |  |  |     (((fixnum)1 << (WORD_SIZE - TAG_BITS - 1)) - 1); | 
					
						
							|  |  |  | static const fixnum fixnum_min = (-((fixnum)1 << (WORD_SIZE - TAG_BITS - 1))); | 
					
						
							|  |  |  | static const fixnum array_size_max = ((cell)1 << (WORD_SIZE - TAG_BITS - 2)); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | inline cell factor_vm::from_signed_cell(fixnum x) { | 
					
						
							|  |  |  |   if (x < fixnum_min || x > fixnum_max) | 
					
						
							|  |  |  |     return tag<bignum>(fixnum_to_bignum(x)); | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     return tag_fixnum(x); | 
					
						
							| 
									
										
										
										
											2009-09-29 14:53:10 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 00:30:55 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | inline cell factor_vm::from_unsigned_cell(cell x) { | 
					
						
							| 
									
										
										
										
											2013-05-13 00:53:47 -04:00
										 |  |  |   if (x > (cell)fixnum_max) | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  |     return tag<bignum>(cell_to_bignum(x)); | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     return tag_fixnum(x); | 
					
						
							| 
									
										
										
										
											2009-09-29 14:53:10 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 00:30:55 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | inline cell factor_vm::allot_float(double n) { | 
					
						
							|  |  |  |   boxed_float* flo = allot<boxed_float>(sizeof(boxed_float)); | 
					
						
							|  |  |  |   flo->n = n; | 
					
						
							|  |  |  |   return tag(flo); | 
					
						
							| 
									
										
										
										
											2009-09-29 14:53:10 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 00:30:55 -04:00
										 |  |  | /* Allocates memory */ | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | inline bignum* factor_vm::float_to_bignum(cell tagged) { | 
					
						
							|  |  |  |   return double_to_bignum(untag_float(tagged)); | 
					
						
							| 
									
										
										
										
											2009-09-29 14:53:10 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-05-04 02:00:30 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | inline double factor_vm::untag_float(cell tagged) { | 
					
						
							|  |  |  |   return untag<boxed_float>(tagged)->n; | 
					
						
							| 
									
										
										
										
											2009-09-29 14:53:10 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | inline double factor_vm::untag_float_check(cell tagged) { | 
					
						
							|  |  |  |   return untag_check<boxed_float>(tagged)->n; | 
					
						
							| 
									
										
										
										
											2009-09-29 14:53:10 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-05-04 02:00:30 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | inline fixnum factor_vm::float_to_fixnum(cell tagged) { | 
					
						
							| 
									
										
										
										
											2013-05-13 00:53:47 -04:00
										 |  |  |   return (fixnum)untag_float(tagged); | 
					
						
							| 
									
										
										
										
											2009-09-29 14:53:10 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-05-04 02:00:30 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | inline double factor_vm::fixnum_to_float(cell tagged) { | 
					
						
							| 
									
										
										
										
											2013-05-13 00:53:47 -04:00
										 |  |  |   return (double)untag_fixnum(tagged); | 
					
						
							| 
									
										
										
										
											2009-09-29 14:53:10 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-05-04 02:00:30 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | inline cell factor_vm::unbox_array_size() { | 
					
						
							| 
									
										
										
										
											2014-05-29 13:08:01 -04:00
										 |  |  |   cell obj = ctx->pop(); | 
					
						
							| 
									
										
										
										
											2014-06-07 15:06:03 -04:00
										 |  |  |   fixnum n = to_fixnum_strict(obj); | 
					
						
							| 
									
										
										
										
											2014-05-29 13:08:01 -04:00
										 |  |  |   if (n >= 0 && n < (fixnum)array_size_max) { | 
					
						
							|  |  |  |     return n; | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-05-29 13:08:01 -04:00
										 |  |  |   general_error(ERROR_ARRAY_SIZE, obj, tag_fixnum(array_size_max)); | 
					
						
							|  |  |  |   return 0; /* can't happen */ | 
					
						
							| 
									
										
										
										
											2009-10-31 03:30:48 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-05-04 02:00:30 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | VM_C_API cell from_signed_cell(fixnum integer, factor_vm* vm); | 
					
						
							|  |  |  | VM_C_API cell from_unsigned_cell(cell integer, factor_vm* vm); | 
					
						
							| 
									
										
										
										
											2013-05-13 00:28:25 -04:00
										 |  |  | VM_C_API cell from_signed_8(int64_t n, factor_vm* vm); | 
					
						
							|  |  |  | VM_C_API cell from_unsigned_8(uint64_t n, factor_vm* vm); | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  | VM_C_API uint64_t to_unsigned_8(cell obj, factor_vm* parent); | 
					
						
							| 
									
										
										
										
											2009-05-04 02:00:30 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-11 22:13:48 -04:00
										 |  |  | VM_C_API fixnum to_fixnum(cell tagged, factor_vm* vm); | 
					
						
							|  |  |  | VM_C_API cell to_cell(cell tagged, factor_vm* vm); | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  | VM_C_API void overflow_fixnum_subtract(fixnum x, fixnum y, factor_vm* parent); | 
					
						
							|  |  |  | VM_C_API void overflow_fixnum_multiply(fixnum x, fixnum y, factor_vm* parent); | 
					
						
							| 
									
										
										
										
											2009-05-04 02:46:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | } |