]> gitweb.factorcode.org Git - factor.git/blob - vm/math.cpp
Merge branch 'master' into startup
[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_slow()
235 {
236         if(tagged<object>(dpeek()).type() == BIGNUM_TYPE)
237         {
238                 bignum *zero = untag<bignum>(bignum_zero);
239                 bignum *max = cell_to_bignum(array_size_max);
240                 bignum *n = untag<bignum>(dpeek());
241                 if(bignum_compare(n,zero) != bignum_comparison_less
242                         && bignum_compare(n,max) == bignum_comparison_less)
243                 {
244                         dpop();
245                         return bignum_to_cell(n);
246                 }
247         }
248
249         general_error(ERROR_ARRAY_SIZE,dpop(),tag_fixnum(array_size_max),NULL);
250         return 0; /* can't happen */
251 }
252
253 void factor_vm::primitive_fixnum_to_float()
254 {
255         drepl(allot_float(fixnum_to_float(dpeek())));
256 }
257
258 void factor_vm::primitive_bignum_to_float()
259 {
260         drepl(allot_float(bignum_to_float(dpeek())));
261 }
262
263 void factor_vm::primitive_str_to_float()
264 {
265         byte_array *bytes = untag_check<byte_array>(dpeek());
266         cell capacity = array_capacity(bytes);
267
268         char *c_str = (char *)(bytes + 1);
269         char *end = c_str;
270         double f = strtod(c_str,&end);
271         if(end == c_str + capacity - 1)
272                 drepl(allot_float(f));
273         else
274                 drepl(false_object);
275 }
276
277 void factor_vm::primitive_float_to_str()
278 {
279         byte_array *array = allot_byte_array(33);
280         snprintf((char *)(array + 1),32,"%.16g",untag_float_check(dpop()));
281         dpush(tag<byte_array>(array));
282 }
283
284 #define POP_FLOATS(x,y) \
285         double y = untag_float(dpop()); \
286         double x = untag_float(dpop());
287
288 void factor_vm::primitive_float_eq()
289 {
290         POP_FLOATS(x,y);
291         box_boolean(x == y);
292 }
293
294 void factor_vm::primitive_float_add()
295 {
296         POP_FLOATS(x,y);
297         box_double(x + y);
298 }
299
300 void factor_vm::primitive_float_subtract()
301 {
302         POP_FLOATS(x,y);
303         box_double(x - y);
304 }
305
306 void factor_vm::primitive_float_multiply()
307 {
308         POP_FLOATS(x,y);
309         box_double(x * y);
310 }
311
312 void factor_vm::primitive_float_divfloat()
313 {
314         POP_FLOATS(x,y);
315         box_double(x / y);
316 }
317
318 void factor_vm::primitive_float_mod()
319 {
320         POP_FLOATS(x,y);
321         box_double(fmod(x,y));
322 }
323
324 void factor_vm::primitive_float_less()
325 {
326         POP_FLOATS(x,y);
327         box_boolean(x < y);
328 }
329
330 void factor_vm::primitive_float_lesseq()
331 {
332         POP_FLOATS(x,y);
333         box_boolean(x <= y);
334 }
335
336 void factor_vm::primitive_float_greater()
337 {
338         POP_FLOATS(x,y);
339         box_boolean(x > y);
340 }
341
342 void factor_vm::primitive_float_greatereq()
343 {
344         POP_FLOATS(x,y);
345         box_boolean(x >= y);
346 }
347
348 void factor_vm::primitive_float_bits()
349 {
350         box_unsigned_4(float_bits(untag_float_check(dpop())));
351 }
352
353 void factor_vm::primitive_bits_float()
354 {
355         box_float(bits_float(to_cell(dpop())));
356 }
357
358 void factor_vm::primitive_double_bits()
359 {
360         box_unsigned_8(double_bits(untag_float_check(dpop())));
361 }
362
363 void factor_vm::primitive_bits_double()
364 {
365         box_double(bits_double(to_unsigned_8(dpop())));
366 }
367
368 fixnum factor_vm::to_fixnum(cell tagged)
369 {
370         switch(TAG(tagged))
371         {
372         case FIXNUM_TYPE:
373                 return untag_fixnum(tagged);
374         case BIGNUM_TYPE:
375                 return bignum_to_fixnum(untag<bignum>(tagged));
376         default:
377                 type_error(FIXNUM_TYPE,tagged);
378                 return 0; /* can't happen */
379         }
380 }
381
382 VM_C_API fixnum to_fixnum(cell tagged, factor_vm *parent)
383 {
384         return parent->to_fixnum(tagged);
385 }
386
387 cell factor_vm::to_cell(cell tagged)
388 {
389         return (cell)to_fixnum(tagged);
390 }
391
392 VM_C_API cell to_cell(cell tagged, factor_vm *parent)
393 {
394         return parent->to_cell(tagged);
395 }
396
397 void factor_vm::box_signed_1(s8 n)
398 {
399         dpush(tag_fixnum(n));
400 }
401
402 VM_C_API void box_signed_1(s8 n, factor_vm *parent)
403 {
404         return parent->box_signed_1(n);
405 }
406
407 void factor_vm::box_unsigned_1(u8 n)
408 {
409         dpush(tag_fixnum(n));
410 }
411
412 VM_C_API void box_unsigned_1(u8 n, factor_vm *parent)
413 {
414         return parent->box_unsigned_1(n);
415 }
416
417 void factor_vm::box_signed_2(s16 n)
418 {
419         dpush(tag_fixnum(n));
420 }
421
422 VM_C_API void box_signed_2(s16 n, factor_vm *parent)
423 {
424         return parent->box_signed_2(n);
425 }
426
427 void factor_vm::box_unsigned_2(u16 n)
428 {
429         dpush(tag_fixnum(n));
430 }
431
432 VM_C_API void box_unsigned_2(u16 n, factor_vm *parent)
433 {
434         return parent->box_unsigned_2(n);
435 }
436
437 void factor_vm::box_signed_4(s32 n)
438 {
439         dpush(allot_integer(n));
440 }
441
442 VM_C_API void box_signed_4(s32 n, factor_vm *parent)
443 {
444         return parent->box_signed_4(n);
445 }
446
447 void factor_vm::box_unsigned_4(u32 n)
448 {
449         dpush(allot_cell(n));
450 }
451
452 VM_C_API void box_unsigned_4(u32 n, factor_vm *parent)
453 {
454         return parent->box_unsigned_4(n);
455 }
456
457 void factor_vm::box_signed_cell(fixnum integer)
458 {
459         dpush(allot_integer(integer));
460 }
461
462 VM_C_API void box_signed_cell(fixnum integer, factor_vm *parent)
463 {
464         return parent->box_signed_cell(integer);
465 }
466
467 void factor_vm::box_unsigned_cell(cell cell)
468 {
469         dpush(allot_cell(cell));
470 }
471
472 VM_C_API void box_unsigned_cell(cell cell, factor_vm *parent)
473 {
474         return parent->box_unsigned_cell(cell);
475 }
476
477 void factor_vm::box_signed_8(s64 n)
478 {
479         if(n < fixnum_min || n > fixnum_max)
480                 dpush(tag<bignum>(long_long_to_bignum(n)));
481         else
482                 dpush(tag_fixnum(n));
483 }
484
485 VM_C_API void box_signed_8(s64 n, factor_vm *parent)
486 {
487         return parent->box_signed_8(n);
488 }
489
490 s64 factor_vm::to_signed_8(cell obj)
491 {
492         switch(tagged<object>(obj).type())
493         {
494         case FIXNUM_TYPE:
495                 return untag_fixnum(obj);
496         case BIGNUM_TYPE:
497                 return bignum_to_long_long(untag<bignum>(obj));
498         default:
499                 type_error(BIGNUM_TYPE,obj);
500                 return 0;
501         }
502 }
503
504 VM_C_API s64 to_signed_8(cell obj, factor_vm *parent)
505 {
506         return parent->to_signed_8(obj);
507 }
508
509 void factor_vm::box_unsigned_8(u64 n)
510 {
511         if(n > (u64)fixnum_max)
512                 dpush(tag<bignum>(ulong_long_to_bignum(n)));
513         else
514                 dpush(tag_fixnum(n));
515 }
516
517 VM_C_API void box_unsigned_8(u64 n, factor_vm *parent)
518 {
519         return parent->box_unsigned_8(n);
520 }
521
522 u64 factor_vm::to_unsigned_8(cell obj)
523 {
524         switch(tagged<object>(obj).type())
525         {
526         case FIXNUM_TYPE:
527                 return untag_fixnum(obj);
528         case BIGNUM_TYPE:
529                 return bignum_to_ulong_long(untag<bignum>(obj));
530         default:
531                 type_error(BIGNUM_TYPE,obj);
532                 return 0;
533         }
534 }
535
536 VM_C_API u64 to_unsigned_8(cell obj, factor_vm *parent)
537 {
538         return parent->to_unsigned_8(obj);
539 }
540  
541 void factor_vm::box_float(float flo)
542 {
543         dpush(allot_float(flo));
544 }
545
546 VM_C_API void box_float(float flo, factor_vm *parent)
547 {
548         return parent->box_float(flo);
549 }
550
551 float factor_vm::to_float(cell value)
552 {
553         return untag_float_check(value);
554 }
555
556 VM_C_API float to_float(cell value, factor_vm *parent)
557 {
558         return parent->to_float(value);
559 }
560
561 void factor_vm::box_double(double flo)
562 {
563         dpush(allot_float(flo));
564 }
565
566 VM_C_API void box_double(double flo, factor_vm *parent)
567 {
568         return parent->box_double(flo);
569 }
570
571 double factor_vm::to_double(cell value)
572 {
573         return untag_float_check(value);
574 }
575
576 VM_C_API double to_double(cell value, factor_vm *parent)
577 {
578         return parent->to_double(value);
579 }
580
581 /* The fixnum+, fixnum- and fixnum* primitives are defined in cpu_*.S. On
582 overflow, they call these functions. */
583 inline void factor_vm::overflow_fixnum_add(fixnum x, fixnum y)
584 {
585         drepl(tag<bignum>(fixnum_to_bignum(
586                 untag_fixnum(x) + untag_fixnum(y))));
587 }
588
589 VM_ASM_API void overflow_fixnum_add(fixnum x, fixnum y, factor_vm *parent)
590 {
591         parent->overflow_fixnum_add(x,y);
592 }
593
594 inline void factor_vm::overflow_fixnum_subtract(fixnum x, fixnum y)
595 {
596         drepl(tag<bignum>(fixnum_to_bignum(
597                 untag_fixnum(x) - untag_fixnum(y))));
598 }
599
600 VM_ASM_API void overflow_fixnum_subtract(fixnum x, fixnum y, factor_vm *parent)
601 {
602         parent->overflow_fixnum_subtract(x,y);
603 }
604
605 inline void factor_vm::overflow_fixnum_multiply(fixnum x, fixnum y)
606 {
607         bignum *bx = fixnum_to_bignum(x);
608         GC_BIGNUM(bx);
609         bignum *by = fixnum_to_bignum(y);
610         GC_BIGNUM(by);
611         drepl(tag<bignum>(bignum_multiply(bx,by)));
612 }
613
614 VM_ASM_API void overflow_fixnum_multiply(fixnum x, fixnum y, factor_vm *parent)
615 {
616         parent->overflow_fixnum_multiply(x,y);
617 }
618
619 }