]> 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         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 /* 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(ctx->pop()); \
21         fixnum x = untag_fixnum(ctx->peek());
22         fixnum result = x / y;
23         if(result == -fixnum_min)
24                 ctx->replace(allot_integer(-fixnum_min));
25         else
26                 ctx->replace(tag_fixnum(result));
27 }
28
29 void factor_vm::primitive_fixnum_divmod()
30 {
31         cell y = ((cell *)ctx->datastack)[0];
32         cell x = ((cell *)ctx->datastack)[-1];
33         if(y == tag_fixnum(-1) && x == tag_fixnum(fixnum_min))
34         {
35                 ((cell *)ctx->datastack)[-1] = allot_integer(-fixnum_min);
36                 ((cell *)ctx->datastack)[0] = tag_fixnum(0);
37         }
38         else
39         {
40                 ((cell *)ctx->datastack)[-1] = tag_fixnum(untag_fixnum(x) / untag_fixnum(y));
41                 ((cell *)ctx->datastack)[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(ctx->pop());
67         fixnum x = untag_fixnum(ctx->peek());
68
69         if(x == 0)
70                 return;
71         else if(y < 0)
72         {
73                 y = branchless_max(y,-WORD_SIZE + 1);
74                 ctx->replace(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                         ctx->replace(tag_fixnum(x << y));
83                         return;
84                 }
85         }
86
87         ctx->replace(tag<bignum>(bignum_arithmetic_shift(
88                 fixnum_to_bignum(x),y)));
89 }
90
91 void factor_vm::primitive_fixnum_to_bignum()
92 {
93         ctx->replace(tag<bignum>(fixnum_to_bignum(untag_fixnum(ctx->peek()))));
94 }
95
96 void factor_vm::primitive_float_to_bignum()
97 {
98         ctx->replace(tag<bignum>(float_to_bignum(ctx->peek())));
99 }
100
101 #define POP_BIGNUMS(x,y) \
102         bignum * y = untag<bignum>(ctx->pop()); \
103         bignum * x = untag<bignum>(ctx->pop());
104
105 void factor_vm::primitive_bignum_eq()
106 {
107         POP_BIGNUMS(x,y);
108         ctx->push(tag_boolean(bignum_equal_p(x,y)));
109 }
110
111 void factor_vm::primitive_bignum_add()
112 {
113         POP_BIGNUMS(x,y);
114         ctx->push(tag<bignum>(bignum_add(x,y)));
115 }
116
117 void factor_vm::primitive_bignum_subtract()
118 {
119         POP_BIGNUMS(x,y);
120         ctx->push(tag<bignum>(bignum_subtract(x,y)));
121 }
122
123 void factor_vm::primitive_bignum_multiply()
124 {
125         POP_BIGNUMS(x,y);
126         ctx->push(tag<bignum>(bignum_multiply(x,y)));
127 }
128
129 void factor_vm::primitive_bignum_divint()
130 {
131         POP_BIGNUMS(x,y);
132         ctx->push(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         ctx->push(tag<bignum>(q));
141         ctx->push(tag<bignum>(r));
142 }
143
144 void factor_vm::primitive_bignum_mod()
145 {
146         POP_BIGNUMS(x,y);
147         ctx->push(tag<bignum>(bignum_remainder(x,y)));
148 }
149
150 void factor_vm::primitive_bignum_and()
151 {
152         POP_BIGNUMS(x,y);
153         ctx->push(tag<bignum>(bignum_bitwise_and(x,y)));
154 }
155
156 void factor_vm::primitive_bignum_or()
157 {
158         POP_BIGNUMS(x,y);
159         ctx->push(tag<bignum>(bignum_bitwise_ior(x,y)));
160 }
161
162 void factor_vm::primitive_bignum_xor()
163 {
164         POP_BIGNUMS(x,y);
165         ctx->push(tag<bignum>(bignum_bitwise_xor(x,y)));
166 }
167
168 void factor_vm::primitive_bignum_shift()
169 {
170         fixnum y = untag_fixnum(ctx->pop());
171         bignum* x = untag<bignum>(ctx->pop());
172         ctx->push(tag<bignum>(bignum_arithmetic_shift(x,y)));
173 }
174
175 void factor_vm::primitive_bignum_less()
176 {
177         POP_BIGNUMS(x,y);
178         ctx->push(tag_boolean(bignum_compare(x,y) == bignum_comparison_less));
179 }
180
181 void factor_vm::primitive_bignum_lesseq()
182 {
183         POP_BIGNUMS(x,y);
184         ctx->push(tag_boolean(bignum_compare(x,y) != bignum_comparison_greater));
185 }
186
187 void factor_vm::primitive_bignum_greater()
188 {
189         POP_BIGNUMS(x,y);
190         ctx->push(tag_boolean(bignum_compare(x,y) == bignum_comparison_greater));
191 }
192
193 void factor_vm::primitive_bignum_greatereq()
194 {
195         POP_BIGNUMS(x,y);
196         ctx->push(tag_boolean(bignum_compare(x,y) != bignum_comparison_less));
197 }
198
199 void factor_vm::primitive_bignum_not()
200 {
201         ctx->replace(tag<bignum>(bignum_bitwise_not(untag<bignum>(ctx->peek()))));
202 }
203
204 void factor_vm::primitive_bignum_bitp()
205 {
206         int bit = (int)to_fixnum(ctx->pop());
207         bignum *x = untag<bignum>(ctx->pop());
208         ctx->push(tag_boolean(bignum_logbitp(bit,x)));
209 }
210
211 void factor_vm::primitive_bignum_log2()
212 {
213         ctx->replace(tag<bignum>(bignum_integer_length(untag<bignum>(ctx->peek()))));
214 }
215
216 unsigned int factor_vm::bignum_producer(unsigned int digit)
217 {
218         unsigned char *ptr = (unsigned char *)alien_offset(ctx->peek());
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         unsigned int n_digits = (unsigned int)array_capacity(untag_check<byte_array>(ctx->peek()));
230         bignum * result = digit_stream_to_bignum(n_digits,factor::bignum_producer,0x100,0);
231         ctx->replace(tag<bignum>(result));
232 }
233
234 cell factor_vm::unbox_array_size_slow()
235 {
236         if(tagged<object>(ctx->peek()).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>(ctx->peek());
241                 if(bignum_compare(n,zero) != bignum_comparison_less
242                         && bignum_compare(n,max) == bignum_comparison_less)
243                 {
244                         ctx->pop();
245                         return bignum_to_cell(n);
246                 }
247         }
248
249         general_error(ERROR_ARRAY_SIZE,ctx->pop(),tag_fixnum(array_size_max));
250         return 0; /* can't happen */
251 }
252
253 void factor_vm::primitive_fixnum_to_float()
254 {
255         ctx->replace(allot_float(fixnum_to_float(ctx->peek())));
256 }
257
258 void factor_vm::primitive_bignum_to_float()
259 {
260         ctx->replace(allot_float(bignum_to_float(ctx->peek())));
261 }
262
263 void factor_vm::primitive_format_float()
264 {
265         byte_array *array = allot_byte_array(100);
266         char *format = alien_offset(ctx->pop());
267         double value = untag_float_check(ctx->pop());
268         SNPRINTF(array->data<char>(),99,format,value);
269         ctx->push(tag<byte_array>(array));
270 }
271
272 #define POP_FLOATS(x,y) \
273         double y = untag_float(ctx->pop()); \
274         double x = untag_float(ctx->pop());
275
276 void factor_vm::primitive_float_eq()
277 {
278         POP_FLOATS(x,y);
279         ctx->push(tag_boolean(x == y));
280 }
281
282 void factor_vm::primitive_float_add()
283 {
284         POP_FLOATS(x,y);
285         ctx->push(allot_float(x + y));
286 }
287
288 void factor_vm::primitive_float_subtract()
289 {
290         POP_FLOATS(x,y);
291         ctx->push(allot_float(x - y));
292 }
293
294 void factor_vm::primitive_float_multiply()
295 {
296         POP_FLOATS(x,y);
297         ctx->push(allot_float(x * y));
298 }
299
300 void factor_vm::primitive_float_divfloat()
301 {
302         POP_FLOATS(x,y);
303         ctx->push(allot_float(x / y));
304 }
305
306 void factor_vm::primitive_float_mod()
307 {
308         POP_FLOATS(x,y);
309         ctx->push(allot_float(fmod(x,y)));
310 }
311
312 void factor_vm::primitive_float_less()
313 {
314         POP_FLOATS(x,y);
315         ctx->push(tag_boolean(x < y));
316 }
317
318 void factor_vm::primitive_float_lesseq()
319 {
320         POP_FLOATS(x,y);
321         ctx->push(tag_boolean(x <= y));
322 }
323
324 void factor_vm::primitive_float_greater()
325 {
326         POP_FLOATS(x,y);
327         ctx->push(tag_boolean(x > y));
328 }
329
330 void factor_vm::primitive_float_greatereq()
331 {
332         POP_FLOATS(x,y);
333         ctx->push(tag_boolean(x >= y));
334 }
335
336 void factor_vm::primitive_float_bits()
337 {
338         ctx->push(from_unsigned_4(float_bits((float)untag_float_check(ctx->pop()))));
339 }
340
341 void factor_vm::primitive_bits_float()
342 {
343         ctx->push(allot_float(bits_float((u32)to_cell(ctx->pop()))));
344 }
345
346 void factor_vm::primitive_double_bits()
347 {
348         ctx->push(from_unsigned_8(double_bits(untag_float_check(ctx->pop()))));
349 }
350
351 void factor_vm::primitive_bits_double()
352 {
353         ctx->push(allot_float(bits_double(to_unsigned_8(ctx->pop()))));
354 }
355
356 /* Cannot allocate */
357 fixnum factor_vm::to_fixnum(cell tagged)
358 {
359         switch(TAG(tagged))
360         {
361         case FIXNUM_TYPE:
362                 return untag_fixnum(tagged);
363         case BIGNUM_TYPE:
364                 return bignum_to_fixnum(untag<bignum>(tagged));
365         default:
366                 type_error(FIXNUM_TYPE,tagged);
367                 return 0; /* can't happen */
368         }
369 }
370
371 VM_C_API fixnum to_fixnum(cell tagged, factor_vm *parent)
372 {
373         return parent->to_fixnum(tagged);
374 }
375
376 cell factor_vm::to_cell(cell tagged)
377 {
378         return (cell)to_fixnum(tagged);
379 }
380
381 VM_C_API cell to_cell(cell tagged, factor_vm *parent)
382 {
383         return parent->to_cell(tagged);
384 }
385
386 cell factor_vm::from_signed_1(s8 n)
387 {
388         return tag_fixnum(n);
389 }
390
391 VM_C_API cell from_signed_1(s8 n, factor_vm *parent)
392 {
393         return parent->from_signed_1(n);
394 }
395
396 cell factor_vm::from_unsigned_1(u8 n)
397 {
398         return tag_fixnum(n);
399 }
400
401 VM_C_API cell from_unsigned_1(u8 n, factor_vm *parent)
402 {
403         return parent->from_unsigned_1(n);
404 }
405
406 cell factor_vm::from_signed_2(s16 n)
407 {
408         return tag_fixnum(n);
409 }
410
411 VM_C_API cell from_signed_2(s16 n, factor_vm *parent)
412 {
413         return parent->from_signed_2(n);
414 }
415
416 cell factor_vm::from_unsigned_2(u16 n)
417 {
418         return tag_fixnum(n);
419 }
420
421 VM_C_API cell from_unsigned_2(u16 n, factor_vm *parent)
422 {
423         return parent->from_unsigned_2(n);
424 }
425
426 cell factor_vm::from_signed_4(s32 n)
427 {
428         return allot_integer(n);
429 }
430
431 VM_C_API cell from_signed_4(s32 n, factor_vm *parent)
432 {
433         return parent->from_signed_4(n);
434 }
435
436 cell factor_vm::from_unsigned_4(u32 n)
437 {
438         return allot_cell(n);
439 }
440
441 VM_C_API cell from_unsigned_4(u32 n, factor_vm *parent)
442 {
443         return parent->from_unsigned_4(n);
444 }
445
446 cell factor_vm::from_signed_cell(fixnum integer)
447 {
448         return allot_integer(integer);
449 }
450
451 cell factor_vm::from_unsigned_cell(cell integer)
452 {
453         return allot_cell(integer);
454 }
455
456 VM_C_API cell from_signed_cell(fixnum integer, factor_vm *parent)
457 {
458         return parent->from_signed_cell(integer);
459 }
460
461 VM_C_API cell from_unsigned_cell(cell integer, factor_vm *parent)
462 {
463         return parent->from_unsigned_cell(integer);
464 }
465
466 cell factor_vm::from_signed_8(s64 n)
467 {
468         if(n < fixnum_min || n > fixnum_max)
469                 return tag<bignum>(long_long_to_bignum(n));
470         else
471                 return tag_fixnum((fixnum)n);
472 }
473
474 VM_C_API cell from_signed_8(s64 n, factor_vm *parent)
475 {
476         return parent->from_signed_8(n);
477 }
478
479 /* Cannot allocate */
480 s64 factor_vm::to_signed_8(cell obj)
481 {
482         switch(tagged<object>(obj).type())
483         {
484         case FIXNUM_TYPE:
485                 return untag_fixnum(obj);
486         case BIGNUM_TYPE:
487                 return bignum_to_long_long(untag<bignum>(obj));
488         default:
489                 type_error(BIGNUM_TYPE,obj);
490                 return 0;
491         }
492 }
493
494 VM_C_API s64 to_signed_8(cell obj, factor_vm *parent)
495 {
496         return parent->to_signed_8(obj);
497 }
498
499 cell factor_vm::from_unsigned_8(u64 n)
500 {
501         if(n > (u64)fixnum_max)
502                 return tag<bignum>(ulong_long_to_bignum(n));
503         else
504                 return tag_fixnum((fixnum)n);
505 }
506
507 VM_C_API cell from_unsigned_8(u64 n, factor_vm *parent)
508 {
509         return parent->from_unsigned_8(n);
510 }
511
512 /* Cannot allocate */
513 u64 factor_vm::to_unsigned_8(cell obj)
514 {
515         switch(tagged<object>(obj).type())
516         {
517         case FIXNUM_TYPE:
518                 return untag_fixnum(obj);
519         case BIGNUM_TYPE:
520                 return bignum_to_ulong_long(untag<bignum>(obj));
521         default:
522                 type_error(BIGNUM_TYPE,obj);
523                 return 0;
524         }
525 }
526
527 VM_C_API u64 to_unsigned_8(cell obj, factor_vm *parent)
528 {
529         return parent->to_unsigned_8(obj);
530 }
531  
532 VM_C_API cell from_float(float flo, factor_vm *parent)
533 {
534         return parent->allot_float(flo);
535 }
536
537 /* Cannot allocate */
538 float factor_vm::to_float(cell value)
539 {
540         return (float)untag_float_check(value);
541 }
542
543 VM_C_API float to_float(cell value, factor_vm *parent)
544 {
545         return parent->to_float(value);
546 }
547
548 VM_C_API cell from_double(double flo, factor_vm *parent)
549 {
550         return parent->allot_float(flo);
551 }
552
553 /* Cannot allocate */
554 double factor_vm::to_double(cell value)
555 {
556         return untag_float_check(value);
557 }
558
559 VM_C_API double to_double(cell value, factor_vm *parent)
560 {
561         return parent->to_double(value);
562 }
563
564 /* The fixnum+, fixnum- and fixnum* primitives are defined in cpu_*.S. On
565 overflow, they call these functions. */
566 inline void factor_vm::overflow_fixnum_add(fixnum x, fixnum y)
567 {
568         ctx->replace(tag<bignum>(fixnum_to_bignum(
569                 untag_fixnum(x) + untag_fixnum(y))));
570 }
571
572 VM_C_API void overflow_fixnum_add(fixnum x, fixnum y, factor_vm *parent)
573 {
574         parent->overflow_fixnum_add(x,y);
575 }
576
577 inline void factor_vm::overflow_fixnum_subtract(fixnum x, fixnum y)
578 {
579         ctx->replace(tag<bignum>(fixnum_to_bignum(
580                 untag_fixnum(x) - untag_fixnum(y))));
581 }
582
583 VM_C_API void overflow_fixnum_subtract(fixnum x, fixnum y, factor_vm *parent)
584 {
585         parent->overflow_fixnum_subtract(x,y);
586 }
587
588 inline void factor_vm::overflow_fixnum_multiply(fixnum x, fixnum y)
589 {
590         bignum *bx = fixnum_to_bignum(x);
591         GC_BIGNUM(bx);
592         bignum *by = fixnum_to_bignum(y);
593         GC_BIGNUM(by);
594         ctx->replace(tag<bignum>(bignum_multiply(bx,by)));
595 }
596
597 VM_C_API void overflow_fixnum_multiply(fixnum x, fixnum y, factor_vm *parent)
598 {
599         parent->overflow_fixnum_multiply(x,y);
600 }
601
602 }