5 void factor_vm::primitive_bignum_to_fixnum() {
6 ctx->replace(tag_fixnum(bignum_to_fixnum(untag<bignum>(ctx->peek()))));
9 void factor_vm::primitive_float_to_fixnum() {
10 ctx->replace(tag_fixnum(float_to_fixnum(ctx->peek())));
13 /* does not allocate, even though from_signed_cell can allocate */
14 /* Division can only overflow when we are dividing the most negative fixnum
16 void factor_vm::primitive_fixnum_divint() {
17 fixnum y = untag_fixnum(ctx->pop());
18 fixnum x = untag_fixnum(ctx->peek());
19 fixnum result = x / y;
20 if (result == -fixnum_min)
21 /* Does not allocate */
22 ctx->replace(from_signed_cell(-fixnum_min));
24 ctx->replace(tag_fixnum(result));
27 /* does not allocate, even though from_signed_cell can allocate */
28 void factor_vm::primitive_fixnum_divmod() {
29 cell* s0 = (cell*)(ctx->datastack);
30 cell* s1 = (cell*)(ctx->datastack - sizeof(cell));
31 fixnum y = untag_fixnum(*s0);
32 fixnum x = untag_fixnum(*s1);
33 if (y == -1 && x == fixnum_min) {
34 /* Does not allocate */
35 *s1 = from_signed_cell(-fixnum_min);
38 *s1 = tag_fixnum(x / y);
39 *s0 = tag_fixnum(x % y);
44 * If we're shifting right by n bits, we won't overflow as long as none of the
45 * high WORD_SIZE-TAG_BITS-n bits are set.
47 inline fixnum factor_vm::sign_mask(fixnum x) {
48 return x >> (WORD_SIZE - 1);
51 inline fixnum factor_vm::branchless_max(fixnum x, fixnum y) {
52 return (x - ((x - y) & sign_mask(x - y)));
55 inline fixnum factor_vm::branchless_abs(fixnum x) {
56 return (x ^ sign_mask(x)) - sign_mask(x);
59 void factor_vm::primitive_fixnum_shift() {
60 fixnum y = untag_fixnum(ctx->pop());
61 fixnum x = untag_fixnum(ctx->peek());
66 y = branchless_max(y, -WORD_SIZE + 1);
67 ctx->replace(tag_fixnum(x >> -y));
69 } else if (y < WORD_SIZE - TAG_BITS) {
70 fixnum mask = -((fixnum)1 << (WORD_SIZE - 1 - TAG_BITS - y));
71 if (!(branchless_abs(x) & mask)) {
72 ctx->replace(tag_fixnum(x << y));
77 ctx->replace(tag<bignum>(bignum_arithmetic_shift(fixnum_to_bignum(x), y)));
80 void factor_vm::primitive_fixnum_to_bignum() {
81 ctx->replace(tag<bignum>(fixnum_to_bignum(untag_fixnum(ctx->peek()))));
84 void factor_vm::primitive_float_to_bignum() {
85 ctx->replace(tag<bignum>(float_to_bignum(ctx->peek())));
88 #define POP_BIGNUMS(x, y) \
89 bignum* y = untag<bignum>(ctx->pop()); \
90 bignum* x = untag<bignum>(ctx->peek());
92 void factor_vm::primitive_bignum_eq() {
94 ctx->replace(tag_boolean(bignum_equal_p(x, y)));
97 void factor_vm::primitive_bignum_add() {
99 ctx->replace(tag<bignum>(bignum_add(x, y)));
102 void factor_vm::primitive_bignum_subtract() {
104 ctx->replace(tag<bignum>(bignum_subtract(x, y)));
107 void factor_vm::primitive_bignum_multiply() {
109 ctx->replace(tag<bignum>(bignum_multiply(x, y)));
112 void factor_vm::primitive_bignum_divint() {
114 ctx->replace(tag<bignum>(bignum_quotient(x, y)));
117 void factor_vm::primitive_bignum_divmod() {
118 cell* s0 = (cell*)(ctx->datastack);
119 cell* s1 = (cell*)(ctx->datastack - sizeof(cell));
120 bignum* y = untag<bignum>(*s0);
121 bignum* x = untag<bignum>(*s1);
123 bignum_divide(x, y, &q, &r);
124 *s1 = tag<bignum>(q);
125 *s0 = tag<bignum>(r);
128 void factor_vm::primitive_bignum_mod() {
130 ctx->replace(tag<bignum>(bignum_remainder(x, y)));
133 void factor_vm::primitive_bignum_gcd() {
135 ctx->replace(tag<bignum>(bignum_gcd(x, y)));
138 void factor_vm::primitive_bignum_and() {
140 ctx->replace(tag<bignum>(bignum_bitwise_and(x, y)));
143 void factor_vm::primitive_bignum_or() {
145 ctx->replace(tag<bignum>(bignum_bitwise_ior(x, y)));
148 void factor_vm::primitive_bignum_xor() {
150 ctx->replace(tag<bignum>(bignum_bitwise_xor(x, y)));
153 void factor_vm::primitive_bignum_shift() {
154 fixnum y = untag_fixnum(ctx->pop());
155 bignum* x = untag<bignum>(ctx->peek());
156 ctx->replace(tag<bignum>(bignum_arithmetic_shift(x, y)));
159 void factor_vm::primitive_bignum_less() {
161 ctx->replace(tag_boolean(bignum_compare(x, y) == bignum_comparison_less));
164 void factor_vm::primitive_bignum_lesseq() {
166 ctx->replace(tag_boolean(bignum_compare(x, y) != bignum_comparison_greater));
169 void factor_vm::primitive_bignum_greater() {
171 ctx->replace(tag_boolean(bignum_compare(x, y) == bignum_comparison_greater));
174 void factor_vm::primitive_bignum_greatereq() {
176 ctx->replace(tag_boolean(bignum_compare(x, y) != bignum_comparison_less));
179 void factor_vm::primitive_bignum_not() {
180 ctx->replace(tag<bignum>(bignum_bitwise_not(untag<bignum>(ctx->peek()))));
183 void factor_vm::primitive_bignum_bitp() {
184 int bit = (int)to_fixnum(ctx->pop());
185 bignum* x = untag<bignum>(ctx->peek());
186 ctx->replace(tag_boolean(bignum_logbitp(bit, x)));
189 void factor_vm::primitive_bignum_log2() {
190 ctx->replace(tag<bignum>(bignum_integer_length(untag<bignum>(ctx->peek()))));
193 /* Allocates memory */
194 void factor_vm::primitive_fixnum_to_float() {
195 ctx->replace(allot_float(fixnum_to_float(ctx->peek())));
198 /* Allocates memory */
199 void factor_vm::primitive_format_float() {
200 byte_array* array = allot_byte_array(100);
201 char* format = alien_offset(ctx->pop());
202 double value = untag_float_check(ctx->peek());
203 SNPRINTF(array->data<char>(), 99, format, value);
204 ctx->replace(tag<byte_array>(array));
207 #define POP_FLOATS(x, y) \
208 double y = untag_float(ctx->pop()); \
209 double x = untag_float(ctx->peek());
211 void factor_vm::primitive_float_eq() {
213 ctx->replace(tag_boolean(x == y));
216 /* Allocates memory */
217 void factor_vm::primitive_float_add() {
219 ctx->replace(allot_float(x + y));
222 /* Allocates memory */
223 void factor_vm::primitive_float_subtract() {
225 ctx->replace(allot_float(x - y));
228 /* Allocates memory */
229 void factor_vm::primitive_float_multiply() {
231 ctx->replace(allot_float(x * y));
234 /* Allocates memory */
235 void factor_vm::primitive_float_divfloat() {
237 ctx->replace(allot_float(x / y));
240 void factor_vm::primitive_float_less() {
242 ctx->replace(tag_boolean(x < y));
245 void factor_vm::primitive_float_lesseq() {
247 ctx->replace(tag_boolean(x <= y));
250 void factor_vm::primitive_float_greater() {
252 ctx->replace(tag_boolean(x > y));
255 void factor_vm::primitive_float_greatereq() {
257 ctx->replace(tag_boolean(x >= y));
260 /* Allocates memory */
261 void factor_vm::primitive_float_bits() {
263 from_unsigned_cell(float_bits((float)untag_float_check(ctx->pop()))));
266 /* Allocates memory */
267 void factor_vm::primitive_bits_float() {
268 ctx->push(allot_float(bits_float((uint32_t)to_cell(ctx->pop()))));
271 void factor_vm::primitive_double_bits() {
272 ctx->push(from_unsigned_8(double_bits(untag_float_check(ctx->pop()))));
275 /* Allocates memory */
276 void factor_vm::primitive_bits_double() {
277 ctx->push(allot_float(bits_double(to_unsigned_8(ctx->pop()))));
280 /* Cannot allocate. */
281 #define CELL_TO_FOO(name, type, converter) \
282 type factor_vm::name(cell tagged) { \
283 switch (TAG(tagged)) { \
285 return (type)untag_fixnum(tagged); \
287 return converter(untag<bignum>(tagged)); \
289 type_error(FIXNUM_TYPE, tagged); \
290 return 0; /* can't happen */ \
293 VM_C_API type name(cell tagged, factor_vm* parent) { \
294 return parent->name(tagged); \
297 /* Note that to_fixnum, unlike the others, is strict. */
298 CELL_TO_FOO(to_fixnum, fixnum, bignum_to_fixnum_strict)
299 CELL_TO_FOO(to_cell, cell, bignum_to_cell)
300 CELL_TO_FOO(to_signed_8, int64_t, bignum_to_long_long)
301 CELL_TO_FOO(to_unsigned_8, uint64_t, bignum_to_ulong_long)
303 /* Allocates memory */
304 VM_C_API cell from_signed_cell(fixnum integer, factor_vm* parent) {
305 return parent->from_signed_cell(integer);
308 /* Allocates memory */
309 VM_C_API cell from_unsigned_cell(cell integer, factor_vm* parent) {
310 return parent->from_unsigned_cell(integer);
313 /* Allocates memory */
314 cell factor_vm::from_signed_8(int64_t n) {
315 if (n < fixnum_min || n > fixnum_max)
316 return tag<bignum>(long_long_to_bignum(n));
318 return tag_fixnum((fixnum)n);
321 VM_C_API cell from_signed_8(int64_t n, factor_vm* parent) {
322 return parent->from_signed_8(n);
325 /* Allocates memory */
326 cell factor_vm::from_unsigned_8(uint64_t n) {
327 if (n > (uint64_t)fixnum_max)
328 return tag<bignum>(ulong_long_to_bignum(n));
330 return tag_fixnum((fixnum)n);
333 VM_C_API cell from_unsigned_8(uint64_t n, factor_vm* parent) {
334 return parent->from_unsigned_8(n);
337 /* Cannot allocate */
338 float factor_vm::to_float(cell value) {
339 return (float)untag_float_check(value);
342 /* Cannot allocate */
343 double factor_vm::to_double(cell value) { return untag_float_check(value); }
345 /* The fixnum+, fixnum- and fixnum* primitives are defined in cpu_*.S. On
346 overflow, they call these functions. */
347 /* Allocates memory */
348 inline void factor_vm::overflow_fixnum_add(fixnum x, fixnum y) {
350 tag<bignum>(fixnum_to_bignum(untag_fixnum(x) + untag_fixnum(y))));
353 VM_C_API void overflow_fixnum_add(fixnum x, fixnum y, factor_vm* parent) {
354 parent->overflow_fixnum_add(x, y);
357 /* Allocates memory */
358 inline void factor_vm::overflow_fixnum_subtract(fixnum x, fixnum y) {
360 tag<bignum>(fixnum_to_bignum(untag_fixnum(x) - untag_fixnum(y))));
363 VM_C_API void overflow_fixnum_subtract(fixnum x, fixnum y, factor_vm* parent) {
364 parent->overflow_fixnum_subtract(x, y);
367 /* Allocates memory */
368 inline void factor_vm::overflow_fixnum_multiply(fixnum x, fixnum y) {
369 bignum* bx = fixnum_to_bignum(x);
371 bignum* by = fixnum_to_bignum(y);
373 ctx->replace(tag<bignum>(bignum_multiply(bx, by)));
376 VM_C_API void overflow_fixnum_multiply(fixnum x, fixnum y, factor_vm* parent) {
377 parent->overflow_fixnum_multiply(x, y);