]> gitweb.factorcode.org Git - factor.git/blob - vm/math.cpp
Merge branch 'master' of git://factorcode.org/git/factor
[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 *parent)
223 {
224         return parent->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(false_object);
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 *parent)
397 {
398         return parent->to_fixnum(tagged);
399 }
400
401 cell factor_vm::to_cell(cell tagged)
402 {
403         return (cell)to_fixnum(tagged);
404 }
405
406 VM_C_API cell to_cell(cell tagged, factor_vm *parent)
407 {
408         return parent->to_cell(tagged);
409 }
410
411 void factor_vm::box_signed_1(s8 n)
412 {
413         dpush(tag_fixnum(n));
414 }
415
416 VM_C_API void box_signed_1(s8 n,factor_vm *parent)
417 {
418         return parent->box_signed_1(n);
419 }
420
421 void factor_vm::box_unsigned_1(u8 n)
422 {
423         dpush(tag_fixnum(n));
424 }
425
426 VM_C_API void box_unsigned_1(u8 n,factor_vm *parent)
427 {
428         return parent->box_unsigned_1(n);
429 }
430
431 void factor_vm::box_signed_2(s16 n)
432 {
433         dpush(tag_fixnum(n));
434 }
435
436 VM_C_API void box_signed_2(s16 n,factor_vm *parent)
437 {
438         return parent->box_signed_2(n);
439 }
440
441 void factor_vm::box_unsigned_2(u16 n)
442 {
443         dpush(tag_fixnum(n));
444 }
445
446 VM_C_API void box_unsigned_2(u16 n,factor_vm *parent)
447 {
448         return parent->box_unsigned_2(n);
449 }
450
451 void factor_vm::box_signed_4(s32 n)
452 {
453         dpush(allot_integer(n));
454 }
455
456 VM_C_API void box_signed_4(s32 n,factor_vm *parent)
457 {
458         return parent->box_signed_4(n);
459 }
460
461 void factor_vm::box_unsigned_4(u32 n)
462 {
463         dpush(allot_cell(n));
464 }
465
466 VM_C_API void box_unsigned_4(u32 n,factor_vm *parent)
467 {
468         return parent->box_unsigned_4(n);
469 }
470
471 void factor_vm::box_signed_cell(fixnum integer)
472 {
473         dpush(allot_integer(integer));
474 }
475
476 VM_C_API void box_signed_cell(fixnum integer,factor_vm *parent)
477 {
478         return parent->box_signed_cell(integer);
479 }
480
481 void factor_vm::box_unsigned_cell(cell cell)
482 {
483         dpush(allot_cell(cell));
484 }
485
486 VM_C_API void box_unsigned_cell(cell cell,factor_vm *parent)
487 {
488         return parent->box_unsigned_cell(cell);
489 }
490
491 void factor_vm::box_signed_8(s64 n)
492 {
493         if(n < fixnum_min || n > fixnum_max)
494                 dpush(tag<bignum>(long_long_to_bignum(n)));
495         else
496                 dpush(tag_fixnum(n));
497 }
498
499 VM_C_API void box_signed_8(s64 n,factor_vm *parent)
500 {
501         return parent->box_signed_8(n);
502 }
503
504 s64 factor_vm::to_signed_8(cell obj)
505 {
506         switch(tagged<object>(obj).type())
507         {
508         case FIXNUM_TYPE:
509                 return untag_fixnum(obj);
510         case BIGNUM_TYPE:
511                 return bignum_to_long_long(untag<bignum>(obj));
512         default:
513                 type_error(BIGNUM_TYPE,obj);
514                 return 0;
515         }
516 }
517
518 VM_C_API s64 to_signed_8(cell obj,factor_vm *parent)
519 {
520         return parent->to_signed_8(obj);
521 }
522
523 void factor_vm::box_unsigned_8(u64 n)
524 {
525         if(n > (u64)fixnum_max)
526                 dpush(tag<bignum>(ulong_long_to_bignum(n)));
527         else
528                 dpush(tag_fixnum(n));
529 }
530
531 VM_C_API void box_unsigned_8(u64 n,factor_vm *parent)
532 {
533         return parent->box_unsigned_8(n);
534 }
535
536 u64 factor_vm::to_unsigned_8(cell obj)
537 {
538         switch(tagged<object>(obj).type())
539         {
540         case FIXNUM_TYPE:
541                 return untag_fixnum(obj);
542         case BIGNUM_TYPE:
543                 return bignum_to_ulong_long(untag<bignum>(obj));
544         default:
545                 type_error(BIGNUM_TYPE,obj);
546                 return 0;
547         }
548 }
549
550 VM_C_API u64 to_unsigned_8(cell obj,factor_vm *parent)
551 {
552         return parent->to_unsigned_8(obj);
553 }
554  
555 void factor_vm::box_float(float flo)
556 {
557         dpush(allot_float(flo));
558 }
559
560 VM_C_API void box_float(float flo, factor_vm *parent)
561 {
562         return parent->box_float(flo);
563 }
564
565 float factor_vm::to_float(cell value)
566 {
567         return untag_float_check(value);
568 }
569
570 VM_C_API float to_float(cell value,factor_vm *parent)
571 {
572         return parent->to_float(value);
573 }
574
575 void factor_vm::box_double(double flo)
576 {
577         dpush(allot_float(flo));
578 }
579
580 VM_C_API void box_double(double flo,factor_vm *parent)
581 {
582         return parent->box_double(flo);
583 }
584
585 double factor_vm::to_double(cell value)
586 {
587         return untag_float_check(value);
588 }
589
590 VM_C_API double to_double(cell value,factor_vm *parent)
591 {
592         return parent->to_double(value);
593 }
594
595 /* The fixnum+, fixnum- and fixnum* primitives are defined in cpu_*.S. On
596 overflow, they call these functions. */
597 inline void factor_vm::overflow_fixnum_add(fixnum x, fixnum y)
598 {
599         drepl(tag<bignum>(fixnum_to_bignum(
600                 untag_fixnum(x) + untag_fixnum(y))));
601 }
602
603 VM_ASM_API void overflow_fixnum_add(fixnum x, fixnum y, factor_vm *parent)
604 {
605         parent->overflow_fixnum_add(x,y);
606 }
607
608 inline void factor_vm::overflow_fixnum_subtract(fixnum x, fixnum y)
609 {
610         drepl(tag<bignum>(fixnum_to_bignum(
611                 untag_fixnum(x) - untag_fixnum(y))));
612 }
613
614 VM_ASM_API void overflow_fixnum_subtract(fixnum x, fixnum y, factor_vm *parent)
615 {
616         parent->overflow_fixnum_subtract(x,y);
617 }
618
619 inline void factor_vm::overflow_fixnum_multiply(fixnum x, fixnum y)
620 {
621         bignum *bx = fixnum_to_bignum(x);
622         GC_BIGNUM(bx);
623         bignum *by = fixnum_to_bignum(y);
624         GC_BIGNUM(by);
625         drepl(tag<bignum>(bignum_multiply(bx,by)));
626 }
627
628 VM_ASM_API void overflow_fixnum_multiply(fixnum x, fixnum y, factor_vm *parent)
629 {
630         parent->overflow_fixnum_multiply(x,y);
631 }
632
633 }