1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2017 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
54 static bool daz
= false; /* denormals as zero */
55 static enum float_round rc
= FLOAT_RC_NEAR
; /* rounding control */
63 /* "A limb is like a digit but bigger */
64 typedef uint32_t fp_limb
;
65 typedef uint64_t fp_2limb
;
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 */
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 * ---------------------------------------------------------------------------
94 #define dprintf(x) printf x
96 #define dprintf(x) do { } while (0)
100 * ---------------------------------------------------------------------------
102 * ---------------------------------------------------------------------------
104 static int float_multiply(fp_limb
*to
, fp_limb
*from
)
106 fp_2limb temp
[MANT_LIMBS
* 2];
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
++) {
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));
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));
151 * ---------------------------------------------------------------------------
152 * read an exponent; returns INT32_MAX on error
153 * ---------------------------------------------------------------------------
155 static int32_t read_exponent(const char *string
, int32_t max
)
160 if (*string
== '+') {
162 } else if (*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.
182 } else if (*string
== '_') {
185 nasm_error(ERR_NONFATAL
|ERR_PASS1
,
186 "invalid character in floating-point constant %s: '%c'",
187 "exponent", *string
);
197 * ---------------------------------------------------------------------------
199 * ---------------------------------------------------------------------------
201 static bool ieee_flconvert(const char *string
, fp_limb
*mant
,
204 char digits
[MANT_DIGITS
];
206 fp_limb mult
[MANT_LIMBS
], bit
;
208 int32_t tenpwr
, twopwr
;
210 bool started
, seendot
, warned
;
215 started
= seendot
= false;
217 while (*string
&& *string
!= 'E' && *string
!= 'e') {
218 if (*string
== '.') {
222 nasm_error(ERR_NONFATAL
|ERR_PASS1
,
223 "too many periods in floating-point constant");
226 } else if (*string
>= '0' && *string
<= '9') {
227 if (*string
== '0' && !started
) {
233 if (p
< digits
+ sizeof(digits
)) {
234 *p
++ = *string
- '0';
237 nasm_error(ERR_WARNING
|ERR_WARN_FL_TOOLONG
|ERR_PASS1
,
238 "floating-point constant significand contains "
239 "more than %i digits", MANT_DIGITS
);
247 } else if (*string
== '_') {
250 nasm_error(ERR_NONFATAL
|ERR_PASS1
,
251 "invalid character in floating-point constant %s: '%c'",
252 "significand", *string
);
261 string
++; /* eat the E */
262 e
= read_exponent(string
, 5000);
269 * At this point, the memory interval [digits,p) contains a
270 * series of decimal digits zzzzzzz, such that our number X
271 * satisfies X = 0.zzzzzzz * 10^tenpwr.
276 dprintf(("%c", *q
+ '0'));
279 dprintf((" * 10^%i\n", tenpwr
));
282 * Now convert [digits,p) to our internal representation.
285 for (m
= mant
; m
< mant
+ MANT_LIMBS
; m
++) {
292 while (m
< mant
+ MANT_LIMBS
) {
294 while (p
> q
&& !p
[-1]) {
300 for (r
= p
; r
-- > q
;) {
329 * At this point, the 'mant' array contains the first frac-
330 * tional places of a base-2^16 real number which when mul-
331 * tiplied by 2^twopwr and 5^tenpwr gives X.
333 dprintf(("X = " MANT_FMT
" * 2^%i * 5^%i\n", MANT_ARG
, twopwr
,
337 * Now multiply 'mant' by 5^tenpwr.
339 if (tenpwr
< 0) { /* mult = 5^-1 = 0.2 */
340 for (m
= mult
; m
< mult
+ MANT_LIMBS
- 1; m
++) {
341 *m
= LIMB_BYTE(0xcc);
343 mult
[MANT_LIMBS
- 1] = LIMB_BYTE(0xcc)+1;
348 * If tenpwr was 1000...000b, then it becomes 1000...000b. See
349 * the "ANSI C" comment below for more details on that case.
351 * Because we already truncated tenpwr to +5000...-5000 inside
352 * the exponent parsing code, this shouldn't happen though.
354 } else if (tenpwr
> 0) { /* mult = 5^+1 = 5.0 */
355 mult
[0] = (fp_limb
)5 << (LIMB_BITS
-3); /* 0xA000... */
356 for (m
= mult
+ 1; m
< mult
+ MANT_LIMBS
; m
++) {
364 dprintf(("loop=" MANT_FMT
" * 2^%i * 5^%i (%i)\n", MANT_ARG
,
365 twopwr
, tenpwr
, extratwos
));
367 dprintf(("mant*mult\n"));
368 twopwr
+= extratwos
+ float_multiply(mant
, mult
);
370 dprintf(("mult*mult\n"));
371 extratwos
= extratwos
* 2 + float_multiply(mult
, mult
);
375 * In ANSI C, the result of right-shifting a signed integer is
376 * considered implementation-specific. To ensure that the loop
377 * terminates even if tenpwr was 1000...000b to begin with, we
378 * manually clear the MSB, in case a 1 was shifted in.
380 * Because we already truncated tenpwr to +5000...-5000 inside
381 * the exponent parsing code, this shouldn't matter; neverthe-
382 * less it is the right thing to do here.
384 tenpwr
&= (uint32_t) - 1 >> 1;
388 * At this point, the 'mant' array contains the first frac-
389 * tional places of a base-2^16 real number in [0.5,1) that
390 * when multiplied by 2^twopwr gives X. Or it contains zero
391 * of course. We are done.
398 * ---------------------------------------------------------------------------
399 * operations of specific bits
400 * ---------------------------------------------------------------------------
403 /* Set a bit, using *bigendian* bit numbering (0 = MSB) */
404 static void set_bit(fp_limb
*mant
, int bit
)
406 mant
[bit
/LIMB_BITS
] |= LIMB_TOP_BIT
>> (bit
& (LIMB_BITS
-1));
409 /* Test a single bit */
410 static int test_bit(const fp_limb
*mant
, int bit
)
412 return (mant
[bit
/LIMB_BITS
] >> (~bit
& (LIMB_BITS
-1))) & 1;
415 /* Report if the mantissa value is all zero */
416 static bool is_zero(const fp_limb
*mant
)
420 for (i
= 0; i
< MANT_LIMBS
; i
++)
428 * ---------------------------------------------------------------------------
429 * round a mantissa off after i words
430 * ---------------------------------------------------------------------------
433 #define ROUND_COLLECT_BITS \
435 m = mant[i] & (2*bit-1); \
436 for (j = i+1; j < MANT_LIMBS; j++) \
440 #define ROUND_ABS_DOWN \
442 mant[i] &= ~(bit-1); \
443 for (j = i+1; j < MANT_LIMBS; j++) \
448 #define ROUND_ABS_UP \
450 mant[i] = (mant[i] & ~(bit-1)) + bit; \
451 for (j = i+1; j < MANT_LIMBS; j++) \
453 while (i > 0 && !mant[i]) \
458 static bool ieee_round(bool minus
, fp_limb
*mant
, int bits
)
462 int i
= bits
/ LIMB_BITS
;
463 int p
= bits
% LIMB_BITS
;
464 fp_limb bit
= LIMB_TOP_BIT
>> p
;
466 if (rc
== FLOAT_RC_NEAR
) {
474 if (test_bit(mant
, bits
-1)) {
483 } else if (rc
== FLOAT_RC_ZERO
||
484 rc
== (minus
? FLOAT_RC_UP
: FLOAT_RC_DOWN
)) {
487 /* rc == (minus ? FLOAT_RC_DOWN : FLOAT_RC_UP) */
488 /* Round toward +/- infinity */
499 /* Returns a value >= 16 if not a valid hex digit */
500 static unsigned int hexval(char c
)
502 unsigned int v
= (unsigned char) c
;
504 if (v
>= '0' && v
<= '9')
507 return (v
|0x20) - 'a' + 10;
510 /* Handle floating-point numbers with radix 2^bits and binary exponent */
511 static bool ieee_flconvert_bin(const char *string
, int bits
,
512 fp_limb
*mant
, int32_t *exponent
)
514 static const int log2tbl
[16] =
515 { -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3 };
516 fp_limb mult
[MANT_LIMBS
+ 1], *mp
;
519 bool seendot
, seendigit
;
521 const int radix
= 1 << bits
;
525 seendot
= seendigit
= false;
529 memset(mult
, 0, sizeof mult
);
531 while ((c
= *string
++) != '\0') {
536 nasm_error(ERR_NONFATAL
|ERR_PASS1
,
537 "too many periods in floating-point constant");
540 } else if ((v
= hexval(c
)) < (unsigned int)radix
) {
541 if (!seendigit
&& v
) {
546 ms
= (LIMB_BITS
-1)-l
;
555 if (mp
> &mult
[MANT_LIMBS
])
556 mp
= &mult
[MANT_LIMBS
]; /* Guard slot */
568 } else if (c
== 'p' || c
== 'P') {
570 e
= read_exponent(string
, 20000);
575 } else if (c
== '_') {
578 nasm_error(ERR_NONFATAL
|ERR_PASS1
,
579 "floating-point constant: `%c' is invalid character", c
);
585 memset(mant
, 0, MANT_LIMBS
*sizeof(fp_limb
)); /* Zero */
588 memcpy(mant
, mult
, MANT_LIMBS
*sizeof(fp_limb
));
596 * Shift a mantissa to the right by i bits.
598 static void ieee_shr(fp_limb
*mant
, int i
)
604 sr
= i
% LIMB_BITS
; sl
= LIMB_BITS
-sr
;
609 for (j
= MANT_LIMBS
-1; j
>= offs
; j
--)
610 mant
[j
] = mant
[j
-offs
];
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_error(ERR_NONFATAL
|ERR_PASS1
,
677 "packed BCD requires an 80-bit format");
683 if (c
>= '0' && c
<= '9') {
686 nasm_error(ERR_WARNING
|ERR_PASS1
,
687 "packed BCD truncated to 18 digits");
692 *result
++ = tv
+ ((c
-'0') << 4);
696 } else if (c
== '_') {
699 nasm_error(ERR_NONFATAL
|ERR_PASS1
,
700 "invalid character `%c' in packed BCD constant", c
);
713 *result
= (s
< 0) ? 0x80 : 0;
715 return 1; /* success */
718 static int to_float(const char *str
, int s
, uint8_t *result
,
719 const struct ieee_format
*fmt
)
721 fp_limb mant
[MANT_LIMBS
];
722 int32_t exponent
= 0;
723 const int32_t expmax
= 1 << (fmt
->exponent
- 1);
724 fp_limb one_mask
= LIMB_TOP_BIT
>>
725 ((fmt
->exponent
+fmt
->explicit) % LIMB_BITS
);
726 const int one_pos
= (fmt
->exponent
+fmt
->explicit)/LIMB_BITS
;
731 const bool minus
= s
< 0;
732 const int bits
= fmt
->bytes
* 8;
737 "internal errror: empty string passed to float_const");
741 strend
= strchr(str
, '\0');
742 if (strend
[-1] == 'P' || strend
[-1] == 'p')
743 return to_packed_bcd(str
, strend
-2, s
, result
, fmt
);
749 case 'n': /* __nan__ */
751 case 'q': /* __qnan__ */
755 case 's': /* __snan__ */
759 case 'i': /* __infinity__ */
764 nasm_error(ERR_NONFATAL
|ERR_PASS1
,
765 "internal error: unknown FP constant token `%s'\n", str
);
774 ok
= ieee_flconvert_bin(str
+2, 4, mant
, &exponent
);
778 ok
= ieee_flconvert_bin(str
+2, 3, mant
, &exponent
);
782 ok
= ieee_flconvert_bin(str
+2, 1, mant
, &exponent
);
786 ok
= ieee_flconvert(str
+2, mant
, &exponent
);
789 return to_packed_bcd(str
+2, strend
-1, s
, result
, fmt
);
791 /* Leading zero was just a zero? */
792 ok
= ieee_flconvert(str
, mant
, &exponent
);
795 } else if (str
[0] == '$') {
796 ok
= ieee_flconvert_bin(str
+1, 4, mant
, &exponent
);
798 ok
= ieee_flconvert(str
, mant
, &exponent
);
803 } else if (mant
[0] & LIMB_TOP_BIT
) {
808 if (exponent
>= 2 - expmax
&& exponent
<= expmax
) {
810 } else if (exponent
> 0) {
812 nasm_error(ERR_WARNING
|ERR_WARN_FL_OVERFLOW
|ERR_PASS1
,
813 "overflow in floating-point constant");
816 /* underflow or denormal; the denormal code handles
829 memset(mant
, 0, sizeof mant
);
834 shift
= -(exponent
+ expmax
- 2 - fmt
->exponent
)
836 ieee_shr(mant
, shift
);
837 ieee_round(minus
, mant
, bits
);
838 if (mant
[one_pos
] & one_mask
) {
839 /* One's position is set, we rounded up into normal range */
842 mant
[one_pos
] &= ~one_mask
; /* remove explicit one */
843 mant
[0] |= exponent
<< (LIMB_BITS
-1 - fmt
->exponent
);
845 if (daz
|| is_zero(mant
)) {
846 /* Flush denormals to zero */
847 nasm_error(ERR_WARNING
|ERR_WARN_FL_UNDERFLOW
|ERR_PASS1
,
848 "underflow in floating-point constant");
851 nasm_error(ERR_WARNING
|ERR_WARN_FL_DENORM
|ERR_PASS1
,
852 "denormal floating-point constant");
859 exponent
+= expmax
- 1;
860 ieee_shr(mant
, fmt
->exponent
+fmt
->explicit);
861 ieee_round(minus
, mant
, bits
);
862 /* did we scale up by one? */
863 if (test_bit(mant
, fmt
->exponent
+fmt
->explicit-1)) {
866 if (exponent
>= (expmax
<< 1)-1) {
867 nasm_error(ERR_WARNING
|ERR_WARN_FL_OVERFLOW
|ERR_PASS1
,
868 "overflow in floating-point constant");
875 mant
[one_pos
] &= ~one_mask
; /* remove explicit one */
876 mant
[0] |= exponent
<< (LIMB_BITS
-1 - fmt
->exponent
);
883 memset(mant
, 0, sizeof mant
);
884 mant
[0] = (((fp_limb
)1 << fmt
->exponent
)-1)
885 << (LIMB_BITS
-1 - fmt
->exponent
);
887 mant
[one_pos
] |= one_mask
;
889 set_bit(mant
, fmt
->exponent
+fmt
->explicit+1);
890 else if (type
== FL_SNAN
)
891 set_bit(mant
, fmt
->exponent
+fmt
->explicit+fmt
->mantissa
);
895 mant
[0] |= minus
? LIMB_TOP_BIT
: 0;
897 for (i
= fmt
->bytes
- 1; i
>= 0; i
--)
898 *result
++ = mant
[i
/LIMB_BYTES
] >> (((LIMB_BYTES
-1)-(i
%LIMB_BYTES
))*8);
900 return 1; /* success */
903 int float_const(const char *number
, int sign
, uint8_t *result
, int bytes
)
907 return to_float(number
, sign
, result
, &ieee_8
);
909 return to_float(number
, sign
, result
, &ieee_16
);
911 return to_float(number
, sign
, result
, &ieee_32
);
913 return to_float(number
, sign
, result
, &ieee_64
);
915 return to_float(number
, sign
, result
, &ieee_80
);
917 return to_float(number
, sign
, result
, &ieee_128
);
919 nasm_panic(0, "strange value %d passed to float_const", bytes
);
924 /* Set floating-point options */
925 int float_option(const char *option
)
927 if (!nasm_stricmp(option
, "daz")) {
930 } else if (!nasm_stricmp(option
, "nodaz")) {
933 } else if (!nasm_stricmp(option
, "near")) {
936 } else if (!nasm_stricmp(option
, "down")) {
939 } else if (!nasm_stricmp(option
, "up")) {
942 } else if (!nasm_stricmp(option
, "zero")) {
945 } else if (!nasm_stricmp(option
, "default")) {
950 return -1; /* Unknown option */