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