aboutsummaryrefslogtreecommitdiffstats
path: root/lib/erl_interface/src/decode/decode_big.c
blob: 016ed2eac2caa30a6e2fb8adf4ddfb326b907c35 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 * %CopyrightBegin%
 * 
 * Copyright Ericsson AB 2002-2011. All Rights Reserved.
 * 
 * The contents of this file are subject to the Erlang Public License,
 * Version 1.1, (the "License"); you may not use this file except in
 * compliance with the License. You should have received a copy of the
 * Erlang Public License along with this software. If not, it can be
 * retrieved online at http://www.erlang.org/.
 * 
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 * 
 * %CopyrightEnd%
 */
#include <string.h>
#include <stdlib.h>

#include "eidef.h"
#include "eiext.h"
#include "putget.h"

int ei_decode_big(const char *buf, int *index, erlang_big *b) {
    unsigned int digit_bytes;
    const unsigned char *s = (unsigned char*) buf + *index;
    const unsigned char *s0 = s;

    switch ( get8(s) ) {
	case ERL_SMALL_BIG_EXT:
	digit_bytes = get8(s);
	break;
    case ERL_LARGE_BIG_EXT:
	digit_bytes = get32be(s);
	break;
    default:
	return -1;
    }
    if ( b ) {
	unsigned short *dt = b->digits;
	unsigned int n = (digit_bytes+1)/2;
	int i;

	if ( digit_bytes != b->arity ) {
	    return -1;
	}

	b->is_neg = get8(s);
	  
	for (i = 0; i < n; ++i) {
	    dt[i] = s[i*2];
	    if ((i*2 + 1) < digit_bytes) {
		dt[i] |= ((unsigned short) s[(i*2)+1]) << 8;
	    }
	}
    } else {
	s++; /* skip sign byte */
    }

    s += digit_bytes;

    *index += s-s0; 
  
    return 0; 
}

erlang_big *ei_alloc_big(unsigned int digit_bytes) {
    erlang_big *b;
    unsigned int n = (digit_bytes+1)/2;

    if ( (b = malloc(sizeof(erlang_big))) == NULL) return NULL;
    memset(b,(char)0,sizeof(erlang_big));
    if ( (b->digits = malloc(2*n)) == NULL) {
        free(b);
        return NULL;
    }
   
    b->arity = digit_bytes;
    memset(b->digits,(char)0, 2*n);
    return b;
}

void ei_free_big(erlang_big *b)
{
    if (!b) return;
    if (b->digits) free(b->digits);
    free(b);
}

/* big compare functions */

typedef unsigned short Uint16;
typedef unsigned int Uint;

typedef Uint16 digit_t;
typedef Uint dsize_t;

static int I_comp(digit_t *x, dsize_t xl, digit_t *y, dsize_t yl)
{
    if (xl<yl) {
        return -1;
    } else if (xl>yl) {
        return 1;
    } else {
        if ( x == y ) return 0;
        x += (xl-1);
        y += (yl-1);
        while( (xl>0) && (*x==*y) ) {
            x--;
            y--;
            xl--;
        }
        if ( xl == 0 ) return 0;
        return ( *x < *y ) ? -1 : 1;
    }
}

int ei_big_comp(erlang_big *x, erlang_big *y)
{
    if ( x->is_neg == y->is_neg ) {
        int c = I_comp(x->digits,(x->arity+1)/2,y->digits,(y->arity+1)/2);
        if ( x->is_neg ) 
            return -c;
        else
            return c;
    } else {
        return x->is_neg ? -1 : 1;
    }
}

#define D_EXP      16
#define D_BASE     (1<<D_EXP)

#define D_DECIMAL_EXP   4           /* 10^4 == 10000 */
#define D_DECIMAL_BASE  10000       /* Max decimal exponent in a digit */

#define DLOW(x)        ((digit_t)((x) & (D_BASE-1)))
#define DHIGH(x)       ((digit_t)((x) >> D_EXP))

/*
 * Handling of floating point exceptions.
 */

#if defined(VXWORKS) && CPU == PPC860
#undef NO_FPE_SIGNALS
#define NO_FPE_SIGNALS 1
#undef INLINED_FP_CONVERSION
#define INLINED_FP_CONVERSION 1
#endif

#ifdef NO_FPE_SIGNALS
#  define ERTS_FP_CHECK_INIT() do {} while (0)
#  define ERTS_FP_ERROR(f, Action) if (!isfinite(f)) { Action; } else {}
#  define ERTS_SAVE_FP_EXCEPTION()
#  define ERTS_RESTORE_FP_EXCEPTION()
#else
/* extern volatile int erl_fp_exception; */
static volatile int erl_fp_exception;
#  define ERTS_FP_CHECK_INIT() do {erl_fp_exception = 0;} while (0)
#  if defined(__i386__) && defined(__GNUC__)
/* extern void erts_restore_x87(void); */

static void unmask_fpe(void)
{
    unsigned short cw;
    __asm__ __volatile__("fstcw %0" : "=m"(cw));
    cw &= ~(0x01|0x04|0x08);   /* unmask IM, ZM, OM */
    __asm__ __volatile__("fldcw %0" : : "m"(cw));
}

static void erts_restore_x87(void)
{
    __asm__ __volatile__("fninit");
    unmask_fpe();
}

static int erts_check_x87(double f)
{
    __asm__ __volatile__("fwait" : "=m"(erl_fp_exception) : "m"(f));
    if( !erl_fp_exception )
       return 0;
    erts_restore_x87();
    return 1;
}
#  define ERTS_FP_ERROR(f, Action) do { if( erts_check_x87((f)) ) { Action; } } while (0)
#  else
#  define ERTS_FP_ERROR(f, Action) if (erl_fp_exception) { Action; } else {}
#  endif
#  define ERTS_SAVE_FP_EXCEPTION() int old_erl_fp_exception = erl_fp_exception
#  define ERTS_RESTORE_FP_EXCEPTION() \
              do {erl_fp_exception = old_erl_fp_exception;} while (0)
#endif


#ifdef INLINED_FP_CONVERSION
static void join(unsigned d_split[4], unsigned *d)
{
    d[0] = (d_split[0] << 31) |         /* Sign bit */
	((d_split[1] & 0x7FFU) << 20) | /* Exponent */
	(d_split[2] & 0xFFFFFU);        /* Mantissa MS bits */
    d[1] = d_split[3];                  /* Mantissa LS bits */
}

static int blength(unsigned long l)
{
    int i;
    for(i = 0; l; ++i)
	l >>= 1;
    return i;
}

static int bblength(erlang_big *b)
{
    unsigned int wholebytes = (b->arity+1)/2;
    digit_t *dp = b->digits;

    while(wholebytes > 0 && dp[--wholebytes] == 0U)
	;

    return (wholebytes * sizeof(digit_t) * 8) +  blength(dp[wholebytes]);
}

static unsigned long bindex(erlang_big *b, int ndx) {
    digit_t *dp = b->digits;
    int skipdigits;
    int dnum;

    if (ndx < 0)
	return 0;

    skipdigits = ndx / (sizeof(digit_t) * 8);
    dnum = ndx % (sizeof(digit_t) * 8);
    return !!(dp[skipdigits] & (1UL << dnum));
}


#endif


int ei_big_to_double(erlang_big *b, double *resp)
{
#ifdef INLINED_FP_CONVERSION
    unsigned d_split[4];
    unsigned *uresp = (unsigned *) resp;
    unsigned len = bblength(b);
    int i;
    unsigned long msm = 0, lsm = 0;
    
    /* OK, this is not the most efficient conversion in the world, especially
       not the bit-by-bit copying to the mantissa.... Simple, working and 
       only for vxworks ppc860 where no sane person would use floating
       point anyway, eh? /Patrik */

    if (!len) {
	memset(d_split,0,sizeof(d_split)); /* 0 */
    } else {
	--len;
	if (len > 1023) { /* Infinite */
	    d_split[1] = 2047;
	    d_split[2] = d_split[3] = 0;
	} else {
	    d_split[1] = 1023 + len; 
	    --len; /* skip the implicit binary 1. */
	    for (i = 0; i < 20; ++i, --len) {
		msm <<= 1;
		msm |= bindex(b,len);
	    }
	    for (i = 0; i < 32; ++i, --len) {
		lsm <<= 1;
		lsm |= bindex(b,len);
	    }
	    d_split[2] = msm;
	    d_split[3] = lsm;
	}	
    }
    d_split[0] = (unsigned) !!(b->is_neg);
    join(d_split,uresp);
    return 0;
#else
    double d = 0.0;
    double d_base = 1.0;

    digit_t* s = (digit_t *)b->digits;
    dsize_t xl = (b->arity + 1)/2;
    short xsgn = b->is_neg;
    ERTS_SAVE_FP_EXCEPTION();

    ERTS_FP_CHECK_INIT();
    while(xl--) {
	digit_t ds = *s;
	double d_next = ds * d_base + d;

	ERTS_FP_ERROR(d_next, ERTS_RESTORE_FP_EXCEPTION(); {fprintf(stderr,"\r\n### fp exception ###\r\n"); return -1;});
	s++;
	d = d_next;
	d_base *= D_BASE;
    }

    /*
     * Note: The last multiplication in the loop could trigger an exception,
     * which we will ignore because the result will never be used.
     */

    *resp = xsgn ? -d : d;
    ERTS_FP_ERROR(*resp,;);
    ERTS_RESTORE_FP_EXCEPTION();
    return 0;
#endif
}

int ei_small_to_big(int s, erlang_big *b)
{
    digit_t *d;
    unsigned int n = (b->arity+1)/2;

    if ( n < 2 ) return -1;

    b->is_neg = ( s < 0 );
    d = (digit_t *)b->digits;
    d[0] = DLOW(s);
    d[1] = DHIGH(s);

    return 0;
}