7 void factor_vm::primitive_bignum_to_fixnum() {
8 ctx->replace(tag_fixnum(bignum_to_fixnum(untag<bignum>(ctx->peek()))));
11 void factor_vm::primitive_bignum_to_fixnum_strict() {
12 ctx->replace(tag_fixnum(bignum_to_fixnum_strict(untag<bignum>(ctx->peek()))));
15 void factor_vm::primitive_float_to_fixnum() {
16 ctx->replace(tag_fixnum(float_to_fixnum(ctx->peek())));
19 /* does not allocate, even though from_signed_cell can allocate */
20 /* Division can only overflow when we are dividing the most negative fixnum
22 void factor_vm::primitive_fixnum_divint() {
23 fixnum y = untag_fixnum(ctx->pop());
24 fixnum x = untag_fixnum(ctx->peek());
25 fixnum result = x / y;
26 if (result == -fixnum_min)
27 /* Does not allocate */
28 ctx->replace(from_signed_cell(-fixnum_min));
30 ctx->replace(tag_fixnum(result));
33 /* does not allocate, even though from_signed_cell can allocate */
34 void factor_vm::primitive_fixnum_divmod() {
35 cell* s0 = (cell*)(ctx->datastack);
36 cell* s1 = (cell*)(ctx->datastack - sizeof(cell));
37 fixnum y = untag_fixnum(*s0);
38 fixnum x = untag_fixnum(*s1);
39 if (y == -1 && x == fixnum_min) {
40 /* Does not allocate */
41 *s1 = from_signed_cell(-fixnum_min);
44 *s1 = tag_fixnum(x / y);
45 *s0 = tag_fixnum(x % 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 inline fixnum factor_vm::sign_mask(fixnum x) {
54 return x >> (WORD_SIZE - 1);
57 inline fixnum factor_vm::branchless_max(fixnum x, fixnum y) {
58 return (x - ((x - y) & sign_mask(x - y)));
61 inline fixnum factor_vm::branchless_abs(fixnum x) {
62 return (x ^ sign_mask(x)) - sign_mask(x);
65 /* Allocates memory */
66 void factor_vm::primitive_fixnum_shift() {
67 fixnum y = untag_fixnum(ctx->pop());
68 fixnum x = untag_fixnum(ctx->peek());
73 y = branchless_max(y, -WORD_SIZE + 1);
74 ctx->replace(tag_fixnum(x >> -y));
76 } else if (y < WORD_SIZE - TAG_BITS) {
77 fixnum mask = -((fixnum)1 << (WORD_SIZE - 1 - TAG_BITS - y));
78 if (!(branchless_abs(x) & mask)) {
79 ctx->replace(tag_fixnum(x << y));
84 ctx->replace(tag<bignum>(bignum_arithmetic_shift(fixnum_to_bignum(x), y)));
87 /* Allocates memory */
88 void factor_vm::primitive_fixnum_to_bignum() {
89 ctx->replace(tag<bignum>(fixnum_to_bignum(untag_fixnum(ctx->peek()))));
92 /* Allocates memory */
93 void factor_vm::primitive_float_to_bignum() {
94 ctx->replace(tag<bignum>(float_to_bignum(ctx->peek())));
97 #define POP_BIGNUMS(x, y) \
98 bignum* y = untag<bignum>(ctx->pop()); \
99 bignum* x = untag<bignum>(ctx->peek());
101 void factor_vm::primitive_bignum_eq() {
103 ctx->replace(tag_boolean(bignum_equal_p(x, y)));
106 /* Allocates memory */
107 void factor_vm::primitive_bignum_add() {
109 ctx->replace(tag<bignum>(bignum_add(x, y)));
112 /* Allocates memory */
113 void factor_vm::primitive_bignum_subtract() {
115 ctx->replace(tag<bignum>(bignum_subtract(x, y)));
118 /* Allocates memory */
119 void factor_vm::primitive_bignum_multiply() {
121 ctx->replace(tag<bignum>(bignum_multiply(x, y)));
124 /* Allocates memory */
125 void factor_vm::primitive_bignum_divint() {
127 ctx->replace(tag<bignum>(bignum_quotient(x, y)));
130 /* Allocates memory */
131 void factor_vm::primitive_bignum_divmod() {
132 cell* s0 = (cell*)(ctx->datastack);
133 cell* s1 = (cell*)(ctx->datastack - sizeof(cell));
134 bignum* y = untag<bignum>(*s0);
135 bignum* x = untag<bignum>(*s1);
137 bignum_divide(x, y, &q, &r);
138 *s1 = tag<bignum>(q);
139 *s0 = bignum_maybe_to_fixnum(r);
142 void factor_vm::primitive_bignum_mod() {
144 cell val = bignum_maybe_to_fixnum(bignum_remainder(x, y));
148 void factor_vm::primitive_bignum_gcd() {
150 ctx->replace(tag<bignum>(bignum_gcd(x, y)));
153 void factor_vm::primitive_bignum_and() {
155 ctx->replace(tag<bignum>(bignum_bitwise_and(x, y)));
158 void factor_vm::primitive_bignum_or() {
160 ctx->replace(tag<bignum>(bignum_bitwise_ior(x, y)));
163 void factor_vm::primitive_bignum_xor() {
165 ctx->replace(tag<bignum>(bignum_bitwise_xor(x, y)));
168 /* Allocates memory */
169 void factor_vm::primitive_bignum_shift() {
170 fixnum y = untag_fixnum(ctx->pop());
171 bignum* x = untag<bignum>(ctx->peek());
172 ctx->replace(tag<bignum>(bignum_arithmetic_shift(x, y)));
175 void factor_vm::primitive_bignum_less() {
177 ctx->replace(tag_boolean(bignum_compare(x, y) == bignum_comparison_less));
180 void factor_vm::primitive_bignum_lesseq() {
182 ctx->replace(tag_boolean(bignum_compare(x, y) != bignum_comparison_greater));
185 void factor_vm::primitive_bignum_greater() {
187 ctx->replace(tag_boolean(bignum_compare(x, y) == bignum_comparison_greater));
190 void factor_vm::primitive_bignum_greatereq() {
192 ctx->replace(tag_boolean(bignum_compare(x, y) != bignum_comparison_less));
195 void factor_vm::primitive_bignum_not() {
196 ctx->replace(tag<bignum>(bignum_bitwise_not(untag<bignum>(ctx->peek()))));
199 void factor_vm::primitive_bignum_bitp() {
200 int bit = (int)to_fixnum(ctx->pop());
201 bignum* x = untag<bignum>(ctx->peek());
202 ctx->replace(tag_boolean(bignum_logbitp(bit, x)));
205 void factor_vm::primitive_bignum_log2() {
206 ctx->replace(tag<bignum>(bignum_integer_length(untag<bignum>(ctx->peek()))));
209 /* Allocates memory */
210 void factor_vm::primitive_fixnum_to_float() {
211 ctx->replace(allot_float(fixnum_to_float(ctx->peek())));
214 /* Allocates memory */
215 void factor_vm::primitive_format_float() {
216 char* locale = alien_offset(ctx->pop());
217 char* format = alien_offset(ctx->pop());
218 fixnum precision = untag_fixnum(ctx->pop());
219 fixnum width = untag_fixnum(ctx->pop());
220 char* fill = alien_offset(ctx->pop());
221 double value = untag_float_check(ctx->peek());
222 std::ostringstream localized_stream;
223 localized_stream.imbue(std::locale(locale));
225 case 'f': localized_stream << std::fixed; break;
226 case 'e': localized_stream << std::scientific; break;
228 if (isupper(format[0])) {
229 localized_stream << std::uppercase;
231 if (fill[0] != '\0') {
232 localized_stream << std::setfill(fill[0]);
235 localized_stream << std::setw(width);
237 if (precision >= 0) {
238 localized_stream << std::setprecision(precision);
240 localized_stream << value;
241 const std::string& tmp = localized_stream.str();
242 const char* cstr = tmp.c_str();
243 int size = tmp.length()+1;
244 byte_array* array = allot_byte_array(size);
245 memcpy(array->data<char>(), cstr, size);
246 ctx->replace(tag<byte_array>(array));
249 #define POP_FLOATS(x, y) \
250 double y = untag_float(ctx->pop()); \
251 double x = untag_float(ctx->peek());
253 void factor_vm::primitive_float_eq() {
255 ctx->replace(tag_boolean(x == y));
258 /* Allocates memory */
259 void factor_vm::primitive_float_add() {
261 ctx->replace(allot_float(x + y));
264 /* Allocates memory */
265 void factor_vm::primitive_float_subtract() {
267 ctx->replace(allot_float(x - y));
270 /* Allocates memory */
271 void factor_vm::primitive_float_multiply() {
273 ctx->replace(allot_float(x * y));
276 /* Allocates memory */
277 void factor_vm::primitive_float_divfloat() {
279 ctx->replace(allot_float(x / y));
282 void factor_vm::primitive_float_less() {
284 ctx->replace(tag_boolean(x < y));
287 void factor_vm::primitive_float_lesseq() {
289 ctx->replace(tag_boolean(x <= y));
292 void factor_vm::primitive_float_greater() {
294 ctx->replace(tag_boolean(x > y));
297 void factor_vm::primitive_float_greatereq() {
299 ctx->replace(tag_boolean(x >= y));
302 /* Allocates memory */
303 void factor_vm::primitive_float_bits() {
305 from_unsigned_cell(float_bits((float)untag_float_check(ctx->peek()))));
308 /* Allocates memory */
309 void factor_vm::primitive_bits_float() {
310 ctx->replace(allot_float(bits_float((uint32_t)to_cell(ctx->peek()))));
313 void factor_vm::primitive_double_bits() {
314 ctx->replace(from_unsigned_8(double_bits(untag_float_check(ctx->peek()))));
317 /* Allocates memory */
318 void factor_vm::primitive_bits_double() {
319 ctx->replace(allot_float(bits_double(to_unsigned_8(ctx->peek()))));
322 /* Cannot allocate. */
323 #define CELL_TO_FOO(name, type, converter) \
324 type factor_vm::name(cell tagged) { \
325 switch (TAG(tagged)) { \
327 return (type)untag_fixnum(tagged); \
329 return converter(untag<bignum>(tagged)); \
331 type_error(FIXNUM_TYPE, tagged); \
332 return 0; /* can't happen */ \
335 VM_C_API type name(cell tagged, factor_vm* parent) { \
336 return parent->name(tagged); \
339 CELL_TO_FOO(to_fixnum, fixnum, bignum_to_fixnum)
340 CELL_TO_FOO(to_fixnum_strict, fixnum, bignum_to_fixnum_strict)
341 CELL_TO_FOO(to_cell, cell, bignum_to_cell)
342 CELL_TO_FOO(to_signed_8, int64_t, bignum_to_long_long)
343 CELL_TO_FOO(to_unsigned_8, uint64_t, bignum_to_ulong_long)
345 /* Allocates memory */
346 VM_C_API cell from_signed_cell(fixnum integer, factor_vm* parent) {
347 return parent->from_signed_cell(integer);
350 /* Allocates memory */
351 VM_C_API cell from_unsigned_cell(cell integer, factor_vm* parent) {
352 return parent->from_unsigned_cell(integer);
355 /* Allocates memory */
356 cell factor_vm::from_signed_8(int64_t n) {
357 if (n < fixnum_min || n > fixnum_max)
358 return tag<bignum>(long_long_to_bignum(n));
360 return tag_fixnum((fixnum)n);
363 VM_C_API cell from_signed_8(int64_t n, factor_vm* parent) {
364 return parent->from_signed_8(n);
367 /* Allocates memory */
368 cell factor_vm::from_unsigned_8(uint64_t n) {
369 if (n > (uint64_t)fixnum_max)
370 return tag<bignum>(ulong_long_to_bignum(n));
372 return tag_fixnum((fixnum)n);
375 VM_C_API cell from_unsigned_8(uint64_t n, factor_vm* parent) {
376 return parent->from_unsigned_8(n);
379 /* Cannot allocate */
380 float factor_vm::to_float(cell value) {
381 return (float)untag_float_check(value);
384 /* Cannot allocate */
385 double factor_vm::to_double(cell value) { return untag_float_check(value); }
387 /* The fixnum+, fixnum- and fixnum* primitives are defined in cpu_*.S. On
388 overflow, they call these functions. */
389 /* Allocates memory */
390 inline void factor_vm::overflow_fixnum_add(fixnum x, fixnum y) {
392 tag<bignum>(fixnum_to_bignum(untag_fixnum(x) + untag_fixnum(y))));
395 VM_C_API void overflow_fixnum_add(fixnum x, fixnum y, factor_vm* parent) {
396 parent->overflow_fixnum_add(x, y);
399 /* Allocates memory */
400 inline void factor_vm::overflow_fixnum_subtract(fixnum x, fixnum y) {
402 tag<bignum>(fixnum_to_bignum(untag_fixnum(x) - untag_fixnum(y))));
405 VM_C_API void overflow_fixnum_subtract(fixnum x, fixnum y, factor_vm* parent) {
406 parent->overflow_fixnum_subtract(x, y);
409 /* Allocates memory */
410 inline void factor_vm::overflow_fixnum_multiply(fixnum x, fixnum y) {
411 data_root<bignum> bx(fixnum_to_bignum(x), this);
412 data_root<bignum> by(fixnum_to_bignum(y), this);
413 cell ret = tag<bignum>(bignum_multiply(bx.untagged(), by.untagged()));
417 VM_C_API void overflow_fixnum_multiply(fixnum x, fixnum y, factor_vm* parent) {
418 parent->overflow_fixnum_multiply(x, y);