33
33
/* #define DEBUG_OP */ /* Count arithmetic operations */
36
#define D_BASE (1<<D_EXP)
40
#define D_BASE (1L<<D_EXP)
43
#define D_DECIMAL_EXP 9
44
#define D_DECIMAL_BASE 1000000000
38
46
#define D_DECIMAL_EXP 4 /* 10^4 == 10000 */
39
47
#define D_DECIMAL_BASE 10000 /* Max decimal exponent in a digit */
41
50
/* macros for bignum objects */
42
51
#define big_v(x) BIG_V(big_val(x))
55
64
#define BIG_DIGITS_PER_WORD (sizeof(Uint)/sizeof(digit_t))
59
#define BIG_SIZE(xp) \
61
((BIG_DIGIT(xp, 4*BIG_ARITY(xp)-1) == 0) ? \
62
((BIG_DIGIT(xp, 4*BIG_ARITY(xp)-2) == 0) ? \
63
((BIG_DIGIT(xp, 4*BIG_ARITY(xp)-3) == 0) ? 3 : 2) : 1) : 0))
65
67
#define BIG_SIZE(xp) \
66
68
( 2*BIG_ARITY(xp) - ((BIG_DIGIT(xp, 2*BIG_ARITY(xp)-1) == 0) ? 1 : 0))
69
70
/* Check for small */
70
71
#define IS_USMALL(sgn,x) ((sgn) ? ((x) <= MAX_SMALL+1) : ((x) <= MAX_SMALL))
71
72
#define IS_SSMALL(x) (((x) >= MIN_SMALL) && ((x) <= MAX_SMALL))
73
74
/* The heap size needed for a bignum is
74
** Number of digits 'x' in words = (x+1)/2 plus
78
** Replace with: ( ((x)+sizeof(Uint)-1) / sizeof(Uint) ) + 1
81
#define BIG_NEED_SIZE(x) ((((x)+3) >> 2) + 1)
75
* Number of digits 'x' in words = (x+1)/2 plus
83
79
#define BIG_NEED_SIZE(x) ((((x)+1) >> 1) + 1)
81
#define BIG_UINT_HEAP_SIZE (1 + 1) /* always, since sizeof(Uint) <= sizeof(Eterm) */
87
83
/* sizeof(digit_t) <= sizeof(D_BASE-1) */
86
typedef Uint32 digit_t;
89
89
typedef Uint32 reg_t; /* register type 32 bit */
90
90
typedef Uint16 digit_t; /* digit type 16 bit */
91
92
typedef Uint dsize_t; /* Vector size type */
140
141
#define DSUM(a,b,c1,c0) do { \
141
reg_t _t = (a)+(b); \
142
reg_t _t = ((reg_t)(a))+(b); \
143
144
c1 = DHIGH(_t); \
145
146
#define DSUMc(a,b,c,s) do { \
146
reg_t _t = (a)+(b); \
147
reg_t _t = ((reg_t)(a))+(b); \
147
148
if (c) _t += (c); \
151
152
#define DMULc(a,b,c,p) do { \
152
reg_t _t = (a)*(b); \
153
reg_t _t = ((reg_t)(a))*(b); \
153
154
if (c) _t += (c); \
157
158
#define DMUL(a,b,c1,c0) do { \
158
reg_t _t = (a)*(b); \
159
reg_t _t = ((reg_t)(a))*(b); \
160
161
c1 = DHIGH(_t); \
163
164
#define DSUBb(a,b,r,d) do { \
164
reg_t _t = (b)+(r); \
165
reg_t _t = ((reg_t)(b))+(r); \
165
166
if ((a) < _t) { \
166
167
d = (D_BASE-_t)+(a); r = 1; \
182
183
#define DDIV(a0,a1,b,q) do { \
183
reg_t _t = (a0)*D_BASE+(a1); \
184
reg_t _t = ((reg_t)(a0))*D_BASE+(a1); \
187
188
#define DDIV2(a0,a1,b0,b1,q) do { \
188
reg_t _t = (a0)*D_BASE+(a1); \
189
q = _t / ((b0)*D_BASE+(b1)); \
189
reg_t _t = ((reg_t)(a0))*D_BASE+(a1); \
190
q = _t / (((reg_t)(b0))*D_BASE+(b1)); \
192
193
#define DREM(a0,a1,b,r) do { \
193
reg_t _t = (a0)*D_BASE+(a1); \
194
reg_t _t = ((reg_t)(a0))*D_BASE+(a1); \
197
#define BIG_UINT_HEAP_SIZE (BIG_NEED_SIZE(2))
199
198
int big_decimal_estimate(Eterm);
199
#if 0 /* XXX: unused */
200
200
char* big_to_decimal(Eterm, char*, int);
201
Eterm big_to_list(Eterm, Eterm**);
202
Eterm erts_big_to_list(Eterm, Eterm**);
203
char *erts_big_to_string(Eterm x, char *buf, Uint buf_sz);
203
205
Eterm big_plus(Eterm, Eterm, Eterm*);
204
206
Eterm big_minus(Eterm, Eterm, Eterm*);
222
224
int big_to_double(Eterm x, double* resp);
223
225
Eterm small_to_big(Sint, Eterm*);
224
226
Eterm uint_to_big(Uint, Eterm*);
225
Eterm make_small_or_big(Uint, Process*);
227
Eterm erts_make_integer(Uint, Process *);
227
229
dsize_t big_bytes(Eterm);
230
#if 0 /* XXX: unused */
228
231
int bytes_eq_big(byte*, dsize_t, int, Eterm);
229
233
Eterm bytes_to_big(byte*, dsize_t, int, Eterm*);
230
234
byte* big_to_bytes(Eterm, byte*);
232
int big_fits_in_sint32(Eterm b);
233
int big_fits_in_uint32(Eterm b);
236
int term_to_Uint(Eterm, Uint*);
237
int term_to_Sint(Eterm, Sint*);
234
239
Uint32 big_to_uint32(Eterm b);
235
Sint32 big_to_sint32(Eterm b);
240
int term_equals_2pow32(Eterm);