]> gitweb.factorcode.org Git - factor.git/blob - vm/math.cpp
vm: Standardize /* Allocates memory */ comments so you can grep -A1
[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 /* does not allocate, even though from_signed_cell can allocate */
17 /* Division can only overflow when we are dividing the most negative fixnum
18 by -1. */
19 void factor_vm::primitive_fixnum_divint()
20 {
21         fixnum y = untag_fixnum(ctx->pop());
22         fixnum x = untag_fixnum(ctx->peek());
23         fixnum result = x / y;
24         if(result == -fixnum_min)
25                 /* Does not allocate */
26                 ctx->replace(from_signed_cell(-fixnum_min));
27         else
28                 ctx->replace(tag_fixnum(result));
29 }
30
31 /* does not allocate, even though from_signed_cell can allocate */
32 void factor_vm::primitive_fixnum_divmod()
33 {
34         cell *s0 = (cell *)(ctx->datastack);
35         cell *s1 = (cell *)(ctx->datastack - sizeof(cell));
36         fixnum y = untag_fixnum(*s0);
37         fixnum x = untag_fixnum(*s1);
38         if(y == -1 && x == fixnum_min)
39         {
40                 /* Does not allocate */
41                 *s1 = from_signed_cell(-fixnum_min);
42                 *s0 = tag_fixnum(0);
43         }
44         else
45         {
46                 *s1 = tag_fixnum(x / y);
47                 *s0 = tag_fixnum(x % y);
48         }
49 }
50
51 /*
52  * If we're shifting right by n bits, we won't overflow as long as none of the
53  * high WORD_SIZE-TAG_BITS-n bits are set.
54  */
55 inline fixnum factor_vm::sign_mask(fixnum x)
56 {
57         return x >> (WORD_SIZE - 1);
58 }
59
60 inline fixnum factor_vm::branchless_max(fixnum x, fixnum y)
61 {
62         return (x - ((x - y) & sign_mask(x - y)));
63 }
64
65 inline fixnum factor_vm::branchless_abs(fixnum x)
66 {
67         return (x ^ sign_mask(x)) - sign_mask(x);
68 }
69
70 void factor_vm::primitive_fixnum_shift()
71 {
72         fixnum y = untag_fixnum(ctx->pop());
73         fixnum x = untag_fixnum(ctx->peek());
74
75         if(x == 0)
76                 return;
77         else if(y < 0)
78         {
79                 y = branchless_max(y,-WORD_SIZE + 1);
80                 ctx->replace(tag_fixnum(x >> -y));
81                 return;
82         }
83         else if(y < WORD_SIZE - TAG_BITS)
84         {
85                 fixnum mask = -((fixnum)1 << (WORD_SIZE - 1 - TAG_BITS - y));
86                 if(!(branchless_abs(x) & mask))
87                 {
88                         ctx->replace(tag_fixnum(x << y));
89                         return;
90                 }
91         }
92
93         ctx->replace(tag<bignum>(bignum_arithmetic_shift(
94                 fixnum_to_bignum(x),y)));
95 }
96
97 void factor_vm::primitive_fixnum_to_bignum()
98 {
99         ctx->replace(tag<bignum>(fixnum_to_bignum(untag_fixnum(ctx->peek()))));
100 }
101
102 void factor_vm::primitive_float_to_bignum()
103 {
104         ctx->replace(tag<bignum>(float_to_bignum(ctx->peek())));
105 }
106
107 #define POP_BIGNUMS(x,y) \
108         bignum * y = untag<bignum>(ctx->pop()); \
109         bignum * x = untag<bignum>(ctx->peek());
110
111 void factor_vm::primitive_bignum_eq()
112 {
113         POP_BIGNUMS(x,y);
114         ctx->replace(tag_boolean(bignum_equal_p(x,y)));
115 }
116
117 void factor_vm::primitive_bignum_add()
118 {
119         POP_BIGNUMS(x,y);
120         ctx->replace(tag<bignum>(bignum_add(x,y)));
121 }
122
123 void factor_vm::primitive_bignum_subtract()
124 {
125         POP_BIGNUMS(x,y);
126         ctx->replace(tag<bignum>(bignum_subtract(x,y)));
127 }
128
129 void factor_vm::primitive_bignum_multiply()
130 {
131         POP_BIGNUMS(x,y);
132         ctx->replace(tag<bignum>(bignum_multiply(x,y)));
133 }
134
135 void factor_vm::primitive_bignum_divint()
136 {
137         POP_BIGNUMS(x,y);
138         ctx->replace(tag<bignum>(bignum_quotient(x,y)));
139 }
140
141 void factor_vm::primitive_bignum_divmod()
142 {
143         cell *s0 = (cell *)(ctx->datastack);
144         cell *s1 = (cell *)(ctx->datastack - sizeof(cell));
145         bignum *y = untag<bignum>(*s0);
146         bignum *x = untag<bignum>(*s1);
147         bignum *q, *r;
148         bignum_divide(x, y, &q, &r);
149         *s1 = tag<bignum>(q);
150         *s0 = tag<bignum>(r);
151 }
152
153 void factor_vm::primitive_bignum_mod()
154 {
155         POP_BIGNUMS(x,y);
156         ctx->replace(tag<bignum>(bignum_remainder(x,y)));
157 }
158
159 void factor_vm::primitive_bignum_gcd()
160 {
161         POP_BIGNUMS(x,y);
162         ctx->replace(tag<bignum>(bignum_gcd(x,y)));
163 }
164
165 void factor_vm::primitive_bignum_and()
166 {
167         POP_BIGNUMS(x,y);
168         ctx->replace(tag<bignum>(bignum_bitwise_and(x,y)));
169 }
170
171 void factor_vm::primitive_bignum_or()
172 {
173         POP_BIGNUMS(x,y);
174         ctx->replace(tag<bignum>(bignum_bitwise_ior(x,y)));
175 }
176
177 void factor_vm::primitive_bignum_xor()
178 {
179         POP_BIGNUMS(x,y);
180         ctx->replace(tag<bignum>(bignum_bitwise_xor(x,y)));
181 }
182
183 void factor_vm::primitive_bignum_shift()
184 {
185         fixnum y = untag_fixnum(ctx->pop());
186         bignum* x = untag<bignum>(ctx->peek());
187         ctx->replace(tag<bignum>(bignum_arithmetic_shift(x,y)));
188 }
189
190 void factor_vm::primitive_bignum_less()
191 {
192         POP_BIGNUMS(x,y);
193         ctx->replace(tag_boolean(bignum_compare(x,y) == bignum_comparison_less));
194 }
195
196 void factor_vm::primitive_bignum_lesseq()
197 {
198         POP_BIGNUMS(x,y);
199         ctx->replace(tag_boolean(bignum_compare(x,y) != bignum_comparison_greater));
200 }
201
202 void factor_vm::primitive_bignum_greater()
203 {
204         POP_BIGNUMS(x,y);
205         ctx->replace(tag_boolean(bignum_compare(x,y) == bignum_comparison_greater));
206 }
207
208 void factor_vm::primitive_bignum_greatereq()
209 {
210         POP_BIGNUMS(x,y);
211         ctx->replace(tag_boolean(bignum_compare(x,y) != bignum_comparison_less));
212 }
213
214 void factor_vm::primitive_bignum_not()
215 {
216         ctx->replace(tag<bignum>(bignum_bitwise_not(untag<bignum>(ctx->peek()))));
217 }
218
219 void factor_vm::primitive_bignum_bitp()
220 {
221         int bit = (int)to_fixnum(ctx->pop());
222         bignum *x = untag<bignum>(ctx->peek());
223         ctx->replace(tag_boolean(bignum_logbitp(bit,x)));
224 }
225
226 void factor_vm::primitive_bignum_log2()
227 {
228         ctx->replace(tag<bignum>(bignum_integer_length(untag<bignum>(ctx->peek()))));
229 }
230
231 /* Allocates memory */
232 cell factor_vm::unbox_array_size_slow()
233 {
234         if(tagged<object>(ctx->peek()).type() == BIGNUM_TYPE)
235         {
236                 bignum *zero = untag<bignum>(bignum_zero);
237                 GC_BIGNUM(zero);
238                 bignum *max = cell_to_bignum(array_size_max);
239                 bignum *n = untag<bignum>(ctx->peek());
240                 if(bignum_compare(n,zero) != bignum_comparison_less
241                         && bignum_compare(n,max) == bignum_comparison_less)
242                 {
243                         ctx->pop();
244                         return bignum_to_cell(n);
245                 }
246         }
247
248         general_error(ERROR_ARRAY_SIZE,ctx->pop(),tag_fixnum(array_size_max));
249         return 0; /* can't happen */
250 }
251
252 /* Allocates memory */
253 void factor_vm::primitive_fixnum_to_float()
254 {
255         ctx->replace(allot_float(fixnum_to_float(ctx->peek())));
256 }
257
258 /* Allocates memory */
259 void factor_vm::primitive_format_float()
260 {
261         byte_array *array = allot_byte_array(100);
262         char *format = alien_offset(ctx->pop());
263         double value = untag_float_check(ctx->peek());
264         SNPRINTF(array->data<char>(),99,format,value);
265         ctx->replace(tag<byte_array>(array));
266 }
267
268 #define POP_FLOATS(x,y) \
269         double y = untag_float(ctx->pop()); \
270         double x = untag_float(ctx->peek());
271
272 void factor_vm::primitive_float_eq()
273 {
274         POP_FLOATS(x,y);
275         ctx->replace(tag_boolean(x == y));
276 }
277
278 /* Allocates memory */
279 void factor_vm::primitive_float_add()
280 {
281         POP_FLOATS(x,y);
282         ctx->replace(allot_float(x + y));
283 }
284
285 /* Allocates memory */
286 void factor_vm::primitive_float_subtract()
287 {
288         POP_FLOATS(x,y);
289         ctx->replace(allot_float(x - y));
290 }
291
292 /* Allocates memory */
293 void factor_vm::primitive_float_multiply()
294 {
295         POP_FLOATS(x,y);
296         ctx->replace(allot_float(x * y));
297 }
298
299 /* Allocates memory */
300 void factor_vm::primitive_float_divfloat()
301 {
302         POP_FLOATS(x,y);
303         ctx->replace(allot_float(x / y));
304 }
305
306 void factor_vm::primitive_float_less()
307 {
308         POP_FLOATS(x,y);
309         ctx->replace(tag_boolean(x < y));
310 }
311
312 void factor_vm::primitive_float_lesseq()
313 {
314         POP_FLOATS(x,y);
315         ctx->replace(tag_boolean(x <= y));
316 }
317
318 void factor_vm::primitive_float_greater()
319 {
320         POP_FLOATS(x,y);
321         ctx->replace(tag_boolean(x > y));
322 }
323
324 void factor_vm::primitive_float_greatereq()
325 {
326         POP_FLOATS(x,y);
327         ctx->replace(tag_boolean(x >= y));
328 }
329
330 /* Allocates memory */
331 void factor_vm::primitive_float_bits()
332 {
333         ctx->push(from_unsigned_cell(float_bits((float)untag_float_check(ctx->pop()))));
334 }
335
336 /* Allocates memory */
337 void factor_vm::primitive_bits_float()
338 {
339         ctx->push(allot_float(bits_float((u32)to_cell(ctx->pop()))));
340 }
341
342 void factor_vm::primitive_double_bits()
343 {
344         ctx->push(from_unsigned_8(double_bits(untag_float_check(ctx->pop()))));
345 }
346
347 /* Allocates memory */
348 void factor_vm::primitive_bits_double()
349 {
350         ctx->push(allot_float(bits_double(to_unsigned_8(ctx->pop()))));
351 }
352
353 /* Cannot allocate */
354 fixnum factor_vm::to_fixnum(cell tagged)
355 {
356         switch(TAG(tagged))
357         {
358         case FIXNUM_TYPE:
359                 return untag_fixnum(tagged);
360         case BIGNUM_TYPE:
361                 return bignum_to_fixnum(untag<bignum>(tagged));
362         default:
363                 type_error(FIXNUM_TYPE,tagged);
364                 return 0; /* can't happen */
365         }
366 }
367
368 VM_C_API fixnum to_fixnum(cell tagged, factor_vm *parent)
369 {
370         return parent->to_fixnum(tagged);
371 }
372
373 cell factor_vm::to_cell(cell tagged)
374 {
375         return (cell)to_fixnum(tagged);
376 }
377
378 VM_C_API cell to_cell(cell tagged, factor_vm *parent)
379 {
380         return parent->to_cell(tagged);
381 }
382
383 /* Allocates memory */
384 VM_C_API cell from_signed_cell(fixnum integer, factor_vm *parent)
385 {
386         return parent->from_signed_cell(integer);
387 }
388
389 /* Allocates memory */
390 VM_C_API cell from_unsigned_cell(cell integer, factor_vm *parent)
391 {
392         return parent->from_unsigned_cell(integer);
393 }
394
395 /* Allocates memory */
396 cell factor_vm::from_signed_8(s64 n)
397 {
398         if(n < fixnum_min || n > fixnum_max)
399                 return tag<bignum>(long_long_to_bignum(n));
400         else
401                 return tag_fixnum((fixnum)n);
402 }
403
404 VM_C_API cell from_signed_8(s64 n, factor_vm *parent)
405 {
406         return parent->from_signed_8(n);
407 }
408
409 /* Cannot allocate */
410 s64 factor_vm::to_signed_8(cell obj)
411 {
412         switch(tagged<object>(obj).type())
413         {
414         case FIXNUM_TYPE:
415                 return untag_fixnum(obj);
416         case BIGNUM_TYPE:
417                 return bignum_to_long_long(untag<bignum>(obj));
418         default:
419                 type_error(BIGNUM_TYPE,obj);
420                 return 0;
421         }
422 }
423
424 VM_C_API s64 to_signed_8(cell obj, factor_vm *parent)
425 {
426         return parent->to_signed_8(obj);
427 }
428
429 /* Allocates memory */
430 cell factor_vm::from_unsigned_8(u64 n)
431 {
432         if(n > (u64)fixnum_max)
433                 return tag<bignum>(ulong_long_to_bignum(n));
434         else
435                 return tag_fixnum((fixnum)n);
436 }
437
438 VM_C_API cell from_unsigned_8(u64 n, factor_vm *parent)
439 {
440         return parent->from_unsigned_8(n);
441 }
442
443 /* Cannot allocate */
444 u64 factor_vm::to_unsigned_8(cell obj)
445 {
446         switch(tagged<object>(obj).type())
447         {
448         case FIXNUM_TYPE:
449                 return untag_fixnum(obj);
450         case BIGNUM_TYPE:
451                 return bignum_to_ulong_long(untag<bignum>(obj));
452         default:
453                 type_error(BIGNUM_TYPE,obj);
454                 return 0;
455         }
456 }
457
458 VM_C_API u64 to_unsigned_8(cell obj, factor_vm *parent)
459 {
460         return parent->to_unsigned_8(obj);
461 }
462  
463 /* Cannot allocate */
464 float factor_vm::to_float(cell value)
465 {
466         return (float)untag_float_check(value);
467 }
468
469 /* Cannot allocate */
470 double factor_vm::to_double(cell value)
471 {
472         return untag_float_check(value);
473 }
474
475 /* The fixnum+, fixnum- and fixnum* primitives are defined in cpu_*.S. On
476 overflow, they call these functions. */
477 inline void factor_vm::overflow_fixnum_add(fixnum x, fixnum y)
478 {
479         ctx->replace(tag<bignum>(fixnum_to_bignum(
480                 untag_fixnum(x) + untag_fixnum(y))));
481 }
482
483 VM_C_API void overflow_fixnum_add(fixnum x, fixnum y, factor_vm *parent)
484 {
485         parent->overflow_fixnum_add(x,y);
486 }
487
488 inline void factor_vm::overflow_fixnum_subtract(fixnum x, fixnum y)
489 {
490         ctx->replace(tag<bignum>(fixnum_to_bignum(
491                 untag_fixnum(x) - untag_fixnum(y))));
492 }
493
494 VM_C_API void overflow_fixnum_subtract(fixnum x, fixnum y, factor_vm *parent)
495 {
496         parent->overflow_fixnum_subtract(x,y);
497 }
498
499 inline void factor_vm::overflow_fixnum_multiply(fixnum x, fixnum y)
500 {
501         bignum *bx = fixnum_to_bignum(x);
502         GC_BIGNUM(bx);
503         bignum *by = fixnum_to_bignum(y);
504         GC_BIGNUM(by);
505         ctx->replace(tag<bignum>(bignum_multiply(bx,by)));
506 }
507
508 VM_C_API void overflow_fixnum_multiply(fixnum x, fixnum y, factor_vm *parent)
509 {
510         parent->overflow_fixnum_multiply(x,y);
511 }
512
513 }