10 PRIMITIVE(bignum_to_fixnum)
12 drepl(tag_fixnum(bignum_to_fixnum(untag<bignum>(dpeek()))));
15 PRIMITIVE(float_to_fixnum)
17 drepl(tag_fixnum(float_to_fixnum(dpeek())));
20 /* Division can only overflow when we are dividing the most negative fixnum
22 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(fixnum_divmod)
35 cell y = ((cell *)ds)[0];
36 cell x = ((cell *)ds)[-1];
37 if(y == tag_fixnum(-1) && x == tag_fixnum(fixnum_min))
39 ((cell *)ds)[-1] = allot_integer(-fixnum_min);
40 ((cell *)ds)[0] = tag_fixnum(0);
44 ((cell *)ds)[-1] = tag_fixnum(untag_fixnum(x) / untag_fixnum(y));
45 ((cell *)ds)[0] = (fixnum)x % (fixnum)y;
50 * If we're shifting right by n bits, we won't overflow as long as none of the
51 * high WORD_SIZE-TAG_BITS-n bits are set.
53 static inline fixnum sign_mask(fixnum x)
55 return x >> (WORD_SIZE - 1);
58 static inline fixnum branchless_max(fixnum x, fixnum y)
60 return (x - ((x - y) & sign_mask(x - y)));
63 static inline fixnum branchless_abs(fixnum x)
65 return (x ^ sign_mask(x)) - sign_mask(x);
68 PRIMITIVE(fixnum_shift)
70 fixnum y = untag_fixnum(dpop());
71 fixnum x = untag_fixnum(dpeek());
77 y = branchless_max(y,-WORD_SIZE + 1);
78 drepl(tag_fixnum(x >> -y));
81 else if(y < WORD_SIZE - TAG_BITS)
83 fixnum mask = -((fixnum)1 << (WORD_SIZE - 1 - TAG_BITS - y));
84 if(!(branchless_abs(x) & mask))
86 drepl(tag_fixnum(x << y));
91 drepl(tag<bignum>(bignum_arithmetic_shift(
92 fixnum_to_bignum(x),y)));
95 PRIMITIVE(fixnum_to_bignum)
97 drepl(tag<bignum>(fixnum_to_bignum(untag_fixnum(dpeek()))));
100 PRIMITIVE(float_to_bignum)
102 drepl(tag<bignum>(float_to_bignum(dpeek())));
105 #define POP_BIGNUMS(x,y) \
106 bignum * y = untag<bignum>(dpop()); \
107 bignum * x = untag<bignum>(dpop());
112 box_boolean(bignum_equal_p(x,y));
115 PRIMITIVE(bignum_add)
118 dpush(tag<bignum>(bignum_add(x,y)));
121 PRIMITIVE(bignum_subtract)
124 dpush(tag<bignum>(bignum_subtract(x,y)));
127 PRIMITIVE(bignum_multiply)
130 dpush(tag<bignum>(bignum_multiply(x,y)));
133 PRIMITIVE(bignum_divint)
136 dpush(tag<bignum>(bignum_quotient(x,y)));
139 PRIMITIVE(bignum_divmod)
143 bignum_divide(x,y,&q,&r);
144 dpush(tag<bignum>(q));
145 dpush(tag<bignum>(r));
148 PRIMITIVE(bignum_mod)
151 dpush(tag<bignum>(bignum_remainder(x,y)));
154 PRIMITIVE(bignum_and)
157 dpush(tag<bignum>(bignum_bitwise_and(x,y)));
163 dpush(tag<bignum>(bignum_bitwise_ior(x,y)));
166 PRIMITIVE(bignum_xor)
169 dpush(tag<bignum>(bignum_bitwise_xor(x,y)));
172 PRIMITIVE(bignum_shift)
174 fixnum y = untag_fixnum(dpop());
175 bignum* x = untag<bignum>(dpop());
176 dpush(tag<bignum>(bignum_arithmetic_shift(x,y)));
179 PRIMITIVE(bignum_less)
182 box_boolean(bignum_compare(x,y) == bignum_comparison_less);
185 PRIMITIVE(bignum_lesseq)
188 box_boolean(bignum_compare(x,y) != bignum_comparison_greater);
191 PRIMITIVE(bignum_greater)
194 box_boolean(bignum_compare(x,y) == bignum_comparison_greater);
197 PRIMITIVE(bignum_greatereq)
200 box_boolean(bignum_compare(x,y) != bignum_comparison_less);
203 PRIMITIVE(bignum_not)
205 drepl(tag<bignum>(bignum_bitwise_not(untag<bignum>(dpeek()))));
208 PRIMITIVE(bignum_bitp)
210 fixnum bit = to_fixnum(dpop());
211 bignum *x = untag<bignum>(dpop());
212 box_boolean(bignum_logbitp(bit,x));
215 PRIMITIVE(bignum_log2)
217 drepl(tag<bignum>(bignum_integer_length(untag<bignum>(dpeek()))));
220 unsigned int bignum_producer(unsigned int digit)
222 unsigned char *ptr = (unsigned char *)alien_offset(dpeek());
223 return *(ptr + digit);
226 PRIMITIVE(byte_array_to_bignum)
228 cell n_digits = array_capacity(untag_check<byte_array>(dpeek()));
229 bignum * result = digit_stream_to_bignum(n_digits,bignum_producer,0x100,0);
230 drepl(tag<bignum>(result));
233 cell unbox_array_size()
235 switch(tagged<object>(dpeek()).type())
239 fixnum n = untag_fixnum(dpeek());
240 if(n >= 0 && n < (fixnum)array_size_max)
249 bignum * zero = untag<bignum>(bignum_zero);
250 bignum * max = cell_to_bignum(array_size_max);
251 bignum * n = untag<bignum>(dpeek());
252 if(bignum_compare(n,zero) != bignum_comparison_less
253 && bignum_compare(n,max) == bignum_comparison_less)
256 return bignum_to_cell(n);
262 general_error(ERROR_ARRAY_SIZE,dpop(),tag_fixnum(array_size_max),NULL);
263 return 0; /* can't happen */
266 PRIMITIVE(fixnum_to_float)
268 drepl(allot_float(fixnum_to_float(dpeek())));
271 PRIMITIVE(bignum_to_float)
273 drepl(allot_float(bignum_to_float(dpeek())));
276 PRIMITIVE(str_to_float)
278 byte_array *bytes = untag_check<byte_array>(dpeek());
279 cell capacity = array_capacity(bytes);
281 char *c_str = (char *)(bytes + 1);
283 double f = strtod(c_str,&end);
284 if(end == c_str + capacity - 1)
285 drepl(allot_float(f));
290 PRIMITIVE(float_to_str)
292 byte_array *array = allot_byte_array(33);
293 snprintf((char *)(array + 1),32,"%.16g",untag_float_check(dpop()));
294 dpush(tag<byte_array>(array));
297 #define POP_FLOATS(x,y) \
298 double y = untag_float(dpop()); \
299 double x = untag_float(dpop());
313 PRIMITIVE(float_subtract)
319 PRIMITIVE(float_multiply)
325 PRIMITIVE(float_divfloat)
334 box_double(fmod(x,y));
337 PRIMITIVE(float_less)
343 PRIMITIVE(float_lesseq)
349 PRIMITIVE(float_greater)
355 PRIMITIVE(float_greatereq)
361 PRIMITIVE(float_bits)
363 box_unsigned_4(float_bits(untag_float_check(dpop())));
366 PRIMITIVE(bits_float)
368 box_float(bits_float(to_cell(dpop())));
371 PRIMITIVE(double_bits)
373 box_unsigned_8(double_bits(untag_float_check(dpop())));
376 PRIMITIVE(bits_double)
378 box_double(bits_double(to_unsigned_8(dpop())));
381 VM_C_API fixnum to_fixnum(cell tagged)
386 return untag_fixnum(tagged);
388 return bignum_to_fixnum(untag<bignum>(tagged));
390 type_error(FIXNUM_TYPE,tagged);
391 return 0; /* can't happen */
395 VM_C_API cell to_cell(cell tagged)
397 return (cell)to_fixnum(tagged);
400 VM_C_API void box_signed_1(s8 n)
402 dpush(tag_fixnum(n));
405 VM_C_API void box_unsigned_1(u8 n)
407 dpush(tag_fixnum(n));
410 VM_C_API void box_signed_2(s16 n)
412 dpush(tag_fixnum(n));
415 VM_C_API void box_unsigned_2(u16 n)
417 dpush(tag_fixnum(n));
420 VM_C_API void box_signed_4(s32 n)
422 dpush(allot_integer(n));
425 VM_C_API void box_unsigned_4(u32 n)
427 dpush(allot_cell(n));
430 VM_C_API void box_signed_cell(fixnum integer)
432 dpush(allot_integer(integer));
435 VM_C_API void box_unsigned_cell(cell cell)
437 dpush(allot_cell(cell));
440 VM_C_API void box_signed_8(s64 n)
442 if(n < fixnum_min || n > fixnum_max)
443 dpush(tag<bignum>(long_long_to_bignum(n)));
445 dpush(tag_fixnum(n));
448 VM_C_API s64 to_signed_8(cell obj)
450 switch(tagged<object>(obj).type())
453 return untag_fixnum(obj);
455 return bignum_to_long_long(untag<bignum>(obj));
457 type_error(BIGNUM_TYPE,obj);
462 VM_C_API void box_unsigned_8(u64 n)
464 if(n > (u64)fixnum_max)
465 dpush(tag<bignum>(ulong_long_to_bignum(n)));
467 dpush(tag_fixnum(n));
470 VM_C_API u64 to_unsigned_8(cell obj)
472 switch(tagged<object>(obj).type())
475 return untag_fixnum(obj);
477 return bignum_to_ulong_long(untag<bignum>(obj));
479 type_error(BIGNUM_TYPE,obj);
484 VM_C_API void box_float(float flo)
486 dpush(allot_float(flo));
489 VM_C_API float to_float(cell value)
491 return untag_float_check(value);
494 VM_C_API void box_double(double flo)
496 dpush(allot_float(flo));
499 VM_C_API double to_double(cell value)
501 return untag_float_check(value);
504 /* The fixnum+, fixnum- and fixnum* primitives are defined in cpu_*.S. On
505 overflow, they call these functions. */
506 VM_ASM_API void overflow_fixnum_add(fixnum x, fixnum y)
508 drepl(tag<bignum>(fixnum_to_bignum(
509 untag_fixnum(x) + untag_fixnum(y))));
512 VM_ASM_API void overflow_fixnum_subtract(fixnum x, fixnum y)
514 drepl(tag<bignum>(fixnum_to_bignum(
515 untag_fixnum(x) - untag_fixnum(y))));
518 VM_ASM_API void overflow_fixnum_multiply(fixnum x, fixnum y)
520 bignum *bx = fixnum_to_bignum(x);
522 bignum *by = fixnum_to_bignum(y);
524 drepl(tag<bignum>(bignum_multiply(bx,by)));