6 inline void factor_vm::primitive_bignum_to_fixnum()
8 drepl(tag_fixnum(bignum_to_fixnum(untag<bignum>(dpeek()))));
11 PRIMITIVE_FORWARD(bignum_to_fixnum)
13 inline void factor_vm::primitive_float_to_fixnum()
15 drepl(tag_fixnum(float_to_fixnum(dpeek())));
18 PRIMITIVE_FORWARD(float_to_fixnum)
20 /* Division can only overflow when we are dividing the most negative fixnum
22 inline void factor_vm::primitive_fixnum_divint()
24 fixnum y = untag_fixnum(dpop()); \
25 fixnum x = untag_fixnum(dpeek());
26 fixnum result = x / y;
27 if(result == -fixnum_min)
28 drepl(allot_integer(-fixnum_min));
30 drepl(tag_fixnum(result));
33 PRIMITIVE_FORWARD(fixnum_divint)
35 inline void factor_vm::primitive_fixnum_divmod()
37 cell y = ((cell *)ds)[0];
38 cell x = ((cell *)ds)[-1];
39 if(y == tag_fixnum(-1) && x == tag_fixnum(fixnum_min))
41 ((cell *)ds)[-1] = allot_integer(-fixnum_min);
42 ((cell *)ds)[0] = tag_fixnum(0);
46 ((cell *)ds)[-1] = tag_fixnum(untag_fixnum(x) / untag_fixnum(y));
47 ((cell *)ds)[0] = (fixnum)x % (fixnum)y;
51 PRIMITIVE_FORWARD(fixnum_divmod)
54 * If we're shifting right by n bits, we won't overflow as long as none of the
55 * high WORD_SIZE-TAG_BITS-n bits are set.
57 inline fixnum factor_vm::sign_mask(fixnum x)
59 return x >> (WORD_SIZE - 1);
62 inline fixnum factor_vm::branchless_max(fixnum x, fixnum y)
64 return (x - ((x - y) & sign_mask(x - y)));
67 inline fixnum factor_vm::branchless_abs(fixnum x)
69 return (x ^ sign_mask(x)) - sign_mask(x);
72 inline void factor_vm::primitive_fixnum_shift()
74 fixnum y = untag_fixnum(dpop());
75 fixnum x = untag_fixnum(dpeek());
81 y = branchless_max(y,-WORD_SIZE + 1);
82 drepl(tag_fixnum(x >> -y));
85 else if(y < WORD_SIZE - TAG_BITS)
87 fixnum mask = -((fixnum)1 << (WORD_SIZE - 1 - TAG_BITS - y));
88 if(!(branchless_abs(x) & mask))
90 drepl(tag_fixnum(x << y));
95 drepl(tag<bignum>(bignum_arithmetic_shift(
96 fixnum_to_bignum(x),y)));
99 PRIMITIVE_FORWARD(fixnum_shift)
101 inline void factor_vm::primitive_fixnum_to_bignum()
103 drepl(tag<bignum>(fixnum_to_bignum(untag_fixnum(dpeek()))));
106 PRIMITIVE_FORWARD(fixnum_to_bignum)
108 inline void factor_vm::primitive_float_to_bignum()
110 drepl(tag<bignum>(float_to_bignum(dpeek())));
113 PRIMITIVE_FORWARD(float_to_bignum)
115 #define POP_BIGNUMS(x,y) \
116 bignum * y = untag<bignum>(dpop()); \
117 bignum * x = untag<bignum>(dpop());
119 inline void factor_vm::primitive_bignum_eq()
122 box_boolean(bignum_equal_p(x,y));
125 PRIMITIVE_FORWARD(bignum_eq)
127 inline void factor_vm::primitive_bignum_add()
130 dpush(tag<bignum>(bignum_add(x,y)));
133 PRIMITIVE_FORWARD(bignum_add)
135 inline void factor_vm::primitive_bignum_subtract()
138 dpush(tag<bignum>(bignum_subtract(x,y)));
141 PRIMITIVE_FORWARD(bignum_subtract)
143 inline void factor_vm::primitive_bignum_multiply()
146 dpush(tag<bignum>(bignum_multiply(x,y)));
149 PRIMITIVE_FORWARD(bignum_multiply)
151 inline void factor_vm::primitive_bignum_divint()
154 dpush(tag<bignum>(bignum_quotient(x,y)));
157 PRIMITIVE_FORWARD(bignum_divint)
159 inline void factor_vm::primitive_bignum_divmod()
163 bignum_divide(x,y,&q,&r);
164 dpush(tag<bignum>(q));
165 dpush(tag<bignum>(r));
168 PRIMITIVE_FORWARD(bignum_divmod)
170 inline void factor_vm::primitive_bignum_mod()
173 dpush(tag<bignum>(bignum_remainder(x,y)));
176 PRIMITIVE_FORWARD(bignum_mod)
178 inline void factor_vm::primitive_bignum_and()
181 dpush(tag<bignum>(bignum_bitwise_and(x,y)));
184 PRIMITIVE_FORWARD(bignum_and)
186 inline void factor_vm::primitive_bignum_or()
189 dpush(tag<bignum>(bignum_bitwise_ior(x,y)));
192 PRIMITIVE_FORWARD(bignum_or)
194 inline void factor_vm::primitive_bignum_xor()
197 dpush(tag<bignum>(bignum_bitwise_xor(x,y)));
200 PRIMITIVE_FORWARD(bignum_xor)
202 inline void factor_vm::primitive_bignum_shift()
204 fixnum y = untag_fixnum(dpop());
205 bignum* x = untag<bignum>(dpop());
206 dpush(tag<bignum>(bignum_arithmetic_shift(x,y)));
209 PRIMITIVE_FORWARD(bignum_shift)
211 inline void factor_vm::primitive_bignum_less()
214 box_boolean(bignum_compare(x,y) == bignum_comparison_less);
217 PRIMITIVE_FORWARD(bignum_less)
219 inline void factor_vm::primitive_bignum_lesseq()
222 box_boolean(bignum_compare(x,y) != bignum_comparison_greater);
225 PRIMITIVE_FORWARD(bignum_lesseq)
227 inline void factor_vm::primitive_bignum_greater()
230 box_boolean(bignum_compare(x,y) == bignum_comparison_greater);
233 PRIMITIVE_FORWARD(bignum_greater)
235 inline void factor_vm::primitive_bignum_greatereq()
238 box_boolean(bignum_compare(x,y) != bignum_comparison_less);
241 PRIMITIVE_FORWARD(bignum_greatereq)
243 inline void factor_vm::primitive_bignum_not()
245 drepl(tag<bignum>(bignum_bitwise_not(untag<bignum>(dpeek()))));
248 PRIMITIVE_FORWARD(bignum_not)
250 inline void factor_vm::primitive_bignum_bitp()
252 fixnum bit = to_fixnum(dpop());
253 bignum *x = untag<bignum>(dpop());
254 box_boolean(bignum_logbitp(bit,x));
257 PRIMITIVE_FORWARD(bignum_bitp)
259 inline void factor_vm::primitive_bignum_log2()
261 drepl(tag<bignum>(bignum_integer_length(untag<bignum>(dpeek()))));
264 PRIMITIVE_FORWARD(bignum_log2)
266 unsigned int factor_vm::bignum_producer(unsigned int digit)
268 unsigned char *ptr = (unsigned char *)alien_offset(dpeek());
269 return *(ptr + digit);
272 unsigned int bignum_producer(unsigned int digit, factor_vm *myvm)
274 return myvm->bignum_producer(digit);
277 inline void factor_vm::primitive_byte_array_to_bignum()
279 cell n_digits = array_capacity(untag_check<byte_array>(dpeek()));
280 bignum * result = digit_stream_to_bignum(n_digits,factor::bignum_producer,0x100,0);
281 drepl(tag<bignum>(result));
284 PRIMITIVE_FORWARD(byte_array_to_bignum)
286 cell factor_vm::unbox_array_size()
288 switch(tagged<object>(dpeek()).type())
292 fixnum n = untag_fixnum(dpeek());
293 if(n >= 0 && n < (fixnum)array_size_max)
302 bignum * zero = untag<bignum>(bignum_zero);
303 bignum * max = cell_to_bignum(array_size_max);
304 bignum * n = untag<bignum>(dpeek());
305 if(bignum_compare(n,zero) != bignum_comparison_less
306 && bignum_compare(n,max) == bignum_comparison_less)
309 return bignum_to_cell(n);
315 general_error(ERROR_ARRAY_SIZE,dpop(),tag_fixnum(array_size_max),NULL);
316 return 0; /* can't happen */
319 inline void factor_vm::primitive_fixnum_to_float()
321 drepl(allot_float(fixnum_to_float(dpeek())));
324 PRIMITIVE_FORWARD(fixnum_to_float)
326 inline void factor_vm::primitive_bignum_to_float()
328 drepl(allot_float(bignum_to_float(dpeek())));
331 PRIMITIVE_FORWARD(bignum_to_float)
333 inline void factor_vm::primitive_str_to_float()
335 byte_array *bytes = untag_check<byte_array>(dpeek());
336 cell capacity = array_capacity(bytes);
338 char *c_str = (char *)(bytes + 1);
340 double f = strtod(c_str,&end);
341 if(end == c_str + capacity - 1)
342 drepl(allot_float(f));
347 PRIMITIVE_FORWARD(str_to_float)
349 inline void factor_vm::primitive_float_to_str()
351 byte_array *array = allot_byte_array(33);
352 snprintf((char *)(array + 1),32,"%.16g",untag_float_check(dpop()));
353 dpush(tag<byte_array>(array));
356 PRIMITIVE_FORWARD(float_to_str)
358 #define POP_FLOATS(x,y) \
359 double y = untag_float(dpop()); \
360 double x = untag_float(dpop());
362 inline void factor_vm::primitive_float_eq()
368 PRIMITIVE_FORWARD(float_eq)
370 inline void factor_vm::primitive_float_add()
376 PRIMITIVE_FORWARD(float_add)
378 inline void factor_vm::primitive_float_subtract()
384 PRIMITIVE_FORWARD(float_subtract)
386 inline void factor_vm::primitive_float_multiply()
392 PRIMITIVE_FORWARD(float_multiply)
394 inline void factor_vm::primitive_float_divfloat()
400 PRIMITIVE_FORWARD(float_divfloat)
402 inline void factor_vm::primitive_float_mod()
405 box_double(fmod(x,y));
408 PRIMITIVE_FORWARD(float_mod)
410 inline void factor_vm::primitive_float_less()
416 PRIMITIVE_FORWARD(float_less)
418 inline void factor_vm::primitive_float_lesseq()
424 PRIMITIVE_FORWARD(float_lesseq)
426 inline void factor_vm::primitive_float_greater()
432 PRIMITIVE_FORWARD(float_greater)
434 inline void factor_vm::primitive_float_greatereq()
440 PRIMITIVE_FORWARD(float_greatereq)
442 inline void factor_vm::primitive_float_bits()
444 box_unsigned_4(float_bits(untag_float_check(dpop())));
447 PRIMITIVE_FORWARD(float_bits)
449 inline void factor_vm::primitive_bits_float()
451 box_float(bits_float(to_cell(dpop())));
454 PRIMITIVE_FORWARD(bits_float)
456 inline void factor_vm::primitive_double_bits()
458 box_unsigned_8(double_bits(untag_float_check(dpop())));
461 PRIMITIVE_FORWARD(double_bits)
463 inline void factor_vm::primitive_bits_double()
465 box_double(bits_double(to_unsigned_8(dpop())));
468 PRIMITIVE_FORWARD(bits_double)
470 fixnum factor_vm::to_fixnum(cell tagged)
475 return untag_fixnum(tagged);
477 return bignum_to_fixnum(untag<bignum>(tagged));
479 type_error(FIXNUM_TYPE,tagged);
480 return 0; /* can't happen */
484 VM_C_API fixnum to_fixnum(cell tagged,factor_vm *myvm)
487 return VM_PTR->to_fixnum(tagged);
490 cell factor_vm::to_cell(cell tagged)
492 return (cell)to_fixnum(tagged);
495 VM_C_API cell to_cell(cell tagged, factor_vm *myvm)
498 return VM_PTR->to_cell(tagged);
501 void factor_vm::box_signed_1(s8 n)
503 dpush(tag_fixnum(n));
506 VM_C_API void box_signed_1(s8 n,factor_vm *myvm)
509 return VM_PTR->box_signed_1(n);
512 void factor_vm::box_unsigned_1(u8 n)
514 dpush(tag_fixnum(n));
517 VM_C_API void box_unsigned_1(u8 n,factor_vm *myvm)
520 return VM_PTR->box_unsigned_1(n);
523 void factor_vm::box_signed_2(s16 n)
525 dpush(tag_fixnum(n));
528 VM_C_API void box_signed_2(s16 n,factor_vm *myvm)
531 return VM_PTR->box_signed_2(n);
534 void factor_vm::box_unsigned_2(u16 n)
536 dpush(tag_fixnum(n));
539 VM_C_API void box_unsigned_2(u16 n,factor_vm *myvm)
542 return VM_PTR->box_unsigned_2(n);
545 void factor_vm::box_signed_4(s32 n)
547 dpush(allot_integer(n));
550 VM_C_API void box_signed_4(s32 n,factor_vm *myvm)
553 return VM_PTR->box_signed_4(n);
556 void factor_vm::box_unsigned_4(u32 n)
558 dpush(allot_cell(n));
561 VM_C_API void box_unsigned_4(u32 n,factor_vm *myvm)
564 return VM_PTR->box_unsigned_4(n);
567 void factor_vm::box_signed_cell(fixnum integer)
569 dpush(allot_integer(integer));
572 VM_C_API void box_signed_cell(fixnum integer,factor_vm *myvm)
575 return VM_PTR->box_signed_cell(integer);
578 void factor_vm::box_unsigned_cell(cell cell)
580 dpush(allot_cell(cell));
583 VM_C_API void box_unsigned_cell(cell cell,factor_vm *myvm)
586 return VM_PTR->box_unsigned_cell(cell);
589 void factor_vm::box_signed_8(s64 n)
591 if(n < fixnum_min || n > fixnum_max)
592 dpush(tag<bignum>(long_long_to_bignum(n)));
594 dpush(tag_fixnum(n));
597 VM_C_API void box_signed_8(s64 n,factor_vm *myvm)
600 return VM_PTR->box_signed_8(n);
603 s64 factor_vm::to_signed_8(cell obj)
605 switch(tagged<object>(obj).type())
608 return untag_fixnum(obj);
610 return bignum_to_long_long(untag<bignum>(obj));
612 type_error(BIGNUM_TYPE,obj);
617 VM_C_API s64 to_signed_8(cell obj,factor_vm *myvm)
620 return VM_PTR->to_signed_8(obj);
623 void factor_vm::box_unsigned_8(u64 n)
625 if(n > (u64)fixnum_max)
626 dpush(tag<bignum>(ulong_long_to_bignum(n)));
628 dpush(tag_fixnum(n));
631 VM_C_API void box_unsigned_8(u64 n,factor_vm *myvm)
634 return VM_PTR->box_unsigned_8(n);
637 u64 factor_vm::to_unsigned_8(cell obj)
639 switch(tagged<object>(obj).type())
642 return untag_fixnum(obj);
644 return bignum_to_ulong_long(untag<bignum>(obj));
646 type_error(BIGNUM_TYPE,obj);
651 VM_C_API u64 to_unsigned_8(cell obj,factor_vm *myvm)
654 return VM_PTR->to_unsigned_8(obj);
657 void factor_vm::box_float(float flo)
659 dpush(allot_float(flo));
662 VM_C_API void box_float(float flo, factor_vm *myvm)
665 return VM_PTR->box_float(flo);
668 float factor_vm::to_float(cell value)
670 return untag_float_check(value);
673 VM_C_API float to_float(cell value,factor_vm *myvm)
676 return VM_PTR->to_float(value);
679 void factor_vm::box_double(double flo)
681 dpush(allot_float(flo));
684 VM_C_API void box_double(double flo,factor_vm *myvm)
687 return VM_PTR->box_double(flo);
690 double factor_vm::to_double(cell value)
692 return untag_float_check(value);
695 VM_C_API double to_double(cell value,factor_vm *myvm)
698 return VM_PTR->to_double(value);
701 /* The fixnum+, fixnum- and fixnum* primitives are defined in cpu_*.S. On
702 overflow, they call these functions. */
703 inline void factor_vm::overflow_fixnum_add(fixnum x, fixnum y)
705 drepl(tag<bignum>(fixnum_to_bignum(
706 untag_fixnum(x) + untag_fixnum(y))));
709 VM_ASM_API void overflow_fixnum_add(fixnum x, fixnum y, factor_vm *myvm)
711 PRIMITIVE_OVERFLOW_GETVM()->overflow_fixnum_add(x,y);
714 inline void factor_vm::overflow_fixnum_subtract(fixnum x, fixnum y)
716 drepl(tag<bignum>(fixnum_to_bignum(
717 untag_fixnum(x) - untag_fixnum(y))));
720 VM_ASM_API void overflow_fixnum_subtract(fixnum x, fixnum y, factor_vm *myvm)
722 PRIMITIVE_OVERFLOW_GETVM()->overflow_fixnum_subtract(x,y);
725 inline void factor_vm::overflow_fixnum_multiply(fixnum x, fixnum y)
727 bignum *bx = fixnum_to_bignum(x);
729 bignum *by = fixnum_to_bignum(y);
731 drepl(tag<bignum>(bignum_multiply(bx,by)));
734 VM_ASM_API void overflow_fixnum_multiply(fixnum x, fixnum y, factor_vm *myvm)
736 PRIMITIVE_OVERFLOW_GETVM()->overflow_fixnum_multiply(x,y);