]> gitweb.factorcode.org Git - factor.git/blob - vm/math.hpp
webapps: better style
[factor.git] / vm / math.hpp
1 namespace factor {
2
3 static const fixnum fixnum_max =
4     (((fixnum)1 << (WORD_SIZE - TAG_BITS - 1)) - 1);
5 static const fixnum fixnum_min = (-((fixnum)1 << (WORD_SIZE - TAG_BITS - 1)));
6 static const fixnum array_size_max = ((cell)1 << (WORD_SIZE - TAG_BITS - 2));
7
8 // Allocates memory
9 inline cell factor_vm::from_signed_cell(fixnum x) {
10   if (x < fixnum_min || x > fixnum_max)
11     return tag<bignum>(fixnum_to_bignum(x));
12   return tag_fixnum(x);
13 }
14
15 // Allocates memory
16 inline cell factor_vm::from_unsigned_cell(cell x) {
17   if (x > (cell)fixnum_max)
18     return tag<bignum>(cell_to_bignum(x));
19   return tag_fixnum(x);
20 }
21
22 // Allocates memory
23 inline cell factor_vm::allot_float(double n) {
24   boxed_float* flo = allot<boxed_float>(sizeof(boxed_float));
25   flo->n = n;
26   return tag(flo);
27 }
28
29 // Allocates memory
30 inline bignum* factor_vm::float_to_bignum(cell tagged) {
31   return double_to_bignum(untag_float(tagged));
32 }
33
34 inline double factor_vm::untag_float(cell tagged) {
35   return untag<boxed_float>(tagged)->n;
36 }
37
38 inline double factor_vm::untag_float_check(cell tagged) {
39   return untag_check<boxed_float>(tagged)->n;
40 }
41
42 inline fixnum factor_vm::float_to_fixnum(cell tagged) {
43   return (fixnum)untag_float(tagged);
44 }
45
46 inline double factor_vm::fixnum_to_float(cell tagged) {
47   return (double)untag_fixnum(tagged);
48 }
49
50 inline cell factor_vm::unbox_array_size() {
51   cell obj = ctx->pop();
52   fixnum n = to_fixnum_strict(obj);
53   if (n >= 0 && n < (fixnum)array_size_max) {
54     return n;
55   }
56   general_error(ERROR_ARRAY_SIZE, obj, tag_fixnum(array_size_max));
57   return 0; // can't happen
58 }
59
60 VM_C_API cell from_signed_cell(fixnum integer, factor_vm* vm);
61 VM_C_API cell from_unsigned_cell(cell integer, factor_vm* vm);
62 VM_C_API cell from_signed_8(int64_t n, factor_vm* vm);
63 VM_C_API cell from_unsigned_8(uint64_t n, factor_vm* vm);
64
65 VM_C_API int64_t to_signed_8(cell obj, factor_vm* parent);
66 VM_C_API uint64_t to_unsigned_8(cell obj, factor_vm* parent);
67
68 VM_C_API fixnum to_fixnum(cell tagged, factor_vm* vm);
69 VM_C_API cell to_cell(cell tagged, factor_vm* vm);
70
71 VM_C_API void overflow_fixnum_add(fixnum x, fixnum y, factor_vm* parent);
72 VM_C_API void overflow_fixnum_subtract(fixnum x, fixnum y, factor_vm* parent);
73 VM_C_API void overflow_fixnum_multiply(fixnum x, fixnum y, factor_vm* parent);
74
75 }