8 F_FIXNUM to_fixnum(CELL tagged)
13 return untag_fixnum_fast(tagged);
15 return bignum_to_fixnum(untag_bignum_fast(tagged));
17 type_error(FIXNUM_TYPE,tagged);
18 return -1; /* can't happen */
22 CELL to_cell(CELL tagged)
24 return (CELL)to_fixnum(tagged);
27 void primitive_bignum_to_fixnum(void)
29 drepl(tag_fixnum(bignum_to_fixnum(untag_bignum_fast(dpeek()))));
32 void primitive_float_to_fixnum(void)
34 drepl(tag_fixnum(float_to_fixnum(dpeek())));
37 /* The fixnum+, fixnum- and fixnum* primitives are defined in cpu_*.S. On
38 overflow, they call these functions. */
39 F_FASTCALL void overflow_fixnum_add(F_FIXNUM x, F_FIXNUM y)
41 drepl(tag_bignum(fixnum_to_bignum(
42 untag_fixnum_fast(x) + untag_fixnum_fast(y))));
45 F_FASTCALL void overflow_fixnum_subtract(F_FIXNUM x, F_FIXNUM y)
47 drepl(tag_bignum(fixnum_to_bignum(
48 untag_fixnum_fast(x) - untag_fixnum_fast(y))));
51 F_FASTCALL void overflow_fixnum_multiply(F_FIXNUM x, F_FIXNUM y)
53 F_BIGNUM *bx = fixnum_to_bignum(x);
55 F_BIGNUM *by = fixnum_to_bignum(y);
56 UNREGISTER_BIGNUM(bx);
57 drepl(tag_bignum(bignum_multiply(bx,by)));
60 /* Division can only overflow when we are dividing the most negative fixnum
62 void primitive_fixnum_divint(void)
64 F_FIXNUM y = untag_fixnum_fast(dpop()); \
65 F_FIXNUM x = untag_fixnum_fast(dpeek());
66 F_FIXNUM result = x / y;
67 if(result == -FIXNUM_MIN)
68 drepl(allot_integer(-FIXNUM_MIN));
70 drepl(tag_fixnum(result));
73 void primitive_fixnum_divmod(void)
76 CELL x = get(ds - CELLS);
77 if(y == tag_fixnum(-1) && x == tag_fixnum(FIXNUM_MIN))
79 put(ds - CELLS,allot_integer(-FIXNUM_MIN));
80 put(ds,tag_fixnum(0));
84 put(ds - CELLS,tag_fixnum(untag_fixnum_fast(x) / untag_fixnum_fast(y)));
85 put(ds,(F_FIXNUM)x % (F_FIXNUM)y);
90 * If we're shifting right by n bits, we won't overflow as long as none of the
91 * high WORD_SIZE-TAG_BITS-n bits are set.
93 #define SIGN_MASK(x) ((x) >> (WORD_SIZE - 1))
94 #define BRANCHLESS_MAX(x,y) ((x) - (((x) - (y)) & SIGN_MASK((x) - (y))))
95 #define BRANCHLESS_ABS(x) ((x ^ SIGN_MASK(x)) - SIGN_MASK(x))
97 void primitive_fixnum_shift(void)
99 F_FIXNUM y = untag_fixnum_fast(dpop()); \
100 F_FIXNUM x = untag_fixnum_fast(dpeek());
106 y = BRANCHLESS_MAX(y,-WORD_SIZE + 1);
107 drepl(tag_fixnum(x >> -y));
110 else if(y < WORD_SIZE - TAG_BITS)
112 F_FIXNUM mask = -((F_FIXNUM)1 << (WORD_SIZE - 1 - TAG_BITS - y));
113 if(!(BRANCHLESS_ABS(x) & mask))
115 drepl(tag_fixnum(x << y));
120 drepl(tag_bignum(bignum_arithmetic_shift(
121 fixnum_to_bignum(x),y)));
125 void primitive_fixnum_to_bignum(void)
127 drepl(tag_bignum(fixnum_to_bignum(untag_fixnum_fast(dpeek()))));
130 void primitive_float_to_bignum(void)
132 drepl(tag_bignum(float_to_bignum(dpeek())));
135 #define POP_BIGNUMS(x,y) \
136 F_BIGNUM * y = untag_bignum_fast(dpop()); \
137 F_BIGNUM * x = untag_bignum_fast(dpop());
139 void primitive_bignum_eq(void)
142 box_boolean(bignum_equal_p(x,y));
145 void primitive_bignum_add(void)
148 dpush(tag_bignum(bignum_add(x,y)));
151 void primitive_bignum_subtract(void)
154 dpush(tag_bignum(bignum_subtract(x,y)));
157 void primitive_bignum_multiply(void)
160 dpush(tag_bignum(bignum_multiply(x,y)));
163 void primitive_bignum_divint(void)
166 dpush(tag_bignum(bignum_quotient(x,y)));
169 void primitive_bignum_divmod(void)
173 bignum_divide(x,y,&q,&r);
174 dpush(tag_bignum(q));
175 dpush(tag_bignum(r));
178 void primitive_bignum_mod(void)
181 dpush(tag_bignum(bignum_remainder(x,y)));
184 void primitive_bignum_and(void)
187 dpush(tag_bignum(bignum_bitwise_and(x,y)));
190 void primitive_bignum_or(void)
193 dpush(tag_bignum(bignum_bitwise_ior(x,y)));
196 void primitive_bignum_xor(void)
199 dpush(tag_bignum(bignum_bitwise_xor(x,y)));
202 void primitive_bignum_shift(void)
204 F_FIXNUM y = untag_fixnum_fast(dpop());
205 F_BIGNUM* x = untag_bignum_fast(dpop());
206 dpush(tag_bignum(bignum_arithmetic_shift(x,y)));
209 void primitive_bignum_less(void)
212 box_boolean(bignum_compare(x,y) == bignum_comparison_less);
215 void primitive_bignum_lesseq(void)
218 box_boolean(bignum_compare(x,y) != bignum_comparison_greater);
221 void primitive_bignum_greater(void)
224 box_boolean(bignum_compare(x,y) == bignum_comparison_greater);
227 void primitive_bignum_greatereq(void)
230 box_boolean(bignum_compare(x,y) != bignum_comparison_less);
233 void primitive_bignum_not(void)
235 drepl(tag_bignum(bignum_bitwise_not(untag_bignum_fast(dpeek()))));
238 void primitive_bignum_bitp(void)
240 F_FIXNUM bit = to_fixnum(dpop());
241 F_BIGNUM *x = untag_bignum_fast(dpop());
242 box_boolean(bignum_logbitp(bit,x));
245 void primitive_bignum_log2(void)
247 drepl(tag_bignum(bignum_integer_length(untag_bignum_fast(dpeek()))));
250 unsigned int bignum_producer(unsigned int digit)
252 unsigned char *ptr = (unsigned char *)alien_offset(dpeek());
253 return *(ptr + digit);
256 void primitive_byte_array_to_bignum(void)
258 type_check(BYTE_ARRAY_TYPE,dpeek());
259 CELL n_digits = array_capacity(untag_byte_array_fast(dpeek())) / CELLS;
260 F_BIGNUM * bignum = digit_stream_to_bignum(
261 n_digits,bignum_producer,0x100,0);
262 drepl(tag_bignum(bignum));
265 void box_signed_1(s8 n)
267 dpush(tag_fixnum(n));
270 void box_unsigned_1(u8 n)
272 dpush(tag_fixnum(n));
275 void box_signed_2(s16 n)
277 dpush(tag_fixnum(n));
280 void box_unsigned_2(u16 n)
282 dpush(tag_fixnum(n));
285 void box_signed_4(s32 n)
287 dpush(allot_integer(n));
290 void box_unsigned_4(u32 n)
292 dpush(allot_cell(n));
295 void box_signed_cell(F_FIXNUM integer)
297 dpush(allot_integer(integer));
300 void box_unsigned_cell(CELL cell)
302 dpush(allot_cell(cell));
305 void box_signed_8(s64 n)
307 if(n < FIXNUM_MIN || n > FIXNUM_MAX)
308 dpush(tag_bignum(long_long_to_bignum(n)));
310 dpush(tag_fixnum(n));
313 s64 to_signed_8(CELL obj)
318 return untag_fixnum_fast(obj);
320 return bignum_to_long_long(untag_bignum_fast(obj));
322 type_error(BIGNUM_TYPE,obj);
327 void box_unsigned_8(u64 n)
330 dpush(tag_bignum(ulong_long_to_bignum(n)));
332 dpush(tag_fixnum(n));
335 u64 to_unsigned_8(CELL obj)
340 return untag_fixnum_fast(obj);
342 return bignum_to_ulong_long(untag_bignum_fast(obj));
344 type_error(BIGNUM_TYPE,obj);
349 CELL unbox_array_size(void)
351 switch(type_of(dpeek()))
355 F_FIXNUM n = untag_fixnum_fast(dpeek());
356 if(n >= 0 && n < (F_FIXNUM)ARRAY_SIZE_MAX)
365 F_BIGNUM * zero = untag_bignum_fast(bignum_zero);
366 F_BIGNUM * max = cell_to_bignum(ARRAY_SIZE_MAX);
367 F_BIGNUM * n = untag_bignum_fast(dpeek());
368 if(bignum_compare(n,zero) != bignum_comparison_less
369 && bignum_compare(n,max) == bignum_comparison_less)
372 return bignum_to_cell(n);
378 general_error(ERROR_ARRAY_SIZE,dpop(),tag_fixnum(ARRAY_SIZE_MAX),NULL);
379 return 0; /* can't happen */
383 void primitive_fixnum_to_float(void)
385 drepl(allot_float(fixnum_to_float(dpeek())));
388 void primitive_bignum_to_float(void)
390 drepl(allot_float(bignum_to_float(dpeek())));
393 void primitive_str_to_float(void)
397 F_STRING *str = untag_string(dpeek());
398 CELL capacity = string_capacity(str);
400 c_str = to_char_string(str,false);
402 f = strtod(c_str,&end);
403 if(end != c_str + capacity)
406 drepl(allot_float(f));
409 void primitive_float_to_str(void)
412 snprintf(tmp,32,"%.16g",untag_float(dpop()));
414 box_char_string(tmp);
417 #define POP_FLOATS(x,y) \
418 double y = untag_float_fast(dpop()); \
419 double x = untag_float_fast(dpop());
421 void primitive_float_eq(void)
427 void primitive_float_add(void)
433 void primitive_float_subtract(void)
439 void primitive_float_multiply(void)
445 void primitive_float_divfloat(void)
451 void primitive_float_mod(void)
454 box_double(fmod(x,y));
457 void primitive_float_less(void)
463 void primitive_float_lesseq(void)
469 void primitive_float_greater(void)
475 void primitive_float_greatereq(void)
481 void primitive_float_bits(void)
483 box_unsigned_4(float_bits(untag_float(dpop())));
486 void primitive_bits_float(void)
488 box_float(bits_float(to_cell(dpop())));
491 void primitive_double_bits(void)
493 box_unsigned_8(double_bits(untag_float(dpop())));
496 void primitive_bits_double(void)
498 box_double(bits_double(to_unsigned_8(dpop())));
501 float to_float(CELL value)
503 return untag_float(value);
506 double to_double(CELL value)
508 return untag_float(value);
511 void box_float(float flo)
513 dpush(allot_float(flo));
516 void box_double(double flo)
518 dpush(allot_float(flo));