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
53 static bool daz
= false; /* denormals as zero */
54 static enum float_round rc
= FLOAT_RC_NEAR
; /* rounding control */
62 /* "A limb is like a digit but bigger */
63 typedef uint32_t fp_limb
;
64 typedef uint64_t fp_2limb
;
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 */
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 * ---------------------------------------------------------------------------
93 #define dprintf(x) printf x
95 #define dprintf(x) do { } while (0)
99 * ---------------------------------------------------------------------------
101 * ---------------------------------------------------------------------------
103 static int float_multiply(fp_limb
*to
, fp_limb
*from
)
105 fp_2limb temp
[MANT_LIMBS
* 2];
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
++) {
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));
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));
150 * ---------------------------------------------------------------------------
151 * read an exponent; returns INT32_MAX on error
152 * ---------------------------------------------------------------------------
154 static int32_t read_exponent(const char *string
, int32_t max
)
159 if (*string
== '+') {
161 } else if (*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.
181 } else if (*string
== '_') {
184 nasm_error(ERR_NONFATAL
|ERR_PASS1
,
185 "invalid character in floating-point constant %s: '%c'",
186 "exponent", *string
);
196 * ---------------------------------------------------------------------------
198 * ---------------------------------------------------------------------------
200 static bool ieee_flconvert(const char *string
, fp_limb
*mant
,
203 char digits
[MANT_DIGITS
];
205 fp_limb mult
[MANT_LIMBS
], bit
;
207 int32_t tenpwr
, twopwr
;
209 bool started
, seendot
, warned
;
214 started
= seendot
= false;
216 while (*string
&& *string
!= 'E' && *string
!= 'e') {
217 if (*string
== '.') {
221 nasm_error(ERR_NONFATAL
|ERR_PASS1
,
222 "too many periods in floating-point constant");
225 } else if (*string
>= '0' && *string
<= '9') {
226 if (*string
== '0' && !started
) {
232 if (p
< digits
+ sizeof(digits
)) {
233 *p
++ = *string
- '0';
236 nasm_error(ERR_WARNING
|ERR_WARN_FL_TOOLONG
|ERR_PASS1
,
237 "floating-point constant significand contains "
238 "more than %i digits", MANT_DIGITS
);
246 } else if (*string
== '_') {
249 nasm_error(ERR_NONFATAL
|ERR_PASS1
,
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_error(ERR_NONFATAL
|ERR_PASS1
,
536 "too many periods in floating-point constant");
539 } else if ((v
= hexval(c
)) < (unsigned int)radix
) {
540 if (!seendigit
&& v
) {
545 ms
= (LIMB_BITS
-1)-l
;
547 twopwr
= seendot
? twopwr
-bits
+l
: l
+1-bits
;
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_error(ERR_NONFATAL
|ERR_PASS1
,
578 "floating-point constant: `%c' is invalid character", c
);
584 memset(mant
, 0, MANT_LIMBS
*sizeof(fp_limb
)); /* Zero */
587 memcpy(mant
, mult
, MANT_LIMBS
*sizeof(fp_limb
));
595 * Shift a mantissa to the right by i bits.
597 static void ieee_shr(fp_limb
*mant
, int i
)
603 sr
= i
% LIMB_BITS
; sl
= LIMB_BITS
-sr
;
608 for (j
= MANT_LIMBS
-1; j
>= offs
; j
--)
609 mant
[j
] = mant
[j
-offs
];
611 n
= mant
[MANT_LIMBS
-1-offs
] >> sr
;
612 for (j
= MANT_LIMBS
-1; j
> offs
; j
--) {
614 mant
[j
] = (m
<< sl
) | n
;
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 */
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 */
666 static int to_packed_bcd(const char *str
, const char *p
,
667 int s
, uint8_t *result
,
668 const struct ieee_format
*fmt
)
674 if (fmt
!= &ieee_80
) {
675 nasm_error(ERR_NONFATAL
|ERR_PASS1
,
676 "packed BCD requires an 80-bit format");
682 if (c
>= '0' && c
<= '9') {
685 nasm_error(ERR_WARNING
|ERR_PASS1
,
686 "packed BCD truncated to 18 digits");
691 *result
++ = tv
+ ((c
-'0') << 4);
695 } else if (c
== '_') {
698 nasm_error(ERR_NONFATAL
|ERR_PASS1
,
699 "invalid character `%c' in packed BCD constant", c
);
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
;
730 const bool minus
= s
< 0;
731 const int bits
= fmt
->bytes
* 8;
736 "internal errror: empty string passed to float_const");
740 strend
= strchr(str
, '\0');
741 if (strend
[-1] == 'P' || strend
[-1] == 'p')
742 return to_packed_bcd(str
, strend
-2, s
, result
, fmt
);
748 case 'n': /* __nan__ */
750 case 'q': /* __qnan__ */
754 case 's': /* __snan__ */
758 case 'i': /* __infinity__ */
763 nasm_error(ERR_NONFATAL
|ERR_PASS1
,
764 "internal error: unknown FP constant token `%s'\n", str
);
773 ok
= ieee_flconvert_bin(str
+2, 4, mant
, &exponent
);
777 ok
= ieee_flconvert_bin(str
+2, 3, mant
, &exponent
);
781 ok
= ieee_flconvert_bin(str
+2, 1, mant
, &exponent
);
785 ok
= ieee_flconvert(str
+2, mant
, &exponent
);
788 return to_packed_bcd(str
+2, strend
-1, s
, result
, fmt
);
790 /* Leading zero was just a zero? */
791 ok
= ieee_flconvert(str
, mant
, &exponent
);
794 } else if (str
[0] == '$') {
795 ok
= ieee_flconvert_bin(str
+1, 4, mant
, &exponent
);
797 ok
= ieee_flconvert(str
, mant
, &exponent
);
802 } else if (mant
[0] & LIMB_TOP_BIT
) {
807 if (exponent
>= 2 - expmax
&& exponent
<= expmax
) {
809 } else if (exponent
> 0) {
811 nasm_error(ERR_WARNING
|ERR_WARN_FL_OVERFLOW
|ERR_PASS1
,
812 "overflow in floating-point constant");
815 /* underflow or denormal; the denormal code handles
828 memset(mant
, 0, sizeof mant
);
833 shift
= -(exponent
+ expmax
- 2 - fmt
->exponent
)
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 */
841 mant
[one_pos
] &= ~one_mask
; /* remove explicit one */
842 mant
[0] |= exponent
<< (LIMB_BITS
-1 - fmt
->exponent
);
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");
850 nasm_error(ERR_WARNING
|ERR_WARN_FL_DENORM
|ERR_PASS1
,
851 "denormal floating-point constant");
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)) {
865 if (exponent
>= (expmax
<< 1)-1) {
866 nasm_error(ERR_WARNING
|ERR_WARN_FL_OVERFLOW
|ERR_PASS1
,
867 "overflow in floating-point constant");
874 mant
[one_pos
] &= ~one_mask
; /* remove explicit one */
875 mant
[0] |= exponent
<< (LIMB_BITS
-1 - fmt
->exponent
);
882 memset(mant
, 0, sizeof mant
);
883 mant
[0] = (((fp_limb
)1 << fmt
->exponent
)-1)
884 << (LIMB_BITS
-1 - fmt
->exponent
);
886 mant
[one_pos
] |= one_mask
;
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
);
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
)
906 return to_float(number
, sign
, result
, &ieee_8
);
908 return to_float(number
, sign
, result
, &ieee_16
);
910 return to_float(number
, sign
, result
, &ieee_32
);
912 return to_float(number
, sign
, result
, &ieee_64
);
914 return to_float(number
, sign
, result
, &ieee_80
);
916 return to_float(number
, sign
, result
, &ieee_128
);
918 nasm_panic(0, "strange value %d passed to float_const", bytes
);
923 /* Set floating-point options */
924 int float_option(const char *option
)
926 if (!nasm_stricmp(option
, "daz")) {
929 } else if (!nasm_stricmp(option
, "nodaz")) {
932 } else if (!nasm_stricmp(option
, "near")) {
935 } else if (!nasm_stricmp(option
, "down")) {
938 } else if (!nasm_stricmp(option
, "up")) {
941 } else if (!nasm_stricmp(option
, "zero")) {
944 } else if (!nasm_stricmp(option
, "default")) {
949 return -1; /* Unknown option */