6 inline void factorvm::vmprim_bignum_to_fixnum()
8 drepl(tag_fixnum(bignum_to_fixnum(untag<bignum>(dpeek()))));
11 PRIMITIVE(bignum_to_fixnum)
13 PRIMITIVE_GETVM()->vmprim_bignum_to_fixnum();
16 inline void factorvm::vmprim_float_to_fixnum()
18 drepl(tag_fixnum(float_to_fixnum(dpeek())));
21 PRIMITIVE(float_to_fixnum)
23 PRIMITIVE_GETVM()->vmprim_float_to_fixnum();
26 /* Division can only overflow when we are dividing the most negative fixnum
28 inline void factorvm::vmprim_fixnum_divint()
30 fixnum y = untag_fixnum(dpop()); \
31 fixnum x = untag_fixnum(dpeek());
32 fixnum result = x / y;
33 if(result == -fixnum_min)
34 drepl(allot_integer(-fixnum_min));
36 drepl(tag_fixnum(result));
39 PRIMITIVE(fixnum_divint)
41 PRIMITIVE_GETVM()->vmprim_fixnum_divint();
44 inline void factorvm::vmprim_fixnum_divmod()
46 cell y = ((cell *)ds)[0];
47 cell x = ((cell *)ds)[-1];
48 if(y == tag_fixnum(-1) && x == tag_fixnum(fixnum_min))
50 ((cell *)ds)[-1] = allot_integer(-fixnum_min);
51 ((cell *)ds)[0] = tag_fixnum(0);
55 ((cell *)ds)[-1] = tag_fixnum(untag_fixnum(x) / untag_fixnum(y));
56 ((cell *)ds)[0] = (fixnum)x % (fixnum)y;
60 PRIMITIVE(fixnum_divmod)
62 PRIMITIVE_GETVM()->vmprim_fixnum_divmod();
66 * If we're shifting right by n bits, we won't overflow as long as none of the
67 * high WORD_SIZE-TAG_BITS-n bits are set.
69 inline fixnum factorvm::sign_mask(fixnum x)
71 return x >> (WORD_SIZE - 1);
75 inline fixnum factorvm::branchless_max(fixnum x, fixnum y)
77 return (x - ((x - y) & sign_mask(x - y)));
81 inline fixnum factorvm::branchless_abs(fixnum x)
83 return (x ^ sign_mask(x)) - sign_mask(x);
87 inline void factorvm::vmprim_fixnum_shift()
89 fixnum y = untag_fixnum(dpop());
90 fixnum x = untag_fixnum(dpeek());
96 y = branchless_max(y,-WORD_SIZE + 1);
97 drepl(tag_fixnum(x >> -y));
100 else if(y < WORD_SIZE - TAG_BITS)
102 fixnum mask = -((fixnum)1 << (WORD_SIZE - 1 - TAG_BITS - y));
103 if(!(branchless_abs(x) & mask))
105 drepl(tag_fixnum(x << y));
110 drepl(tag<bignum>(bignum_arithmetic_shift(
111 fixnum_to_bignum(x),y)));
114 PRIMITIVE(fixnum_shift)
116 PRIMITIVE_GETVM()->vmprim_fixnum_shift();
119 inline void factorvm::vmprim_fixnum_to_bignum()
121 drepl(tag<bignum>(fixnum_to_bignum(untag_fixnum(dpeek()))));
124 PRIMITIVE(fixnum_to_bignum)
126 PRIMITIVE_GETVM()->vmprim_fixnum_to_bignum();
129 inline void factorvm::vmprim_float_to_bignum()
131 drepl(tag<bignum>(float_to_bignum(dpeek())));
134 PRIMITIVE(float_to_bignum)
136 PRIMITIVE_GETVM()->vmprim_float_to_bignum();
139 #define POP_BIGNUMS(x,y) \
140 bignum * y = untag<bignum>(dpop()); \
141 bignum * x = untag<bignum>(dpop());
143 inline void factorvm::vmprim_bignum_eq()
146 box_boolean(bignum_equal_p(x,y));
151 PRIMITIVE_GETVM()->vmprim_bignum_eq();
154 inline void factorvm::vmprim_bignum_add()
157 dpush(tag<bignum>(bignum_add(x,y)));
160 PRIMITIVE(bignum_add)
162 PRIMITIVE_GETVM()->vmprim_bignum_add();
165 inline void factorvm::vmprim_bignum_subtract()
168 dpush(tag<bignum>(bignum_subtract(x,y)));
171 PRIMITIVE(bignum_subtract)
173 PRIMITIVE_GETVM()->vmprim_bignum_subtract();
176 inline void factorvm::vmprim_bignum_multiply()
179 dpush(tag<bignum>(bignum_multiply(x,y)));
182 PRIMITIVE(bignum_multiply)
184 PRIMITIVE_GETVM()->vmprim_bignum_multiply();
187 inline void factorvm::vmprim_bignum_divint()
190 dpush(tag<bignum>(bignum_quotient(x,y)));
193 PRIMITIVE(bignum_divint)
195 PRIMITIVE_GETVM()->vmprim_bignum_divint();
198 inline void factorvm::vmprim_bignum_divmod()
202 bignum_divide(x,y,&q,&r);
203 dpush(tag<bignum>(q));
204 dpush(tag<bignum>(r));
207 PRIMITIVE(bignum_divmod)
209 PRIMITIVE_GETVM()->vmprim_bignum_divmod();
212 inline void factorvm::vmprim_bignum_mod()
215 dpush(tag<bignum>(bignum_remainder(x,y)));
218 PRIMITIVE(bignum_mod)
220 PRIMITIVE_GETVM()->vmprim_bignum_mod();
223 inline void factorvm::vmprim_bignum_and()
226 dpush(tag<bignum>(bignum_bitwise_and(x,y)));
229 PRIMITIVE(bignum_and)
231 PRIMITIVE_GETVM()->vmprim_bignum_and();
234 inline void factorvm::vmprim_bignum_or()
237 dpush(tag<bignum>(bignum_bitwise_ior(x,y)));
242 PRIMITIVE_GETVM()->vmprim_bignum_or();
245 inline void factorvm::vmprim_bignum_xor()
248 dpush(tag<bignum>(bignum_bitwise_xor(x,y)));
251 PRIMITIVE(bignum_xor)
253 PRIMITIVE_GETVM()->vmprim_bignum_xor();
256 inline void factorvm::vmprim_bignum_shift()
258 fixnum y = untag_fixnum(dpop());
259 bignum* x = untag<bignum>(dpop());
260 dpush(tag<bignum>(bignum_arithmetic_shift(x,y)));
263 PRIMITIVE(bignum_shift)
265 PRIMITIVE_GETVM()->vmprim_bignum_shift();
268 inline void factorvm::vmprim_bignum_less()
271 box_boolean(bignum_compare(x,y) == bignum_comparison_less);
274 PRIMITIVE(bignum_less)
276 PRIMITIVE_GETVM()->vmprim_bignum_less();
279 inline void factorvm::vmprim_bignum_lesseq()
282 box_boolean(bignum_compare(x,y) != bignum_comparison_greater);
285 PRIMITIVE(bignum_lesseq)
287 PRIMITIVE_GETVM()->vmprim_bignum_lesseq();
290 inline void factorvm::vmprim_bignum_greater()
293 box_boolean(bignum_compare(x,y) == bignum_comparison_greater);
296 PRIMITIVE(bignum_greater)
298 PRIMITIVE_GETVM()->vmprim_bignum_greater();
301 inline void factorvm::vmprim_bignum_greatereq()
304 box_boolean(bignum_compare(x,y) != bignum_comparison_less);
307 PRIMITIVE(bignum_greatereq)
309 PRIMITIVE_GETVM()->vmprim_bignum_greatereq();
312 inline void factorvm::vmprim_bignum_not()
314 drepl(tag<bignum>(bignum_bitwise_not(untag<bignum>(dpeek()))));
317 PRIMITIVE(bignum_not)
319 PRIMITIVE_GETVM()->vmprim_bignum_not();
322 inline void factorvm::vmprim_bignum_bitp()
324 fixnum bit = to_fixnum(dpop());
325 bignum *x = untag<bignum>(dpop());
326 box_boolean(bignum_logbitp(bit,x));
329 PRIMITIVE(bignum_bitp)
331 PRIMITIVE_GETVM()->vmprim_bignum_bitp();
334 inline void factorvm::vmprim_bignum_log2()
336 drepl(tag<bignum>(bignum_integer_length(untag<bignum>(dpeek()))));
339 PRIMITIVE(bignum_log2)
341 PRIMITIVE_GETVM()->vmprim_bignum_log2();
344 unsigned int factorvm::bignum_producer(unsigned int digit)
346 unsigned char *ptr = (unsigned char *)alien_offset(dpeek());
347 return *(ptr + digit);
350 unsigned int bignum_producer(unsigned int digit, factorvm *myvm)
352 return myvm->bignum_producer(digit);
355 inline void factorvm::vmprim_byte_array_to_bignum()
357 cell n_digits = array_capacity(untag_check<byte_array>(dpeek()));
358 // bignum * result = factor::digit_stream_to_bignum(n_digits,factor::bignum_producer,0x100,0);
359 bignum * result = digit_stream_to_bignum(n_digits,factor::bignum_producer,0x100,0);
360 drepl(tag<bignum>(result));
363 PRIMITIVE(byte_array_to_bignum)
365 PRIMITIVE_GETVM()->vmprim_byte_array_to_bignum();
368 cell factorvm::unbox_array_size()
370 switch(tagged<object>(dpeek()).type())
374 fixnum n = untag_fixnum(dpeek());
375 if(n >= 0 && n < (fixnum)array_size_max)
384 bignum * zero = untag<bignum>(bignum_zero);
385 bignum * max = cell_to_bignum(array_size_max);
386 bignum * n = untag<bignum>(dpeek());
387 if(bignum_compare(n,zero) != bignum_comparison_less
388 && bignum_compare(n,max) == bignum_comparison_less)
391 return bignum_to_cell(n);
397 general_error(ERROR_ARRAY_SIZE,dpop(),tag_fixnum(array_size_max),NULL);
398 return 0; /* can't happen */
402 inline void factorvm::vmprim_fixnum_to_float()
404 drepl(allot_float(fixnum_to_float(dpeek())));
407 PRIMITIVE(fixnum_to_float)
409 PRIMITIVE_GETVM()->vmprim_fixnum_to_float();
412 inline void factorvm::vmprim_bignum_to_float()
414 drepl(allot_float(bignum_to_float(dpeek())));
417 PRIMITIVE(bignum_to_float)
419 PRIMITIVE_GETVM()->vmprim_bignum_to_float();
422 inline void factorvm::vmprim_str_to_float()
424 byte_array *bytes = untag_check<byte_array>(dpeek());
425 cell capacity = array_capacity(bytes);
427 char *c_str = (char *)(bytes + 1);
429 double f = strtod(c_str,&end);
430 if(end == c_str + capacity - 1)
431 drepl(allot_float(f));
436 PRIMITIVE(str_to_float)
438 PRIMITIVE_GETVM()->vmprim_str_to_float();
441 inline void factorvm::vmprim_float_to_str()
443 byte_array *array = allot_byte_array(33);
444 snprintf((char *)(array + 1),32,"%.16g",untag_float_check(dpop()));
445 dpush(tag<byte_array>(array));
448 PRIMITIVE(float_to_str)
450 PRIMITIVE_GETVM()->vmprim_float_to_str();
453 #define POP_FLOATS(x,y) \
454 double y = untag_float(dpop()); \
455 double x = untag_float(dpop());
457 inline void factorvm::vmprim_float_eq()
465 PRIMITIVE_GETVM()->vmprim_float_eq();
468 inline void factorvm::vmprim_float_add()
476 PRIMITIVE_GETVM()->vmprim_float_add();
479 inline void factorvm::vmprim_float_subtract()
485 PRIMITIVE(float_subtract)
487 PRIMITIVE_GETVM()->vmprim_float_subtract();
490 inline void factorvm::vmprim_float_multiply()
496 PRIMITIVE(float_multiply)
498 PRIMITIVE_GETVM()->vmprim_float_multiply();
501 inline void factorvm::vmprim_float_divfloat()
507 PRIMITIVE(float_divfloat)
509 PRIMITIVE_GETVM()->vmprim_float_divfloat();
512 inline void factorvm::vmprim_float_mod()
515 box_double(fmod(x,y));
520 PRIMITIVE_GETVM()->vmprim_float_mod();
523 inline void factorvm::vmprim_float_less()
529 PRIMITIVE(float_less)
531 PRIMITIVE_GETVM()->vmprim_float_less();
534 inline void factorvm::vmprim_float_lesseq()
540 PRIMITIVE(float_lesseq)
542 PRIMITIVE_GETVM()->vmprim_float_lesseq();
545 inline void factorvm::vmprim_float_greater()
551 PRIMITIVE(float_greater)
553 PRIMITIVE_GETVM()->vmprim_float_greater();
556 inline void factorvm::vmprim_float_greatereq()
562 PRIMITIVE(float_greatereq)
564 PRIMITIVE_GETVM()->vmprim_float_greatereq();
567 inline void factorvm::vmprim_float_bits()
569 box_unsigned_4(float_bits(untag_float_check(dpop())));
572 PRIMITIVE(float_bits)
574 PRIMITIVE_GETVM()->vmprim_float_bits();
577 inline void factorvm::vmprim_bits_float()
579 box_float(bits_float(to_cell(dpop())));
582 PRIMITIVE(bits_float)
584 PRIMITIVE_GETVM()->vmprim_bits_float();
587 inline void factorvm::vmprim_double_bits()
589 box_unsigned_8(double_bits(untag_float_check(dpop())));
592 PRIMITIVE(double_bits)
594 PRIMITIVE_GETVM()->vmprim_double_bits();
597 inline void factorvm::vmprim_bits_double()
599 box_double(bits_double(to_unsigned_8(dpop())));
602 PRIMITIVE(bits_double)
604 PRIMITIVE_GETVM()->vmprim_bits_double();
607 fixnum factorvm::to_fixnum(cell tagged)
612 return untag_fixnum(tagged);
614 return bignum_to_fixnum(untag<bignum>(tagged));
616 type_error(FIXNUM_TYPE,tagged);
617 return 0; /* can't happen */
621 VM_C_API fixnum to_fixnum(cell tagged,factorvm *myvm)
623 return VM_PTR->to_fixnum(tagged);
626 cell factorvm::to_cell(cell tagged)
628 return (cell)to_fixnum(tagged);
631 VM_C_API cell to_cell(cell tagged, factorvm *myvm)
633 return VM_PTR->to_cell(tagged);
636 void factorvm::box_signed_1(s8 n)
638 dpush(tag_fixnum(n));
641 VM_C_API void box_signed_1(s8 n,factorvm *myvm)
644 return VM_PTR->box_signed_1(n);
647 void factorvm::box_unsigned_1(u8 n)
649 dpush(tag_fixnum(n));
652 VM_C_API void box_unsigned_1(u8 n,factorvm *myvm)
655 return VM_PTR->box_unsigned_1(n);
658 void factorvm::box_signed_2(s16 n)
660 dpush(tag_fixnum(n));
663 VM_C_API void box_signed_2(s16 n,factorvm *myvm)
666 return VM_PTR->box_signed_2(n);
669 void factorvm::box_unsigned_2(u16 n)
671 dpush(tag_fixnum(n));
674 VM_C_API void box_unsigned_2(u16 n,factorvm *myvm)
677 return VM_PTR->box_unsigned_2(n);
680 void factorvm::box_signed_4(s32 n)
682 dpush(allot_integer(n));
685 VM_C_API void box_signed_4(s32 n,factorvm *myvm)
688 return VM_PTR->box_signed_4(n);
691 void factorvm::box_unsigned_4(u32 n)
693 dpush(allot_cell(n));
696 VM_C_API void box_unsigned_4(u32 n,factorvm *myvm)
699 return VM_PTR->box_unsigned_4(n);
702 void factorvm::box_signed_cell(fixnum integer)
704 dpush(allot_integer(integer));
707 VM_C_API void box_signed_cell(fixnum integer,factorvm *myvm)
710 return VM_PTR->box_signed_cell(integer);
713 void factorvm::box_unsigned_cell(cell cell)
715 dpush(allot_cell(cell));
718 VM_C_API void box_unsigned_cell(cell cell,factorvm *myvm)
721 return VM_PTR->box_unsigned_cell(cell);
724 void factorvm::box_signed_8(s64 n)
726 if(n < fixnum_min || n > fixnum_max)
727 dpush(tag<bignum>(long_long_to_bignum(n)));
729 dpush(tag_fixnum(n));
732 VM_C_API void box_signed_8(s64 n,factorvm *myvm)
735 return VM_PTR->box_signed_8(n);
738 s64 factorvm::to_signed_8(cell obj)
740 switch(tagged<object>(obj).type())
743 return untag_fixnum(obj);
745 return bignum_to_long_long(untag<bignum>(obj));
747 type_error(BIGNUM_TYPE,obj);
752 VM_C_API s64 to_signed_8(cell obj,factorvm *myvm)
755 return VM_PTR->to_signed_8(obj);
758 void factorvm::box_unsigned_8(u64 n)
760 if(n > (u64)fixnum_max)
761 dpush(tag<bignum>(ulong_long_to_bignum(n)));
763 dpush(tag_fixnum(n));
766 VM_C_API void box_unsigned_8(u64 n,factorvm *myvm)
769 return VM_PTR->box_unsigned_8(n);
772 u64 factorvm::to_unsigned_8(cell obj)
774 switch(tagged<object>(obj).type())
777 return untag_fixnum(obj);
779 return bignum_to_ulong_long(untag<bignum>(obj));
781 type_error(BIGNUM_TYPE,obj);
786 VM_C_API u64 to_unsigned_8(cell obj,factorvm *myvm)
789 return VM_PTR->to_unsigned_8(obj);
792 void factorvm::box_float(float flo)
794 dpush(allot_float(flo));
797 VM_C_API void box_float(float flo,factorvm *myvm) // not sure if this is ever called
800 return VM_PTR->box_float(flo);
803 float factorvm::to_float(cell value)
805 return untag_float_check(value);
808 VM_C_API float to_float(cell value,factorvm *myvm)
810 return VM_PTR->to_float(value);
813 void factorvm::box_double(double flo)
815 dpush(allot_float(flo));
818 VM_C_API void box_double(double flo,factorvm *myvm) // not sure if this is ever called
821 return VM_PTR->box_double(flo);
824 double factorvm::to_double(cell value)
826 return untag_float_check(value);
829 VM_C_API double to_double(cell value,factorvm *myvm)
831 return VM_PTR->to_double(value);
834 /* The fixnum+, fixnum- and fixnum* primitives are defined in cpu_*.S. On
835 overflow, they call these functions. */
836 void factorvm::overflow_fixnum_add(fixnum x, fixnum y)
838 drepl(tag<bignum>(fixnum_to_bignum(
839 untag_fixnum(x) + untag_fixnum(y))));
842 VM_ASM_API void overflow_fixnum_add(fixnum x, fixnum y)
844 return PRIMITIVE_OVERFLOW_GETVM()->overflow_fixnum_add(x,y);
847 void factorvm::overflow_fixnum_subtract(fixnum x, fixnum y)
849 drepl(tag<bignum>(fixnum_to_bignum(
850 untag_fixnum(x) - untag_fixnum(y))));
853 VM_ASM_API void overflow_fixnum_subtract(fixnum x, fixnum y)
855 return PRIMITIVE_OVERFLOW_GETVM()->overflow_fixnum_subtract(x,y);
858 void factorvm::overflow_fixnum_multiply(fixnum x, fixnum y)
860 bignum *bx = fixnum_to_bignum(x);
862 bignum *by = fixnum_to_bignum(y);
864 drepl(tag<bignum>(bignum_multiply(bx,by)));
867 VM_ASM_API void overflow_fixnum_multiply(fixnum x, fixnum y)
869 return PRIMITIVE_OVERFLOW_GETVM()->overflow_fixnum_multiply(x,y);