Add wrappers around fopen(), use mmap on glibc
[nasm.git] / float.c
blob6cd1456102380f25fdad4230a0aae01153bd032c
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2009 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * float.c floating-point constant support for the Netwide Assembler
38 #include "compiler.h"
40 #include <ctype.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
45 #include "nasm.h"
46 #include "float.h"
49 * -----------------
50 * local variables
51 * -----------------
53 static bool daz = false; /* denormals as zero */
54 static enum float_round rc = FLOAT_RC_NEAR; /* rounding control */
57 * -----------
58 * constants
59 * -----------
62 /* "A limb is like a digit but bigger */
63 typedef uint32_t fp_limb;
64 typedef uint64_t fp_2limb;
66 #define LIMB_BITS 32
67 #define LIMB_BYTES (LIMB_BITS/8)
68 #define LIMB_TOP_BIT ((fp_limb)1 << (LIMB_BITS-1))
69 #define LIMB_MASK ((fp_limb)(~0))
70 #define LIMB_ALL_BYTES ((fp_limb)0x01010101)
71 #define LIMB_BYTE(x) ((x)*LIMB_ALL_BYTES)
73 /* 112 bits + 64 bits for accuracy + 16 bits for rounding */
74 #define MANT_LIMBS 6
76 /* 52 digits fit in 176 bits because 10^53 > 2^176 > 10^52 */
77 #define MANT_DIGITS 52
79 /* the format and the argument list depend on MANT_LIMBS */
80 #define MANT_FMT "%08x_%08x_%08x_%08x_%08x_%08x"
81 #define MANT_ARG SOME_ARG(mant, 0)
83 #define SOME_ARG(a,i) (a)[(i)+0], (a)[(i)+1], (a)[(i)+2], \
84 (a)[(i)+3], (a)[(i)+4], (a)[(i)+5]
87 * ---------------------------------------------------------------------------
88 * emit a printf()-like debug message... but only if DEBUG_FLOAT was defined
89 * ---------------------------------------------------------------------------
92 #ifdef DEBUG_FLOAT
93 #define dprintf(x) printf x
94 #else
95 #define dprintf(x) do { } while (0)
96 #endif
99 * ---------------------------------------------------------------------------
100 * multiply
101 * ---------------------------------------------------------------------------
103 static int float_multiply(fp_limb *to, fp_limb *from)
105 fp_2limb temp[MANT_LIMBS * 2];
106 int i, j;
109 * guaranteed that top bit of 'from' is set -- so we only have
110 * to worry about _one_ bit shift to the left
112 dprintf(("%s=" MANT_FMT "\n", "mul1", SOME_ARG(to, 0)));
113 dprintf(("%s=" MANT_FMT "\n", "mul2", SOME_ARG(from, 0)));
115 memset(temp, 0, sizeof temp);
117 for (i = 0; i < MANT_LIMBS; i++) {
118 for (j = 0; j < MANT_LIMBS; j++) {
119 fp_2limb n;
120 n = (fp_2limb) to[i] * (fp_2limb) from[j];
121 temp[i + j] += n >> LIMB_BITS;
122 temp[i + j + 1] += (fp_limb)n;
126 for (i = MANT_LIMBS * 2; --i;) {
127 temp[i - 1] += temp[i] >> LIMB_BITS;
128 temp[i] &= LIMB_MASK;
131 dprintf(("%s=" MANT_FMT "_" MANT_FMT "\n", "temp", SOME_ARG(temp, 0),
132 SOME_ARG(temp, MANT_LIMBS)));
134 if (temp[0] & LIMB_TOP_BIT) {
135 for (i = 0; i < MANT_LIMBS; i++) {
136 to[i] = temp[i] & LIMB_MASK;
138 dprintf(("%s=" MANT_FMT " (%i)\n", "prod", SOME_ARG(to, 0), 0));
139 return 0;
140 } else {
141 for (i = 0; i < MANT_LIMBS; i++) {
142 to[i] = (temp[i] << 1) + !!(temp[i + 1] & LIMB_TOP_BIT);
144 dprintf(("%s=" MANT_FMT " (%i)\n", "prod", SOME_ARG(to, 0), -1));
145 return -1;
150 * ---------------------------------------------------------------------------
151 * read an exponent; returns INT32_MAX on error
152 * ---------------------------------------------------------------------------
154 static int32_t read_exponent(const char *string, int32_t max)
156 int32_t i = 0;
157 bool neg = false;
159 if (*string == '+') {
160 string++;
161 } else if (*string == '-') {
162 neg = true;
163 string++;
165 while (*string) {
166 if (*string >= '0' && *string <= '9') {
167 i = (i * 10) + (*string - '0');
170 * To ensure that underflows and overflows are
171 * handled properly we must avoid wraparounds of
172 * the signed integer value that is used to hold
173 * the exponent. Therefore we cap the exponent at
174 * +/-5000, which is slightly more/less than
175 * what's required for normal and denormal numbers
176 * in single, double, and extended precision, but
177 * sufficient to avoid signed integer wraparound.
179 if (i > max)
180 i = max;
181 } else if (*string == '_') {
182 /* do nothing */
183 } else {
184 nasm_error(ERR_NONFATAL|ERR_PASS1,
185 "invalid character in floating-point constant %s: '%c'",
186 "exponent", *string);
187 return INT32_MAX;
189 string++;
192 return neg ? -i : i;
196 * ---------------------------------------------------------------------------
197 * convert
198 * ---------------------------------------------------------------------------
200 static bool ieee_flconvert(const char *string, fp_limb *mant,
201 int32_t * exponent)
203 char digits[MANT_DIGITS];
204 char *p, *q, *r;
205 fp_limb mult[MANT_LIMBS], bit;
206 fp_limb *m;
207 int32_t tenpwr, twopwr;
208 int32_t extratwos;
209 bool started, seendot, warned;
211 warned = false;
212 p = digits;
213 tenpwr = 0;
214 started = seendot = false;
216 while (*string && *string != 'E' && *string != 'e') {
217 if (*string == '.') {
218 if (!seendot) {
219 seendot = true;
220 } else {
221 nasm_error(ERR_NONFATAL|ERR_PASS1,
222 "too many periods in floating-point constant");
223 return false;
225 } else if (*string >= '0' && *string <= '9') {
226 if (*string == '0' && !started) {
227 if (seendot) {
228 tenpwr--;
230 } else {
231 started = true;
232 if (p < digits + sizeof(digits)) {
233 *p++ = *string - '0';
234 } else {
235 if (!warned) {
236 nasm_error(ERR_WARNING|ERR_WARN_FL_TOOLONG|ERR_PASS1,
237 "floating-point constant significand contains "
238 "more than %i digits", MANT_DIGITS);
239 warned = true;
242 if (!seendot) {
243 tenpwr++;
246 } else if (*string == '_') {
247 /* do nothing */
248 } else {
249 nasm_error(ERR_NONFATAL|ERR_PASS1,
250 "invalid character in floating-point constant %s: '%c'",
251 "significand", *string);
252 return false;
254 string++;
257 if (*string) {
258 int32_t e;
260 string++; /* eat the E */
261 e = read_exponent(string, 5000);
262 if (e == INT32_MAX)
263 return false;
264 tenpwr += e;
268 * At this point, the memory interval [digits,p) contains a
269 * series of decimal digits zzzzzzz, such that our number X
270 * satisfies X = 0.zzzzzzz * 10^tenpwr.
272 q = digits;
273 dprintf(("X = 0."));
274 while (q < p) {
275 dprintf(("%c", *q + '0'));
276 q++;
278 dprintf((" * 10^%i\n", tenpwr));
281 * Now convert [digits,p) to our internal representation.
283 bit = LIMB_TOP_BIT;
284 for (m = mant; m < mant + MANT_LIMBS; m++) {
285 *m = 0;
287 m = mant;
288 q = digits;
289 started = false;
290 twopwr = 0;
291 while (m < mant + MANT_LIMBS) {
292 fp_limb carry = 0;
293 while (p > q && !p[-1]) {
294 p--;
296 if (p <= q) {
297 break;
299 for (r = p; r-- > q;) {
300 int32_t i;
301 i = 2 * *r + carry;
302 if (i >= 10) {
303 carry = 1;
304 i -= 10;
305 } else {
306 carry = 0;
308 *r = i;
310 if (carry) {
311 *m |= bit;
312 started = true;
314 if (started) {
315 if (bit == 1) {
316 bit = LIMB_TOP_BIT;
317 m++;
318 } else {
319 bit >>= 1;
321 } else {
322 twopwr--;
325 twopwr += tenpwr;
328 * At this point, the 'mant' array contains the first frac-
329 * tional places of a base-2^16 real number which when mul-
330 * tiplied by 2^twopwr and 5^tenpwr gives X.
332 dprintf(("X = " MANT_FMT " * 2^%i * 5^%i\n", MANT_ARG, twopwr,
333 tenpwr));
336 * Now multiply 'mant' by 5^tenpwr.
338 if (tenpwr < 0) { /* mult = 5^-1 = 0.2 */
339 for (m = mult; m < mult + MANT_LIMBS - 1; m++) {
340 *m = LIMB_BYTE(0xcc);
342 mult[MANT_LIMBS - 1] = LIMB_BYTE(0xcc)+1;
343 extratwos = -2;
344 tenpwr = -tenpwr;
347 * If tenpwr was 1000...000b, then it becomes 1000...000b. See
348 * the "ANSI C" comment below for more details on that case.
350 * Because we already truncated tenpwr to +5000...-5000 inside
351 * the exponent parsing code, this shouldn't happen though.
353 } else if (tenpwr > 0) { /* mult = 5^+1 = 5.0 */
354 mult[0] = (fp_limb)5 << (LIMB_BITS-3); /* 0xA000... */
355 for (m = mult + 1; m < mult + MANT_LIMBS; m++) {
356 *m = 0;
358 extratwos = 3;
359 } else {
360 extratwos = 0;
362 while (tenpwr) {
363 dprintf(("loop=" MANT_FMT " * 2^%i * 5^%i (%i)\n", MANT_ARG,
364 twopwr, tenpwr, extratwos));
365 if (tenpwr & 1) {
366 dprintf(("mant*mult\n"));
367 twopwr += extratwos + float_multiply(mant, mult);
369 dprintf(("mult*mult\n"));
370 extratwos = extratwos * 2 + float_multiply(mult, mult);
371 tenpwr >>= 1;
374 * In ANSI C, the result of right-shifting a signed integer is
375 * considered implementation-specific. To ensure that the loop
376 * terminates even if tenpwr was 1000...000b to begin with, we
377 * manually clear the MSB, in case a 1 was shifted in.
379 * Because we already truncated tenpwr to +5000...-5000 inside
380 * the exponent parsing code, this shouldn't matter; neverthe-
381 * less it is the right thing to do here.
383 tenpwr &= (uint32_t) - 1 >> 1;
387 * At this point, the 'mant' array contains the first frac-
388 * tional places of a base-2^16 real number in [0.5,1) that
389 * when multiplied by 2^twopwr gives X. Or it contains zero
390 * of course. We are done.
392 *exponent = twopwr;
393 return true;
397 * ---------------------------------------------------------------------------
398 * operations of specific bits
399 * ---------------------------------------------------------------------------
402 /* Set a bit, using *bigendian* bit numbering (0 = MSB) */
403 static void set_bit(fp_limb *mant, int bit)
405 mant[bit/LIMB_BITS] |= LIMB_TOP_BIT >> (bit & (LIMB_BITS-1));
408 /* Test a single bit */
409 static int test_bit(const fp_limb *mant, int bit)
411 return (mant[bit/LIMB_BITS] >> (~bit & (LIMB_BITS-1))) & 1;
414 /* Report if the mantissa value is all zero */
415 static bool is_zero(const fp_limb *mant)
417 int i;
419 for (i = 0; i < MANT_LIMBS; i++)
420 if (mant[i])
421 return false;
423 return true;
427 * ---------------------------------------------------------------------------
428 * round a mantissa off after i words
429 * ---------------------------------------------------------------------------
432 #define ROUND_COLLECT_BITS \
433 do { \
434 m = mant[i] & (2*bit-1); \
435 for (j = i+1; j < MANT_LIMBS; j++) \
436 m = m | mant[j]; \
437 } while (0)
439 #define ROUND_ABS_DOWN \
440 do { \
441 mant[i] &= ~(bit-1); \
442 for (j = i+1; j < MANT_LIMBS; j++) \
443 mant[j] = 0; \
444 return false; \
445 } while (0)
447 #define ROUND_ABS_UP \
448 do { \
449 mant[i] = (mant[i] & ~(bit-1)) + bit; \
450 for (j = i+1; j < MANT_LIMBS; j++) \
451 mant[j] = 0; \
452 while (i > 0 && !mant[i]) \
453 ++mant[--i]; \
454 return !mant[0]; \
455 } while (0)
457 static bool ieee_round(bool minus, fp_limb *mant, int bits)
459 fp_limb m = 0;
460 int32_t j;
461 int i = bits / LIMB_BITS;
462 int p = bits % LIMB_BITS;
463 fp_limb bit = LIMB_TOP_BIT >> p;
465 if (rc == FLOAT_RC_NEAR) {
466 if (mant[i] & bit) {
467 mant[i] &= ~bit;
468 ROUND_COLLECT_BITS;
469 mant[i] |= bit;
470 if (m) {
471 ROUND_ABS_UP;
472 } else {
473 if (test_bit(mant, bits-1)) {
474 ROUND_ABS_UP;
475 } else {
476 ROUND_ABS_DOWN;
479 } else {
480 ROUND_ABS_DOWN;
482 } else if (rc == FLOAT_RC_ZERO ||
483 rc == (minus ? FLOAT_RC_UP : FLOAT_RC_DOWN)) {
484 ROUND_ABS_DOWN;
485 } else {
486 /* rc == (minus ? FLOAT_RC_DOWN : FLOAT_RC_UP) */
487 /* Round toward +/- infinity */
488 ROUND_COLLECT_BITS;
489 if (m) {
490 ROUND_ABS_UP;
491 } else {
492 ROUND_ABS_DOWN;
495 return false;
498 /* Returns a value >= 16 if not a valid hex digit */
499 static unsigned int hexval(char c)
501 unsigned int v = (unsigned char) c;
503 if (v >= '0' && v <= '9')
504 return v - '0';
505 else
506 return (v|0x20) - 'a' + 10;
509 /* Handle floating-point numbers with radix 2^bits and binary exponent */
510 static bool ieee_flconvert_bin(const char *string, int bits,
511 fp_limb *mant, int32_t *exponent)
513 static const int log2tbl[16] =
514 { -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3 };
515 fp_limb mult[MANT_LIMBS + 1], *mp;
516 int ms;
517 int32_t twopwr;
518 bool seendot, seendigit;
519 unsigned char c;
520 const int radix = 1 << bits;
521 fp_limb v;
523 twopwr = 0;
524 seendot = seendigit = false;
525 ms = 0;
526 mp = NULL;
528 memset(mult, 0, sizeof mult);
530 while ((c = *string++) != '\0') {
531 if (c == '.') {
532 if (!seendot)
533 seendot = true;
534 else {
535 nasm_error(ERR_NONFATAL|ERR_PASS1,
536 "too many periods in floating-point constant");
537 return false;
539 } else if ((v = hexval(c)) < (unsigned int)radix) {
540 if (!seendigit && v) {
541 int l = log2tbl[v];
543 seendigit = true;
544 mp = mult;
545 ms = (LIMB_BITS-1)-l;
547 twopwr = seendot ? twopwr-bits+l : l+1-bits;
550 if (seendigit) {
551 if (ms <= 0) {
552 *mp |= v >> -ms;
553 mp++;
554 if (mp > &mult[MANT_LIMBS])
555 mp = &mult[MANT_LIMBS]; /* Guard slot */
556 ms += LIMB_BITS;
558 *mp |= v << ms;
559 ms -= bits;
561 if (!seendot)
562 twopwr += bits;
563 } else {
564 if (seendot)
565 twopwr -= bits;
567 } else if (c == 'p' || c == 'P') {
568 int32_t e;
569 e = read_exponent(string, 20000);
570 if (e == INT32_MAX)
571 return false;
572 twopwr += e;
573 break;
574 } else if (c == '_') {
575 /* ignore */
576 } else {
577 nasm_error(ERR_NONFATAL|ERR_PASS1,
578 "floating-point constant: `%c' is invalid character", c);
579 return false;
583 if (!seendigit) {
584 memset(mant, 0, MANT_LIMBS*sizeof(fp_limb)); /* Zero */
585 *exponent = 0;
586 } else {
587 memcpy(mant, mult, MANT_LIMBS*sizeof(fp_limb));
588 *exponent = twopwr;
591 return true;
595 * Shift a mantissa to the right by i bits.
597 static void ieee_shr(fp_limb *mant, int i)
599 fp_limb n, m;
600 int j = 0;
601 int sr, sl, offs;
603 sr = i % LIMB_BITS; sl = LIMB_BITS-sr;
604 offs = i/LIMB_BITS;
606 if (sr == 0) {
607 if (offs)
608 for (j = MANT_LIMBS-1; j >= offs; j--)
609 mant[j] = mant[j-offs];
610 } else {
611 n = mant[MANT_LIMBS-1-offs] >> sr;
612 for (j = MANT_LIMBS-1; j > offs; j--) {
613 m = mant[j-offs-1];
614 mant[j] = (m << sl) | n;
615 n = m >> sr;
617 mant[j--] = n;
619 while (j >= 0)
620 mant[j--] = 0;
623 /* Produce standard IEEE formats, with implicit or explicit integer
624 bit; this makes the following assumptions:
626 - the sign bit is the MSB, followed by the exponent,
627 followed by the integer bit if present.
628 - the sign bit plus exponent fit in 16 bits.
629 - the exponent bias is 2^(n-1)-1 for an n-bit exponent */
631 struct ieee_format {
632 int bytes;
633 int mantissa; /* Fractional bits in the mantissa */
634 int explicit; /* Explicit integer */
635 int exponent; /* Bits in the exponent */
639 * The 16- and 128-bit formats are expected to be in IEEE 754r.
640 * AMD SSE5 uses the 16-bit format.
642 * The 32- and 64-bit formats are the original IEEE 754 formats.
644 * The 80-bit format is x87-specific, but widely used.
646 * The 8-bit format appears to be the consensus 8-bit floating-point
647 * format. It is apparently used in graphics applications.
649 static const struct ieee_format ieee_8 = { 1, 3, 0, 4 };
650 static const struct ieee_format ieee_16 = { 2, 10, 0, 5 };
651 static const struct ieee_format ieee_32 = { 4, 23, 0, 8 };
652 static const struct ieee_format ieee_64 = { 8, 52, 0, 11 };
653 static const struct ieee_format ieee_80 = { 10, 63, 1, 15 };
654 static const struct ieee_format ieee_128 = { 16, 112, 0, 15 };
656 /* Types of values we can generate */
657 enum floats {
658 FL_ZERO,
659 FL_DENORMAL,
660 FL_NORMAL,
661 FL_INFINITY,
662 FL_QNAN,
663 FL_SNAN
666 static int to_packed_bcd(const char *str, const char *p,
667 int s, uint8_t *result,
668 const struct ieee_format *fmt)
670 int n = 0;
671 char c;
672 int tv = -1;
674 if (fmt != &ieee_80) {
675 nasm_error(ERR_NONFATAL|ERR_PASS1,
676 "packed BCD requires an 80-bit format");
677 return 0;
680 while (p >= str) {
681 c = *p--;
682 if (c >= '0' && c <= '9') {
683 if (tv < 0) {
684 if (n == 9) {
685 nasm_error(ERR_WARNING|ERR_PASS1,
686 "packed BCD truncated to 18 digits");
688 tv = c-'0';
689 } else {
690 if (n < 9)
691 *result++ = tv + ((c-'0') << 4);
692 n++;
693 tv = -1;
695 } else if (c == '_') {
696 /* do nothing */
697 } else {
698 nasm_error(ERR_NONFATAL|ERR_PASS1,
699 "invalid character `%c' in packed BCD constant", c);
700 return 0;
703 if (tv >= 0) {
704 if (n < 9)
705 *result++ = tv;
706 n++;
708 while (n < 9) {
709 *result++ = 0;
710 n++;
712 *result = (s < 0) ? 0x80 : 0;
714 return 1; /* success */
717 static int to_float(const char *str, int s, uint8_t *result,
718 const struct ieee_format *fmt)
720 fp_limb mant[MANT_LIMBS];
721 int32_t exponent = 0;
722 const int32_t expmax = 1 << (fmt->exponent - 1);
723 fp_limb one_mask = LIMB_TOP_BIT >>
724 ((fmt->exponent+fmt->explicit) % LIMB_BITS);
725 const int one_pos = (fmt->exponent+fmt->explicit)/LIMB_BITS;
726 int i;
727 int shift;
728 enum floats type;
729 bool ok;
730 const bool minus = s < 0;
731 const int bits = fmt->bytes * 8;
732 const char *strend;
734 if (!str[0]) {
735 nasm_panic(0,
736 "internal errror: empty string passed to float_const");
737 return 0;
740 strend = strchr(str, '\0');
741 if (strend[-1] == 'P' || strend[-1] == 'p')
742 return to_packed_bcd(str, strend-2, s, result, fmt);
744 if (str[0] == '_') {
745 /* Special tokens */
747 switch (str[2]) {
748 case 'n': /* __nan__ */
749 case 'N':
750 case 'q': /* __qnan__ */
751 case 'Q':
752 type = FL_QNAN;
753 break;
754 case 's': /* __snan__ */
755 case 'S':
756 type = FL_SNAN;
757 break;
758 case 'i': /* __infinity__ */
759 case 'I':
760 type = FL_INFINITY;
761 break;
762 default:
763 nasm_error(ERR_NONFATAL|ERR_PASS1,
764 "internal error: unknown FP constant token `%s'\n", str);
765 type = FL_QNAN;
766 break;
768 } else {
769 if (str[0] == '0') {
770 switch (str[1]) {
771 case 'x': case 'X':
772 case 'h': case 'H':
773 ok = ieee_flconvert_bin(str+2, 4, mant, &exponent);
774 break;
775 case 'o': case 'O':
776 case 'q': case 'Q':
777 ok = ieee_flconvert_bin(str+2, 3, mant, &exponent);
778 break;
779 case 'b': case 'B':
780 case 'y': case 'Y':
781 ok = ieee_flconvert_bin(str+2, 1, mant, &exponent);
782 break;
783 case 'd': case 'D':
784 case 't': case 'T':
785 ok = ieee_flconvert(str+2, mant, &exponent);
786 break;
787 case 'p': case 'P':
788 return to_packed_bcd(str+2, strend-1, s, result, fmt);
789 default:
790 /* Leading zero was just a zero? */
791 ok = ieee_flconvert(str, mant, &exponent);
792 break;
794 } else if (str[0] == '$') {
795 ok = ieee_flconvert_bin(str+1, 4, mant, &exponent);
796 } else {
797 ok = ieee_flconvert(str, mant, &exponent);
800 if (!ok) {
801 type = FL_QNAN;
802 } else if (mant[0] & LIMB_TOP_BIT) {
804 * Non-zero.
806 exponent--;
807 if (exponent >= 2 - expmax && exponent <= expmax) {
808 type = FL_NORMAL;
809 } else if (exponent > 0) {
810 if (pass0 == 1)
811 nasm_error(ERR_WARNING|ERR_WARN_FL_OVERFLOW|ERR_PASS1,
812 "overflow in floating-point constant");
813 type = FL_INFINITY;
814 } else {
815 /* underflow or denormal; the denormal code handles
816 actual underflow. */
817 type = FL_DENORMAL;
819 } else {
820 /* Zero */
821 type = FL_ZERO;
825 switch (type) {
826 case FL_ZERO:
827 zero:
828 memset(mant, 0, sizeof mant);
829 break;
831 case FL_DENORMAL:
833 shift = -(exponent + expmax - 2 - fmt->exponent)
834 + fmt->explicit;
835 ieee_shr(mant, shift);
836 ieee_round(minus, mant, bits);
837 if (mant[one_pos] & one_mask) {
838 /* One's position is set, we rounded up into normal range */
839 exponent = 1;
840 if (!fmt->explicit)
841 mant[one_pos] &= ~one_mask; /* remove explicit one */
842 mant[0] |= exponent << (LIMB_BITS-1 - fmt->exponent);
843 } else {
844 if (daz || is_zero(mant)) {
845 /* Flush denormals to zero */
846 nasm_error(ERR_WARNING|ERR_WARN_FL_UNDERFLOW|ERR_PASS1,
847 "underflow in floating-point constant");
848 goto zero;
849 } else {
850 nasm_error(ERR_WARNING|ERR_WARN_FL_DENORM|ERR_PASS1,
851 "denormal floating-point constant");
854 break;
857 case FL_NORMAL:
858 exponent += expmax - 1;
859 ieee_shr(mant, fmt->exponent+fmt->explicit);
860 ieee_round(minus, mant, bits);
861 /* did we scale up by one? */
862 if (test_bit(mant, fmt->exponent+fmt->explicit-1)) {
863 ieee_shr(mant, 1);
864 exponent++;
865 if (exponent >= (expmax << 1)-1) {
866 nasm_error(ERR_WARNING|ERR_WARN_FL_OVERFLOW|ERR_PASS1,
867 "overflow in floating-point constant");
868 type = FL_INFINITY;
869 goto overflow;
873 if (!fmt->explicit)
874 mant[one_pos] &= ~one_mask; /* remove explicit one */
875 mant[0] |= exponent << (LIMB_BITS-1 - fmt->exponent);
876 break;
878 case FL_INFINITY:
879 case FL_QNAN:
880 case FL_SNAN:
881 overflow:
882 memset(mant, 0, sizeof mant);
883 mant[0] = (((fp_limb)1 << fmt->exponent)-1)
884 << (LIMB_BITS-1 - fmt->exponent);
885 if (fmt->explicit)
886 mant[one_pos] |= one_mask;
887 if (type == FL_QNAN)
888 set_bit(mant, fmt->exponent+fmt->explicit+1);
889 else if (type == FL_SNAN)
890 set_bit(mant, fmt->exponent+fmt->explicit+fmt->mantissa);
891 break;
894 mant[0] |= minus ? LIMB_TOP_BIT : 0;
896 for (i = fmt->bytes - 1; i >= 0; i--)
897 *result++ = mant[i/LIMB_BYTES] >> (((LIMB_BYTES-1)-(i%LIMB_BYTES))*8);
899 return 1; /* success */
902 int float_const(const char *number, int sign, uint8_t *result, int bytes)
904 switch (bytes) {
905 case 1:
906 return to_float(number, sign, result, &ieee_8);
907 case 2:
908 return to_float(number, sign, result, &ieee_16);
909 case 4:
910 return to_float(number, sign, result, &ieee_32);
911 case 8:
912 return to_float(number, sign, result, &ieee_64);
913 case 10:
914 return to_float(number, sign, result, &ieee_80);
915 case 16:
916 return to_float(number, sign, result, &ieee_128);
917 default:
918 nasm_panic(0, "strange value %d passed to float_const", bytes);
919 return 0;
923 /* Set floating-point options */
924 int float_option(const char *option)
926 if (!nasm_stricmp(option, "daz")) {
927 daz = true;
928 return 0;
929 } else if (!nasm_stricmp(option, "nodaz")) {
930 daz = false;
931 return 0;
932 } else if (!nasm_stricmp(option, "near")) {
933 rc = FLOAT_RC_NEAR;
934 return 0;
935 } else if (!nasm_stricmp(option, "down")) {
936 rc = FLOAT_RC_DOWN;
937 return 0;
938 } else if (!nasm_stricmp(option, "up")) {
939 rc = FLOAT_RC_UP;
940 return 0;
941 } else if (!nasm_stricmp(option, "zero")) {
942 rc = FLOAT_RC_ZERO;
943 return 0;
944 } else if (!nasm_stricmp(option, "default")) {
945 rc = FLOAT_RC_NEAR;
946 daz = false;
947 return 0;
948 } else {
949 return -1; /* Unknown option */