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
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
51 static bool daz
= false; /* denormals as zero */
52 static enum float_round rc
= FLOAT_RC_NEAR
; /* rounding control */
60 /* "A limb is like a digit but bigger */
61 typedef uint32_t fp_limb
;
62 typedef uint64_t fp_2limb
;
65 #define LIMB_BYTES (LIMB_BITS/8)
66 #define LIMB_TOP_BIT ((fp_limb)1 << (LIMB_BITS-1))
67 #define LIMB_MASK ((fp_limb)(~0))
68 #define LIMB_ALL_BYTES ((fp_limb)0x01010101)
69 #define LIMB_BYTE(x) ((x)*LIMB_ALL_BYTES)
71 /* 112 bits + 64 bits for accuracy + 16 bits for rounding */
74 /* 52 digits fit in 176 bits because 10^53 > 2^176 > 10^52 */
75 #define MANT_DIGITS 52
77 /* the format and the argument list depend on MANT_LIMBS */
78 #define MANT_FMT "%08x_%08x_%08x_%08x_%08x_%08x"
79 #define MANT_ARG SOME_ARG(mant, 0)
81 #define SOME_ARG(a,i) (a)[(i)+0], (a)[(i)+1], (a)[(i)+2], \
82 (a)[(i)+3], (a)[(i)+4], (a)[(i)+5]
85 * ---------------------------------------------------------------------------
86 * emit a printf()-like debug message... but only if DEBUG_FLOAT was defined
87 * ---------------------------------------------------------------------------
91 #define dprintf(x) printf x
93 #define dprintf(x) do { } while (0)
97 * ---------------------------------------------------------------------------
99 * ---------------------------------------------------------------------------
101 static int float_multiply(fp_limb
*to
, fp_limb
*from
)
103 fp_2limb temp
[MANT_LIMBS
* 2];
107 * guaranteed that top bit of 'from' is set -- so we only have
108 * to worry about _one_ bit shift to the left
110 dprintf(("%s=" MANT_FMT
"\n", "mul1", SOME_ARG(to
, 0)));
111 dprintf(("%s=" MANT_FMT
"\n", "mul2", SOME_ARG(from
, 0)));
113 memset(temp
, 0, sizeof temp
);
115 for (i
= 0; i
< MANT_LIMBS
; i
++) {
116 for (j
= 0; j
< MANT_LIMBS
; j
++) {
118 n
= (fp_2limb
) to
[i
] * (fp_2limb
) from
[j
];
119 temp
[i
+ j
] += n
>> LIMB_BITS
;
120 temp
[i
+ j
+ 1] += (fp_limb
)n
;
124 for (i
= MANT_LIMBS
* 2; --i
;) {
125 temp
[i
- 1] += temp
[i
] >> LIMB_BITS
;
126 temp
[i
] &= LIMB_MASK
;
129 dprintf(("%s=" MANT_FMT
"_" MANT_FMT
"\n", "temp", SOME_ARG(temp
, 0),
130 SOME_ARG(temp
, MANT_LIMBS
)));
132 if (temp
[0] & LIMB_TOP_BIT
) {
133 for (i
= 0; i
< MANT_LIMBS
; i
++) {
134 to
[i
] = temp
[i
] & LIMB_MASK
;
136 dprintf(("%s=" MANT_FMT
" (%i)\n", "prod", SOME_ARG(to
, 0), 0));
139 for (i
= 0; i
< MANT_LIMBS
; i
++) {
140 to
[i
] = (temp
[i
] << 1) + !!(temp
[i
+ 1] & LIMB_TOP_BIT
);
142 dprintf(("%s=" MANT_FMT
" (%i)\n", "prod", SOME_ARG(to
, 0), -1));
148 * ---------------------------------------------------------------------------
149 * read an exponent; returns INT32_MAX on error
150 * ---------------------------------------------------------------------------
152 static int32_t read_exponent(const char *string
, int32_t max
)
157 if (*string
== '+') {
159 } else if (*string
== '-') {
164 if (*string
>= '0' && *string
<= '9') {
165 i
= (i
* 10) + (*string
- '0');
168 * To ensure that underflows and overflows are
169 * handled properly we must avoid wraparounds of
170 * the signed integer value that is used to hold
171 * the exponent. Therefore we cap the exponent at
172 * +/-5000, which is slightly more/less than
173 * what's required for normal and denormal numbers
174 * in single, double, and extended precision, but
175 * sufficient to avoid signed integer wraparound.
179 } else if (*string
== '_') {
182 nasm_nonfatal("invalid character in floating-point constant %s: '%c'",
183 "exponent", *string
);
193 * ---------------------------------------------------------------------------
195 * ---------------------------------------------------------------------------
197 static bool ieee_flconvert(const char *string
, fp_limb
*mant
,
200 char digits
[MANT_DIGITS
];
202 fp_limb mult
[MANT_LIMBS
], bit
;
204 int32_t tenpwr
, twopwr
;
206 bool started
, seendot
, warned
;
211 started
= seendot
= false;
213 while (*string
&& *string
!= 'E' && *string
!= 'e') {
214 if (*string
== '.') {
218 nasm_nonfatal("too many periods in floating-point constant");
221 } else if (*string
>= '0' && *string
<= '9') {
222 if (*string
== '0' && !started
) {
228 if (p
< digits
+ sizeof(digits
)) {
229 *p
++ = *string
- '0';
233 *!float-toolong [on] too many digits in floating-point number
234 *! warns about too many digits in floating-point numbers.
236 nasm_warn(WARN_FLOAT_TOOLONG
|ERR_PASS2
,
237 "floating-point constant significand contains "
238 "more than %i digits", MANT_DIGITS
);
246 } else if (*string
== '_') {
249 nasm_nonfatalf(ERR_PASS2
,
250 "invalid character in floating-point constant %s: '%c'",
251 "significand", *string
);
260 string
++; /* eat the E */
261 e
= read_exponent(string
, 5000);
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.
275 dprintf(("%c", *q
+ '0'));
278 dprintf((" * 10^%i\n", tenpwr
));
281 * Now convert [digits,p) to our internal representation.
284 for (m
= mant
; m
< mant
+ MANT_LIMBS
; m
++) {
291 while (m
< mant
+ MANT_LIMBS
) {
293 while (p
> q
&& !p
[-1]) {
299 for (r
= p
; r
-- > q
;) {
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
,
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;
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
++) {
363 dprintf(("loop=" MANT_FMT
" * 2^%i * 5^%i (%i)\n", MANT_ARG
,
364 twopwr
, tenpwr
, extratwos
));
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
);
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.
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
)
419 for (i
= 0; i
< MANT_LIMBS
; i
++)
427 * ---------------------------------------------------------------------------
428 * round a mantissa off after i words
429 * ---------------------------------------------------------------------------
432 #define ROUND_COLLECT_BITS \
434 m = mant[i] & (2*bit-1); \
435 for (j = i+1; j < MANT_LIMBS; j++) \
439 #define ROUND_ABS_DOWN \
441 mant[i] &= ~(bit-1); \
442 for (j = i+1; j < MANT_LIMBS; j++) \
447 #define ROUND_ABS_UP \
449 mant[i] = (mant[i] & ~(bit-1)) + bit; \
450 for (j = i+1; j < MANT_LIMBS; j++) \
452 while (i > 0 && !mant[i]) \
457 static bool ieee_round(bool minus
, fp_limb
*mant
, int bits
)
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
) {
473 if (test_bit(mant
, bits
-1)) {
482 } else if (rc
== FLOAT_RC_ZERO
||
483 rc
== (minus
? FLOAT_RC_UP
: FLOAT_RC_DOWN
)) {
486 /* rc == (minus ? FLOAT_RC_DOWN : FLOAT_RC_UP) */
487 /* Round toward +/- infinity */
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')
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
;
518 bool seendot
, seendigit
;
520 const int radix
= 1 << bits
;
524 seendot
= seendigit
= false;
528 memset(mult
, 0, sizeof mult
);
530 while ((c
= *string
++) != '\0') {
535 nasm_nonfatal("too many periods in floating-point constant");
538 } else if ((v
= hexval(c
)) < (unsigned int)radix
) {
539 if (!seendigit
&& v
) {
544 ms
= (LIMB_BITS
-1)-l
;
551 /* Cast to fp_2limb as ms == -LIMB_BITS is possible. */
552 *mp
|= (fp_2limb
)v
>> -ms
;
554 if (mp
> &mult
[MANT_LIMBS
])
555 mp
= &mult
[MANT_LIMBS
]; /* Guard slot */
567 } else if (c
== 'p' || c
== 'P') {
569 e
= read_exponent(string
, 20000);
574 } else if (c
== '_') {
577 nasm_nonfatal("floating-point constant: `%c' is invalid character", c
);
583 memset(mant
, 0, MANT_LIMBS
*sizeof(fp_limb
)); /* Zero */
586 memcpy(mant
, mult
, MANT_LIMBS
*sizeof(fp_limb
));
594 * Shift a mantissa to the right by i bits.
596 static void ieee_shr(fp_limb
*mant
, int i
)
602 sr
= i
% LIMB_BITS
; sl
= LIMB_BITS
-sr
;
607 for (j
= MANT_LIMBS
-1; j
>= offs
; j
--)
608 mant
[j
] = mant
[j
-offs
];
609 } else if (MANT_LIMBS
-1-offs
< 0) {
612 n
= mant
[MANT_LIMBS
-1-offs
] >> sr
;
613 for (j
= MANT_LIMBS
-1; j
> offs
; j
--) {
615 mant
[j
] = (m
<< sl
) | n
;
624 /* Produce standard IEEE formats, with implicit or explicit integer
625 bit; this makes the following assumptions:
627 - the sign bit is the MSB, followed by the exponent,
628 followed by the integer bit if present.
629 - the sign bit plus exponent fit in 16 bits.
630 - the exponent bias is 2^(n-1)-1 for an n-bit exponent */
634 int mantissa
; /* Fractional bits in the mantissa */
635 int explicit; /* Explicit integer */
636 int exponent
; /* Bits in the exponent */
640 * The 16- and 128-bit formats are expected to be in IEEE 754r.
641 * AMD SSE5 uses the 16-bit format.
643 * The 32- and 64-bit formats are the original IEEE 754 formats.
645 * The 80-bit format is x87-specific, but widely used.
647 * The 8-bit format appears to be the consensus 8-bit floating-point
648 * format. It is apparently used in graphics applications.
650 static const struct ieee_format ieee_8
= { 1, 3, 0, 4 };
651 static const struct ieee_format ieee_16
= { 2, 10, 0, 5 };
652 static const struct ieee_format ieee_32
= { 4, 23, 0, 8 };
653 static const struct ieee_format ieee_64
= { 8, 52, 0, 11 };
654 static const struct ieee_format ieee_80
= { 10, 63, 1, 15 };
655 static const struct ieee_format ieee_128
= { 16, 112, 0, 15 };
657 /* Types of values we can generate */
667 static int to_packed_bcd(const char *str
, const char *p
,
668 int s
, uint8_t *result
,
669 const struct ieee_format
*fmt
)
675 if (fmt
!= &ieee_80
) {
676 nasm_nonfatal("packed BCD requires an 80-bit format");
682 if (c
>= '0' && c
<= '9') {
685 nasm_warn(WARN_OTHER
|ERR_PASS2
, "packed BCD truncated to 18 digits");
689 *result
++ = tv
+ ((c
-'0') << 4);
693 } else if (c
== '_') {
696 nasm_nonfatal("invalid character `%c' in packed BCD constant", c
);
709 *result
= (s
< 0) ? 0x80 : 0;
711 return 1; /* success */
714 static int to_float(const char *str
, int s
, uint8_t *result
,
715 const struct ieee_format
*fmt
)
717 fp_limb mant
[MANT_LIMBS
];
718 int32_t exponent
= 0;
719 const int32_t expmax
= 1 << (fmt
->exponent
- 1);
720 fp_limb one_mask
= LIMB_TOP_BIT
>>
721 ((fmt
->exponent
+fmt
->explicit) % LIMB_BITS
);
722 const int one_pos
= (fmt
->exponent
+fmt
->explicit)/LIMB_BITS
;
727 const bool minus
= s
< 0;
728 const int bits
= fmt
->bytes
* 8;
733 strend
= strchr(str
, '\0');
734 if (strend
[-1] == 'P' || strend
[-1] == 'p')
735 return to_packed_bcd(str
, strend
-2, s
, result
, fmt
);
741 case 'n': /* __nan__ */
743 case 'q': /* __qnan__ */
747 case 's': /* __snan__ */
751 case 'i': /* __infinity__ */
756 nasm_nonfatal("internal error: unknown FP constant token `%s'", str
);
765 ok
= ieee_flconvert_bin(str
+2, 4, mant
, &exponent
);
769 ok
= ieee_flconvert_bin(str
+2, 3, mant
, &exponent
);
773 ok
= ieee_flconvert_bin(str
+2, 1, mant
, &exponent
);
777 ok
= ieee_flconvert(str
+2, mant
, &exponent
);
780 return to_packed_bcd(str
+2, strend
-1, s
, result
, fmt
);
782 /* Leading zero was just a zero? */
783 ok
= ieee_flconvert(str
, mant
, &exponent
);
786 } else if (str
[0] == '$') {
787 ok
= ieee_flconvert_bin(str
+1, 4, mant
, &exponent
);
789 ok
= ieee_flconvert(str
, mant
, &exponent
);
794 } else if (mant
[0] & LIMB_TOP_BIT
) {
799 if (exponent
>= 2 - expmax
&& exponent
<= expmax
) {
801 } else if (exponent
> 0) {
802 nasm_warn(WARN_FLOAT_OVERFLOW
|ERR_PASS2
,
803 "overflow in floating-point constant");
806 /* underflow or denormal; the denormal code handles
819 memset(mant
, 0, sizeof mant
);
824 shift
= -(exponent
+ expmax
- 2 - fmt
->exponent
)
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 */
832 mant
[one_pos
] &= ~one_mask
; /* remove explicit one */
833 mant
[0] |= exponent
<< (LIMB_BITS
-1 - fmt
->exponent
);
835 if (daz
|| is_zero(mant
)) {
837 *!float-underflow [off] floating point underflow
838 *! warns about floating point underflow (a nonzero
839 *! constant rounded to zero.)
841 nasm_warn(WARN_FLOAT_UNDERFLOW
|ERR_PASS2
,
842 "underflow in floating-point constant");
846 *!float-denorm [off] floating point denormal
847 *! warns about denormal floating point constants.
849 nasm_warn(WARN_FLOAT_DENORM
|ERR_PASS2
,
850 "denormal floating-point constant");
857 exponent
+= expmax
- 1;
858 ieee_shr(mant
, fmt
->exponent
+fmt
->explicit);
859 ieee_round(minus
, mant
, bits
);
860 /* did we scale up by one? */
861 if (test_bit(mant
, fmt
->exponent
+fmt
->explicit-1)) {
864 if (exponent
>= (expmax
<< 1)-1) {
866 *!float-overflow [on] floating point overflow
867 *! warns about floating point underflow.
869 nasm_warn(WARN_FLOAT_OVERFLOW
|ERR_PASS2
,
870 "overflow in floating-point constant");
877 mant
[one_pos
] &= ~one_mask
; /* remove explicit one */
878 mant
[0] |= exponent
<< (LIMB_BITS
-1 - fmt
->exponent
);
885 memset(mant
, 0, sizeof mant
);
886 mant
[0] = (((fp_limb
)1 << fmt
->exponent
)-1)
887 << (LIMB_BITS
-1 - fmt
->exponent
);
889 mant
[one_pos
] |= one_mask
;
891 set_bit(mant
, fmt
->exponent
+fmt
->explicit+1);
892 else if (type
== FL_SNAN
)
893 set_bit(mant
, fmt
->exponent
+fmt
->explicit+fmt
->mantissa
);
897 mant
[0] |= minus
? LIMB_TOP_BIT
: 0;
899 for (i
= fmt
->bytes
- 1; i
>= 0; i
--)
900 *result
++ = mant
[i
/LIMB_BYTES
] >> (((LIMB_BYTES
-1)-(i
%LIMB_BYTES
))*8);
902 return 1; /* success */
905 int float_const(const char *number
, int sign
, uint8_t *result
, int bytes
)
909 return to_float(number
, sign
, result
, &ieee_8
);
911 return to_float(number
, sign
, result
, &ieee_16
);
913 return to_float(number
, sign
, result
, &ieee_32
);
915 return to_float(number
, sign
, result
, &ieee_64
);
917 return to_float(number
, sign
, result
, &ieee_80
);
919 return to_float(number
, sign
, result
, &ieee_128
);
921 nasm_panic("strange value %d passed to float_const", bytes
);
926 /* Set floating-point options */
927 int float_option(const char *option
)
929 if (!nasm_stricmp(option
, "daz")) {
932 } else if (!nasm_stricmp(option
, "nodaz")) {
935 } else if (!nasm_stricmp(option
, "near")) {
938 } else if (!nasm_stricmp(option
, "down")) {
941 } else if (!nasm_stricmp(option
, "up")) {
944 } else if (!nasm_stricmp(option
, "zero")) {
947 } else if (!nasm_stricmp(option
, "default")) {
952 return -1; /* Unknown option */