]> gitweb.factorcode.org Git - factor.git/blob - vm/math.cpp
vm: simplify a bit the fixnum_divmod code.
[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->pop());
110
111 void factor_vm::primitive_bignum_eq()
112 {
113         POP_BIGNUMS(x,y);
114         ctx->push(tag_boolean(bignum_equal_p(x,y)));
115 }
116
117 void factor_vm::primitive_bignum_add()
118 {
119         POP_BIGNUMS(x,y);
120         ctx->push(tag<bignum>(bignum_add(x,y)));
121 }
122
123 void factor_vm::primitive_bignum_subtract()
124 {
125         POP_BIGNUMS(x,y);
126         ctx->push(tag<bignum>(bignum_subtract(x,y)));
127 }
128
129 void factor_vm::primitive_bignum_multiply()
130 {
131         POP_BIGNUMS(x,y);
132         ctx->push(tag<bignum>(bignum_multiply(x,y)));
133 }
134
135 void factor_vm::primitive_bignum_divint()
136 {
137         POP_BIGNUMS(x,y);
138         ctx->push(tag<bignum>(bignum_quotient(x,y)));
139 }
140
141 void factor_vm::primitive_bignum_divmod()
142 {
143         bignum *q, *r;
144         POP_BIGNUMS(x,y);
145         bignum_divide(x,y,&q,&r);
146         ctx->push(tag<bignum>(q));
147         ctx->push(tag<bignum>(r));
148 }
149
150 void factor_vm::primitive_bignum_mod()
151 {
152         POP_BIGNUMS(x,y);
153         ctx->push(tag<bignum>(bignum_remainder(x,y)));
154 }
155
156 void factor_vm::primitive_bignum_gcd()
157 {
158         POP_BIGNUMS(x,y);
159         ctx->push(tag<bignum>(bignum_gcd(x,y)));
160 }
161
162 void factor_vm::primitive_bignum_and()
163 {
164         POP_BIGNUMS(x,y);
165         ctx->push(tag<bignum>(bignum_bitwise_and(x,y)));
166 }
167
168 void factor_vm::primitive_bignum_or()
169 {
170         POP_BIGNUMS(x,y);
171         ctx->push(tag<bignum>(bignum_bitwise_ior(x,y)));
172 }
173
174 void factor_vm::primitive_bignum_xor()
175 {
176         POP_BIGNUMS(x,y);
177         ctx->push(tag<bignum>(bignum_bitwise_xor(x,y)));
178 }
179
180 void factor_vm::primitive_bignum_shift()
181 {
182         fixnum y = untag_fixnum(ctx->pop());
183         bignum* x = untag<bignum>(ctx->pop());
184         ctx->push(tag<bignum>(bignum_arithmetic_shift(x,y)));
185 }
186
187 void factor_vm::primitive_bignum_less()
188 {
189         POP_BIGNUMS(x,y);
190         ctx->push(tag_boolean(bignum_compare(x,y) == bignum_comparison_less));
191 }
192
193 void factor_vm::primitive_bignum_lesseq()
194 {
195         POP_BIGNUMS(x,y);
196         ctx->push(tag_boolean(bignum_compare(x,y) != bignum_comparison_greater));
197 }
198
199 void factor_vm::primitive_bignum_greater()
200 {
201         POP_BIGNUMS(x,y);
202         ctx->push(tag_boolean(bignum_compare(x,y) == bignum_comparison_greater));
203 }
204
205 void factor_vm::primitive_bignum_greatereq()
206 {
207         POP_BIGNUMS(x,y);
208         ctx->push(tag_boolean(bignum_compare(x,y) != bignum_comparison_less));
209 }
210
211 void factor_vm::primitive_bignum_not()
212 {
213         ctx->replace(tag<bignum>(bignum_bitwise_not(untag<bignum>(ctx->peek()))));
214 }
215
216 void factor_vm::primitive_bignum_bitp()
217 {
218         int bit = (int)to_fixnum(ctx->pop());
219         bignum *x = untag<bignum>(ctx->pop());
220         ctx->push(tag_boolean(bignum_logbitp(bit,x)));
221 }
222
223 void factor_vm::primitive_bignum_log2()
224 {
225         ctx->replace(tag<bignum>(bignum_integer_length(untag<bignum>(ctx->peek()))));
226 }
227
228 /* allocates memory */
229 cell factor_vm::unbox_array_size_slow()
230 {
231         if(tagged<object>(ctx->peek()).type() == BIGNUM_TYPE)
232         {
233                 bignum *zero = untag<bignum>(bignum_zero);
234                 GC_BIGNUM(zero);
235                 bignum *max = cell_to_bignum(array_size_max);
236                 bignum *n = untag<bignum>(ctx->peek());
237                 if(bignum_compare(n,zero) != bignum_comparison_less
238                         && bignum_compare(n,max) == bignum_comparison_less)
239                 {
240                         ctx->pop();
241                         return bignum_to_cell(n);
242                 }
243         }
244
245         general_error(ERROR_ARRAY_SIZE,ctx->pop(),tag_fixnum(array_size_max));
246         return 0; /* can't happen */
247 }
248
249 void factor_vm::primitive_fixnum_to_float()
250 {
251         ctx->replace(allot_float(fixnum_to_float(ctx->peek())));
252 }
253
254 void factor_vm::primitive_format_float()
255 {
256         byte_array *array = allot_byte_array(100);
257         char *format = alien_offset(ctx->pop());
258         double value = untag_float_check(ctx->pop());
259         SNPRINTF(array->data<char>(),99,format,value);
260         ctx->push(tag<byte_array>(array));
261 }
262
263 #define POP_FLOATS(x,y) \
264         double y = untag_float(ctx->pop()); \
265         double x = untag_float(ctx->pop());
266
267 void factor_vm::primitive_float_eq()
268 {
269         POP_FLOATS(x,y);
270         ctx->push(tag_boolean(x == y));
271 }
272
273 void factor_vm::primitive_float_add()
274 {
275         POP_FLOATS(x,y);
276         ctx->push(allot_float(x + y));
277 }
278
279 void factor_vm::primitive_float_subtract()
280 {
281         POP_FLOATS(x,y);
282         ctx->push(allot_float(x - y));
283 }
284
285 void factor_vm::primitive_float_multiply()
286 {
287         POP_FLOATS(x,y);
288         ctx->push(allot_float(x * y));
289 }
290
291 void factor_vm::primitive_float_divfloat()
292 {
293         POP_FLOATS(x,y);
294         ctx->push(allot_float(x / y));
295 }
296
297 void factor_vm::primitive_float_less()
298 {
299         POP_FLOATS(x,y);
300         ctx->push(tag_boolean(x < y));
301 }
302
303 void factor_vm::primitive_float_lesseq()
304 {
305         POP_FLOATS(x,y);
306         ctx->push(tag_boolean(x <= y));
307 }
308
309 void factor_vm::primitive_float_greater()
310 {
311         POP_FLOATS(x,y);
312         ctx->push(tag_boolean(x > y));
313 }
314
315 void factor_vm::primitive_float_greatereq()
316 {
317         POP_FLOATS(x,y);
318         ctx->push(tag_boolean(x >= y));
319 }
320
321 /* Allocates memory */
322 void factor_vm::primitive_float_bits()
323 {
324         ctx->push(from_unsigned_cell(float_bits((float)untag_float_check(ctx->pop()))));
325 }
326
327 /* Allocates memory */
328 void factor_vm::primitive_bits_float()
329 {
330         ctx->push(allot_float(bits_float((u32)to_cell(ctx->pop()))));
331 }
332
333 void factor_vm::primitive_double_bits()
334 {
335         ctx->push(from_unsigned_8(double_bits(untag_float_check(ctx->pop()))));
336 }
337
338 void factor_vm::primitive_bits_double()
339 {
340         ctx->push(allot_float(bits_double(to_unsigned_8(ctx->pop()))));
341 }
342
343 /* Cannot allocate */
344 fixnum factor_vm::to_fixnum(cell tagged)
345 {
346         switch(TAG(tagged))
347         {
348         case FIXNUM_TYPE:
349                 return untag_fixnum(tagged);
350         case BIGNUM_TYPE:
351                 return bignum_to_fixnum(untag<bignum>(tagged));
352         default:
353                 type_error(FIXNUM_TYPE,tagged);
354                 return 0; /* can't happen */
355         }
356 }
357
358 VM_C_API fixnum to_fixnum(cell tagged, factor_vm *parent)
359 {
360         return parent->to_fixnum(tagged);
361 }
362
363 cell factor_vm::to_cell(cell tagged)
364 {
365         return (cell)to_fixnum(tagged);
366 }
367
368 VM_C_API cell to_cell(cell tagged, factor_vm *parent)
369 {
370         return parent->to_cell(tagged);
371 }
372
373 /* Allocates memory */
374 VM_C_API cell from_signed_cell(fixnum integer, factor_vm *parent)
375 {
376         return parent->from_signed_cell(integer);
377 }
378
379 /* Allocates memory */
380 VM_C_API cell from_unsigned_cell(cell integer, factor_vm *parent)
381 {
382         return parent->from_unsigned_cell(integer);
383 }
384
385 /* Allocates memory */
386 cell factor_vm::from_signed_8(s64 n)
387 {
388         if(n < fixnum_min || n > fixnum_max)
389                 return tag<bignum>(long_long_to_bignum(n));
390         else
391                 return tag_fixnum((fixnum)n);
392 }
393
394 VM_C_API cell from_signed_8(s64 n, factor_vm *parent)
395 {
396         return parent->from_signed_8(n);
397 }
398
399 /* Cannot allocate */
400 s64 factor_vm::to_signed_8(cell obj)
401 {
402         switch(tagged<object>(obj).type())
403         {
404         case FIXNUM_TYPE:
405                 return untag_fixnum(obj);
406         case BIGNUM_TYPE:
407                 return bignum_to_long_long(untag<bignum>(obj));
408         default:
409                 type_error(BIGNUM_TYPE,obj);
410                 return 0;
411         }
412 }
413
414 VM_C_API s64 to_signed_8(cell obj, factor_vm *parent)
415 {
416         return parent->to_signed_8(obj);
417 }
418
419 cell factor_vm::from_unsigned_8(u64 n)
420 {
421         if(n > (u64)fixnum_max)
422                 return tag<bignum>(ulong_long_to_bignum(n));
423         else
424                 return tag_fixnum((fixnum)n);
425 }
426
427 VM_C_API cell from_unsigned_8(u64 n, factor_vm *parent)
428 {
429         return parent->from_unsigned_8(n);
430 }
431
432 /* Cannot allocate */
433 u64 factor_vm::to_unsigned_8(cell obj)
434 {
435         switch(tagged<object>(obj).type())
436         {
437         case FIXNUM_TYPE:
438                 return untag_fixnum(obj);
439         case BIGNUM_TYPE:
440                 return bignum_to_ulong_long(untag<bignum>(obj));
441         default:
442                 type_error(BIGNUM_TYPE,obj);
443                 return 0;
444         }
445 }
446
447 VM_C_API u64 to_unsigned_8(cell obj, factor_vm *parent)
448 {
449         return parent->to_unsigned_8(obj);
450 }
451  
452 /* Cannot allocate */
453 float factor_vm::to_float(cell value)
454 {
455         return (float)untag_float_check(value);
456 }
457
458 /* Cannot allocate */
459 double factor_vm::to_double(cell value)
460 {
461         return untag_float_check(value);
462 }
463
464 /* The fixnum+, fixnum- and fixnum* primitives are defined in cpu_*.S. On
465 overflow, they call these functions. */
466 inline void factor_vm::overflow_fixnum_add(fixnum x, fixnum y)
467 {
468         ctx->replace(tag<bignum>(fixnum_to_bignum(
469                 untag_fixnum(x) + untag_fixnum(y))));
470 }
471
472 VM_C_API void overflow_fixnum_add(fixnum x, fixnum y, factor_vm *parent)
473 {
474         parent->overflow_fixnum_add(x,y);
475 }
476
477 inline void factor_vm::overflow_fixnum_subtract(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_subtract(fixnum x, fixnum y, factor_vm *parent)
484 {
485         parent->overflow_fixnum_subtract(x,y);
486 }
487
488 inline void factor_vm::overflow_fixnum_multiply(fixnum x, fixnum y)
489 {
490         bignum *bx = fixnum_to_bignum(x);
491         GC_BIGNUM(bx);
492         bignum *by = fixnum_to_bignum(y);
493         GC_BIGNUM(by);
494         ctx->replace(tag<bignum>(bignum_multiply(bx,by)));
495 }
496
497 VM_C_API void overflow_fixnum_multiply(fixnum x, fixnum y, factor_vm *parent)
498 {
499         parent->overflow_fixnum_multiply(x,y);
500 }
501
502 }