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