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