]> gitweb.factorcode.org Git - factor.git/blob - vm/math.cpp
VM: bignum_to_fixnum_strict and an accompanying vm error in case the conversion fails
[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 /* Allocates memory */
281 fixnum factor_vm::to_fixnum(cell tagged) {
282   switch (TAG(tagged)) {
283     case FIXNUM_TYPE:
284       return untag_fixnum(tagged);
285     case BIGNUM_TYPE:
286       return bignum_to_fixnum_strict(untag<bignum>(tagged));
287     default:
288       type_error(FIXNUM_TYPE, tagged);
289       return 0; /* can't happen */
290   }
291 }
292
293 VM_C_API fixnum to_fixnum(cell tagged, factor_vm* parent) {
294   return parent->to_fixnum(tagged);
295 }
296
297 cell factor_vm::to_cell(cell tagged) { return (cell)to_fixnum(tagged); }
298
299 VM_C_API cell to_cell(cell tagged, factor_vm* parent) {
300   return parent->to_cell(tagged);
301 }
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 /* Cannot allocate */
326 int64_t factor_vm::to_signed_8(cell obj) {
327   switch (tagged<object>(obj).type()) {
328     case FIXNUM_TYPE:
329       return untag_fixnum(obj);
330     case BIGNUM_TYPE:
331       return bignum_to_long_long(untag<bignum>(obj));
332     default:
333       type_error(BIGNUM_TYPE, obj);
334       return 0;
335   }
336 }
337
338 VM_C_API int64_t to_signed_8(cell obj, factor_vm* parent) {
339   return parent->to_signed_8(obj);
340 }
341
342 /* Allocates memory */
343 cell factor_vm::from_unsigned_8(uint64_t n) {
344   if (n > (uint64_t)fixnum_max)
345     return tag<bignum>(ulong_long_to_bignum(n));
346   else
347     return tag_fixnum((fixnum)n);
348 }
349
350 VM_C_API cell from_unsigned_8(uint64_t n, factor_vm* parent) {
351   return parent->from_unsigned_8(n);
352 }
353
354 /* Cannot allocate */
355 uint64_t factor_vm::to_unsigned_8(cell obj) {
356   switch (tagged<object>(obj).type()) {
357     case FIXNUM_TYPE:
358       return untag_fixnum(obj);
359     case BIGNUM_TYPE:
360       return bignum_to_ulong_long(untag<bignum>(obj));
361     default:
362       type_error(BIGNUM_TYPE, obj);
363       return 0;
364   }
365 }
366
367 VM_C_API uint64_t to_unsigned_8(cell obj, factor_vm* parent) {
368   return parent->to_unsigned_8(obj);
369 }
370
371 /* Cannot allocate */
372 float factor_vm::to_float(cell value) {
373   return (float)untag_float_check(value);
374 }
375
376 /* Cannot allocate */
377 double factor_vm::to_double(cell value) { return untag_float_check(value); }
378
379 /* The fixnum+, fixnum- and fixnum* primitives are defined in cpu_*.S. On
380    overflow, they call these functions. */
381 /* Allocates memory */
382 inline void factor_vm::overflow_fixnum_add(fixnum x, fixnum y) {
383   ctx->replace(
384       tag<bignum>(fixnum_to_bignum(untag_fixnum(x) + untag_fixnum(y))));
385 }
386
387 VM_C_API void overflow_fixnum_add(fixnum x, fixnum y, factor_vm* parent) {
388   parent->overflow_fixnum_add(x, y);
389 }
390
391 /* Allocates memory */
392 inline void factor_vm::overflow_fixnum_subtract(fixnum x, fixnum y) {
393   ctx->replace(
394       tag<bignum>(fixnum_to_bignum(untag_fixnum(x) - untag_fixnum(y))));
395 }
396
397 VM_C_API void overflow_fixnum_subtract(fixnum x, fixnum y, factor_vm* parent) {
398   parent->overflow_fixnum_subtract(x, y);
399 }
400
401 /* Allocates memory */
402 inline void factor_vm::overflow_fixnum_multiply(fixnum x, fixnum y) {
403   bignum* bx = fixnum_to_bignum(x);
404   GC_BIGNUM(bx);
405   bignum* by = fixnum_to_bignum(y);
406   GC_BIGNUM(by);
407   ctx->replace(tag<bignum>(bignum_multiply(bx, by)));
408 }
409
410 VM_C_API void overflow_fixnum_multiply(fixnum x, fixnum y, factor_vm* parent) {
411   parent->overflow_fixnum_multiply(x, y);
412 }
413
414 }