]> gitweb.factorcode.org Git - factor.git/blob - vm/math.cpp
vm: don't ctx-pop() in bignum_divmod.
[factor.git] / vm / math.cpp
1 #include "master.hpp"
2
3 namespace factor
4 {
5
6 void factor_vm::primitive_bignum_to_fixnum()
7 {
8         ctx->replace(tag_fixnum(bignum_to_fixnum(untag<bignum>(ctx->peek()))));
9 }
10
11 void factor_vm::primitive_float_to_fixnum()
12 {
13         ctx->replace(tag_fixnum(float_to_fixnum(ctx->peek())));
14 }
15
16 /* does not allocate, even though from_signed_cell can allocate */
17 /* Division can only overflow when we are dividing the most negative fixnum
18 by -1. */
19 void factor_vm::primitive_fixnum_divint()
20 {
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 {
34         cell *s0 = (cell *)(ctx->datastack);
35         cell *s1 = (cell *)(ctx->datastack - sizeof(cell));
36         fixnum y = untag_fixnum(*s0);
37         fixnum x = untag_fixnum(*s1);
38         if(y == -1 && x == fixnum_min)
39         {
40                 /* Does not allocate */
41                 *s1 = from_signed_cell(-fixnum_min);
42                 *s0 = tag_fixnum(0);
43         }
44         else
45         {
46                 *s1 = tag_fixnum(x / y);
47                 *s0 = tag_fixnum(x % y);
48         }
49 }
50
51 /*
52  * If we're shifting right by n bits, we won't overflow as long as none of the
53  * high WORD_SIZE-TAG_BITS-n bits are set.
54  */
55 inline fixnum factor_vm::sign_mask(fixnum x)
56 {
57         return x >> (WORD_SIZE - 1);
58 }
59
60 inline fixnum factor_vm::branchless_max(fixnum x, fixnum y)
61 {
62         return (x - ((x - y) & sign_mask(x - y)));
63 }
64
65 inline fixnum factor_vm::branchless_abs(fixnum x)
66 {
67         return (x ^ sign_mask(x)) - sign_mask(x);
68 }
69
70 void factor_vm::primitive_fixnum_shift()
71 {
72         fixnum y = untag_fixnum(ctx->pop());
73         fixnum x = untag_fixnum(ctx->peek());
74
75         if(x == 0)
76                 return;
77         else if(y < 0)
78         {
79                 y = branchless_max(y,-WORD_SIZE + 1);
80                 ctx->replace(tag_fixnum(x >> -y));
81                 return;
82         }
83         else if(y < WORD_SIZE - TAG_BITS)
84         {
85                 fixnum mask = -((fixnum)1 << (WORD_SIZE - 1 - TAG_BITS - y));
86                 if(!(branchless_abs(x) & mask))
87                 {
88                         ctx->replace(tag_fixnum(x << y));
89                         return;
90                 }
91         }
92
93         ctx->replace(tag<bignum>(bignum_arithmetic_shift(
94                 fixnum_to_bignum(x),y)));
95 }
96
97 void factor_vm::primitive_fixnum_to_bignum()
98 {
99         ctx->replace(tag<bignum>(fixnum_to_bignum(untag_fixnum(ctx->peek()))));
100 }
101
102 void factor_vm::primitive_float_to_bignum()
103 {
104         ctx->replace(tag<bignum>(float_to_bignum(ctx->peek())));
105 }
106
107 #define POP_BIGNUMS(x,y) \
108         bignum * y = untag<bignum>(ctx->pop()); \
109         bignum * x = untag<bignum>(ctx->peek());
110
111 void factor_vm::primitive_bignum_eq()
112 {
113         POP_BIGNUMS(x,y);
114         ctx->replace(tag_boolean(bignum_equal_p(x,y)));
115 }
116
117 void factor_vm::primitive_bignum_add()
118 {
119         POP_BIGNUMS(x,y);
120         ctx->replace(tag<bignum>(bignum_add(x,y)));
121 }
122
123 void factor_vm::primitive_bignum_subtract()
124 {
125         POP_BIGNUMS(x,y);
126         ctx->replace(tag<bignum>(bignum_subtract(x,y)));
127 }
128
129 void factor_vm::primitive_bignum_multiply()
130 {
131         POP_BIGNUMS(x,y);
132         ctx->replace(tag<bignum>(bignum_multiply(x,y)));
133 }
134
135 void factor_vm::primitive_bignum_divint()
136 {
137         POP_BIGNUMS(x,y);
138         ctx->replace(tag<bignum>(bignum_quotient(x,y)));
139 }
140
141 void factor_vm::primitive_bignum_divmod()
142 {
143         cell *s0 = (cell *)(ctx->datastack);
144         cell *s1 = (cell *)(ctx->datastack - sizeof(cell));
145         bignum *y = untag<bignum>(*s0);
146         bignum *x = untag<bignum>(*s1);
147         bignum *q, *r;
148         bignum_divide(x, y, &q, &r);
149         *s1 = tag<bignum>(q);
150         *s0 = tag<bignum>(r);
151 }
152
153 void factor_vm::primitive_bignum_mod()
154 {
155         POP_BIGNUMS(x,y);
156         ctx->replace(tag<bignum>(bignum_remainder(x,y)));
157 }
158
159 void factor_vm::primitive_bignum_gcd()
160 {
161         POP_BIGNUMS(x,y);
162         ctx->replace(tag<bignum>(bignum_gcd(x,y)));
163 }
164
165 void factor_vm::primitive_bignum_and()
166 {
167         POP_BIGNUMS(x,y);
168         ctx->replace(tag<bignum>(bignum_bitwise_and(x,y)));
169 }
170
171 void factor_vm::primitive_bignum_or()
172 {
173         POP_BIGNUMS(x,y);
174         ctx->replace(tag<bignum>(bignum_bitwise_ior(x,y)));
175 }
176
177 void factor_vm::primitive_bignum_xor()
178 {
179         POP_BIGNUMS(x,y);
180         ctx->replace(tag<bignum>(bignum_bitwise_xor(x,y)));
181 }
182
183 void factor_vm::primitive_bignum_shift()
184 {
185         fixnum y = untag_fixnum(ctx->pop());
186         bignum* x = untag<bignum>(ctx->peek());
187         ctx->replace(tag<bignum>(bignum_arithmetic_shift(x,y)));
188 }
189
190 void factor_vm::primitive_bignum_less()
191 {
192         POP_BIGNUMS(x,y);
193         ctx->replace(tag_boolean(bignum_compare(x,y) == bignum_comparison_less));
194 }
195
196 void factor_vm::primitive_bignum_lesseq()
197 {
198         POP_BIGNUMS(x,y);
199         ctx->replace(tag_boolean(bignum_compare(x,y) != bignum_comparison_greater));
200 }
201
202 void factor_vm::primitive_bignum_greater()
203 {
204         POP_BIGNUMS(x,y);
205         ctx->replace(tag_boolean(bignum_compare(x,y) == bignum_comparison_greater));
206 }
207
208 void factor_vm::primitive_bignum_greatereq()
209 {
210         POP_BIGNUMS(x,y);
211         ctx->replace(tag_boolean(bignum_compare(x,y) != bignum_comparison_less));
212 }
213
214 void factor_vm::primitive_bignum_not()
215 {
216         ctx->replace(tag<bignum>(bignum_bitwise_not(untag<bignum>(ctx->peek()))));
217 }
218
219 void factor_vm::primitive_bignum_bitp()
220 {
221         int bit = (int)to_fixnum(ctx->pop());
222         bignum *x = untag<bignum>(ctx->pop());
223         ctx->push(tag_boolean(bignum_logbitp(bit,x)));
224 }
225
226 void factor_vm::primitive_bignum_log2()
227 {
228         ctx->replace(tag<bignum>(bignum_integer_length(untag<bignum>(ctx->peek()))));
229 }
230
231 /* allocates memory */
232 cell factor_vm::unbox_array_size_slow()
233 {
234         if(tagged<object>(ctx->peek()).type() == BIGNUM_TYPE)
235         {
236                 bignum *zero = untag<bignum>(bignum_zero);
237                 GC_BIGNUM(zero);
238                 bignum *max = cell_to_bignum(array_size_max);
239                 bignum *n = untag<bignum>(ctx->peek());
240                 if(bignum_compare(n,zero) != bignum_comparison_less
241                         && bignum_compare(n,max) == bignum_comparison_less)
242                 {
243                         ctx->pop();
244                         return bignum_to_cell(n);
245                 }
246         }
247
248         general_error(ERROR_ARRAY_SIZE,ctx->pop(),tag_fixnum(array_size_max));
249         return 0; /* can't happen */
250 }
251
252 void factor_vm::primitive_fixnum_to_float()
253 {
254         ctx->replace(allot_float(fixnum_to_float(ctx->peek())));
255 }
256
257 void factor_vm::primitive_format_float()
258 {
259         byte_array *array = allot_byte_array(100);
260         char *format = alien_offset(ctx->pop());
261         double value = untag_float_check(ctx->pop());
262         SNPRINTF(array->data<char>(),99,format,value);
263         ctx->push(tag<byte_array>(array));
264 }
265
266 #define POP_FLOATS(x,y) \
267         double y = untag_float(ctx->pop()); \
268         double x = untag_float(ctx->peek());
269
270 void factor_vm::primitive_float_eq()
271 {
272         POP_FLOATS(x,y);
273         ctx->replace(tag_boolean(x == y));
274 }
275
276 void factor_vm::primitive_float_add()
277 {
278         POP_FLOATS(x,y);
279         ctx->replace(allot_float(x + y));
280 }
281
282 void factor_vm::primitive_float_subtract()
283 {
284         POP_FLOATS(x,y);
285         ctx->replace(allot_float(x - y));
286 }
287
288 void factor_vm::primitive_float_multiply()
289 {
290         POP_FLOATS(x,y);
291         ctx->replace(allot_float(x * y));
292 }
293
294 void factor_vm::primitive_float_divfloat()
295 {
296         POP_FLOATS(x,y);
297         ctx->replace(allot_float(x / y));
298 }
299
300 void factor_vm::primitive_float_less()
301 {
302         POP_FLOATS(x,y);
303         ctx->replace(tag_boolean(x < y));
304 }
305
306 void factor_vm::primitive_float_lesseq()
307 {
308         POP_FLOATS(x,y);
309         ctx->replace(tag_boolean(x <= y));
310 }
311
312 void factor_vm::primitive_float_greater()
313 {
314         POP_FLOATS(x,y);
315         ctx->replace(tag_boolean(x > y));
316 }
317
318 void factor_vm::primitive_float_greatereq()
319 {
320         POP_FLOATS(x,y);
321         ctx->replace(tag_boolean(x >= y));
322 }
323
324 /* Allocates memory */
325 void factor_vm::primitive_float_bits()
326 {
327         ctx->push(from_unsigned_cell(float_bits((float)untag_float_check(ctx->pop()))));
328 }
329
330 /* Allocates memory */
331 void factor_vm::primitive_bits_float()
332 {
333         ctx->push(allot_float(bits_float((u32)to_cell(ctx->pop()))));
334 }
335
336 void factor_vm::primitive_double_bits()
337 {
338         ctx->push(from_unsigned_8(double_bits(untag_float_check(ctx->pop()))));
339 }
340
341 void factor_vm::primitive_bits_double()
342 {
343         ctx->push(allot_float(bits_double(to_unsigned_8(ctx->pop()))));
344 }
345
346 /* Cannot allocate */
347 fixnum factor_vm::to_fixnum(cell tagged)
348 {
349         switch(TAG(tagged))
350         {
351         case FIXNUM_TYPE:
352                 return untag_fixnum(tagged);
353         case BIGNUM_TYPE:
354                 return bignum_to_fixnum(untag<bignum>(tagged));
355         default:
356                 type_error(FIXNUM_TYPE,tagged);
357                 return 0; /* can't happen */
358         }
359 }
360
361 VM_C_API fixnum to_fixnum(cell tagged, factor_vm *parent)
362 {
363         return parent->to_fixnum(tagged);
364 }
365
366 cell factor_vm::to_cell(cell tagged)
367 {
368         return (cell)to_fixnum(tagged);
369 }
370
371 VM_C_API cell to_cell(cell tagged, factor_vm *parent)
372 {
373         return parent->to_cell(tagged);
374 }
375
376 /* Allocates memory */
377 VM_C_API cell from_signed_cell(fixnum integer, factor_vm *parent)
378 {
379         return parent->from_signed_cell(integer);
380 }
381
382 /* Allocates memory */
383 VM_C_API cell from_unsigned_cell(cell integer, factor_vm *parent)
384 {
385         return parent->from_unsigned_cell(integer);
386 }
387
388 /* Allocates memory */
389 cell factor_vm::from_signed_8(s64 n)
390 {
391         if(n < fixnum_min || n > fixnum_max)
392                 return tag<bignum>(long_long_to_bignum(n));
393         else
394                 return tag_fixnum((fixnum)n);
395 }
396
397 VM_C_API cell from_signed_8(s64 n, factor_vm *parent)
398 {
399         return parent->from_signed_8(n);
400 }
401
402 /* Cannot allocate */
403 s64 factor_vm::to_signed_8(cell obj)
404 {
405         switch(tagged<object>(obj).type())
406         {
407         case FIXNUM_TYPE:
408                 return untag_fixnum(obj);
409         case BIGNUM_TYPE:
410                 return bignum_to_long_long(untag<bignum>(obj));
411         default:
412                 type_error(BIGNUM_TYPE,obj);
413                 return 0;
414         }
415 }
416
417 VM_C_API s64 to_signed_8(cell obj, factor_vm *parent)
418 {
419         return parent->to_signed_8(obj);
420 }
421
422 cell factor_vm::from_unsigned_8(u64 n)
423 {
424         if(n > (u64)fixnum_max)
425                 return tag<bignum>(ulong_long_to_bignum(n));
426         else
427                 return tag_fixnum((fixnum)n);
428 }
429
430 VM_C_API cell from_unsigned_8(u64 n, factor_vm *parent)
431 {
432         return parent->from_unsigned_8(n);
433 }
434
435 /* Cannot allocate */
436 u64 factor_vm::to_unsigned_8(cell obj)
437 {
438         switch(tagged<object>(obj).type())
439         {
440         case FIXNUM_TYPE:
441                 return untag_fixnum(obj);
442         case BIGNUM_TYPE:
443                 return bignum_to_ulong_long(untag<bignum>(obj));
444         default:
445                 type_error(BIGNUM_TYPE,obj);
446                 return 0;
447         }
448 }
449
450 VM_C_API u64 to_unsigned_8(cell obj, factor_vm *parent)
451 {
452         return parent->to_unsigned_8(obj);
453 }
454  
455 /* Cannot allocate */
456 float factor_vm::to_float(cell value)
457 {
458         return (float)untag_float_check(value);
459 }
460
461 /* Cannot allocate */
462 double factor_vm::to_double(cell value)
463 {
464         return untag_float_check(value);
465 }
466
467 /* The fixnum+, fixnum- and fixnum* primitives are defined in cpu_*.S. On
468 overflow, they call these functions. */
469 inline void factor_vm::overflow_fixnum_add(fixnum x, fixnum y)
470 {
471         ctx->replace(tag<bignum>(fixnum_to_bignum(
472                 untag_fixnum(x) + untag_fixnum(y))));
473 }
474
475 VM_C_API void overflow_fixnum_add(fixnum x, fixnum y, factor_vm *parent)
476 {
477         parent->overflow_fixnum_add(x,y);
478 }
479
480 inline void factor_vm::overflow_fixnum_subtract(fixnum x, fixnum y)
481 {
482         ctx->replace(tag<bignum>(fixnum_to_bignum(
483                 untag_fixnum(x) - untag_fixnum(y))));
484 }
485
486 VM_C_API void overflow_fixnum_subtract(fixnum x, fixnum y, factor_vm *parent)
487 {
488         parent->overflow_fixnum_subtract(x,y);
489 }
490
491 inline void factor_vm::overflow_fixnum_multiply(fixnum x, fixnum y)
492 {
493         bignum *bx = fixnum_to_bignum(x);
494         GC_BIGNUM(bx);
495         bignum *by = fixnum_to_bignum(y);
496         GC_BIGNUM(by);
497         ctx->replace(tag<bignum>(bignum_multiply(bx,by)));
498 }
499
500 VM_C_API void overflow_fixnum_multiply(fixnum x, fixnum y, factor_vm *parent)
501 {
502         parent->overflow_fixnum_multiply(x,y);
503 }
504
505 }