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
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
55 static bool daz = false; /* denormals as zero */
56 static enum float_round rc = FLOAT_RC_NEAR; /* rounding control */
64 /* "A limb is like a digit but bigger */
65 typedef uint32_t fp_limb;
66 typedef uint64_t fp_2limb;
69 #define LIMB_BYTES (LIMB_BITS/8)
70 #define LIMB_TOP_BIT ((fp_limb)1 << (LIMB_BITS-1))
71 #define LIMB_MASK ((fp_limb)(~0))
72 #define LIMB_ALL_BYTES ((fp_limb)0x01010101)
73 #define LIMB_BYTE(x) ((x)*LIMB_ALL_BYTES)
75 /* 112 bits + 64 bits for accuracy + 16 bits for rounding */
78 /* 52 digits fit in 176 bits because 10^53 > 2^176 > 10^52 */
79 #define MANT_DIGITS 52
81 /* the format and the argument list depend on MANT_LIMBS */
82 #define MANT_FMT "%08x_%08x_%08x_%08x_%08x_%08x"
83 #define MANT_ARG SOME_ARG(mant, 0)
85 #define SOME_ARG(a,i) (a)[(i)+0], (a)[(i)+1], (a)[(i)+2], \
86 (a)[(i)+3], (a)[(i)+4], (a)[(i)+5]
89 * ---------------------------------------------------------------------------
90 * emit a printf()-like debug message... but only if DEBUG_FLOAT was defined
91 * ---------------------------------------------------------------------------
95 #define dprintf(x) printf x
97 #define dprintf(x) do { } while (0)
101 * ---------------------------------------------------------------------------
103 * ---------------------------------------------------------------------------
105 static int float_multiply(fp_limb *to, fp_limb *from)
107 fp_2limb temp[MANT_LIMBS * 2];
111 * guaranteed that top bit of 'from' is set -- so we only have
112 * to worry about _one_ bit shift to the left
114 dprintf(("%s=" MANT_FMT "\n", "mul1", SOME_ARG(to, 0)));
115 dprintf(("%s=" MANT_FMT "\n", "mul2", SOME_ARG(from, 0)));
117 memset(temp, 0, sizeof temp);
119 for (i = 0; i < MANT_LIMBS; i++) {
120 for (j = 0; j < MANT_LIMBS; j++) {
122 n = (fp_2limb) to[i] * (fp_2limb) from[j];
123 temp[i + j] += n >> LIMB_BITS;
124 temp[i + j + 1] += (fp_limb)n;
128 for (i = MANT_LIMBS * 2; --i;) {
129 temp[i - 1] += temp[i] >> LIMB_BITS;
130 temp[i] &= LIMB_MASK;
133 dprintf(("%s=" MANT_FMT "_" MANT_FMT "\n", "temp", SOME_ARG(temp, 0),
134 SOME_ARG(temp, MANT_LIMBS)));
136 if (temp[0] & LIMB_TOP_BIT) {
137 for (i = 0; i < MANT_LIMBS; i++) {
138 to[i] = temp[i] & LIMB_MASK;
140 dprintf(("%s=" MANT_FMT " (%i)\n", "prod", SOME_ARG(to, 0), 0));
143 for (i = 0; i < MANT_LIMBS; i++) {
144 to[i] = (temp[i] << 1) + !!(temp[i + 1] & LIMB_TOP_BIT);
146 dprintf(("%s=" MANT_FMT " (%i)\n", "prod", SOME_ARG(to, 0), -1));
152 * ---------------------------------------------------------------------------
153 * read an exponent; returns INT32_MAX on error
154 * ---------------------------------------------------------------------------
156 static int32_t read_exponent(const char *string, int32_t max)
161 if (*string == '+') {
163 } else if (*string == '-') {
168 if (*string >= '0' && *string <= '9') {
169 i = (i * 10) + (*string - '0');
172 * To ensure that underflows and overflows are
173 * handled properly we must avoid wraparounds of
174 * the signed integer value that is used to hold
175 * the exponent. Therefore we cap the exponent at
176 * +/-5000, which is slightly more/less than
177 * what's required for normal and denormal numbers
178 * in single, double, and extended precision, but
179 * sufficient to avoid signed integer wraparound.
183 } else if (*string == '_') {
186 error(ERR_NONFATAL|ERR_PASS1,
187 "invalid character in floating-point constant %s: '%c'",
188 "exponent", *string);
198 * ---------------------------------------------------------------------------
200 * ---------------------------------------------------------------------------
202 static bool ieee_flconvert(const char *string, fp_limb *mant,
205 char digits[MANT_DIGITS];
207 fp_limb mult[MANT_LIMBS], bit;
209 int32_t tenpwr, twopwr;
211 bool started, seendot, warned;
216 started = seendot = false;
218 while (*string && *string != 'E' && *string != 'e') {
219 if (*string == '.') {
223 error(ERR_NONFATAL|ERR_PASS1,
224 "too many periods in floating-point constant");
227 } else if (*string >= '0' && *string <= '9') {
228 if (*string == '0' && !started) {
234 if (p < digits + sizeof(digits)) {
235 *p++ = *string - '0';
238 error(ERR_WARNING|ERR_WARN_FL_TOOLONG|ERR_PASS1,
239 "floating-point constant significand contains "
240 "more than %i digits", MANT_DIGITS);
248 } else if (*string == '_') {
251 error(ERR_NONFATAL|ERR_PASS1,
252 "invalid character in floating-point constant %s: '%c'",
253 "significand", *string);
262 string++; /* eat the E */
263 e = read_exponent(string, 5000);
270 * At this point, the memory interval [digits,p) contains a
271 * series of decimal digits zzzzzzz, such that our number X
272 * satisfies X = 0.zzzzzzz * 10^tenpwr.
277 dprintf(("%c", *q + '0'));
280 dprintf((" * 10^%i\n", tenpwr));
283 * Now convert [digits,p) to our internal representation.
286 for (m = mant; m < mant + MANT_LIMBS; m++) {
293 while (m < mant + MANT_LIMBS) {
295 while (p > q && !p[-1]) {
301 for (r = p; r-- > q;) {
330 * At this point, the 'mant' array contains the first frac-
331 * tional places of a base-2^16 real number which when mul-
332 * tiplied by 2^twopwr and 5^tenpwr gives X.
334 dprintf(("X = " MANT_FMT " * 2^%i * 5^%i\n", MANT_ARG, twopwr,
338 * Now multiply 'mant' by 5^tenpwr.
340 if (tenpwr < 0) { /* mult = 5^-1 = 0.2 */
341 for (m = mult; m < mult + MANT_LIMBS - 1; m++) {
342 *m = LIMB_BYTE(0xcc);
344 mult[MANT_LIMBS - 1] = LIMB_BYTE(0xcc)+1;
349 * If tenpwr was 1000...000b, then it becomes 1000...000b. See
350 * the "ANSI C" comment below for more details on that case.
352 * Because we already truncated tenpwr to +5000...-5000 inside
353 * the exponent parsing code, this shouldn't happen though.
355 } else if (tenpwr > 0) { /* mult = 5^+1 = 5.0 */
356 mult[0] = (fp_limb)5 << (LIMB_BITS-3); /* 0xA000... */
357 for (m = mult + 1; m < mult + MANT_LIMBS; m++) {
365 dprintf(("loop=" MANT_FMT " * 2^%i * 5^%i (%i)\n", MANT_ARG,
366 twopwr, tenpwr, extratwos));
368 dprintf(("mant*mult\n"));
369 twopwr += extratwos + float_multiply(mant, mult);
371 dprintf(("mult*mult\n"));
372 extratwos = extratwos * 2 + float_multiply(mult, mult);
376 * In ANSI C, the result of right-shifting a signed integer is
377 * considered implementation-specific. To ensure that the loop
378 * terminates even if tenpwr was 1000...000b to begin with, we
379 * manually clear the MSB, in case a 1 was shifted in.
381 * Because we already truncated tenpwr to +5000...-5000 inside
382 * the exponent parsing code, this shouldn't matter; neverthe-
383 * less it is the right thing to do here.
385 tenpwr &= (uint32_t) - 1 >> 1;
389 * At this point, the 'mant' array contains the first frac-
390 * tional places of a base-2^16 real number in [0.5,1) that
391 * when multiplied by 2^twopwr gives X. Or it contains zero
392 * of course. We are done.
399 * ---------------------------------------------------------------------------
400 * operations of specific bits
401 * ---------------------------------------------------------------------------
404 /* Set a bit, using *bigendian* bit numbering (0 = MSB) */
405 static void set_bit(fp_limb *mant, int bit)
407 mant[bit/LIMB_BITS] |= LIMB_TOP_BIT >> (bit & (LIMB_BITS-1));
410 /* Test a single bit */
411 static int test_bit(const fp_limb *mant, int bit)
413 return (mant[bit/LIMB_BITS] >> (~bit & (LIMB_BITS-1))) & 1;
416 /* Report if the mantissa value is all zero */
417 static bool is_zero(const fp_limb *mant)
421 for (i = 0; i < MANT_LIMBS; i++)
429 * ---------------------------------------------------------------------------
430 * round a mantissa off after i words
431 * ---------------------------------------------------------------------------
434 #define ROUND_COLLECT_BITS \
436 m = mant[i] & (2*bit-1); \
437 for (j = i+1; j < MANT_LIMBS; j++) \
441 #define ROUND_ABS_DOWN \
443 mant[i] &= ~(bit-1); \
444 for (j = i+1; j < MANT_LIMBS; j++) \
449 #define ROUND_ABS_UP \
451 mant[i] = (mant[i] & ~(bit-1)) + bit; \
452 for (j = i+1; j < MANT_LIMBS; j++) \
454 while (i > 0 && !mant[i]) \
459 static bool ieee_round(bool minus, fp_limb *mant, int bits)
463 int i = bits / LIMB_BITS;
464 int p = bits % LIMB_BITS;
465 fp_limb bit = LIMB_TOP_BIT >> p;
467 if (rc == FLOAT_RC_NEAR) {
475 if (test_bit(mant, bits-1)) {
484 } else if (rc == FLOAT_RC_ZERO ||
485 rc == (minus ? FLOAT_RC_UP : FLOAT_RC_DOWN)) {
488 /* rc == (minus ? FLOAT_RC_DOWN : FLOAT_RC_UP) */
489 /* Round toward +/- infinity */
500 /* Returns a value >= 16 if not a valid hex digit */
501 static unsigned int hexval(char c)
503 unsigned int v = (unsigned char) c;
505 if (v >= '0' && v <= '9')
508 return (v|0x20) - 'a' + 10;
511 /* Handle floating-point numbers with radix 2^bits and binary exponent */
512 static bool ieee_flconvert_bin(const char *string, int bits,
513 fp_limb *mant, int32_t *exponent)
515 static const int log2tbl[16] =
516 { -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3 };
517 fp_limb mult[MANT_LIMBS + 1], *mp;
520 bool seendot, seendigit;
522 const int radix = 1 << bits;
526 seendot = seendigit = false;
530 memset(mult, 0, sizeof mult);
532 while ((c = *string++) != '\0') {
537 error(ERR_NONFATAL|ERR_PASS1,
538 "too many periods in floating-point constant");
541 } else if ((v = hexval(c)) < (unsigned int)radix) {
542 if (!seendigit && v) {
547 ms = (LIMB_BITS-1)-l;
549 twopwr = seendot ? twopwr-bits+l : l+1-bits;
556 if (mp > &mult[MANT_LIMBS])
557 mp = &mult[MANT_LIMBS]; /* Guard slot */
569 } else if (c == 'p' || c == 'P') {
571 e = read_exponent(string, 20000);
576 } else if (c == '_') {
579 error(ERR_NONFATAL|ERR_PASS1,
580 "floating-point constant: `%c' is invalid character", c);
586 memset(mant, 0, MANT_LIMBS*sizeof(fp_limb)); /* Zero */
589 memcpy(mant, mult, MANT_LIMBS*sizeof(fp_limb));
597 * Shift a mantissa to the right by i bits.
599 static void ieee_shr(fp_limb *mant, int i)
605 sr = i % LIMB_BITS; sl = LIMB_BITS-sr;
610 for (j = MANT_LIMBS-1; j >= offs; j--)
611 mant[j] = mant[j-offs];
613 n = mant[MANT_LIMBS-1-offs] >> sr;
614 for (j = MANT_LIMBS-1; j > offs; j--) {
616 mant[j] = (m << sl) | n;
625 /* Produce standard IEEE formats, with implicit or explicit integer
626 bit; this makes the following assumptions:
628 - the sign bit is the MSB, followed by the exponent,
629 followed by the integer bit if present.
630 - the sign bit plus exponent fit in 16 bits.
631 - the exponent bias is 2^(n-1)-1 for an n-bit exponent */
635 int mantissa; /* Fractional bits in the mantissa */
636 int explicit; /* Explicit integer */
637 int exponent; /* Bits in the exponent */
641 * The 16- and 128-bit formats are expected to be in IEEE 754r.
642 * AMD SSE5 uses the 16-bit format.
644 * The 32- and 64-bit formats are the original IEEE 754 formats.
646 * The 80-bit format is x87-specific, but widely used.
648 * The 8-bit format appears to be the consensus 8-bit floating-point
649 * format. It is apparently used in graphics applications.
651 static const struct ieee_format ieee_8 = { 1, 3, 0, 4 };
652 static const struct ieee_format ieee_16 = { 2, 10, 0, 5 };
653 static const struct ieee_format ieee_32 = { 4, 23, 0, 8 };
654 static const struct ieee_format ieee_64 = { 8, 52, 0, 11 };
655 static const struct ieee_format ieee_80 = { 10, 63, 1, 15 };
656 static const struct ieee_format ieee_128 = { 16, 112, 0, 15 };
658 /* Types of values we can generate */
668 static int to_packed_bcd(const char *str, const char *p,
669 int s, uint8_t *result,
670 const struct ieee_format *fmt)
676 if (fmt != &ieee_80) {
677 error(ERR_NONFATAL|ERR_PASS1,
678 "packed BCD requires an 80-bit format");
684 if (c >= '0' && c <= '9') {
687 error(ERR_WARNING|ERR_PASS1,
688 "packed BCD truncated to 18 digits");
693 *result++ = tv + ((c-'0') << 4);
697 } else if (c == '_') {
700 error(ERR_NONFATAL|ERR_PASS1,
701 "invalid character `%c' in packed BCD constant", c);
714 *result = (s < 0) ? 0x80 : 0;
716 return 1; /* success */
719 static int to_float(const char *str, int s, uint8_t *result,
720 const struct ieee_format *fmt)
722 fp_limb mant[MANT_LIMBS];
723 int32_t exponent = 0;
724 const int32_t expmax = 1 << (fmt->exponent - 1);
725 fp_limb one_mask = LIMB_TOP_BIT >>
726 ((fmt->exponent+fmt->explicit) % LIMB_BITS);
727 const int one_pos = (fmt->exponent+fmt->explicit)/LIMB_BITS;
732 const bool minus = s < 0;
733 const int bits = fmt->bytes * 8;
738 "internal errror: empty string passed to float_const");
742 strend = strchr(str, '\0');
743 if (strend[-1] == 'P' || strend[-1] == 'p')
744 return to_packed_bcd(str, strend-2, s, result, fmt);
750 case 'n': /* __nan__ */
752 case 'q': /* __qnan__ */
756 case 's': /* __snan__ */
760 case 'i': /* __infinity__ */
765 error(ERR_NONFATAL|ERR_PASS1,
766 "internal error: unknown FP constant token `%s'\n", str);
775 ok = ieee_flconvert_bin(str+2, 4, mant, &exponent);
779 ok = ieee_flconvert_bin(str+2, 3, mant, &exponent);
783 ok = ieee_flconvert_bin(str+2, 1, mant, &exponent);
787 ok = ieee_flconvert(str+2, mant, &exponent);
790 return to_packed_bcd(str+2, strend-1, s, result, fmt);
792 /* Leading zero was just a zero? */
793 ok = ieee_flconvert(str, mant, &exponent);
796 } else if (str[0] == '$') {
797 ok = ieee_flconvert_bin(str+1, 4, mant, &exponent);
799 ok = ieee_flconvert(str, mant, &exponent);
804 } else if (mant[0] & LIMB_TOP_BIT) {
809 if (exponent >= 2 - expmax && exponent <= expmax) {
811 } else if (exponent > 0) {
813 error(ERR_WARNING|ERR_WARN_FL_OVERFLOW|ERR_PASS1,
814 "overflow in floating-point constant");
817 /* underflow or denormal; the denormal code handles
830 memset(mant, 0, sizeof mant);
835 shift = -(exponent + expmax - 2 - fmt->exponent)
837 ieee_shr(mant, shift);
838 ieee_round(minus, mant, bits);
839 if (mant[one_pos] & one_mask) {
840 /* One's position is set, we rounded up into normal range */
843 mant[one_pos] &= ~one_mask; /* remove explicit one */
844 mant[0] |= exponent << (LIMB_BITS-1 - fmt->exponent);
846 if (daz || is_zero(mant)) {
847 /* Flush denormals to zero */
848 error(ERR_WARNING|ERR_WARN_FL_UNDERFLOW|ERR_PASS1,
849 "underflow in floating-point constant");
852 error(ERR_WARNING|ERR_WARN_FL_DENORM|ERR_PASS1,
853 "denormal floating-point constant");
860 exponent += expmax - 1;
861 ieee_shr(mant, fmt->exponent+fmt->explicit);
862 ieee_round(minus, mant, bits);
863 /* did we scale up by one? */
864 if (test_bit(mant, fmt->exponent+fmt->explicit-1)) {
867 if (exponent >= (expmax << 1)-1) {
868 error(ERR_WARNING|ERR_WARN_FL_OVERFLOW|ERR_PASS1,
869 "overflow in floating-point constant");
876 mant[one_pos] &= ~one_mask; /* remove explicit one */
877 mant[0] |= exponent << (LIMB_BITS-1 - fmt->exponent);
884 memset(mant, 0, sizeof mant);
885 mant[0] = (((fp_limb)1 << fmt->exponent)-1)
886 << (LIMB_BITS-1 - fmt->exponent);
888 mant[one_pos] |= one_mask;
890 set_bit(mant, fmt->exponent+fmt->explicit+1);
891 else if (type == FL_SNAN)
892 set_bit(mant, fmt->exponent+fmt->explicit+fmt->mantissa);
896 mant[0] |= minus ? LIMB_TOP_BIT : 0;
898 for (i = fmt->bytes - 1; i >= 0; i--)
899 *result++ = mant[i/LIMB_BYTES] >> (((LIMB_BYTES-1)-(i%LIMB_BYTES))*8);
901 return 1; /* success */
904 int float_const(const char *number, int sign, uint8_t *result,
905 int bytes, efunc err)
911 return to_float(number, sign, result, &ieee_8);
913 return to_float(number, sign, result, &ieee_16);
915 return to_float(number, sign, result, &ieee_32);
917 return to_float(number, sign, result, &ieee_64);
919 return to_float(number, sign, result, &ieee_80);
921 return to_float(number, sign, result, &ieee_128);
923 error(ERR_PANIC, "strange value %d passed to float_const", bytes);
928 /* Set floating-point options */
929 int float_option(const char *option)
931 if (!nasm_stricmp(option, "daz")) {
934 } else if (!nasm_stricmp(option, "nodaz")) {
937 } else if (!nasm_stricmp(option, "near")) {
940 } else if (!nasm_stricmp(option, "down")) {
943 } else if (!nasm_stricmp(option, "up")) {
946 } else if (!nasm_stricmp(option, "zero")) {
949 } else if (!nasm_stricmp(option, "default")) {
954 return -1; /* Unknown option */