output: bin -- Use nasm_error helpers
[nasm.git] / asm / float.c
blobeaa6cd21d027aa0b734d54d72ff165e90994c2be
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2018 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"
47 #include "error.h"
50 * -----------------
51 * local variables
52 * -----------------
54 static bool daz = false; /* denormals as zero */
55 static enum float_round rc = FLOAT_RC_NEAR; /* rounding control */
58 * -----------
59 * constants
60 * -----------
63 /* "A limb is like a digit but bigger */
64 typedef uint32_t fp_limb;
65 typedef uint64_t fp_2limb;
67 #define LIMB_BITS 32
68 #define LIMB_BYTES (LIMB_BITS/8)
69 #define LIMB_TOP_BIT ((fp_limb)1 << (LIMB_BITS-1))
70 #define LIMB_MASK ((fp_limb)(~0))
71 #define LIMB_ALL_BYTES ((fp_limb)0x01010101)
72 #define LIMB_BYTE(x) ((x)*LIMB_ALL_BYTES)
74 /* 112 bits + 64 bits for accuracy + 16 bits for rounding */
75 #define MANT_LIMBS 6
77 /* 52 digits fit in 176 bits because 10^53 > 2^176 > 10^52 */
78 #define MANT_DIGITS 52
80 /* the format and the argument list depend on MANT_LIMBS */
81 #define MANT_FMT "%08x_%08x_%08x_%08x_%08x_%08x"
82 #define MANT_ARG SOME_ARG(mant, 0)
84 #define SOME_ARG(a,i) (a)[(i)+0], (a)[(i)+1], (a)[(i)+2], \
85 (a)[(i)+3], (a)[(i)+4], (a)[(i)+5]
88 * ---------------------------------------------------------------------------
89 * emit a printf()-like debug message... but only if DEBUG_FLOAT was defined
90 * ---------------------------------------------------------------------------
93 #ifdef DEBUG_FLOAT
94 #define dprintf(x) printf x
95 #else
96 #define dprintf(x) do { } while (0)
97 #endif
100 * ---------------------------------------------------------------------------
101 * multiply
102 * ---------------------------------------------------------------------------
104 static int float_multiply(fp_limb *to, fp_limb *from)
106 fp_2limb temp[MANT_LIMBS * 2];
107 int i, j;
110 * guaranteed that top bit of 'from' is set -- so we only have
111 * to worry about _one_ bit shift to the left
113 dprintf(("%s=" MANT_FMT "\n", "mul1", SOME_ARG(to, 0)));
114 dprintf(("%s=" MANT_FMT "\n", "mul2", SOME_ARG(from, 0)));
116 memset(temp, 0, sizeof temp);
118 for (i = 0; i < MANT_LIMBS; i++) {
119 for (j = 0; j < MANT_LIMBS; j++) {
120 fp_2limb n;
121 n = (fp_2limb) to[i] * (fp_2limb) from[j];
122 temp[i + j] += n >> LIMB_BITS;
123 temp[i + j + 1] += (fp_limb)n;
127 for (i = MANT_LIMBS * 2; --i;) {
128 temp[i - 1] += temp[i] >> LIMB_BITS;
129 temp[i] &= LIMB_MASK;
132 dprintf(("%s=" MANT_FMT "_" MANT_FMT "\n", "temp", SOME_ARG(temp, 0),
133 SOME_ARG(temp, MANT_LIMBS)));
135 if (temp[0] & LIMB_TOP_BIT) {
136 for (i = 0; i < MANT_LIMBS; i++) {
137 to[i] = temp[i] & LIMB_MASK;
139 dprintf(("%s=" MANT_FMT " (%i)\n", "prod", SOME_ARG(to, 0), 0));
140 return 0;
141 } else {
142 for (i = 0; i < MANT_LIMBS; i++) {
143 to[i] = (temp[i] << 1) + !!(temp[i + 1] & LIMB_TOP_BIT);
145 dprintf(("%s=" MANT_FMT " (%i)\n", "prod", SOME_ARG(to, 0), -1));
146 return -1;
151 * ---------------------------------------------------------------------------
152 * read an exponent; returns INT32_MAX on error
153 * ---------------------------------------------------------------------------
155 static int32_t read_exponent(const char *string, int32_t max)
157 int32_t i = 0;
158 bool neg = false;
160 if (*string == '+') {
161 string++;
162 } else if (*string == '-') {
163 neg = true;
164 string++;
166 while (*string) {
167 if (*string >= '0' && *string <= '9') {
168 i = (i * 10) + (*string - '0');
171 * To ensure that underflows and overflows are
172 * handled properly we must avoid wraparounds of
173 * the signed integer value that is used to hold
174 * the exponent. Therefore we cap the exponent at
175 * +/-5000, which is slightly more/less than
176 * what's required for normal and denormal numbers
177 * in single, double, and extended precision, but
178 * sufficient to avoid signed integer wraparound.
180 if (i > max)
181 i = max;
182 } else if (*string == '_') {
183 /* do nothing */
184 } else {
185 nasm_nonfatal("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_nonfatal("too many periods in floating-point constant");
222 return false;
224 } else if (*string >= '0' && *string <= '9') {
225 if (*string == '0' && !started) {
226 if (seendot) {
227 tenpwr--;
229 } else {
230 started = true;
231 if (p < digits + sizeof(digits)) {
232 *p++ = *string - '0';
233 } else {
234 if (!warned) {
235 nasm_warnf(ERR_WARN_FL_TOOLONG|ERR_PASS2,
236 "floating-point constant significand contains "
237 "more than %i digits", MANT_DIGITS);
238 warned = true;
241 if (!seendot) {
242 tenpwr++;
245 } else if (*string == '_') {
246 /* do nothing */
247 } else {
248 nasm_nonfatalf(ERR_PASS2,
249 "invalid character in floating-point constant %s: '%c'",
250 "significand", *string);
251 return false;
253 string++;
256 if (*string) {
257 int32_t e;
259 string++; /* eat the E */
260 e = read_exponent(string, 5000);
261 if (e == INT32_MAX)
262 return false;
263 tenpwr += e;
267 * At this point, the memory interval [digits,p) contains a
268 * series of decimal digits zzzzzzz, such that our number X
269 * satisfies X = 0.zzzzzzz * 10^tenpwr.
271 q = digits;
272 dprintf(("X = 0."));
273 while (q < p) {
274 dprintf(("%c", *q + '0'));
275 q++;
277 dprintf((" * 10^%i\n", tenpwr));
280 * Now convert [digits,p) to our internal representation.
282 bit = LIMB_TOP_BIT;
283 for (m = mant; m < mant + MANT_LIMBS; m++) {
284 *m = 0;
286 m = mant;
287 q = digits;
288 started = false;
289 twopwr = 0;
290 while (m < mant + MANT_LIMBS) {
291 fp_limb carry = 0;
292 while (p > q && !p[-1]) {
293 p--;
295 if (p <= q) {
296 break;
298 for (r = p; r-- > q;) {
299 int32_t i;
300 i = 2 * *r + carry;
301 if (i >= 10) {
302 carry = 1;
303 i -= 10;
304 } else {
305 carry = 0;
307 *r = i;
309 if (carry) {
310 *m |= bit;
311 started = true;
313 if (started) {
314 if (bit == 1) {
315 bit = LIMB_TOP_BIT;
316 m++;
317 } else {
318 bit >>= 1;
320 } else {
321 twopwr--;
324 twopwr += tenpwr;
327 * At this point, the 'mant' array contains the first frac-
328 * tional places of a base-2^16 real number which when mul-
329 * tiplied by 2^twopwr and 5^tenpwr gives X.
331 dprintf(("X = " MANT_FMT " * 2^%i * 5^%i\n", MANT_ARG, twopwr,
332 tenpwr));
335 * Now multiply 'mant' by 5^tenpwr.
337 if (tenpwr < 0) { /* mult = 5^-1 = 0.2 */
338 for (m = mult; m < mult + MANT_LIMBS - 1; m++) {
339 *m = LIMB_BYTE(0xcc);
341 mult[MANT_LIMBS - 1] = LIMB_BYTE(0xcc)+1;
342 extratwos = -2;
343 tenpwr = -tenpwr;
346 * If tenpwr was 1000...000b, then it becomes 1000...000b. See
347 * the "ANSI C" comment below for more details on that case.
349 * Because we already truncated tenpwr to +5000...-5000 inside
350 * the exponent parsing code, this shouldn't happen though.
352 } else if (tenpwr > 0) { /* mult = 5^+1 = 5.0 */
353 mult[0] = (fp_limb)5 << (LIMB_BITS-3); /* 0xA000... */
354 for (m = mult + 1; m < mult + MANT_LIMBS; m++) {
355 *m = 0;
357 extratwos = 3;
358 } else {
359 extratwos = 0;
361 while (tenpwr) {
362 dprintf(("loop=" MANT_FMT " * 2^%i * 5^%i (%i)\n", MANT_ARG,
363 twopwr, tenpwr, extratwos));
364 if (tenpwr & 1) {
365 dprintf(("mant*mult\n"));
366 twopwr += extratwos + float_multiply(mant, mult);
368 dprintf(("mult*mult\n"));
369 extratwos = extratwos * 2 + float_multiply(mult, mult);
370 tenpwr >>= 1;
373 * In ANSI C, the result of right-shifting a signed integer is
374 * considered implementation-specific. To ensure that the loop
375 * terminates even if tenpwr was 1000...000b to begin with, we
376 * manually clear the MSB, in case a 1 was shifted in.
378 * Because we already truncated tenpwr to +5000...-5000 inside
379 * the exponent parsing code, this shouldn't matter; neverthe-
380 * less it is the right thing to do here.
382 tenpwr &= (uint32_t) - 1 >> 1;
386 * At this point, the 'mant' array contains the first frac-
387 * tional places of a base-2^16 real number in [0.5,1) that
388 * when multiplied by 2^twopwr gives X. Or it contains zero
389 * of course. We are done.
391 *exponent = twopwr;
392 return true;
396 * ---------------------------------------------------------------------------
397 * operations of specific bits
398 * ---------------------------------------------------------------------------
401 /* Set a bit, using *bigendian* bit numbering (0 = MSB) */
402 static void set_bit(fp_limb *mant, int bit)
404 mant[bit/LIMB_BITS] |= LIMB_TOP_BIT >> (bit & (LIMB_BITS-1));
407 /* Test a single bit */
408 static int test_bit(const fp_limb *mant, int bit)
410 return (mant[bit/LIMB_BITS] >> (~bit & (LIMB_BITS-1))) & 1;
413 /* Report if the mantissa value is all zero */
414 static bool is_zero(const fp_limb *mant)
416 int i;
418 for (i = 0; i < MANT_LIMBS; i++)
419 if (mant[i])
420 return false;
422 return true;
426 * ---------------------------------------------------------------------------
427 * round a mantissa off after i words
428 * ---------------------------------------------------------------------------
431 #define ROUND_COLLECT_BITS \
432 do { \
433 m = mant[i] & (2*bit-1); \
434 for (j = i+1; j < MANT_LIMBS; j++) \
435 m = m | mant[j]; \
436 } while (0)
438 #define ROUND_ABS_DOWN \
439 do { \
440 mant[i] &= ~(bit-1); \
441 for (j = i+1; j < MANT_LIMBS; j++) \
442 mant[j] = 0; \
443 return false; \
444 } while (0)
446 #define ROUND_ABS_UP \
447 do { \
448 mant[i] = (mant[i] & ~(bit-1)) + bit; \
449 for (j = i+1; j < MANT_LIMBS; j++) \
450 mant[j] = 0; \
451 while (i > 0 && !mant[i]) \
452 ++mant[--i]; \
453 return !mant[0]; \
454 } while (0)
456 static bool ieee_round(bool minus, fp_limb *mant, int bits)
458 fp_limb m = 0;
459 int32_t j;
460 int i = bits / LIMB_BITS;
461 int p = bits % LIMB_BITS;
462 fp_limb bit = LIMB_TOP_BIT >> p;
464 if (rc == FLOAT_RC_NEAR) {
465 if (mant[i] & bit) {
466 mant[i] &= ~bit;
467 ROUND_COLLECT_BITS;
468 mant[i] |= bit;
469 if (m) {
470 ROUND_ABS_UP;
471 } else {
472 if (test_bit(mant, bits-1)) {
473 ROUND_ABS_UP;
474 } else {
475 ROUND_ABS_DOWN;
478 } else {
479 ROUND_ABS_DOWN;
481 } else if (rc == FLOAT_RC_ZERO ||
482 rc == (minus ? FLOAT_RC_UP : FLOAT_RC_DOWN)) {
483 ROUND_ABS_DOWN;
484 } else {
485 /* rc == (minus ? FLOAT_RC_DOWN : FLOAT_RC_UP) */
486 /* Round toward +/- infinity */
487 ROUND_COLLECT_BITS;
488 if (m) {
489 ROUND_ABS_UP;
490 } else {
491 ROUND_ABS_DOWN;
494 return false;
497 /* Returns a value >= 16 if not a valid hex digit */
498 static unsigned int hexval(char c)
500 unsigned int v = (unsigned char) c;
502 if (v >= '0' && v <= '9')
503 return v - '0';
504 else
505 return (v|0x20) - 'a' + 10;
508 /* Handle floating-point numbers with radix 2^bits and binary exponent */
509 static bool ieee_flconvert_bin(const char *string, int bits,
510 fp_limb *mant, int32_t *exponent)
512 static const int log2tbl[16] =
513 { -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3 };
514 fp_limb mult[MANT_LIMBS + 1], *mp;
515 int ms;
516 int32_t twopwr;
517 bool seendot, seendigit;
518 unsigned char c;
519 const int radix = 1 << bits;
520 fp_limb v;
522 twopwr = 0;
523 seendot = seendigit = false;
524 ms = 0;
525 mp = NULL;
527 memset(mult, 0, sizeof mult);
529 while ((c = *string++) != '\0') {
530 if (c == '.') {
531 if (!seendot)
532 seendot = true;
533 else {
534 nasm_nonfatal("too many periods in floating-point constant");
535 return false;
537 } else if ((v = hexval(c)) < (unsigned int)radix) {
538 if (!seendigit && v) {
539 int l = log2tbl[v];
541 seendigit = true;
542 mp = mult;
543 ms = (LIMB_BITS-1)-l;
545 twopwr += l+1-bits;
548 if (seendigit) {
549 if (ms < 0) {
550 /* Cast to fp_2limb as ms == -LIMB_BITS is possible. */
551 *mp |= (fp_2limb)v >> -ms;
552 mp++;
553 if (mp > &mult[MANT_LIMBS])
554 mp = &mult[MANT_LIMBS]; /* Guard slot */
555 ms += LIMB_BITS;
557 *mp |= v << ms;
558 ms -= bits;
560 if (!seendot)
561 twopwr += bits;
562 } else {
563 if (seendot)
564 twopwr -= bits;
566 } else if (c == 'p' || c == 'P') {
567 int32_t e;
568 e = read_exponent(string, 20000);
569 if (e == INT32_MAX)
570 return false;
571 twopwr += e;
572 break;
573 } else if (c == '_') {
574 /* ignore */
575 } else {
576 nasm_nonfatal("floating-point constant: `%c' is invalid character", c);
577 return false;
581 if (!seendigit) {
582 memset(mant, 0, MANT_LIMBS*sizeof(fp_limb)); /* Zero */
583 *exponent = 0;
584 } else {
585 memcpy(mant, mult, MANT_LIMBS*sizeof(fp_limb));
586 *exponent = twopwr;
589 return true;
593 * Shift a mantissa to the right by i bits.
595 static void ieee_shr(fp_limb *mant, int i)
597 fp_limb n, m;
598 int j = 0;
599 int sr, sl, offs;
601 sr = i % LIMB_BITS; sl = LIMB_BITS-sr;
602 offs = i/LIMB_BITS;
604 if (sr == 0) {
605 if (offs)
606 for (j = MANT_LIMBS-1; j >= offs; j--)
607 mant[j] = mant[j-offs];
608 } else if (MANT_LIMBS-1-offs < 0) {
609 j = MANT_LIMBS-1;
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_nonfatal("packed BCD requires an 80-bit format");
676 return 0;
679 while (p >= str) {
680 c = *p--;
681 if (c >= '0' && c <= '9') {
682 if (tv < 0) {
683 if (n == 9)
684 nasm_warnf(ERR_PASS2, "packed BCD truncated to 18 digits");
685 tv = c-'0';
686 } else {
687 if (n < 9)
688 *result++ = tv + ((c-'0') << 4);
689 n++;
690 tv = -1;
692 } else if (c == '_') {
693 /* do nothing */
694 } else {
695 nasm_nonfatal("invalid character `%c' in packed BCD constant", c);
696 return 0;
699 if (tv >= 0) {
700 if (n < 9)
701 *result++ = tv;
702 n++;
704 while (n < 9) {
705 *result++ = 0;
706 n++;
708 *result = (s < 0) ? 0x80 : 0;
710 return 1; /* success */
713 static int to_float(const char *str, int s, uint8_t *result,
714 const struct ieee_format *fmt)
716 fp_limb mant[MANT_LIMBS];
717 int32_t exponent = 0;
718 const int32_t expmax = 1 << (fmt->exponent - 1);
719 fp_limb one_mask = LIMB_TOP_BIT >>
720 ((fmt->exponent+fmt->explicit) % LIMB_BITS);
721 const int one_pos = (fmt->exponent+fmt->explicit)/LIMB_BITS;
722 int i;
723 int shift;
724 enum floats type;
725 bool ok;
726 const bool minus = s < 0;
727 const int bits = fmt->bytes * 8;
728 const char *strend;
730 nasm_assert(str[0]);
732 strend = strchr(str, '\0');
733 if (strend[-1] == 'P' || strend[-1] == 'p')
734 return to_packed_bcd(str, strend-2, s, result, fmt);
736 if (str[0] == '_') {
737 /* Special tokens */
739 switch (str[2]) {
740 case 'n': /* __nan__ */
741 case 'N':
742 case 'q': /* __qnan__ */
743 case 'Q':
744 type = FL_QNAN;
745 break;
746 case 's': /* __snan__ */
747 case 'S':
748 type = FL_SNAN;
749 break;
750 case 'i': /* __infinity__ */
751 case 'I':
752 type = FL_INFINITY;
753 break;
754 default:
755 nasm_nonfatal("internal error: unknown FP constant token `%s'", str);
756 type = FL_QNAN;
757 break;
759 } else {
760 if (str[0] == '0') {
761 switch (str[1]) {
762 case 'x': case 'X':
763 case 'h': case 'H':
764 ok = ieee_flconvert_bin(str+2, 4, mant, &exponent);
765 break;
766 case 'o': case 'O':
767 case 'q': case 'Q':
768 ok = ieee_flconvert_bin(str+2, 3, mant, &exponent);
769 break;
770 case 'b': case 'B':
771 case 'y': case 'Y':
772 ok = ieee_flconvert_bin(str+2, 1, mant, &exponent);
773 break;
774 case 'd': case 'D':
775 case 't': case 'T':
776 ok = ieee_flconvert(str+2, mant, &exponent);
777 break;
778 case 'p': case 'P':
779 return to_packed_bcd(str+2, strend-1, s, result, fmt);
780 default:
781 /* Leading zero was just a zero? */
782 ok = ieee_flconvert(str, mant, &exponent);
783 break;
785 } else if (str[0] == '$') {
786 ok = ieee_flconvert_bin(str+1, 4, mant, &exponent);
787 } else {
788 ok = ieee_flconvert(str, mant, &exponent);
791 if (!ok) {
792 type = FL_QNAN;
793 } else if (mant[0] & LIMB_TOP_BIT) {
795 * Non-zero.
797 exponent--;
798 if (exponent >= 2 - expmax && exponent <= expmax) {
799 type = FL_NORMAL;
800 } else if (exponent > 0) {
801 if (pass0 == 1)
802 nasm_warnf(ERR_WARN_FL_OVERFLOW|ERR_PASS2,
803 "overflow in floating-point constant");
804 type = FL_INFINITY;
805 } else {
806 /* underflow or denormal; the denormal code handles
807 actual underflow. */
808 type = FL_DENORMAL;
810 } else {
811 /* Zero */
812 type = FL_ZERO;
816 switch (type) {
817 case FL_ZERO:
818 zero:
819 memset(mant, 0, sizeof mant);
820 break;
822 case FL_DENORMAL:
824 shift = -(exponent + expmax - 2 - fmt->exponent)
825 + fmt->explicit;
826 ieee_shr(mant, shift);
827 ieee_round(minus, mant, bits);
828 if (mant[one_pos] & one_mask) {
829 /* One's position is set, we rounded up into normal range */
830 exponent = 1;
831 if (!fmt->explicit)
832 mant[one_pos] &= ~one_mask; /* remove explicit one */
833 mant[0] |= exponent << (LIMB_BITS-1 - fmt->exponent);
834 } else {
835 if (daz || is_zero(mant)) {
836 /* Flush denormals to zero */
837 nasm_warnf(ERR_WARN_FL_UNDERFLOW|ERR_PASS2,
838 "underflow in floating-point constant");
839 goto zero;
840 } else
841 nasm_warnf(ERR_WARN_FL_DENORM|ERR_PASS2,
842 "denormal floating-point constant");
844 break;
847 case FL_NORMAL:
848 exponent += expmax - 1;
849 ieee_shr(mant, fmt->exponent+fmt->explicit);
850 ieee_round(minus, mant, bits);
851 /* did we scale up by one? */
852 if (test_bit(mant, fmt->exponent+fmt->explicit-1)) {
853 ieee_shr(mant, 1);
854 exponent++;
855 if (exponent >= (expmax << 1)-1) {
856 nasm_warnf(ERR_WARN_FL_OVERFLOW|ERR_PASS2,
857 "overflow in floating-point constant");
858 type = FL_INFINITY;
859 goto overflow;
863 if (!fmt->explicit)
864 mant[one_pos] &= ~one_mask; /* remove explicit one */
865 mant[0] |= exponent << (LIMB_BITS-1 - fmt->exponent);
866 break;
868 case FL_INFINITY:
869 case FL_QNAN:
870 case FL_SNAN:
871 overflow:
872 memset(mant, 0, sizeof mant);
873 mant[0] = (((fp_limb)1 << fmt->exponent)-1)
874 << (LIMB_BITS-1 - fmt->exponent);
875 if (fmt->explicit)
876 mant[one_pos] |= one_mask;
877 if (type == FL_QNAN)
878 set_bit(mant, fmt->exponent+fmt->explicit+1);
879 else if (type == FL_SNAN)
880 set_bit(mant, fmt->exponent+fmt->explicit+fmt->mantissa);
881 break;
884 mant[0] |= minus ? LIMB_TOP_BIT : 0;
886 for (i = fmt->bytes - 1; i >= 0; i--)
887 *result++ = mant[i/LIMB_BYTES] >> (((LIMB_BYTES-1)-(i%LIMB_BYTES))*8);
889 return 1; /* success */
892 int float_const(const char *number, int sign, uint8_t *result, int bytes)
894 switch (bytes) {
895 case 1:
896 return to_float(number, sign, result, &ieee_8);
897 case 2:
898 return to_float(number, sign, result, &ieee_16);
899 case 4:
900 return to_float(number, sign, result, &ieee_32);
901 case 8:
902 return to_float(number, sign, result, &ieee_64);
903 case 10:
904 return to_float(number, sign, result, &ieee_80);
905 case 16:
906 return to_float(number, sign, result, &ieee_128);
907 default:
908 nasm_panic("strange value %d passed to float_const", bytes);
909 return 0;
913 /* Set floating-point options */
914 int float_option(const char *option)
916 if (!nasm_stricmp(option, "daz")) {
917 daz = true;
918 return 0;
919 } else if (!nasm_stricmp(option, "nodaz")) {
920 daz = false;
921 return 0;
922 } else if (!nasm_stricmp(option, "near")) {
923 rc = FLOAT_RC_NEAR;
924 return 0;
925 } else if (!nasm_stricmp(option, "down")) {
926 rc = FLOAT_RC_DOWN;
927 return 0;
928 } else if (!nasm_stricmp(option, "up")) {
929 rc = FLOAT_RC_UP;
930 return 0;
931 } else if (!nasm_stricmp(option, "zero")) {
932 rc = FLOAT_RC_ZERO;
933 return 0;
934 } else if (!nasm_stricmp(option, "default")) {
935 rc = FLOAT_RC_NEAR;
936 daz = false;
937 return 0;
938 } else {
939 return -1; /* Unknown option */