]> gitweb.factorcode.org Git - factor.git/blob - vm/math.cpp
VM: CELL_TO_FOO macro-magic in the same spirit as BIGNUM_TO_FOO
[factor.git] / vm / math.cpp
1 #include "master.hpp"
2
3 namespace factor {
4
5 void factor_vm::primitive_bignum_to_fixnum() {
6   ctx->replace(tag_fixnum(bignum_to_fixnum(untag<bignum>(ctx->peek()))));
7 }
8
9 void factor_vm::primitive_float_to_fixnum() {
10   ctx->replace(tag_fixnum(float_to_fixnum(ctx->peek())));
11 }
12
13 /* does not allocate, even though from_signed_cell can allocate */
14 /* Division can only overflow when we are dividing the most negative fixnum
15 by -1. */
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));
23   else
24     ctx->replace(tag_fixnum(result));
25 }
26
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);
36     *s0 = tag_fixnum(0);
37   } else {
38     *s1 = tag_fixnum(x / y);
39     *s0 = tag_fixnum(x % y);
40   }
41 }
42
43 /*
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.
46  */
47 inline fixnum factor_vm::sign_mask(fixnum x) {
48     return x >> (WORD_SIZE - 1);
49 }
50
51 inline fixnum factor_vm::branchless_max(fixnum x, fixnum y) {
52   return (x - ((x - y) & sign_mask(x - y)));
53 }
54
55 inline fixnum factor_vm::branchless_abs(fixnum x) {
56   return (x ^ sign_mask(x)) - sign_mask(x);
57 }
58
59 void factor_vm::primitive_fixnum_shift() {
60   fixnum y = untag_fixnum(ctx->pop());
61   fixnum x = untag_fixnum(ctx->peek());
62
63   if (x == 0)
64     return;
65   else if (y < 0) {
66     y = branchless_max(y, -WORD_SIZE + 1);
67     ctx->replace(tag_fixnum(x >> -y));
68     return;
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));
73       return;
74     }
75   }
76
77   ctx->replace(tag<bignum>(bignum_arithmetic_shift(fixnum_to_bignum(x), y)));
78 }
79
80 void factor_vm::primitive_fixnum_to_bignum() {
81   ctx->replace(tag<bignum>(fixnum_to_bignum(untag_fixnum(ctx->peek()))));
82 }
83
84 void factor_vm::primitive_float_to_bignum() {
85   ctx->replace(tag<bignum>(float_to_bignum(ctx->peek())));
86 }
87
88 #define POP_BIGNUMS(x, y)                \
89   bignum* y = untag<bignum>(ctx->pop()); \
90   bignum* x = untag<bignum>(ctx->peek());
91
92 void factor_vm::primitive_bignum_eq() {
93   POP_BIGNUMS(x, y);
94   ctx->replace(tag_boolean(bignum_equal_p(x, y)));
95 }
96
97 void factor_vm::primitive_bignum_add() {
98   POP_BIGNUMS(x, y);
99   ctx->replace(tag<bignum>(bignum_add(x, y)));
100 }
101
102 void factor_vm::primitive_bignum_subtract() {
103   POP_BIGNUMS(x, y);
104   ctx->replace(tag<bignum>(bignum_subtract(x, y)));
105 }
106
107 void factor_vm::primitive_bignum_multiply() {
108   POP_BIGNUMS(x, y);
109   ctx->replace(tag<bignum>(bignum_multiply(x, y)));
110 }
111
112 void factor_vm::primitive_bignum_divint() {
113   POP_BIGNUMS(x, y);
114   ctx->replace(tag<bignum>(bignum_quotient(x, y)));
115 }
116
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);
122   bignum* q, *r;
123   bignum_divide(x, y, &q, &r);
124   *s1 = tag<bignum>(q);
125   *s0 = tag<bignum>(r);
126 }
127
128 void factor_vm::primitive_bignum_mod() {
129   POP_BIGNUMS(x, y);
130   ctx->replace(tag<bignum>(bignum_remainder(x, y)));
131 }
132
133 void factor_vm::primitive_bignum_gcd() {
134   POP_BIGNUMS(x, y);
135   ctx->replace(tag<bignum>(bignum_gcd(x, y)));
136 }
137
138 void factor_vm::primitive_bignum_and() {
139   POP_BIGNUMS(x, y);
140   ctx->replace(tag<bignum>(bignum_bitwise_and(x, y)));
141 }
142
143 void factor_vm::primitive_bignum_or() {
144   POP_BIGNUMS(x, y);
145   ctx->replace(tag<bignum>(bignum_bitwise_ior(x, y)));
146 }
147
148 void factor_vm::primitive_bignum_xor() {
149   POP_BIGNUMS(x, y);
150   ctx->replace(tag<bignum>(bignum_bitwise_xor(x, y)));
151 }
152
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)));
157 }
158
159 void factor_vm::primitive_bignum_less() {
160   POP_BIGNUMS(x, y);
161   ctx->replace(tag_boolean(bignum_compare(x, y) == bignum_comparison_less));
162 }
163
164 void factor_vm::primitive_bignum_lesseq() {
165   POP_BIGNUMS(x, y);
166   ctx->replace(tag_boolean(bignum_compare(x, y) != bignum_comparison_greater));
167 }
168
169 void factor_vm::primitive_bignum_greater() {
170   POP_BIGNUMS(x, y);
171   ctx->replace(tag_boolean(bignum_compare(x, y) == bignum_comparison_greater));
172 }
173
174 void factor_vm::primitive_bignum_greatereq() {
175   POP_BIGNUMS(x, y);
176   ctx->replace(tag_boolean(bignum_compare(x, y) != bignum_comparison_less));
177 }
178
179 void factor_vm::primitive_bignum_not() {
180   ctx->replace(tag<bignum>(bignum_bitwise_not(untag<bignum>(ctx->peek()))));
181 }
182
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)));
187 }
188
189 void factor_vm::primitive_bignum_log2() {
190   ctx->replace(tag<bignum>(bignum_integer_length(untag<bignum>(ctx->peek()))));
191 }
192
193 /* Allocates memory */
194 void factor_vm::primitive_fixnum_to_float() {
195   ctx->replace(allot_float(fixnum_to_float(ctx->peek())));
196 }
197
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));
205 }
206
207 #define POP_FLOATS(x, y)              \
208   double y = untag_float(ctx->pop()); \
209   double x = untag_float(ctx->peek());
210
211 void factor_vm::primitive_float_eq() {
212   POP_FLOATS(x, y);
213   ctx->replace(tag_boolean(x == y));
214 }
215
216 /* Allocates memory */
217 void factor_vm::primitive_float_add() {
218   POP_FLOATS(x, y);
219   ctx->replace(allot_float(x + y));
220 }
221
222 /* Allocates memory */
223 void factor_vm::primitive_float_subtract() {
224   POP_FLOATS(x, y);
225   ctx->replace(allot_float(x - y));
226 }
227
228 /* Allocates memory */
229 void factor_vm::primitive_float_multiply() {
230   POP_FLOATS(x, y);
231   ctx->replace(allot_float(x * y));
232 }
233
234 /* Allocates memory */
235 void factor_vm::primitive_float_divfloat() {
236   POP_FLOATS(x, y);
237   ctx->replace(allot_float(x / y));
238 }
239
240 void factor_vm::primitive_float_less() {
241   POP_FLOATS(x, y);
242   ctx->replace(tag_boolean(x < y));
243 }
244
245 void factor_vm::primitive_float_lesseq() {
246   POP_FLOATS(x, y);
247   ctx->replace(tag_boolean(x <= y));
248 }
249
250 void factor_vm::primitive_float_greater() {
251   POP_FLOATS(x, y);
252   ctx->replace(tag_boolean(x > y));
253 }
254
255 void factor_vm::primitive_float_greatereq() {
256   POP_FLOATS(x, y);
257   ctx->replace(tag_boolean(x >= y));
258 }
259
260 /* Allocates memory */
261 void factor_vm::primitive_float_bits() {
262   ctx->push(
263       from_unsigned_cell(float_bits((float)untag_float_check(ctx->pop()))));
264 }
265
266 /* Allocates memory */
267 void factor_vm::primitive_bits_float() {
268   ctx->push(allot_float(bits_float((uint32_t)to_cell(ctx->pop()))));
269 }
270
271 void factor_vm::primitive_double_bits() {
272   ctx->push(from_unsigned_8(double_bits(untag_float_check(ctx->pop()))));
273 }
274
275 /* Allocates memory */
276 void factor_vm::primitive_bits_double() {
277   ctx->push(allot_float(bits_double(to_unsigned_8(ctx->pop()))));
278 }
279
280 /* Cannot allocate. */
281 #define CELL_TO_FOO(name, type, converter)              \
282   type factor_vm::name(cell tagged) {                   \
283     switch (TAG(tagged)) {                              \
284       case FIXNUM_TYPE:                                 \
285         return (type)untag_fixnum(tagged);              \
286       case BIGNUM_TYPE:                                 \
287         return converter(untag<bignum>(tagged));        \
288       default:                                          \
289         type_error(FIXNUM_TYPE, tagged);                \
290         return 0; /* can't happen */                    \
291     }                                                   \
292   }                                                     \
293   VM_C_API type name(cell tagged, factor_vm* parent) {  \
294     return parent->name(tagged);                        \
295   }
296
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)
302
303 /* Allocates memory */
304 VM_C_API cell from_signed_cell(fixnum integer, factor_vm* parent) {
305   return parent->from_signed_cell(integer);
306 }
307
308 /* Allocates memory */
309 VM_C_API cell from_unsigned_cell(cell integer, factor_vm* parent) {
310   return parent->from_unsigned_cell(integer);
311 }
312
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));
317   else
318     return tag_fixnum((fixnum)n);
319 }
320
321 VM_C_API cell from_signed_8(int64_t n, factor_vm* parent) {
322   return parent->from_signed_8(n);
323 }
324
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));
329   else
330     return tag_fixnum((fixnum)n);
331 }
332
333 VM_C_API cell from_unsigned_8(uint64_t n, factor_vm* parent) {
334   return parent->from_unsigned_8(n);
335 }
336
337 /* Cannot allocate */
338 float factor_vm::to_float(cell value) {
339   return (float)untag_float_check(value);
340 }
341
342 /* Cannot allocate */
343 double factor_vm::to_double(cell value) { return untag_float_check(value); }
344
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) {
349   ctx->replace(
350       tag<bignum>(fixnum_to_bignum(untag_fixnum(x) + untag_fixnum(y))));
351 }
352
353 VM_C_API void overflow_fixnum_add(fixnum x, fixnum y, factor_vm* parent) {
354   parent->overflow_fixnum_add(x, y);
355 }
356
357 /* Allocates memory */
358 inline void factor_vm::overflow_fixnum_subtract(fixnum x, fixnum y) {
359   ctx->replace(
360       tag<bignum>(fixnum_to_bignum(untag_fixnum(x) - untag_fixnum(y))));
361 }
362
363 VM_C_API void overflow_fixnum_subtract(fixnum x, fixnum y, factor_vm* parent) {
364   parent->overflow_fixnum_subtract(x, y);
365 }
366
367 /* Allocates memory */
368 inline void factor_vm::overflow_fixnum_multiply(fixnum x, fixnum y) {
369   bignum* bx = fixnum_to_bignum(x);
370   GC_BIGNUM(bx);
371   bignum* by = fixnum_to_bignum(y);
372   GC_BIGNUM(by);
373   ctx->replace(tag<bignum>(bignum_multiply(bx, by)));
374 }
375
376 VM_C_API void overflow_fixnum_multiply(fixnum x, fixnum y, factor_vm* parent) {
377   parent->overflow_fixnum_multiply(x, y);
378 }
379
380 }