PageRenderTime 63ms CodeModel.GetById 51ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/vm/math.hpp

http://github.com/abeaumont/factor
C++ Header | 91 lines | 71 code | 16 blank | 4 comment | 7 complexity | 814cd4b02992721b228ff28cd1d853ef MD5 | raw file
 1namespace factor
 2{
 3
 4static const fixnum fixnum_max = (((fixnum)1 << (WORD_SIZE - TAG_BITS - 1)) - 1);
 5static const fixnum fixnum_min = (-((fixnum)1 << (WORD_SIZE - TAG_BITS - 1)));
 6static const fixnum array_size_max = ((cell)1 << (WORD_SIZE - TAG_BITS - 2));
 7
 8/* Allocates memory */
 9inline cell factor_vm::from_signed_cell(fixnum x)
10{
11	if(x < fixnum_min || x > fixnum_max)
12		return tag<bignum>(fixnum_to_bignum(x));
13	else
14		return tag_fixnum(x);
15}
16
17/* Allocates memory */
18inline cell factor_vm::from_unsigned_cell(cell x)
19{
20	if(x > (cell)fixnum_max)
21		return tag<bignum>(cell_to_bignum(x));
22	else
23		return tag_fixnum(x);
24}
25
26/* Allocates memory */
27inline cell factor_vm::allot_float(double n)
28{
29	boxed_float *flo = allot<boxed_float>(sizeof(boxed_float));
30	flo->n = n;
31	return tag(flo);
32}
33
34/* Allocates memory */
35inline bignum *factor_vm::float_to_bignum(cell tagged)
36{
37	return double_to_bignum(untag_float(tagged));
38}
39
40inline double factor_vm::untag_float(cell tagged)
41{
42	return untag<boxed_float>(tagged)->n;
43}
44
45inline double factor_vm::untag_float_check(cell tagged)
46{
47	return untag_check<boxed_float>(tagged)->n;
48}
49
50inline fixnum factor_vm::float_to_fixnum(cell tagged)
51{
52	return (fixnum)untag_float(tagged);
53}
54
55inline double factor_vm::fixnum_to_float(cell tagged)
56{
57	return (double)untag_fixnum(tagged);
58}
59
60inline cell factor_vm::unbox_array_size()
61{
62	cell obj = ctx->peek();
63	if(TAG(obj) == FIXNUM_TYPE)
64	{
65		fixnum n = untag_fixnum(obj);
66		if(n >= 0 && n < (fixnum)array_size_max)
67		{
68			ctx->pop();
69			return n;
70		}
71	}
72
73	return unbox_array_size_slow();
74}
75
76VM_C_API cell from_signed_cell(fixnum integer, factor_vm *vm);
77VM_C_API cell from_unsigned_cell(cell integer, factor_vm *vm);
78VM_C_API cell from_signed_8(s64 n, factor_vm *vm);
79VM_C_API cell from_unsigned_8(u64 n, factor_vm *vm);
80
81VM_C_API s64 to_signed_8(cell obj, factor_vm *parent);
82VM_C_API u64 to_unsigned_8(cell obj, factor_vm *parent);
83
84VM_C_API fixnum to_fixnum(cell tagged, factor_vm *vm);
85VM_C_API cell to_cell(cell tagged, factor_vm *vm);
86
87VM_C_API void overflow_fixnum_add(fixnum x, fixnum y, factor_vm *parent);
88VM_C_API void overflow_fixnum_subtract(fixnum x, fixnum y, factor_vm *parent);
89VM_C_API void overflow_fixnum_multiply(fixnum x, fixnum y, factor_vm *parent);
90
91}