summaryrefslogtreecommitdiff
path: root/float.c
blob: 545ae77381e495e317b0ad1aea3898efb58d57c6 (plain)
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/* float.c     floating-point constant support for the Netwide Assembler
 *
 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
 * Julian Hall. All rights reserved. The software is
 * redistributable under the licence given in the file "Licence"
 * distributed in the NASM archive.
 *
 * initial version 13/ix/96 by Simon Tatham
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "nasm.h"

#define TRUE 1
#define FALSE 0

#define MANT_WORDS 6		       /* 64 bits + 32 for accuracy == 96 */
#define MANT_DIGITS 28		       /* 29 digits don't fit in 96 bits */

/*
 * guaranteed top bit of from is set
 * => we only have to worry about _one_ bit shift to the left
 */

static int multiply(unsigned short *to, unsigned short *from) 
{
    unsigned long temp[MANT_WORDS*2];
    int           i, j;

    for (i=0; i<MANT_WORDS*2; i++)
	temp[i] = 0;

    for (i=0; i<MANT_WORDS; i++)
	for (j=0; j<MANT_WORDS; j++) {
	    unsigned long n;
	    n = (unsigned long)to[i] * (unsigned long)from[j];
	    temp[i+j] += n >> 16;
	    temp[i+j+1] += n & 0xFFFF;
	}

    for (i=MANT_WORDS*2; --i ;) {
	temp[i-1] += temp[i] >> 16;
	temp[i] &= 0xFFFF;
    }
    if (temp[0] & 0x8000) {
	for (i=0; i<MANT_WORDS; i++)
	    to[i] = temp[i] & 0xFFFF;
	return 0;
    } else {
	for (i=0; i<MANT_WORDS; i++)
	    to[i] = (temp[i] << 1) + !!(temp[i+1] & 0x8000);
	return -1;
    }
}

static void flconvert(char *string, unsigned short *mant, long *exponent,
		      efunc error) 
{
    char           digits[MANT_DIGITS];
    char           *p, *q, *r;
    unsigned short mult[MANT_WORDS], bit;
    unsigned short * m;
    long           tenpwr, twopwr;
    int            extratwos, started, seendot;

    p = digits;
    tenpwr = 0;
    started = seendot = FALSE;
    while (*string && *string != 'E' && *string != 'e') {
	if (*string == '.') {
	    if (!seendot)
		seendot = TRUE;
	    else {
		error (ERR_NONFATAL,
		       "too many periods in floating-point constant");
		return;
	    }
	} else if (*string >= '0' && *string <= '9') {
	    if (*string == '0' && !started) {
		if (seendot)
		    tenpwr--;
	    } else {
		started = TRUE;
		if (p < digits+sizeof(digits))
		    *p++ = *string - '0';
		if (!seendot)
		    tenpwr++;
	    }
	} else {
	    error (ERR_NONFATAL,
		   "floating-point constant: `%c' is invalid character",
		   *string);
	    return;
	}
	string++;
    }
    if (*string) {
	string++;		       /* eat the E */
	tenpwr += atoi(string);
    }

    /*
     * At this point, the memory interval [digits,p) contains a
     * series of decimal digits zzzzzzz such that our number X
     * satisfies
     *
     * X = 0.zzzzzzz * 10^tenpwr
     */

    bit = 0x8000;
    for (m=mant; m<mant+MANT_WORDS; m++)
	*m = 0;
    m = mant;
    q = digits;
    started = FALSE;
    twopwr = 0;
    while (m < mant+MANT_WORDS) {
	unsigned short carry = 0;
	while (p > q && !p[-1])
	    p--;
	if (p <= q)
	    break;
	for (r = p; r-- > q ;) {
	    int i;

	    i = 2 * *r + carry;
	    if (i >= 10)
		carry = 1, i -= 10;
	    else
		carry = 0;
	    *r = i;
	}
	if (carry)
	    *m |= bit, started = TRUE;
	if (started) {
	    if (bit == 1)
		bit = 0x8000, m++;
	    else
		bit >>= 1;
	} else
	    twopwr--;
    }
    twopwr += tenpwr;

    /*
     * At this point the `mant' array contains the first six
     * fractional places of a base-2^16 real number, which when
     * multiplied by 2^twopwr and 5^tenpwr gives X. So now we
     * really do multiply by 5^tenpwr.
     */

    if (tenpwr < 0) {
	for (m=mult; m<mult+MANT_WORDS; m++)
	    *m = 0xCCCC;
	extratwos = -2;
	tenpwr = -tenpwr;
    } else if (tenpwr > 0) {
	mult[0] = 0xA000;
	for (m=mult+1; m<mult+MANT_WORDS; m++)
	    *m = 0;
	extratwos = 3;
    } else
	extratwos = 0;
    while (tenpwr) {
	if (tenpwr & 1)
	    twopwr += extratwos + multiply (mant, mult);
	extratwos = extratwos * 2 + multiply (mult, mult);
	tenpwr >>= 1;
    }

    /*
     * Conversion is done. The elements of `mant' contain the first
     * fractional places of a base-2^16 real number in [0.5,1)
     * which we can multiply by 2^twopwr to get X. Or, of course,
     * it contains zero.
     */
    *exponent = twopwr;
}

/*
 * Shift a mantissa to the right by i (i < 16) bits.
 */
static void shr(unsigned short *mant, int i) 
{
    unsigned short n = 0, m;
    int            j;

    for (j=0; j<MANT_WORDS; j++) {
	m = (mant[j] << (16-i)) & 0xFFFF;
	mant[j] = (mant[j] >> i) | n;
	n = m;
    }
}

/*
 * Round a mantissa off after i words.
 */
static int round(unsigned short *mant, int i) 
{
    if (mant[i] & 0x8000) {
	do {
	    ++mant[--i];
	    mant[i] &= 0xFFFF;
	} while (i > 0 && !mant[i]);
	return !i && !mant[i];
    }
    return 0;
}

#define put(a,b) ( (*(a)=(b)), ((a)[1]=(b)>>8) )

static int to_double(char *str, long sign, unsigned char *result,
		     efunc error) 
{
    unsigned short mant[MANT_WORDS];
    long exponent;

    sign = (sign < 0 ? 0x8000L : 0L);

    flconvert (str, mant, &exponent, error);
    if (mant[0] & 0x8000) {
	/*
	 * Non-zero.
	 */
	exponent--;
	if (exponent >= -1022 && exponent <= 1024) {
	    /*
	     * Normalised.
	     */
	    exponent += 1023;
	    shr(mant, 11);
	    round(mant, 4);
	    if (mant[0] & 0x20)	       /* did we scale up by one? */
		shr(mant, 1), exponent++;
	    mant[0] &= 0xF;	       /* remove leading one */
	    put(result+6,(exponent << 4) | mant[0] | sign);
	    put(result+4,mant[1]);
	    put(result+2,mant[2]);
	    put(result+0,mant[3]);
	} else if (exponent < -1022 && exponent >= -1074) {
	    /*
	     * Denormal.
	     */
	    int shift = -(exponent+1011);
	    int sh = shift % 16, wds = shift / 16;
	    shr(mant, sh);
	    if (round(mant, 4-wds) || (sh>0 && (mant[0]&(0x8000>>(sh-1))))) {
		shr(mant, 1);
		if (sh==0)
		    mant[0] |= 0x8000;
		exponent++;
	    }
	    put(result+6,(wds == 0 ? mant[0] : 0) | sign);
	    put(result+4,(wds <= 1 ? mant[1-wds] : 0));
	    put(result+2,(wds <= 2 ? mant[2-wds] : 0));
	    put(result+0,(wds <= 3 ? mant[3-wds] : 0));
	} else {
	    if (exponent > 0) {
		error(ERR_NONFATAL, "overflow in floating-point constant");
		return 0;
	    } else
		memset (result, 0, 8);
	}
    } else {
	/*
	 * Zero.
	 */
	memset (result, 0, 8);
    }
    return 1;			       /* success */
}

static int to_float(char *str, long sign, unsigned char *result,
		    efunc error) 
{
    unsigned short mant[MANT_WORDS];
    long exponent;

    sign = (sign < 0 ? 0x8000L : 0L);

    flconvert (str, mant, &exponent, error);
    if (mant[0] & 0x8000) {
	/*
	 * Non-zero.
	 */
	exponent--;
	if (exponent >= -126 && exponent <= 128) {
	    /*
	     * Normalised.
	     */
	    exponent += 127;
	    shr(mant, 8);
	    round(mant, 2);
	    if (mant[0] & 0x100)       /* did we scale up by one? */
		shr(mant, 1), exponent++;
	    mant[0] &= 0x7F;	       /* remove leading one */
	    put(result+2,(exponent << 7) | mant[0] | sign);
	    put(result+0,mant[1]);
	} else if (exponent < -126 && exponent >= -149) {
	    /*
	     * Denormal.
	     */
	    int shift = -(exponent+118);
	    int sh = shift % 16, wds = shift / 16;
	    shr(mant, sh);
	    if (round(mant, 2-wds) || (sh>0 && (mant[0]&(0x8000>>(sh-1))))) {
		shr(mant, 1);
		if (sh==0)
		    mant[0] |= 0x8000;
		exponent++;
	    }
	    put(result+2,(wds == 0 ? mant[0] : 0) | sign);
	    put(result+0,(wds <= 1 ? mant[1-wds] : 0));
	} else {
	    if (exponent > 0) {
		error(ERR_NONFATAL, "overflow in floating-point constant");
		return 0;
	    } else
		memset (result, 0, 4);
	}
    } else {
	memset (result, 0, 4);
    }
    return 1;
}

static int to_ldoub(char *str, long sign, unsigned char *result,
		    efunc error) 
{
    unsigned short mant[MANT_WORDS];
    long exponent;

    sign = (sign < 0 ? 0x8000L : 0L);

    flconvert (str, mant, &exponent, error);
    if (mant[0] & 0x8000) {
	/*
	 * Non-zero.
	 */
	exponent--;
	if (exponent >= -16383 && exponent <= 16384) {
	    /*
	     * Normalised.
	     */
	    exponent += 16383;
	    if (round(mant, 4))	       /* did we scale up by one? */
		shr(mant, 1), mant[0] |= 0x8000, exponent++;
	    put(result+8,exponent | sign);
	    put(result+6,mant[0]);
	    put(result+4,mant[1]);
	    put(result+2,mant[2]);
	    put(result+0,mant[3]);
	} else if (exponent < -16383 && exponent >= -16446) {
	    /*
	     * Denormal.
	     */
	    int shift = -(exponent+16383);
	    int sh = shift % 16, wds = shift / 16;
	    shr(mant, sh);
	    if (round(mant, 4-wds) || (sh>0 && (mant[0]&(0x8000>>(sh-1))))) {
		shr(mant, 1);
		if (sh==0)
		    mant[0] |= 0x8000;
		exponent++;
	    }
	    put(result+8,sign);
	    put(result+6,(wds == 0 ? mant[0] : 0));
	    put(result+4,(wds <= 1 ? mant[1-wds] : 0));
	    put(result+2,(wds <= 2 ? mant[2-wds] : 0));
	    put(result+0,(wds <= 3 ? mant[3-wds] : 0));
	} else {
	    if (exponent > 0) {
		error(ERR_NONFATAL, "overflow in floating-point constant");
		return 0;
	    } else
		memset (result, 0, 10);
	}
    } else {
	/*
	 * Zero.
	 */
	memset (result, 0, 10);
    }
    return 1;
}

int float_const (char *number, long sign, unsigned char *result, int bytes,
		 efunc error) 
{
    if (bytes == 4)
	return to_float (number, sign, result, error);
    else if (bytes == 8)
	return to_double (number, sign, result, error);
    else if (bytes == 10)
	return to_ldoub (number, sign, result, error);
    else {
	error(ERR_PANIC, "strange value %d passed to float_const", bytes);
	return 0;
    }
}