]> gitweb.factorcode.org Git - factor.git/blob - vm/math.cpp
fde2bc6748b3b871d9a11744c31250d88adeb768
[factor.git] / vm / math.cpp
1 #include "master.hpp"
2
3 namespace factor
4 {
5
6 inline void factor_vm::primitive_bignum_to_fixnum()
7 {
8         drepl(tag_fixnum(bignum_to_fixnum(untag<bignum>(dpeek()))));
9 }
10
11 PRIMITIVE_FORWARD(bignum_to_fixnum)
12
13 inline void factor_vm::primitive_float_to_fixnum()
14 {
15         drepl(tag_fixnum(float_to_fixnum(dpeek())));
16 }
17
18 PRIMITIVE_FORWARD(float_to_fixnum)
19
20 /* Division can only overflow when we are dividing the most negative fixnum
21 by -1. */
22 inline void factor_vm::primitive_fixnum_divint()
23 {
24         fixnum y = untag_fixnum(dpop()); \
25         fixnum x = untag_fixnum(dpeek());
26         fixnum result = x / y;
27         if(result == -fixnum_min)
28                 drepl(allot_integer(-fixnum_min));
29         else
30                 drepl(tag_fixnum(result));
31 }
32
33 PRIMITIVE_FORWARD(fixnum_divint)
34
35 inline void factor_vm::primitive_fixnum_divmod()
36 {
37         cell y = ((cell *)ds)[0];
38         cell x = ((cell *)ds)[-1];
39         if(y == tag_fixnum(-1) && x == tag_fixnum(fixnum_min))
40         {
41                 ((cell *)ds)[-1] = allot_integer(-fixnum_min);
42                 ((cell *)ds)[0] = tag_fixnum(0);
43         }
44         else
45         {
46                 ((cell *)ds)[-1] = tag_fixnum(untag_fixnum(x) / untag_fixnum(y));
47                 ((cell *)ds)[0] = (fixnum)x % (fixnum)y;
48         }
49 }
50
51 PRIMITIVE_FORWARD(fixnum_divmod)
52
53 /*
54  * If we're shifting right by n bits, we won't overflow as long as none of the
55  * high WORD_SIZE-TAG_BITS-n bits are set.
56  */
57 inline fixnum factor_vm::sign_mask(fixnum x)
58 {
59         return x >> (WORD_SIZE - 1);
60 }
61
62 inline fixnum factor_vm::branchless_max(fixnum x, fixnum y)
63 {
64         return (x - ((x - y) & sign_mask(x - y)));
65 }
66
67 inline fixnum factor_vm::branchless_abs(fixnum x)
68 {
69         return (x ^ sign_mask(x)) - sign_mask(x);
70 }
71
72 inline void factor_vm::primitive_fixnum_shift()
73 {
74         fixnum y = untag_fixnum(dpop());
75         fixnum x = untag_fixnum(dpeek());
76
77         if(x == 0)
78                 return;
79         else if(y < 0)
80         {
81                 y = branchless_max(y,-WORD_SIZE + 1);
82                 drepl(tag_fixnum(x >> -y));
83                 return;
84         }
85         else if(y < WORD_SIZE - TAG_BITS)
86         {
87                 fixnum mask = -((fixnum)1 << (WORD_SIZE - 1 - TAG_BITS - y));
88                 if(!(branchless_abs(x) & mask))
89                 {
90                         drepl(tag_fixnum(x << y));
91                         return;
92                 }
93         }
94
95         drepl(tag<bignum>(bignum_arithmetic_shift(
96                 fixnum_to_bignum(x),y)));
97 }
98
99 PRIMITIVE_FORWARD(fixnum_shift)
100
101 inline void factor_vm::primitive_fixnum_to_bignum()
102 {
103         drepl(tag<bignum>(fixnum_to_bignum(untag_fixnum(dpeek()))));
104 }
105
106 PRIMITIVE_FORWARD(fixnum_to_bignum)
107
108 inline void factor_vm::primitive_float_to_bignum()
109 {
110         drepl(tag<bignum>(float_to_bignum(dpeek())));
111 }
112
113 PRIMITIVE_FORWARD(float_to_bignum)
114
115 #define POP_BIGNUMS(x,y) \
116         bignum * y = untag<bignum>(dpop()); \
117         bignum * x = untag<bignum>(dpop());
118
119 inline void factor_vm::primitive_bignum_eq()
120 {
121         POP_BIGNUMS(x,y);
122         box_boolean(bignum_equal_p(x,y));
123 }
124
125 PRIMITIVE_FORWARD(bignum_eq)
126
127 inline void factor_vm::primitive_bignum_add()
128 {
129         POP_BIGNUMS(x,y);
130         dpush(tag<bignum>(bignum_add(x,y)));
131 }
132
133 PRIMITIVE_FORWARD(bignum_add)
134
135 inline void factor_vm::primitive_bignum_subtract()
136 {
137         POP_BIGNUMS(x,y);
138         dpush(tag<bignum>(bignum_subtract(x,y)));
139 }
140
141 PRIMITIVE_FORWARD(bignum_subtract)
142
143 inline void factor_vm::primitive_bignum_multiply()
144 {
145         POP_BIGNUMS(x,y);
146         dpush(tag<bignum>(bignum_multiply(x,y)));
147 }
148
149 PRIMITIVE_FORWARD(bignum_multiply)
150
151 inline void factor_vm::primitive_bignum_divint()
152 {
153         POP_BIGNUMS(x,y);
154         dpush(tag<bignum>(bignum_quotient(x,y)));
155 }
156
157 PRIMITIVE_FORWARD(bignum_divint)
158
159 inline void factor_vm::primitive_bignum_divmod()
160 {
161         bignum *q, *r;
162         POP_BIGNUMS(x,y);
163         bignum_divide(x,y,&q,&r);
164         dpush(tag<bignum>(q));
165         dpush(tag<bignum>(r));
166 }
167
168 PRIMITIVE_FORWARD(bignum_divmod)
169
170 inline void factor_vm::primitive_bignum_mod()
171 {
172         POP_BIGNUMS(x,y);
173         dpush(tag<bignum>(bignum_remainder(x,y)));
174 }
175
176 PRIMITIVE_FORWARD(bignum_mod)
177
178 inline void factor_vm::primitive_bignum_and()
179 {
180         POP_BIGNUMS(x,y);
181         dpush(tag<bignum>(bignum_bitwise_and(x,y)));
182 }
183
184 PRIMITIVE_FORWARD(bignum_and)
185
186 inline void factor_vm::primitive_bignum_or()
187 {
188         POP_BIGNUMS(x,y);
189         dpush(tag<bignum>(bignum_bitwise_ior(x,y)));
190 }
191
192 PRIMITIVE_FORWARD(bignum_or)
193
194 inline void factor_vm::primitive_bignum_xor()
195 {
196         POP_BIGNUMS(x,y);
197         dpush(tag<bignum>(bignum_bitwise_xor(x,y)));
198 }
199
200 PRIMITIVE_FORWARD(bignum_xor)
201
202 inline void factor_vm::primitive_bignum_shift()
203 {
204         fixnum y = untag_fixnum(dpop());
205         bignum* x = untag<bignum>(dpop());
206         dpush(tag<bignum>(bignum_arithmetic_shift(x,y)));
207 }
208
209 PRIMITIVE_FORWARD(bignum_shift)
210
211 inline void factor_vm::primitive_bignum_less()
212 {
213         POP_BIGNUMS(x,y);
214         box_boolean(bignum_compare(x,y) == bignum_comparison_less);
215 }
216
217 PRIMITIVE_FORWARD(bignum_less)
218
219 inline void factor_vm::primitive_bignum_lesseq()
220 {
221         POP_BIGNUMS(x,y);
222         box_boolean(bignum_compare(x,y) != bignum_comparison_greater);
223 }
224
225 PRIMITIVE_FORWARD(bignum_lesseq)
226
227 inline void factor_vm::primitive_bignum_greater()
228 {
229         POP_BIGNUMS(x,y);
230         box_boolean(bignum_compare(x,y) == bignum_comparison_greater);
231 }
232
233 PRIMITIVE_FORWARD(bignum_greater)
234
235 inline void factor_vm::primitive_bignum_greatereq()
236 {
237         POP_BIGNUMS(x,y);
238         box_boolean(bignum_compare(x,y) != bignum_comparison_less);
239 }
240
241 PRIMITIVE_FORWARD(bignum_greatereq)
242
243 inline void factor_vm::primitive_bignum_not()
244 {
245         drepl(tag<bignum>(bignum_bitwise_not(untag<bignum>(dpeek()))));
246 }
247
248 PRIMITIVE_FORWARD(bignum_not)
249
250 inline void factor_vm::primitive_bignum_bitp()
251 {
252         fixnum bit = to_fixnum(dpop());
253         bignum *x = untag<bignum>(dpop());
254         box_boolean(bignum_logbitp(bit,x));
255 }
256
257 PRIMITIVE_FORWARD(bignum_bitp)
258
259 inline void factor_vm::primitive_bignum_log2()
260 {
261         drepl(tag<bignum>(bignum_integer_length(untag<bignum>(dpeek()))));
262 }
263
264 PRIMITIVE_FORWARD(bignum_log2)
265
266 unsigned int factor_vm::bignum_producer(unsigned int digit)
267 {
268         unsigned char *ptr = (unsigned char *)alien_offset(dpeek());
269         return *(ptr + digit);
270 }
271
272 unsigned int bignum_producer(unsigned int digit, factor_vm *myvm)
273 {
274         return myvm->bignum_producer(digit);
275 }
276
277 inline void factor_vm::primitive_byte_array_to_bignum()
278 {
279         cell n_digits = array_capacity(untag_check<byte_array>(dpeek()));
280         bignum * result = digit_stream_to_bignum(n_digits,factor::bignum_producer,0x100,0);
281         drepl(tag<bignum>(result));
282 }
283
284 PRIMITIVE_FORWARD(byte_array_to_bignum)
285
286 cell factor_vm::unbox_array_size()
287 {
288         switch(tagged<object>(dpeek()).type())
289         {
290         case FIXNUM_TYPE:
291                 {
292                         fixnum n = untag_fixnum(dpeek());
293                         if(n >= 0 && n < (fixnum)array_size_max)
294                         {
295                                 dpop();
296                                 return n;
297                         }
298                         break;
299                 }
300         case BIGNUM_TYPE:
301                 {
302                         bignum * zero = untag<bignum>(bignum_zero);
303                         bignum * max = cell_to_bignum(array_size_max);
304                         bignum * n = untag<bignum>(dpeek());
305                         if(bignum_compare(n,zero) != bignum_comparison_less
306                                 && bignum_compare(n,max) == bignum_comparison_less)
307                         {
308                                 dpop();
309                                 return bignum_to_cell(n);
310                         }
311                         break;
312                 }
313         }
314
315         general_error(ERROR_ARRAY_SIZE,dpop(),tag_fixnum(array_size_max),NULL);
316         return 0; /* can't happen */
317 }
318
319 inline void factor_vm::primitive_fixnum_to_float()
320 {
321         drepl(allot_float(fixnum_to_float(dpeek())));
322 }
323
324 PRIMITIVE_FORWARD(fixnum_to_float)
325
326 inline void factor_vm::primitive_bignum_to_float()
327 {
328         drepl(allot_float(bignum_to_float(dpeek())));
329 }
330
331 PRIMITIVE_FORWARD(bignum_to_float)
332
333 inline void factor_vm::primitive_str_to_float()
334 {
335         byte_array *bytes = untag_check<byte_array>(dpeek());
336         cell capacity = array_capacity(bytes);
337
338         char *c_str = (char *)(bytes + 1);
339         char *end = c_str;
340         double f = strtod(c_str,&end);
341         if(end == c_str + capacity - 1)
342                 drepl(allot_float(f));
343         else
344                 drepl(F);
345 }
346
347 PRIMITIVE_FORWARD(str_to_float)
348
349 inline void factor_vm::primitive_float_to_str()
350 {
351         byte_array *array = allot_byte_array(33);
352         snprintf((char *)(array + 1),32,"%.16g",untag_float_check(dpop()));
353         dpush(tag<byte_array>(array));
354 }
355
356 PRIMITIVE_FORWARD(float_to_str)
357
358 #define POP_FLOATS(x,y) \
359         double y = untag_float(dpop()); \
360         double x = untag_float(dpop());
361
362 inline void factor_vm::primitive_float_eq()
363 {
364         POP_FLOATS(x,y);
365         box_boolean(x == y);
366 }
367
368 PRIMITIVE_FORWARD(float_eq)
369
370 inline void factor_vm::primitive_float_add()
371 {
372         POP_FLOATS(x,y);
373         box_double(x + y);
374 }
375
376 PRIMITIVE_FORWARD(float_add)
377
378 inline void factor_vm::primitive_float_subtract()
379 {
380         POP_FLOATS(x,y);
381         box_double(x - y);
382 }
383
384 PRIMITIVE_FORWARD(float_subtract)
385
386 inline void factor_vm::primitive_float_multiply()
387 {
388         POP_FLOATS(x,y);
389         box_double(x * y);
390 }
391
392 PRIMITIVE_FORWARD(float_multiply)
393
394 inline void factor_vm::primitive_float_divfloat()
395 {
396         POP_FLOATS(x,y);
397         box_double(x / y);
398 }
399
400 PRIMITIVE_FORWARD(float_divfloat)
401
402 inline void factor_vm::primitive_float_mod()
403 {
404         POP_FLOATS(x,y);
405         box_double(fmod(x,y));
406 }
407
408 PRIMITIVE_FORWARD(float_mod)
409
410 inline void factor_vm::primitive_float_less()
411 {
412         POP_FLOATS(x,y);
413         box_boolean(x < y);
414 }
415
416 PRIMITIVE_FORWARD(float_less)
417
418 inline void factor_vm::primitive_float_lesseq()
419 {
420         POP_FLOATS(x,y);
421         box_boolean(x <= y);
422 }
423
424 PRIMITIVE_FORWARD(float_lesseq)
425
426 inline void factor_vm::primitive_float_greater()
427 {
428         POP_FLOATS(x,y);
429         box_boolean(x > y);
430 }
431
432 PRIMITIVE_FORWARD(float_greater)
433
434 inline void factor_vm::primitive_float_greatereq()
435 {
436         POP_FLOATS(x,y);
437         box_boolean(x >= y);
438 }
439
440 PRIMITIVE_FORWARD(float_greatereq)
441
442 inline void factor_vm::primitive_float_bits()
443 {
444         box_unsigned_4(float_bits(untag_float_check(dpop())));
445 }
446
447 PRIMITIVE_FORWARD(float_bits)
448
449 inline void factor_vm::primitive_bits_float()
450 {
451         box_float(bits_float(to_cell(dpop())));
452 }
453
454 PRIMITIVE_FORWARD(bits_float)
455
456 inline void factor_vm::primitive_double_bits()
457 {
458         box_unsigned_8(double_bits(untag_float_check(dpop())));
459 }
460
461 PRIMITIVE_FORWARD(double_bits)
462
463 inline void factor_vm::primitive_bits_double()
464 {
465         box_double(bits_double(to_unsigned_8(dpop())));
466 }
467
468 PRIMITIVE_FORWARD(bits_double)
469
470 fixnum factor_vm::to_fixnum(cell tagged)
471 {
472         switch(TAG(tagged))
473         {
474         case FIXNUM_TYPE:
475                 return untag_fixnum(tagged);
476         case BIGNUM_TYPE:
477                 return bignum_to_fixnum(untag<bignum>(tagged));
478         default:
479                 type_error(FIXNUM_TYPE,tagged);
480                 return 0; /* can't happen */
481         }
482 }
483
484 VM_C_API fixnum to_fixnum(cell tagged,factor_vm *myvm)
485 {
486         ASSERTVM();
487         return VM_PTR->to_fixnum(tagged);
488 }
489
490 cell factor_vm::to_cell(cell tagged)
491 {
492         return (cell)to_fixnum(tagged);
493 }
494
495 VM_C_API cell to_cell(cell tagged, factor_vm *myvm)
496 {
497         ASSERTVM();
498         return VM_PTR->to_cell(tagged);
499 }
500
501 void factor_vm::box_signed_1(s8 n)
502 {
503         dpush(tag_fixnum(n));
504 }
505
506 VM_C_API void box_signed_1(s8 n,factor_vm *myvm)
507 {
508         ASSERTVM();
509         return VM_PTR->box_signed_1(n);
510 }
511
512 void factor_vm::box_unsigned_1(u8 n)
513 {
514         dpush(tag_fixnum(n));
515 }
516
517 VM_C_API void box_unsigned_1(u8 n,factor_vm *myvm)
518 {
519         ASSERTVM();
520         return VM_PTR->box_unsigned_1(n);
521 }
522
523 void factor_vm::box_signed_2(s16 n)
524 {
525         dpush(tag_fixnum(n));
526 }
527
528 VM_C_API void box_signed_2(s16 n,factor_vm *myvm)
529 {
530         ASSERTVM();
531         return VM_PTR->box_signed_2(n);
532 }
533
534 void factor_vm::box_unsigned_2(u16 n)
535 {
536         dpush(tag_fixnum(n));
537 }
538
539 VM_C_API void box_unsigned_2(u16 n,factor_vm *myvm)
540 {
541         ASSERTVM();
542         return VM_PTR->box_unsigned_2(n);
543 }
544
545 void factor_vm::box_signed_4(s32 n)
546 {
547         dpush(allot_integer(n));
548 }
549
550 VM_C_API void box_signed_4(s32 n,factor_vm *myvm)
551 {
552         ASSERTVM();
553         return VM_PTR->box_signed_4(n);
554 }
555
556 void factor_vm::box_unsigned_4(u32 n)
557 {
558         dpush(allot_cell(n));
559 }
560
561 VM_C_API void box_unsigned_4(u32 n,factor_vm *myvm)
562 {
563         ASSERTVM();
564         return VM_PTR->box_unsigned_4(n);
565 }
566
567 void factor_vm::box_signed_cell(fixnum integer)
568 {
569         dpush(allot_integer(integer));
570 }
571
572 VM_C_API void box_signed_cell(fixnum integer,factor_vm *myvm)
573 {
574         ASSERTVM();
575         return VM_PTR->box_signed_cell(integer);
576 }
577
578 void factor_vm::box_unsigned_cell(cell cell)
579 {
580         dpush(allot_cell(cell));
581 }
582
583 VM_C_API void box_unsigned_cell(cell cell,factor_vm *myvm)
584 {
585         ASSERTVM();
586         return VM_PTR->box_unsigned_cell(cell);
587 }
588
589 void factor_vm::box_signed_8(s64 n)
590 {
591         if(n < fixnum_min || n > fixnum_max)
592                 dpush(tag<bignum>(long_long_to_bignum(n)));
593         else
594                 dpush(tag_fixnum(n));
595 }
596
597 VM_C_API void box_signed_8(s64 n,factor_vm *myvm)
598 {
599         ASSERTVM();
600         return VM_PTR->box_signed_8(n);
601 }
602
603 s64 factor_vm::to_signed_8(cell obj)
604 {
605         switch(tagged<object>(obj).type())
606         {
607         case FIXNUM_TYPE:
608                 return untag_fixnum(obj);
609         case BIGNUM_TYPE:
610                 return bignum_to_long_long(untag<bignum>(obj));
611         default:
612                 type_error(BIGNUM_TYPE,obj);
613                 return 0;
614         }
615 }
616
617 VM_C_API s64 to_signed_8(cell obj,factor_vm *myvm)
618 {
619         ASSERTVM();
620         return VM_PTR->to_signed_8(obj);
621 }
622
623 void factor_vm::box_unsigned_8(u64 n)
624 {
625         if(n > (u64)fixnum_max)
626                 dpush(tag<bignum>(ulong_long_to_bignum(n)));
627         else
628                 dpush(tag_fixnum(n));
629 }
630
631 VM_C_API void box_unsigned_8(u64 n,factor_vm *myvm)
632 {
633         ASSERTVM();
634         return VM_PTR->box_unsigned_8(n);
635 }
636
637 u64 factor_vm::to_unsigned_8(cell obj)
638 {
639         switch(tagged<object>(obj).type())
640         {
641         case FIXNUM_TYPE:
642                 return untag_fixnum(obj);
643         case BIGNUM_TYPE:
644                 return bignum_to_ulong_long(untag<bignum>(obj));
645         default:
646                 type_error(BIGNUM_TYPE,obj);
647                 return 0;
648         }
649 }
650
651 VM_C_API u64 to_unsigned_8(cell obj,factor_vm *myvm)
652 {
653         ASSERTVM();
654         return VM_PTR->to_unsigned_8(obj);
655 }
656  
657 void factor_vm::box_float(float flo)
658 {
659         dpush(allot_float(flo));
660 }
661
662 VM_C_API void box_float(float flo, factor_vm *myvm)
663 {
664         ASSERTVM();
665         return VM_PTR->box_float(flo);
666 }
667
668 float factor_vm::to_float(cell value)
669 {
670         return untag_float_check(value);
671 }
672
673 VM_C_API float to_float(cell value,factor_vm *myvm)
674 {
675         ASSERTVM();
676         return VM_PTR->to_float(value);
677 }
678
679 void factor_vm::box_double(double flo)
680 {
681         dpush(allot_float(flo));
682 }
683
684 VM_C_API void box_double(double flo,factor_vm *myvm)
685 {
686         ASSERTVM();
687         return VM_PTR->box_double(flo);
688 }
689
690 double factor_vm::to_double(cell value)
691 {
692         return untag_float_check(value);
693 }
694
695 VM_C_API double to_double(cell value,factor_vm *myvm)
696 {
697         ASSERTVM();
698         return VM_PTR->to_double(value);
699 }
700
701 /* The fixnum+, fixnum- and fixnum* primitives are defined in cpu_*.S. On
702 overflow, they call these functions. */
703 inline void factor_vm::overflow_fixnum_add(fixnum x, fixnum y)
704 {
705         drepl(tag<bignum>(fixnum_to_bignum(
706                 untag_fixnum(x) + untag_fixnum(y))));
707 }
708
709 VM_ASM_API void overflow_fixnum_add(fixnum x, fixnum y, factor_vm *myvm)
710 {
711         PRIMITIVE_OVERFLOW_GETVM()->overflow_fixnum_add(x,y);
712 }
713
714 inline void factor_vm::overflow_fixnum_subtract(fixnum x, fixnum y)
715 {
716         drepl(tag<bignum>(fixnum_to_bignum(
717                 untag_fixnum(x) - untag_fixnum(y))));
718 }
719
720 VM_ASM_API void overflow_fixnum_subtract(fixnum x, fixnum y, factor_vm *myvm)
721 {
722         PRIMITIVE_OVERFLOW_GETVM()->overflow_fixnum_subtract(x,y);
723 }
724
725 inline void factor_vm::overflow_fixnum_multiply(fixnum x, fixnum y)
726 {
727         bignum *bx = fixnum_to_bignum(x);
728         GC_BIGNUM(bx);
729         bignum *by = fixnum_to_bignum(y);
730         GC_BIGNUM(by);
731         drepl(tag<bignum>(bignum_multiply(bx,by)));
732 }
733
734 VM_ASM_API void overflow_fixnum_multiply(fixnum x, fixnum y, factor_vm *myvm)
735 {
736         PRIMITIVE_OVERFLOW_GETVM()->overflow_fixnum_multiply(x,y);
737 }
738
739 }