1 /*
2 * %CopyrightBegin%
3 *
4 * Copyright Ericsson AB 2002-2016. All Rights Reserved.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * %CopyrightEnd%
19 */
20 #include <string.h>
21 #include <stdlib.h>
22
23 #include "eidef.h"
24 #include "eiext.h"
25 #include "putget.h"
26
ei_decode_big(const char * buf,int * index,erlang_big * b)27 int ei_decode_big(const char *buf, int *index, erlang_big *b) {
28 unsigned int digit_bytes;
29 const unsigned char *s = (unsigned char*) buf + *index;
30 const unsigned char *s0 = s;
31
32 switch ( get8(s) ) {
33 case ERL_SMALL_BIG_EXT:
34 digit_bytes = get8(s);
35 break;
36 case ERL_LARGE_BIG_EXT:
37 digit_bytes = get32be(s);
38 break;
39 default:
40 return -1;
41 }
42 if ( b ) {
43 unsigned short *dt = b->digits;
44 unsigned int n = (digit_bytes+1)/2;
45 int i;
46
47 if ( digit_bytes != b->arity ) {
48 return -1;
49 }
50
51 b->is_neg = get8(s);
52
53 for (i = 0; i < n; ++i) {
54 dt[i] = s[i*2];
55 if ((i*2 + 1) < digit_bytes) {
56 dt[i] |= ((unsigned short) s[(i*2)+1]) << 8;
57 }
58 }
59 } else {
60 s++; /* skip sign byte */
61 }
62
63 s += digit_bytes;
64
65 *index += s-s0;
66
67 return 0;
68 }
69
ei_alloc_big(unsigned int digit_bytes)70 erlang_big *ei_alloc_big(unsigned int digit_bytes) {
71 erlang_big *b;
72 unsigned int n = (digit_bytes+1)/2;
73
74 if ( (b = malloc(sizeof(erlang_big))) == NULL) return NULL;
75 memset(b,(char)0,sizeof(erlang_big));
76 if ( (b->digits = malloc(2*n)) == NULL) {
77 free(b);
78 return NULL;
79 }
80
81 b->arity = digit_bytes;
82 memset(b->digits,(char)0, 2*n);
83 return b;
84 }
85
ei_free_big(erlang_big * b)86 void ei_free_big(erlang_big *b)
87 {
88 if (!b) return;
89 if (b->digits) free(b->digits);
90 free(b);
91 }
92
93 /* big compare functions */
94
95 typedef unsigned short Uint16;
96 typedef unsigned int Uint;
97
98 typedef Uint16 digit_t;
99 typedef Uint dsize_t;
100
I_comp(digit_t * x,dsize_t xl,digit_t * y,dsize_t yl)101 static int I_comp(digit_t *x, dsize_t xl, digit_t *y, dsize_t yl)
102 {
103 if (xl<yl) {
104 return -1;
105 } else if (xl>yl) {
106 return 1;
107 } else {
108 if ( x == y ) return 0;
109 x += (xl-1);
110 y += (yl-1);
111 while( (xl>0) && (*x==*y) ) {
112 x--;
113 y--;
114 xl--;
115 }
116 if ( xl == 0 ) return 0;
117 return ( *x < *y ) ? -1 : 1;
118 }
119 }
120
ei_big_comp(erlang_big * x,erlang_big * y)121 int ei_big_comp(erlang_big *x, erlang_big *y)
122 {
123 if ( x->is_neg == y->is_neg ) {
124 int c = I_comp(x->digits,(x->arity+1)/2,y->digits,(y->arity+1)/2);
125 if ( x->is_neg )
126 return -c;
127 else
128 return c;
129 } else {
130 return x->is_neg ? -1 : 1;
131 }
132 }
133
134 #define D_EXP 16
135 #define D_BASE (1<<D_EXP)
136
137 #define D_DECIMAL_EXP 4 /* 10^4 == 10000 */
138 #define D_DECIMAL_BASE 10000 /* Max decimal exponent in a digit */
139
140 #define DLOW(x) ((digit_t)((x) & (D_BASE-1)))
141 #define DHIGH(x) ((digit_t)((x) >> D_EXP))
142
143 /*
144 * Handling of floating point exceptions.
145 */
146
147 #if defined(VXWORKS) && CPU == PPC860
148 #undef NO_FPE_SIGNALS
149 #define NO_FPE_SIGNALS 1
150 #undef INLINED_FP_CONVERSION
151 #define INLINED_FP_CONVERSION 1
152 #endif
153
154 #ifdef NO_FPE_SIGNALS
155 # define ERTS_FP_CHECK_INIT() do {} while (0)
156 # define ERTS_FP_ERROR(f, Action) if (!isfinite(f)) { Action; } else {}
157 # define ERTS_SAVE_FP_EXCEPTION()
158 # define ERTS_RESTORE_FP_EXCEPTION()
159 #else
160 /* extern volatile int erl_fp_exception; */
161 static volatile int erl_fp_exception;
162 # define ERTS_FP_CHECK_INIT() do {erl_fp_exception = 0;} while (0)
163 # if defined(__i386__) && defined(__GNUC__)
164 /* extern void erts_restore_x87(void); */
165
unmask_fpe(void)166 static void unmask_fpe(void)
167 {
168 unsigned short cw;
169 __asm__ __volatile__("fstcw %0" : "=m"(cw));
170 cw &= ~(0x01|0x04|0x08); /* unmask IM, ZM, OM */
171 __asm__ __volatile__("fldcw %0" : : "m"(cw));
172 }
173
erts_restore_x87(void)174 static void erts_restore_x87(void)
175 {
176 __asm__ __volatile__("fninit");
177 unmask_fpe();
178 }
179
erts_check_x87(double f)180 static int erts_check_x87(double f)
181 {
182 __asm__ __volatile__("fwait" : "=m"(erl_fp_exception) : "m"(f));
183 if( !erl_fp_exception )
184 return 0;
185 erts_restore_x87();
186 return 1;
187 }
188 # define ERTS_FP_ERROR(f, Action) do { if( erts_check_x87((f)) ) { Action; } } while (0)
189 # else
190 # define ERTS_FP_ERROR(f, Action) if (erl_fp_exception) { Action; } else {}
191 # endif
192 # define ERTS_SAVE_FP_EXCEPTION() int old_erl_fp_exception = erl_fp_exception
193 # define ERTS_RESTORE_FP_EXCEPTION() \
194 do {erl_fp_exception = old_erl_fp_exception;} while (0)
195 #endif
196
197
198 #ifdef INLINED_FP_CONVERSION
join(unsigned d_split[4],unsigned * d)199 static void join(unsigned d_split[4], unsigned *d)
200 {
201 d[0] = (d_split[0] << 31) | /* Sign bit */
202 ((d_split[1] & 0x7FFU) << 20) | /* Exponent */
203 (d_split[2] & 0xFFFFFU); /* Mantissa MS bits */
204 d[1] = d_split[3]; /* Mantissa LS bits */
205 }
206
blength(unsigned long l)207 static int blength(unsigned long l)
208 {
209 int i;
210 for(i = 0; l; ++i)
211 l >>= 1;
212 return i;
213 }
214
bblength(erlang_big * b)215 static int bblength(erlang_big *b)
216 {
217 unsigned int wholebytes = (b->arity+1)/2;
218 digit_t *dp = b->digits;
219
220 while(wholebytes > 0 && dp[--wholebytes] == 0U)
221 ;
222
223 return (wholebytes * sizeof(digit_t) * 8) + blength(dp[wholebytes]);
224 }
225
bindex(erlang_big * b,int ndx)226 static unsigned long bindex(erlang_big *b, int ndx) {
227 digit_t *dp = b->digits;
228 int skipdigits;
229 int dnum;
230
231 if (ndx < 0)
232 return 0;
233
234 skipdigits = ndx / (sizeof(digit_t) * 8);
235 dnum = ndx % (sizeof(digit_t) * 8);
236 return !!(dp[skipdigits] & (1UL << dnum));
237 }
238
239
240 #endif
241
242
ei_big_to_double(erlang_big * b,double * resp)243 int ei_big_to_double(erlang_big *b, double *resp)
244 {
245 #ifdef INLINED_FP_CONVERSION
246 unsigned d_split[4];
247 unsigned *uresp = (unsigned *) resp;
248 unsigned len = bblength(b);
249 int i;
250 unsigned long msm = 0, lsm = 0;
251
252 /* OK, this is not the most efficient conversion in the world, especially
253 not the bit-by-bit copying to the mantissa.... Simple, working and
254 only for vxworks ppc860 where no sane person would use floating
255 point anyway, eh? /Patrik */
256
257 if (!len) {
258 memset(d_split,0,sizeof(d_split)); /* 0 */
259 } else {
260 --len;
261 if (len > 1023) { /* Infinite */
262 d_split[1] = 2047;
263 d_split[2] = d_split[3] = 0;
264 } else {
265 d_split[1] = 1023 + len;
266 --len; /* skip the implicit binary 1. */
267 for (i = 0; i < 20; ++i, --len) {
268 msm <<= 1;
269 msm |= bindex(b,len);
270 }
271 for (i = 0; i < 32; ++i, --len) {
272 lsm <<= 1;
273 lsm |= bindex(b,len);
274 }
275 d_split[2] = msm;
276 d_split[3] = lsm;
277 }
278 }
279 d_split[0] = (unsigned) !!(b->is_neg);
280 join(d_split,uresp);
281 return 0;
282 #else
283 double d = 0.0;
284 double d_base = 1.0;
285
286 digit_t* s = (digit_t *)b->digits;
287 dsize_t xl = (b->arity + 1)/2;
288 short xsgn = b->is_neg;
289 ERTS_SAVE_FP_EXCEPTION();
290
291 ERTS_FP_CHECK_INIT();
292 while(xl--) {
293 digit_t ds = *s;
294 double d_next = ds * d_base + d;
295
296 ERTS_FP_ERROR(d_next, ERTS_RESTORE_FP_EXCEPTION(); {fprintf(stderr,"\r\n### fp exception ###\r\n"); return -1;});
297 s++;
298 d = d_next;
299 d_base *= D_BASE;
300 }
301
302 /*
303 * Note: The last multiplication in the loop could trigger an exception,
304 * which we will ignore because the result will never be used.
305 */
306
307 *resp = xsgn ? -d : d;
308 ERTS_FP_ERROR(*resp,;);
309 ERTS_RESTORE_FP_EXCEPTION();
310 return 0;
311 #endif
312 }
313
ei_small_to_big(int s,erlang_big * b)314 int ei_small_to_big(int s, erlang_big *b)
315 {
316 digit_t *d;
317 unsigned int n = (b->arity+1)/2;
318
319 if ( n < 2 ) return -1;
320
321 b->is_neg = ( s < 0 );
322 d = (digit_t *)b->digits;
323 d[0] = DLOW(s);
324 d[1] = DHIGH(s);
325
326 return 0;
327 }
328