blob: df70fe1d1f29efb2b67b98916db7f4c94359ee51 [file] [log] [blame]
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001/* float.c floating-point constant support for the Netwide Assembler
2 *
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.
7 *
8 * initial version 13/ix/96 by Simon Tatham
9 */
10
H. Peter Anvinfe501952007-10-02 21:53:51 -070011#include "compiler.h"
12
H. Peter Anvinfe2177f2007-09-18 18:31:26 -070013#include <ctype.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000014#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000017#include <inttypes.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000018
19#include "nasm.h"
H. Peter Anvin214f5492007-10-15 19:46:32 -070020#include "float.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000021
22/*
H. Peter Anvin214f5492007-10-15 19:46:32 -070023 * -----------------
24 * local variables
25 * -----------------
26 */
27static efunc error;
28static bool daz = false; /* denormals as zero */
29static enum float_round rc = FLOAT_RC_NEAR; /* rounding control */
30
31/*
32 * -----------
33 * constants
34 * -----------
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000035 */
36
H. Peter Anvin214f5492007-10-15 19:46:32 -070037/* 112 bits + 64 bits for accuracy + 16 bits for rounding */
38#define MANT_WORDS 12
39
40/* 52 digits fit in 176 bits because 10^53 > 2^176 > 10^52 */
41#define MANT_DIGITS 52
42
43/* the format and the argument list depend on MANT_WORDS */
44#define MANT_FMT "%04x%04x_%04x%04x_%04x%04x_%04x%04x_%04x%04x_%04x%04x"
45#define MANT_ARG SOME_ARG(mant, 0)
46
47#define SOME_ARG(a,i) (a)[(i)+0], (a)[(i)+1], (a)[(i)+2], (a)[(i)+3], \
48 (a)[(i)+4], (a)[(i)+5], (a)[(i)+6], (a)[(i)+7], (a)[(i)+8], \
49 (a)[(i)+9], (a)[(i)+10], (a)[(i)+11]
50
51/*
52 * ---------------------------------------------------------------------------
53 * emit a printf()-like debug message... but only if DEBUG_FLOAT was defined
54 * ---------------------------------------------------------------------------
55 */
56
57#ifdef DEBUG_FLOAT
58#define dprintf(x) printf x
59#else /* */
60#define dprintf(x) do { } while (0)
61#endif /* */
62
63/*
64 * ---------------------------------------------------------------------------
65 * multiply
66 * ---------------------------------------------------------------------------
67 */
68static int float_multiply(uint16_t * to, uint16_t * from)
H. Peter Anvineba20a72002-04-30 20:53:55 +000069{
Keith Kaniosb7a89542007-04-12 02:40:54 +000070 uint32_t temp[MANT_WORDS * 2];
H. Peter Anvin214f5492007-10-15 19:46:32 -070071 int32_t i, j;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000072
H. Peter Anvin214f5492007-10-15 19:46:32 -070073 /*
74 * guaranteed that top bit of 'from' is set -- so we only have
75 * to worry about _one_ bit shift to the left
76 */
77 dprintf(("%s=" MANT_FMT "\n", "mul1", SOME_ARG(to, 0)));
78 dprintf(("%s=" MANT_FMT "\n", "mul2", SOME_ARG(from, 0)));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000079
H. Peter Anvin214f5492007-10-15 19:46:32 -070080 memset(temp, 0, sizeof temp);
81
82 for (i = 0; i < MANT_WORDS; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +000083 for (j = 0; j < MANT_WORDS; j++) {
Keith Kaniosb7a89542007-04-12 02:40:54 +000084 uint32_t n;
H. Peter Anvin214f5492007-10-15 19:46:32 -070085 n = (uint32_t) to[i] * (uint32_t) from[j];
H. Peter Anvine2c80182005-01-15 22:15:51 +000086 temp[i + j] += n >> 16;
87 temp[i + j + 1] += n & 0xFFFF;
88 }
H. Peter Anvin214f5492007-10-15 19:46:32 -070089 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000090
H. Peter Anvine2c80182005-01-15 22:15:51 +000091 for (i = MANT_WORDS * 2; --i;) {
92 temp[i - 1] += temp[i] >> 16;
93 temp[i] &= 0xFFFF;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000094 }
H. Peter Anvin214f5492007-10-15 19:46:32 -070095
96 dprintf(("%s=" MANT_FMT "_" MANT_FMT "\n", "temp", SOME_ARG(temp, 0),
97 SOME_ARG(temp, MANT_WORDS)));
98
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000099 if (temp[0] & 0x8000) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700100 for (i = 0; i < MANT_WORDS; i++) {
101 to[i] = temp[i] & 0xFFFF;
102 }
103 dprintf(("%s=" MANT_FMT " (%i)\n", "prod", SOME_ARG(to, 0), 0));
104 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000105 } else {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700106 for (i = 0; i < MANT_WORDS; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107 to[i] = (temp[i] << 1) + !!(temp[i + 1] & 0x8000);
H. Peter Anvin214f5492007-10-15 19:46:32 -0700108 }
109 dprintf(("%s=" MANT_FMT " (%i)\n", "prod", SOME_ARG(to, 0), -1));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000110 return -1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000111 }
112}
113
H. Peter Anvin214f5492007-10-15 19:46:32 -0700114/*
115 * ---------------------------------------------------------------------------
116 * convert
117 * ---------------------------------------------------------------------------
118 */
119static bool ieee_flconvert(const char *string, uint16_t * mant,
120 int32_t * exponent)
121{
122 char digits[MANT_DIGITS];
123 char *p, *q, *r;
124 uint16_t mult[MANT_WORDS], bit;
125 uint16_t *m;
126 int32_t tenpwr, twopwr;
127 int32_t extratwos;
128 bool started, seendot, warned;
129 p = digits;
130 tenpwr = 0;
131 started = seendot = warned = false;
132 while (*string && *string != 'E' && *string != 'e') {
133 if (*string == '.') {
134 if (!seendot) {
135 seendot = true;
136 } else {
137 error(ERR_NONFATAL,
138 "too many periods in floating-point constant");
139 return false;
140 }
141 } else if (*string >= '0' && *string <= '9') {
142 if (*string == '0' && !started) {
143 if (seendot) {
144 tenpwr--;
145 }
146 } else {
147 started = true;
148 if (p < digits + sizeof(digits)) {
149 *p++ = *string - '0';
150 } else {
151 if (!warned) {
152 error(ERR_WARNING,
153 "floating-point constant significand contains "
154 "more than %i digits", MANT_DIGITS);
155 warned = true;
156 }
157 }
158 if (!seendot) {
159 tenpwr++;
160 }
161 }
162 } else if (*string == '_') {
163
164 /* do nothing */
165 } else {
166 error(ERR_NONFATAL,
167 "invalid character in floating-point constant %s: '%c'",
168 "significand", *string);
169 return false;
170 }
171 string++;
172 }
173 if (*string) {
174 int32_t i = 0;
175 bool neg = false;
176 string++; /* eat the E */
177 if (*string == '+') {
178 string++;
179 } else if (*string == '-') {
180 neg = true;
181 string++;
182 }
183 while (*string) {
184 if (*string >= '0' && *string <= '9') {
185 i = (i * 10) + (*string - '0');
186
187 /*
188 * To ensure that underflows and overflows are
189 * handled properly we must avoid wraparounds of
190 * the signed integer value that is used to hold
191 * the exponent. Therefore we cap the exponent at
192 * +/-5000, which is slightly more/less than
193 * what's required for normal and denormal numbers
194 * in single, double, and extended precision, but
195 * sufficient to avoid signed integer wraparound.
196 */
197 if (i > 5000) {
198 break;
199 }
200 } else if (*string == '_') {
201
202 /* do nothing */
203 } else {
204 error(ERR_NONFATAL,
205 "invalid character in floating-point constant %s: '%c'",
206 "exponent", *string);
207 return false;
208 }
209 string++;
210 }
211 if (neg) {
212 i = 0 - i;
213 }
214 tenpwr += i;
215 }
216
217 /*
218 * At this point, the memory interval [digits,p) contains a
219 * series of decimal digits zzzzzzz, such that our number X
220 * satisfies X = 0.zzzzzzz * 10^tenpwr.
221 */
222 q = digits;
223 dprintf(("X = 0."));
224 while (q < p) {
225 dprintf(("%c", *q + '0'));
226 q++;
227 }
228 dprintf((" * 10^%i\n", tenpwr));
229
230 /*
231 * Now convert [digits,p) to our internal representation.
232 */
233 bit = 0x8000;
234 for (m = mant; m < mant + MANT_WORDS; m++) {
235 *m = 0;
236 }
237 m = mant;
238 q = digits;
239 started = false;
240 twopwr = 0;
241 while (m < mant + MANT_WORDS) {
242 uint16_t carry = 0;
243 while (p > q && !p[-1]) {
244 p--;
245 }
246 if (p <= q) {
247 break;
248 }
249 for (r = p; r-- > q;) {
250 int32_t i;
251 i = 2 * *r + carry;
252 if (i >= 10) {
253 carry = 1;
254 i -= 10;
255 } else {
256 carry = 0;
257 }
258 *r = i;
259 }
260 if (carry) {
261 *m |= bit;
262 started = true;
263 }
264 if (started) {
265 if (bit == 1) {
266 bit = 0x8000;
267 m++;
268 } else {
269 bit >>= 1;
270 }
271 } else {
272 twopwr--;
273 }
274 }
275 twopwr += tenpwr;
276
277 /*
278 * At this point, the 'mant' array contains the first frac-
279 * tional places of a base-2^16 real number which when mul-
280 * tiplied by 2^twopwr and 5^tenpwr gives X.
281 */
282 dprintf(("X = " MANT_FMT " * 2^%i * 5^%i\n", MANT_ARG, twopwr,
283 tenpwr));
284
285 /*
286 * Now multiply 'mant' by 5^tenpwr.
287 */
288 if (tenpwr < 0) { /* mult = 5^-1 = 0.2 */
289 for (m = mult; m < mult + MANT_WORDS - 1; m++) {
290 *m = 0xCCCC;
291 }
292 mult[MANT_WORDS - 1] = 0xCCCD;
293 extratwos = -2;
294 tenpwr = -tenpwr;
295
296 /*
297 * If tenpwr was 1000...000b, then it becomes 1000...000b. See
298 * the "ANSI C" comment below for more details on that case.
299 *
300 * Because we already truncated tenpwr to +5000...-5000 inside
301 * the exponent parsing code, this shouldn't happen though.
302 */
303 } else if (tenpwr > 0) { /* mult = 5^+1 = 5.0 */
304 mult[0] = 0xA000;
305 for (m = mult + 1; m < mult + MANT_WORDS; m++) {
306 *m = 0;
307 }
308 extratwos = 3;
309 } else {
310 extratwos = 0;
311 }
312 while (tenpwr) {
313 dprintf(("loop=" MANT_FMT " * 2^%i * 5^%i (%i)\n", MANT_ARG,
314 twopwr, tenpwr, extratwos));
315 if (tenpwr & 1) {
316 dprintf(("mant*mult\n"));
317 twopwr += extratwos + float_multiply(mant, mult);
318 }
319 dprintf(("mult*mult\n"));
320 extratwos = extratwos * 2 + float_multiply(mult, mult);
321 tenpwr >>= 1;
322
323 /*
324 * In ANSI C, the result of right-shifting a signed integer is
325 * considered implementation-specific. To ensure that the loop
326 * terminates even if tenpwr was 1000...000b to begin with, we
327 * manually clear the MSB, in case a 1 was shifted in.
328 *
329 * Because we already truncated tenpwr to +5000...-5000 inside
330 * the exponent parsing code, this shouldn't matter; neverthe-
331 * less it is the right thing to do here.
332 */
333 tenpwr &= (uint32_t) - 1 >> 1;
334 }
335
336 /*
337 * At this point, the 'mant' array contains the first frac-
338 * tional places of a base-2^16 real number in [0.5,1) that
339 * when multiplied by 2^twopwr gives X. Or it contains zero
340 * of course. We are done.
341 */
342 *exponent = twopwr;
343 return true;
344}
345
346/*
347 * ---------------------------------------------------------------------------
348 * round a mantissa off after i words
349 * ---------------------------------------------------------------------------
350 */
351
352#define ROUND_COLLECT_BITS \
353 for (j = i; j < MANT_WORDS; j++) { \
354 m = m | mant[j]; \
355 }
356
357#define ROUND_ABS_DOWN \
358 for (j = i; j < MANT_WORDS; j++) { \
359 mant[j] = 0x0000; \
360 }
361
362#define ROUND_ABS_UP \
363 do { \
364 ++mant[--i]; \
365 mant[i] &= 0xFFFF; \
366 } while (i > 0 && !mant[i]); \
367 return (!i && !mant[i]);
368
369static int32_t ieee_round(int sign, uint16_t * mant, int32_t i)
370{
371 uint16_t m = 0;
372 int32_t j;
373 if ((sign == 0x0000) || (sign == 0x8000)) {
374 if (rc == FLOAT_RC_NEAR) {
375 if (mant[i] & 0x8000) {
376 mant[i] &= 0x7FFF;
377 ROUND_COLLECT_BITS;
378 mant[i] |= 0x8000;
379 if (m) {
380 ROUND_ABS_UP;
381 } else {
382 if (mant[i - 1] & 1) {
383 ROUND_ABS_UP;
384 } else {
385 ROUND_ABS_DOWN;
386 }
387 }
388 } else {
389 ROUND_ABS_DOWN;
390 }
391 } else if (((sign == 0x0000) && (rc == FLOAT_RC_DOWN))
392 || ((sign == 0x8000) && (rc == FLOAT_RC_UP))) {
393 ROUND_COLLECT_BITS;
394 if (m) {
395 ROUND_ABS_DOWN;
396 }
397 } else if (((sign == 0x0000) && (rc == FLOAT_RC_UP))
398 || ((sign == 0x8000) && (rc == FLOAT_RC_DOWN))) {
399 ROUND_COLLECT_BITS;
400 if (m) {
401 ROUND_ABS_UP;
402 }
403 } else if (rc == FLOAT_RC_ZERO) {
404 ROUND_ABS_DOWN;
405 } else {
406 error(ERR_PANIC, "float_round() can't handle rc=%i", rc);
407 }
408 } else {
409 error(ERR_PANIC, "float_round() can't handle sign=%i", sign);
410 }
411 return (0);
412}
413
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700414static int hexval(char c)
415{
416 if (c >= '0' && c <= '9')
H. Peter Anvin214f5492007-10-15 19:46:32 -0700417 return c - '0';
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700418 else if (c >= 'a' && c <= 'f')
H. Peter Anvin214f5492007-10-15 19:46:32 -0700419 return c - 'a' + 10;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700420 else
H. Peter Anvin214f5492007-10-15 19:46:32 -0700421 return c - 'A' + 10;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700422}
423
H. Peter Anvin214f5492007-10-15 19:46:32 -0700424static void ieee_flconvert_hex(const char *string, uint16_t * mant,
425 int32_t * exponent)
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700426{
427 static const int log2tbl[16] =
H. Peter Anvin214f5492007-10-15 19:46:32 -0700428 { -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3 };
429 uint16_t mult[MANT_WORDS + 1], *mp;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700430 int ms;
431 int32_t twopwr;
432 int seendot, seendigit;
433 unsigned char c;
434
435 twopwr = 0;
436 seendot = seendigit = 0;
H. Peter Anvin82f9f632007-09-24 20:53:48 -0700437 ms = 0;
438 mp = NULL;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700439
440 memset(mult, 0, sizeof mult);
441
442 while ((c = *string++) != '\0') {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700443 if (c == '.') {
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700444 if (!seendot)
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700445 seendot = true;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700446 else {
447 error(ERR_NONFATAL,
448 "too many periods in floating-point constant");
449 return;
450 }
H. Peter Anvin214f5492007-10-15 19:46:32 -0700451 } else if (isxdigit(c)) {
452 int v = hexval(c);
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700453
H. Peter Anvin214f5492007-10-15 19:46:32 -0700454 if (!seendigit && v) {
455 int l = log2tbl[v];
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700456
H. Peter Anvin214f5492007-10-15 19:46:32 -0700457 seendigit = 1;
458 mp = mult;
459 ms = 15 - l;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700460
H. Peter Anvin214f5492007-10-15 19:46:32 -0700461 twopwr = seendot ? twopwr - 4 + l : l - 3;
462 }
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700463
H. Peter Anvin214f5492007-10-15 19:46:32 -0700464 if (seendigit) {
465 if (ms <= 0) {
466 *mp |= v >> -ms;
467 mp++;
468 if (mp > &mult[MANT_WORDS])
469 mp = &mult[MANT_WORDS]; /* Guard slot */
470 ms += 16;
471 }
472 *mp |= v << ms;
473 ms -= 4;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700474
H. Peter Anvin214f5492007-10-15 19:46:32 -0700475 if (!seendot)
476 twopwr += 4;
477 } else {
478 if (seendot)
479 twopwr -= 4;
480 }
481 } else if (c == 'p' || c == 'P') {
482 twopwr += atoi(string);
483 break;
484 } else {
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700485 error(ERR_NONFATAL,
H. Peter Anvin214f5492007-10-15 19:46:32 -0700486 "floating-point constant: `%c' is invalid character", c);
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700487 return;
488 }
489 }
490
491 if (!seendigit) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700492 memset(mant, 0, 2 * MANT_WORDS); /* Zero */
493 *exponent = 0;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700494 } else {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700495 memcpy(mant, mult, 2 * MANT_WORDS);
496 *exponent = twopwr;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700497 }
498}
499
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000500/*
501 * Shift a mantissa to the right by i (i < 16) bits.
502 */
H. Peter Anvin214f5492007-10-15 19:46:32 -0700503static void ieee_shr(uint16_t * mant, int i)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000504{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000505 uint16_t n = 0, m;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000506 int j;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000507
H. Peter Anvine2c80182005-01-15 22:15:51 +0000508 for (j = 0; j < MANT_WORDS; j++) {
509 m = (mant[j] << (16 - i)) & 0xFFFF;
510 mant[j] = (mant[j] >> i) | n;
511 n = m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000512 }
513}
514
H. Peter Anvin214f5492007-10-15 19:46:32 -0700515#if defined(__i386__) || defined(__x86_64__)
516#define put(a,b) (*(uint16_t *)(a) = (b))
517#else
518#define put(a,b) (((a)[0] = (b)), ((a)[1] = (b) >> 8))
519#endif
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000520
H. Peter Anvinf48bc6f2007-09-18 21:55:56 -0700521/* Set a bit, using *bigendian* bit numbering (0 = MSB) */
H. Peter Anvin214f5492007-10-15 19:46:32 -0700522static void set_bit(uint16_t * mant, int bit)
H. Peter Anvinf48bc6f2007-09-18 21:55:56 -0700523{
524 mant[bit >> 4] |= 1 << (~bit & 15);
525}
526
H. Peter Anvine31747e2007-09-18 17:50:34 -0700527/* Produce standard IEEE formats, with implicit "1" bit; this makes
528 the following assumptions:
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000529
H. Peter Anvine31747e2007-09-18 17:50:34 -0700530 - the sign bit is the MSB, followed by the exponent.
531 - the sign bit plus exponent fit in 16 bits.
532 - the exponent bias is 2^(n-1)-1 for an n-bit exponent */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000533
H. Peter Anvine31747e2007-09-18 17:50:34 -0700534struct ieee_format {
535 int words;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700536 int mantissa; /* Bits in the mantissa */
537 int exponent; /* Bits in the exponent */
H. Peter Anvine31747e2007-09-18 17:50:34 -0700538};
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000539
H. Peter Anvin214f5492007-10-15 19:46:32 -0700540static const struct ieee_format ieee_16 = { 1, 10, 5 };
541static const struct ieee_format ieee_32 = { 2, 23, 8 };
542static const struct ieee_format ieee_64 = { 4, 52, 11 };
H. Peter Anvine31747e2007-09-18 17:50:34 -0700543static const struct ieee_format ieee_128 = { 8, 112, 15 };
544
545/* Produce all the standard IEEE formats: 16, 32, 64, and 128 bits */
H. Peter Anvin214f5492007-10-15 19:46:32 -0700546static int to_float(const char *str, int sign, uint8_t * result,
547 const struct ieee_format *fmt)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000548{
H. Peter Anvine31747e2007-09-18 17:50:34 -0700549 uint16_t mant[MANT_WORDS], *mp;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000550 int32_t exponent;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700551 int32_t expmax = 1 << (fmt->exponent - 1);
H. Peter Anvine31747e2007-09-18 17:50:34 -0700552 uint16_t implicit_one = 0x8000 >> fmt->exponent;
553 int i;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000554
555 sign = (sign < 0 ? 0x8000L : 0L);
556
H. Peter Anvinf48bc6f2007-09-18 21:55:56 -0700557 if (str[0] == '_') {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700558 /* NaN or Infinity */
559 int32_t expmask = (1 << fmt->exponent) - 1;
H. Peter Anvine31747e2007-09-18 17:50:34 -0700560
H. Peter Anvin214f5492007-10-15 19:46:32 -0700561 memset(mant, 0, sizeof mant);
562 mant[0] = expmask << (15 - fmt->exponent); /* Exponent: all bits one */
H. Peter Anvine31747e2007-09-18 17:50:34 -0700563
H. Peter Anvin214f5492007-10-15 19:46:32 -0700564 switch (str[2]) {
565 case 'n': /* __nan__ */
566 case 'N':
567 case 'q': /* __qnan__ */
568 case 'Q':
569 set_bit(mant, fmt->exponent + 1); /* Highest bit in mantissa */
570 break;
571 case 's': /* __snan__ */
572 case 'S':
573 set_bit(mant, fmt->exponent + fmt->mantissa); /* Last bit */
574 break;
575 case 'i': /* __infinity__ */
576 case 'I':
577 break;
578 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000579 } else {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700580 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
581 ieee_flconvert_hex(str + 2, mant, &exponent);
582 else
583 ieee_flconvert(str, mant, &exponent);
584
585 if (mant[0] & 0x8000) {
586 /*
587 * Non-zero.
588 */
589 exponent--;
590 if (exponent >= 2 - expmax && exponent <= expmax) {
591 /*
592 * Normalised.
593 */
594 exponent += expmax - 1;
595 ieee_shr(mant, fmt->exponent);
596 ieee_round(sign, mant, fmt->words);
597 /* did we scale up by one? */
598 if (mant[0] & (implicit_one << 1)) {
599 ieee_shr(mant, 1);
600 exponent++;
601 }
602
603 mant[0] &= (implicit_one - 1); /* remove leading one */
604 mant[0] |= exponent << (15 - fmt->exponent);
605 } else if (!daz && exponent < 2 - expmax &&
606 exponent >= 2 - expmax - fmt->mantissa) {
607 /*
608 * Denormal.
609 */
610 int shift = -(exponent + expmax - 2 - fmt->exponent);
611 int sh = shift % 16, wds = shift / 16;
612 ieee_shr(mant, sh);
613 if (ieee_round(sign, mant, fmt->words - wds)
614 || (sh > 0 && (mant[0] & (0x8000 >> (sh - 1))))) {
615 ieee_shr(mant, 1);
616 if (sh == 0)
617 mant[0] |= 0x8000;
618 exponent++;
619 }
620
621 if (wds) {
622 for (i = fmt->words - 1; i >= wds; i--)
623 mant[i] = mant[i - wds];
624 for (; i >= 0; i--)
625 mant[i] = 0;
626 }
627 } else {
628 if (exponent > 0) {
629 error(ERR_NONFATAL,
630 "overflow in floating-point constant");
631 /* We should generate Inf here */
632 return 0;
633 } else {
634 memset(mant, 0, 2 * fmt->words);
635 }
636 }
637 } else {
638 /* Zero */
639 memset(mant, 0, 2 * fmt->words);
640 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000641 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000642
H. Peter Anvine31747e2007-09-18 17:50:34 -0700643 mant[0] |= sign;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700644
H. Peter Anvine31747e2007-09-18 17:50:34 -0700645 for (mp = &mant[fmt->words], i = 0; i < fmt->words; i++) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700646 uint16_t m = *--mp;
647 put(result, m);
648 result += 2;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700649 }
H. Peter Anvine31747e2007-09-18 17:50:34 -0700650
651 return 1; /* success */
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700652}
653
654/* 80-bit format with 64-bit mantissa *including an explicit integer 1*
655 and 15-bit exponent. */
H. Peter Anvin214f5492007-10-15 19:46:32 -0700656static int to_ldoub(const char *str, int sign, uint8_t * result)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000657{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000658 uint16_t mant[MANT_WORDS];
659 int32_t exponent;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000660
661 sign = (sign < 0 ? 0x8000L : 0L);
662
H. Peter Anvinf48bc6f2007-09-18 21:55:56 -0700663 if (str[0] == '_') {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700664 uint16_t is_snan = 0, is_qnan = 0x8000;
665 switch (str[2]) {
666 case 'n':
667 case 'N':
668 case 'q':
669 case 'Q':
670 is_qnan = 0xc000;
671 break;
672 case 's':
673 case 'S':
674 is_snan = 1;
675 break;
676 case 'i':
677 case 'I':
678 break;
679 }
680 put(result + 0, is_snan);
681 put(result + 2, 0);
682 put(result + 4, 0);
683 put(result + 6, is_qnan);
684 put(result + 8, 0x7fff | sign);
685 return 1;
H. Peter Anvinf48bc6f2007-09-18 21:55:56 -0700686 }
687
H. Peter Anvin214f5492007-10-15 19:46:32 -0700688 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
689 ieee_flconvert_hex(str + 2, mant, &exponent);
690 else
691 ieee_flconvert(str, mant, &exponent);
692
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000693 if (mant[0] & 0x8000) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000694 /*
695 * Non-zero.
696 */
697 exponent--;
698 if (exponent >= -16383 && exponent <= 16384) {
699 /*
700 * Normalised.
701 */
702 exponent += 16383;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700703 if (ieee_round(sign, mant, 4)) /* did we scale up by one? */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000704 ieee_shr(mant, 1), mant[0] |= 0x8000, exponent++;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000705 put(result + 0, mant[3]);
H. Peter Anvinf48bc6f2007-09-18 21:55:56 -0700706 put(result + 2, mant[2]);
707 put(result + 4, mant[1]);
708 put(result + 6, mant[0]);
709 put(result + 8, exponent | sign);
H. Peter Anvin214f5492007-10-15 19:46:32 -0700710 } else if (!daz && exponent < -16383 && exponent >= -16446) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000711 /*
712 * Denormal.
713 */
714 int shift = -(exponent + 16383);
715 int sh = shift % 16, wds = shift / 16;
716 ieee_shr(mant, sh);
H. Peter Anvin214f5492007-10-15 19:46:32 -0700717 if (ieee_round(sign, mant, 4 - wds)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000718 || (sh > 0 && (mant[0] & (0x8000 >> (sh - 1))))) {
719 ieee_shr(mant, 1);
720 if (sh == 0)
721 mant[0] |= 0x8000;
722 exponent++;
723 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000724 put(result + 0, (wds <= 3 ? mant[3 - wds] : 0));
H. Peter Anvinf48bc6f2007-09-18 21:55:56 -0700725 put(result + 2, (wds <= 2 ? mant[2 - wds] : 0));
726 put(result + 4, (wds <= 1 ? mant[1 - wds] : 0));
727 put(result + 6, (wds == 0 ? mant[0] : 0));
728 put(result + 8, sign);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000729 } else {
730 if (exponent > 0) {
731 error(ERR_NONFATAL, "overflow in floating-point constant");
H. Peter Anvin214f5492007-10-15 19:46:32 -0700732 /* We should generate Inf here */
H. Peter Anvine2c80182005-01-15 22:15:51 +0000733 return 0;
H. Peter Anvinf48bc6f2007-09-18 21:55:56 -0700734 } else {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700735 goto zero;
736 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000737 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000738 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000739 /*
740 * Zero.
741 */
H. Peter Anvin214f5492007-10-15 19:46:32 -0700742 zero:
743 put(result + 0, 0);
744 put(result + 2, 0);
745 put(result + 4, 0);
746 put(result + 6, 0);
747 put(result + 8, sign);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000748 }
749 return 1;
750}
751
H. Peter Anvin214f5492007-10-15 19:46:32 -0700752int float_const(const char *number, int32_t sign, uint8_t * result,
753 int bytes, efunc err)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000754{
H. Peter Anvin214f5492007-10-15 19:46:32 -0700755 error = err;
756
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700757 switch (bytes) {
758 case 2:
H. Peter Anvin214f5492007-10-15 19:46:32 -0700759 return to_float(number, sign, result, &ieee_16);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700760 case 4:
H. Peter Anvin214f5492007-10-15 19:46:32 -0700761 return to_float(number, sign, result, &ieee_32);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700762 case 8:
H. Peter Anvin214f5492007-10-15 19:46:32 -0700763 return to_float(number, sign, result, &ieee_64);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700764 case 10:
H. Peter Anvin214f5492007-10-15 19:46:32 -0700765 return to_ldoub(number, sign, result);
H. Peter Anvine31747e2007-09-18 17:50:34 -0700766 case 16:
H. Peter Anvin214f5492007-10-15 19:46:32 -0700767 return to_float(number, sign, result, &ieee_128);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700768 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000769 error(ERR_PANIC, "strange value %d passed to float_const", bytes);
770 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000771 }
772}