]> gitweb.factorcode.org Git - factor.git/blob - vm/math.cpp
VM: cell -> ret
[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_bignum_to_fixnum_strict() {
10   ctx->replace(tag_fixnum(bignum_to_fixnum_strict(untag<bignum>(ctx->peek()))));
11 }
12
13 void factor_vm::primitive_float_to_fixnum() {
14   ctx->replace(tag_fixnum(float_to_fixnum(ctx->peek())));
15 }
16
17 /* does not allocate, even though from_signed_cell can allocate */
18 /* Division can only overflow when we are dividing the most negative fixnum
19 by -1. */
20 void factor_vm::primitive_fixnum_divint() {
21   fixnum y = untag_fixnum(ctx->pop());
22   fixnum x = untag_fixnum(ctx->peek());
23   fixnum result = x / y;
24   if (result == -fixnum_min)
25     /* Does not allocate */
26     ctx->replace(from_signed_cell(-fixnum_min));
27   else
28     ctx->replace(tag_fixnum(result));
29 }
30
31 /* does not allocate, even though from_signed_cell can allocate */
32 void factor_vm::primitive_fixnum_divmod() {
33   cell* s0 = (cell*)(ctx->datastack);
34   cell* s1 = (cell*)(ctx->datastack - sizeof(cell));
35   fixnum y = untag_fixnum(*s0);
36   fixnum x = untag_fixnum(*s1);
37   if (y == -1 && x == fixnum_min) {
38     /* Does not allocate */
39     *s1 = from_signed_cell(-fixnum_min);
40     *s0 = tag_fixnum(0);
41   } else {
42     *s1 = tag_fixnum(x / y);
43     *s0 = tag_fixnum(x % y);
44   }
45 }
46
47 /*
48  * If we're shifting right by n bits, we won't overflow as long as none of the
49  * high WORD_SIZE-TAG_BITS-n bits are set.
50  */
51 inline fixnum factor_vm::sign_mask(fixnum x) {
52     return x >> (WORD_SIZE - 1);
53 }
54
55 inline fixnum factor_vm::branchless_max(fixnum x, fixnum y) {
56   return (x - ((x - y) & sign_mask(x - y)));
57 }
58
59 inline fixnum factor_vm::branchless_abs(fixnum x) {
60   return (x ^ sign_mask(x)) - sign_mask(x);
61 }
62
63 void factor_vm::primitive_fixnum_shift() {
64   fixnum y = untag_fixnum(ctx->pop());
65   fixnum x = untag_fixnum(ctx->peek());
66
67   if (x == 0)
68     return;
69   else if (y < 0) {
70     y = branchless_max(y, -WORD_SIZE + 1);
71     ctx->replace(tag_fixnum(x >> -y));
72     return;
73   } else if (y < WORD_SIZE - TAG_BITS) {
74     fixnum mask = -((fixnum)1 << (WORD_SIZE - 1 - TAG_BITS - y));
75     if (!(branchless_abs(x) & mask)) {
76       ctx->replace(tag_fixnum(x << y));
77       return;
78     }
79   }
80
81   ctx->replace(tag<bignum>(bignum_arithmetic_shift(fixnum_to_bignum(x), y)));
82 }
83
84 void factor_vm::primitive_fixnum_to_bignum() {
85   ctx->replace(tag<bignum>(fixnum_to_bignum(untag_fixnum(ctx->peek()))));
86 }
87
88 void factor_vm::primitive_float_to_bignum() {
89   ctx->replace(tag<bignum>(float_to_bignum(ctx->peek())));
90 }
91
92 #define POP_BIGNUMS(x, y)                \
93   bignum* y = untag<bignum>(ctx->pop()); \
94   bignum* x = untag<bignum>(ctx->peek());
95
96 void factor_vm::primitive_bignum_eq() {
97   POP_BIGNUMS(x, y);
98   ctx->replace(tag_boolean(bignum_equal_p(x, y)));
99 }
100
101 void factor_vm::primitive_bignum_add() {
102   POP_BIGNUMS(x, y);
103   ctx->replace(tag<bignum>(bignum_add(x, y)));
104 }
105
106 void factor_vm::primitive_bignum_subtract() {
107   POP_BIGNUMS(x, y);
108   ctx->replace(tag<bignum>(bignum_subtract(x, y)));
109 }
110
111 void factor_vm::primitive_bignum_multiply() {
112   POP_BIGNUMS(x, y);
113   ctx->replace(tag<bignum>(bignum_multiply(x, y)));
114 }
115
116 void factor_vm::primitive_bignum_divint() {
117   POP_BIGNUMS(x, y);
118   ctx->replace(tag<bignum>(bignum_quotient(x, y)));
119 }
120
121 void factor_vm::primitive_bignum_divmod() {
122   cell* s0 = (cell*)(ctx->datastack);
123   cell* s1 = (cell*)(ctx->datastack - sizeof(cell));
124   bignum* y = untag<bignum>(*s0);
125   bignum* x = untag<bignum>(*s1);
126   bignum* q, *r;
127   bignum_divide(x, y, &q, &r);
128   *s1 = tag<bignum>(q);
129   *s0 = tag<bignum>(r);
130 }
131
132 void factor_vm::primitive_bignum_mod() {
133   POP_BIGNUMS(x, y);
134   ctx->replace(tag<bignum>(bignum_remainder(x, y)));
135 }
136
137 void factor_vm::primitive_bignum_gcd() {
138   POP_BIGNUMS(x, y);
139   ctx->replace(tag<bignum>(bignum_gcd(x, y)));
140 }
141
142 void factor_vm::primitive_bignum_and() {
143   POP_BIGNUMS(x, y);
144   ctx->replace(tag<bignum>(bignum_bitwise_and(x, y)));
145 }
146
147 void factor_vm::primitive_bignum_or() {
148   POP_BIGNUMS(x, y);
149   ctx->replace(tag<bignum>(bignum_bitwise_ior(x, y)));
150 }
151
152 void factor_vm::primitive_bignum_xor() {
153   POP_BIGNUMS(x, y);
154   ctx->replace(tag<bignum>(bignum_bitwise_xor(x, y)));
155 }
156
157 void factor_vm::primitive_bignum_shift() {
158   fixnum y = untag_fixnum(ctx->pop());
159   bignum* x = untag<bignum>(ctx->peek());
160   ctx->replace(tag<bignum>(bignum_arithmetic_shift(x, y)));
161 }
162
163 void factor_vm::primitive_bignum_less() {
164   POP_BIGNUMS(x, y);
165   ctx->replace(tag_boolean(bignum_compare(x, y) == bignum_comparison_less));
166 }
167
168 void factor_vm::primitive_bignum_lesseq() {
169   POP_BIGNUMS(x, y);
170   ctx->replace(tag_boolean(bignum_compare(x, y) != bignum_comparison_greater));
171 }
172
173 void factor_vm::primitive_bignum_greater() {
174   POP_BIGNUMS(x, y);
175   ctx->replace(tag_boolean(bignum_compare(x, y) == bignum_comparison_greater));
176 }
177
178 void factor_vm::primitive_bignum_greatereq() {
179   POP_BIGNUMS(x, y);
180   ctx->replace(tag_boolean(bignum_compare(x, y) != bignum_comparison_less));
181 }
182
183 void factor_vm::primitive_bignum_not() {
184   ctx->replace(tag<bignum>(bignum_bitwise_not(untag<bignum>(ctx->peek()))));
185 }
186
187 void factor_vm::primitive_bignum_bitp() {
188   int bit = (int)to_fixnum(ctx->pop());
189   bignum* x = untag<bignum>(ctx->peek());
190   ctx->replace(tag_boolean(bignum_logbitp(bit, x)));
191 }
192
193 void factor_vm::primitive_bignum_log2() {
194   ctx->replace(tag<bignum>(bignum_integer_length(untag<bignum>(ctx->peek()))));
195 }
196
197 /* Allocates memory */
198 void factor_vm::primitive_fixnum_to_float() {
199   ctx->replace(allot_float(fixnum_to_float(ctx->peek())));
200 }
201
202 /* Allocates memory */
203 void factor_vm::primitive_format_float() {
204   byte_array* array = allot_byte_array(100);
205   char* format = alien_offset(ctx->pop());
206   double value = untag_float_check(ctx->peek());
207   SNPRINTF(array->data<char>(), 99, format, value);
208   ctx->replace(tag<byte_array>(array));
209 }
210
211 #define POP_FLOATS(x, y)              \
212   double y = untag_float(ctx->pop()); \
213   double x = untag_float(ctx->peek());
214
215 void factor_vm::primitive_float_eq() {
216   POP_FLOATS(x, y);
217   ctx->replace(tag_boolean(x == y));
218 }
219
220 /* Allocates memory */
221 void factor_vm::primitive_float_add() {
222   POP_FLOATS(x, y);
223   ctx->replace(allot_float(x + y));
224 }
225
226 /* Allocates memory */
227 void factor_vm::primitive_float_subtract() {
228   POP_FLOATS(x, y);
229   ctx->replace(allot_float(x - y));
230 }
231
232 /* Allocates memory */
233 void factor_vm::primitive_float_multiply() {
234   POP_FLOATS(x, y);
235   ctx->replace(allot_float(x * y));
236 }
237
238 /* Allocates memory */
239 void factor_vm::primitive_float_divfloat() {
240   POP_FLOATS(x, y);
241   ctx->replace(allot_float(x / y));
242 }
243
244 void factor_vm::primitive_float_less() {
245   POP_FLOATS(x, y);
246   ctx->replace(tag_boolean(x < y));
247 }
248
249 void factor_vm::primitive_float_lesseq() {
250   POP_FLOATS(x, y);
251   ctx->replace(tag_boolean(x <= y));
252 }
253
254 void factor_vm::primitive_float_greater() {
255   POP_FLOATS(x, y);
256   ctx->replace(tag_boolean(x > y));
257 }
258
259 void factor_vm::primitive_float_greatereq() {
260   POP_FLOATS(x, y);
261   ctx->replace(tag_boolean(x >= y));
262 }
263
264 /* Allocates memory */
265 void factor_vm::primitive_float_bits() {
266   ctx->replace(
267       from_unsigned_cell(float_bits((float)untag_float_check(ctx->peek()))));
268 }
269
270 /* Allocates memory */
271 void factor_vm::primitive_bits_float() {
272   ctx->replace(allot_float(bits_float((uint32_t)to_cell(ctx->peek()))));
273 }
274
275 void factor_vm::primitive_double_bits() {
276   ctx->replace(from_unsigned_8(double_bits(untag_float_check(ctx->peek()))));
277 }
278
279 /* Allocates memory */
280 void factor_vm::primitive_bits_double() {
281   ctx->replace(allot_float(bits_double(to_unsigned_8(ctx->peek()))));
282 }
283
284 /* Cannot allocate. */
285 #define CELL_TO_FOO(name, type, converter)              \
286   type factor_vm::name(cell tagged) {                   \
287     switch (TAG(tagged)) {                              \
288       case FIXNUM_TYPE:                                 \
289         return (type)untag_fixnum(tagged);              \
290       case BIGNUM_TYPE:                                 \
291         return converter(untag<bignum>(tagged));        \
292       default:                                          \
293         type_error(FIXNUM_TYPE, tagged);                \
294         return 0; /* can't happen */                    \
295     }                                                   \
296   }                                                     \
297   VM_C_API type name(cell tagged, factor_vm* parent) {  \
298     return parent->name(tagged);                        \
299   }
300
301 CELL_TO_FOO(to_fixnum, fixnum, bignum_to_fixnum)
302 CELL_TO_FOO(to_fixnum_strict, fixnum, bignum_to_fixnum_strict)
303 CELL_TO_FOO(to_cell, cell, bignum_to_cell)
304 CELL_TO_FOO(to_signed_8, int64_t, bignum_to_long_long)
305 CELL_TO_FOO(to_unsigned_8, uint64_t, bignum_to_ulong_long)
306
307 /* Allocates memory */
308 VM_C_API cell from_signed_cell(fixnum integer, factor_vm* parent) {
309   return parent->from_signed_cell(integer);
310 }
311
312 /* Allocates memory */
313 VM_C_API cell from_unsigned_cell(cell integer, factor_vm* parent) {
314   return parent->from_unsigned_cell(integer);
315 }
316
317 /* Allocates memory */
318 cell factor_vm::from_signed_8(int64_t n) {
319   if (n < fixnum_min || n > fixnum_max)
320     return tag<bignum>(long_long_to_bignum(n));
321   else
322     return tag_fixnum((fixnum)n);
323 }
324
325 VM_C_API cell from_signed_8(int64_t n, factor_vm* parent) {
326   return parent->from_signed_8(n);
327 }
328
329 /* Allocates memory */
330 cell factor_vm::from_unsigned_8(uint64_t n) {
331   if (n > (uint64_t)fixnum_max)
332     return tag<bignum>(ulong_long_to_bignum(n));
333   else
334     return tag_fixnum((fixnum)n);
335 }
336
337 VM_C_API cell from_unsigned_8(uint64_t n, factor_vm* parent) {
338   return parent->from_unsigned_8(n);
339 }
340
341 /* Cannot allocate */
342 float factor_vm::to_float(cell value) {
343   return (float)untag_float_check(value);
344 }
345
346 /* Cannot allocate */
347 double factor_vm::to_double(cell value) { return untag_float_check(value); }
348
349 /* The fixnum+, fixnum- and fixnum* primitives are defined in cpu_*.S. On
350    overflow, they call these functions. */
351 /* Allocates memory */
352 inline void factor_vm::overflow_fixnum_add(fixnum x, fixnum y) {
353   ctx->replace(
354       tag<bignum>(fixnum_to_bignum(untag_fixnum(x) + untag_fixnum(y))));
355 }
356
357 VM_C_API void overflow_fixnum_add(fixnum x, fixnum y, factor_vm* parent) {
358   parent->overflow_fixnum_add(x, y);
359 }
360
361 /* Allocates memory */
362 inline void factor_vm::overflow_fixnum_subtract(fixnum x, fixnum y) {
363   ctx->replace(
364       tag<bignum>(fixnum_to_bignum(untag_fixnum(x) - untag_fixnum(y))));
365 }
366
367 VM_C_API void overflow_fixnum_subtract(fixnum x, fixnum y, factor_vm* parent) {
368   parent->overflow_fixnum_subtract(x, y);
369 }
370
371 /* Allocates memory */
372 inline void factor_vm::overflow_fixnum_multiply(fixnum x, fixnum y) {
373   data_root<bignum> bx(fixnum_to_bignum(x), this);
374   data_root<bignum> by(fixnum_to_bignum(y), this);
375   cell ret = tag<bignum>(bignum_multiply(bx.untagged(), by.untagged()));
376   ctx->replace(ret);
377 }
378
379 VM_C_API void overflow_fixnum_multiply(fixnum x, fixnum y, factor_vm* parent) {
380   parent->overflow_fixnum_multiply(x, y);
381 }
382
383 }