1 /* float.c floating-point constant support for the Netwide Assembler
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the licence given in the file "Licence"
6 * distributed in the NASM archive.
8 * initial version 13/ix/96 by Simon Tatham
20 #define MANT_WORDS 6 /* 64 bits + 32 for accuracy == 96 */
21 #define MANT_DIGITS 28 /* 29 digits don't fit in 96 bits */
24 * guaranteed top bit of from is set
25 * => we only have to worry about _one_ bit shift to the left
28 static int multiply(unsigned short *to, unsigned short *from) {
29 unsigned long temp[MANT_WORDS*2];
32 for (i=0; i<MANT_WORDS*2; i++)
35 for (i=0; i<MANT_WORDS; i++)
36 for (j=0; j<MANT_WORDS; j++) {
38 n = (unsigned long)to[i] * (unsigned long)from[j];
40 temp[i+j+1] += n & 0xFFFF;
43 for (i=MANT_WORDS*2; --i ;) {
44 temp[i-1] += temp[i] >> 16;
47 if (temp[0] & 0x8000) {
48 for (i=0; i<MANT_WORDS; i++)
49 to[i] = temp[i] & 0xFFFF;
52 for (i=0; i<MANT_WORDS; i++)
53 to[i] = (temp[i] << 1) + !!(temp[i+1] & 0x8000);
58 static void flconvert(char *string, unsigned short *mant, long *exponent) {
59 char digits[MANT_DIGITS], *p, *q, *r;
60 unsigned short mult[MANT_WORDS], *m, bit;
62 int extratwos, started, seendot;
66 started = seendot = FALSE;
67 while (*string && *string != 'E' && *string != 'e') {
72 fprintf(stderr, "too many periods!\n");
75 } else if (*string >= '0' && *string <= '9') {
76 if (*string == '0' && !started) {
81 if (p < digits+sizeof(digits))
87 fprintf(stderr, "`%c' is invalid char\n", *string);
93 string++; /* eat the E */
94 tenpwr += atoi(string);
98 * At this point, the memory interval [digits,p) contains a
99 * series of decimal digits zzzzzzz such that our number X
102 * X = 0.zzzzzzz * 10^tenpwr
106 for (m=mant; m<mant+MANT_WORDS; m++)
112 while (m < mant+MANT_WORDS) {
113 unsigned short carry = 0;
114 while (p > q && !p[-1])
118 for (r = p; r-- > q ;) {
129 *m |= bit, started = TRUE;
141 * At this point the `mant' array contains the first six
142 * fractional places of a base-2^16 real number, which when
143 * multiplied by 2^twopwr and 5^tenpwr gives X. So now we
144 * really do multiply by 5^tenpwr.
148 for (m=mult; m<mult+MANT_WORDS; m++)
152 } else if (tenpwr > 0) {
154 for (m=mult+1; m<mult+MANT_WORDS; m++)
161 twopwr += extratwos + multiply (mant, mult);
162 extratwos = extratwos * 2 + multiply (mult, mult);
167 * Conversion is done. The elements of `mant' contain the first
168 * fractional places of a base-2^16 real number in [0.5,1)
169 * which we can multiply by 2^twopwr to get X. Or, of course,
176 * Shift a mantissa to the right by i (i < 16) bits.
178 static void shr(unsigned short *mant, int i) {
179 unsigned short n = 0, m;
182 for (j=0; j<MANT_WORDS; j++) {
183 m = (mant[j] << (16-i)) & 0xFFFF;
184 mant[j] = (mant[j] >> i) | n;
190 * Round a mantissa off after i words.
192 static int round(unsigned short *mant, int i) {
193 if (mant[i] & 0x8000) {
197 } while (i > 0 && !mant[i]);
198 return !i && !mant[i];
203 #define put(a,b) ( (*(a)=(b)), ((a)[1]=(b)>>8) )
205 static int to_double(char *str, long sign, unsigned char *result,
207 unsigned short mant[MANT_WORDS];
210 sign = (sign < 0 ? 0x8000L : 0L);
212 flconvert (str, mant, &exponent);
213 if (mant[0] & 0x8000) {
218 if (exponent >= -1022 && exponent <= 1024) {
225 if (mant[0] & 0x20) /* did we scale up by one? */
226 shr(mant, 1), exponent++;
227 mant[0] &= 0xF; /* remove leading one */
228 put(result+6,(exponent << 4) | mant[0] | sign);
229 put(result+4,mant[1]);
230 put(result+2,mant[2]);
231 put(result+0,mant[3]);
232 } else if (exponent < -1022 && exponent >= -1074) {
236 int shift = -(exponent+1011);
237 int sh = shift % 16, wds = shift / 16;
239 if (round(mant, 4-wds) || (sh>0 && (mant[0]&(0x8000>>(sh-1))))) {
245 put(result+6,(wds == 0 ? mant[0] : 0) | sign);
246 put(result+4,(wds <= 1 ? mant[1-wds] : 0));
247 put(result+2,(wds <= 2 ? mant[2-wds] : 0));
248 put(result+0,(wds <= 3 ? mant[3-wds] : 0));
251 error(ERR_NONFATAL, "overflow in floating-point constant");
254 memset (result, 0, 8);
260 memset (result, 0, 8);
262 return 1; /* success */
265 static int to_float(char *str, long sign, unsigned char *result,
267 unsigned short mant[MANT_WORDS];
270 sign = (sign < 0 ? 0x8000L : 0L);
272 flconvert (str, mant, &exponent);
273 if (mant[0] & 0x8000) {
278 if (exponent >= -126 && exponent <= 128) {
285 if (mant[0] & 0x100) /* did we scale up by one? */
286 shr(mant, 1), exponent++;
287 mant[0] &= 0x7F; /* remove leading one */
288 put(result+2,(exponent << 7) | mant[0] | sign);
289 put(result+0,mant[1]);
290 } else if (exponent < -126 && exponent >= -149) {
294 int shift = -(exponent+118);
295 int sh = shift % 16, wds = shift / 16;
297 if (round(mant, 2-wds) || (sh>0 && (mant[0]&(0x8000>>(sh-1))))) {
303 put(result+2,(wds == 0 ? mant[0] : 0) | sign);
304 put(result+0,(wds <= 1 ? mant[1-wds] : 0));
307 error(ERR_NONFATAL, "overflow in floating-point constant");
310 memset (result, 0, 4);
313 memset (result, 0, 4);
318 static int to_ldoub(char *str, long sign, unsigned char *result,
320 unsigned short mant[MANT_WORDS];
323 sign = (sign < 0 ? 0x8000L : 0L);
325 flconvert (str, mant, &exponent);
326 if (mant[0] & 0x8000) {
331 if (exponent >= -16383 && exponent <= 16384) {
336 if (round(mant, 4)) /* did we scale up by one? */
337 shr(mant, 1), mant[0] |= 0x8000, exponent++;
338 put(result+8,exponent | sign);
339 put(result+6,mant[0]);
340 put(result+4,mant[1]);
341 put(result+2,mant[2]);
342 put(result+0,mant[3]);
343 } else if (exponent < -16383 && exponent >= -16446) {
347 int shift = -(exponent+16383);
348 int sh = shift % 16, wds = shift / 16;
350 if (round(mant, 4-wds) || (sh>0 && (mant[0]&(0x8000>>(sh-1))))) {
357 put(result+6,(wds == 0 ? mant[0] : 0));
358 put(result+4,(wds <= 1 ? mant[1-wds] : 0));
359 put(result+2,(wds <= 2 ? mant[2-wds] : 0));
360 put(result+0,(wds <= 3 ? mant[3-wds] : 0));
363 error(ERR_NONFATAL, "overflow in floating-point constant");
366 memset (result, 0, 10);
372 memset (result, 0, 10);
377 int float_const (char *number, long sign, unsigned char *result, int bytes,
380 return to_float (number, sign, result, error);
382 return to_double (number, sign, result, error);
383 else if (bytes == 10)
384 return to_ldoub (number, sign, result, error);
386 error(ERR_PANIC, "strange value %d passed to float_const", bytes);