1 /* float.c floating-point constant support for the Netwide Assembler
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the licence given in the file "Licence"
6 * distributed in the NASM archive.
8 * initial version 13/ix/96 by Simon Tatham
28 static bool daz
= false; /* denormals as zero */
29 static enum float_round rc
= FLOAT_RC_NEAR
; /* rounding control */
37 /* 112 bits + 64 bits for accuracy + 16 bits for rounding */
40 /* 52 digits fit in 176 bits because 10^53 > 2^176 > 10^52 */
41 #define MANT_DIGITS 52
43 /* the format and the argument list depend on MANT_WORDS */
44 #define MANT_FMT "%04x%04x_%04x%04x_%04x%04x_%04x%04x_%04x%04x_%04x%04x"
45 #define MANT_ARG SOME_ARG(mant, 0)
47 #define SOME_ARG(a,i) (a)[(i)+0], (a)[(i)+1], (a)[(i)+2], (a)[(i)+3], \
48 (a)[(i)+4], (a)[(i)+5], (a)[(i)+6], (a)[(i)+7], (a)[(i)+8], \
49 (a)[(i)+9], (a)[(i)+10], (a)[(i)+11]
52 * ---------------------------------------------------------------------------
53 * emit a printf()-like debug message... but only if DEBUG_FLOAT was defined
54 * ---------------------------------------------------------------------------
58 #define dprintf(x) printf x
60 #define dprintf(x) do { } while (0)
64 * ---------------------------------------------------------------------------
66 * ---------------------------------------------------------------------------
68 static int float_multiply(uint16_t * to
, uint16_t * from
)
70 uint32_t temp
[MANT_WORDS
* 2];
74 * guaranteed that top bit of 'from' is set -- so we only have
75 * to worry about _one_ bit shift to the left
77 dprintf(("%s=" MANT_FMT
"\n", "mul1", SOME_ARG(to
, 0)));
78 dprintf(("%s=" MANT_FMT
"\n", "mul2", SOME_ARG(from
, 0)));
80 memset(temp
, 0, sizeof temp
);
82 for (i
= 0; i
< MANT_WORDS
; i
++) {
83 for (j
= 0; j
< MANT_WORDS
; j
++) {
85 n
= (uint32_t) to
[i
] * (uint32_t) from
[j
];
86 temp
[i
+ j
] += n
>> 16;
87 temp
[i
+ j
+ 1] += n
& 0xFFFF;
91 for (i
= MANT_WORDS
* 2; --i
;) {
92 temp
[i
- 1] += temp
[i
] >> 16;
96 dprintf(("%s=" MANT_FMT
"_" MANT_FMT
"\n", "temp", SOME_ARG(temp
, 0),
97 SOME_ARG(temp
, MANT_WORDS
)));
99 if (temp
[0] & 0x8000) {
100 for (i
= 0; i
< MANT_WORDS
; i
++) {
101 to
[i
] = temp
[i
] & 0xFFFF;
103 dprintf(("%s=" MANT_FMT
" (%i)\n", "prod", SOME_ARG(to
, 0), 0));
106 for (i
= 0; i
< MANT_WORDS
; i
++) {
107 to
[i
] = (temp
[i
] << 1) + !!(temp
[i
+ 1] & 0x8000);
109 dprintf(("%s=" MANT_FMT
" (%i)\n", "prod", SOME_ARG(to
, 0), -1));
115 * ---------------------------------------------------------------------------
116 * read an exponent; returns INT32_MAX on error
117 * ---------------------------------------------------------------------------
119 static int32_t read_exponent(const char *string
, int32_t max
)
124 if (*string
== '+') {
126 } else if (*string
== '-') {
131 if (*string
>= '0' && *string
<= '9') {
132 i
= (i
* 10) + (*string
- '0');
135 * To ensure that underflows and overflows are
136 * handled properly we must avoid wraparounds of
137 * the signed integer value that is used to hold
138 * the exponent. Therefore we cap the exponent at
139 * +/-5000, which is slightly more/less than
140 * what's required for normal and denormal numbers
141 * in single, double, and extended precision, but
142 * sufficient to avoid signed integer wraparound.
146 } else if (*string
== '_') {
150 "invalid character in floating-point constant %s: '%c'",
151 "exponent", *string
);
161 * ---------------------------------------------------------------------------
163 * ---------------------------------------------------------------------------
165 static bool ieee_flconvert(const char *string
, uint16_t * mant
,
168 char digits
[MANT_DIGITS
];
170 uint16_t mult
[MANT_WORDS
], bit
;
172 int32_t tenpwr
, twopwr
;
174 bool started
, seendot
, warned
;
177 started
= seendot
= false;
178 warned
= (pass0
!= 1);
179 while (*string
&& *string
!= 'E' && *string
!= 'e') {
180 if (*string
== '.') {
185 "too many periods in floating-point constant");
188 } else if (*string
>= '0' && *string
<= '9') {
189 if (*string
== '0' && !started
) {
195 if (p
< digits
+ sizeof(digits
)) {
196 *p
++ = *string
- '0';
199 error(ERR_WARNING
|ERR_WARN_FL_TOOLONG
,
200 "floating-point constant significand contains "
201 "more than %i digits", MANT_DIGITS
);
209 } else if (*string
== '_') {
213 "invalid character in floating-point constant %s: '%c'",
214 "significand", *string
);
223 string
++; /* eat the E */
224 e
= read_exponent(string
, 5000);
231 * At this point, the memory interval [digits,p) contains a
232 * series of decimal digits zzzzzzz, such that our number X
233 * satisfies X = 0.zzzzzzz * 10^tenpwr.
238 dprintf(("%c", *q
+ '0'));
241 dprintf((" * 10^%i\n", tenpwr
));
244 * Now convert [digits,p) to our internal representation.
247 for (m
= mant
; m
< mant
+ MANT_WORDS
; m
++) {
254 while (m
< mant
+ MANT_WORDS
) {
256 while (p
> q
&& !p
[-1]) {
262 for (r
= p
; r
-- > q
;) {
291 * At this point, the 'mant' array contains the first frac-
292 * tional places of a base-2^16 real number which when mul-
293 * tiplied by 2^twopwr and 5^tenpwr gives X.
295 dprintf(("X = " MANT_FMT
" * 2^%i * 5^%i\n", MANT_ARG
, twopwr
,
299 * Now multiply 'mant' by 5^tenpwr.
301 if (tenpwr
< 0) { /* mult = 5^-1 = 0.2 */
302 for (m
= mult
; m
< mult
+ MANT_WORDS
- 1; m
++) {
305 mult
[MANT_WORDS
- 1] = 0xCCCD;
310 * If tenpwr was 1000...000b, then it becomes 1000...000b. See
311 * the "ANSI C" comment below for more details on that case.
313 * Because we already truncated tenpwr to +5000...-5000 inside
314 * the exponent parsing code, this shouldn't happen though.
316 } else if (tenpwr
> 0) { /* mult = 5^+1 = 5.0 */
318 for (m
= mult
+ 1; m
< mult
+ MANT_WORDS
; m
++) {
326 dprintf(("loop=" MANT_FMT
" * 2^%i * 5^%i (%i)\n", MANT_ARG
,
327 twopwr
, tenpwr
, extratwos
));
329 dprintf(("mant*mult\n"));
330 twopwr
+= extratwos
+ float_multiply(mant
, mult
);
332 dprintf(("mult*mult\n"));
333 extratwos
= extratwos
* 2 + float_multiply(mult
, mult
);
337 * In ANSI C, the result of right-shifting a signed integer is
338 * considered implementation-specific. To ensure that the loop
339 * terminates even if tenpwr was 1000...000b to begin with, we
340 * manually clear the MSB, in case a 1 was shifted in.
342 * Because we already truncated tenpwr to +5000...-5000 inside
343 * the exponent parsing code, this shouldn't matter; neverthe-
344 * less it is the right thing to do here.
346 tenpwr
&= (uint32_t) - 1 >> 1;
350 * At this point, the 'mant' array contains the first frac-
351 * tional places of a base-2^16 real number in [0.5,1) that
352 * when multiplied by 2^twopwr gives X. Or it contains zero
353 * of course. We are done.
360 * ---------------------------------------------------------------------------
361 * round a mantissa off after i words
362 * ---------------------------------------------------------------------------
365 #define ROUND_COLLECT_BITS \
366 for (j = i; j < MANT_WORDS; j++) { \
370 #define ROUND_ABS_DOWN \
371 for (j = i; j < MANT_WORDS; j++) { \
375 #define ROUND_ABS_UP \
379 } while (i > 0 && !mant[i]); \
380 return (!i && !mant[i]);
382 static bool ieee_round(int sign
, uint16_t * mant
, int32_t i
)
386 if ((sign
== 0x0000) || (sign
== 0x8000)) {
387 if (rc
== FLOAT_RC_NEAR
) {
388 if (mant
[i
] & 0x8000) {
395 if (mant
[i
- 1] & 1) {
404 } else if (((sign
== 0x0000) && (rc
== FLOAT_RC_DOWN
))
405 || ((sign
== 0x8000) && (rc
== FLOAT_RC_UP
))) {
410 } else if (((sign
== 0x0000) && (rc
== FLOAT_RC_UP
))
411 || ((sign
== 0x8000) && (rc
== FLOAT_RC_DOWN
))) {
416 } else if (rc
== FLOAT_RC_ZERO
) {
419 error(ERR_PANIC
, "float_round() can't handle rc=%i", rc
);
422 error(ERR_PANIC
, "float_round() can't handle sign=%i", sign
);
427 /* Returns a value >= 16 if not a valid hex digit */
428 static unsigned int hexval(char c
)
430 unsigned int v
= (unsigned char) c
;
432 if (v
>= '0' && v
<= '9')
435 return (v
|0x20) - 'a' + 10;
438 /* Handle floating-point numbers with radix 2^bits and binary exponent */
439 static bool ieee_flconvert_bin(const char *string
, int bits
,
440 uint16_t * mant
, int32_t * exponent
)
442 static const int log2tbl
[16] =
443 { -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3 };
444 uint16_t mult
[MANT_WORDS
+ 1], *mp
;
447 bool seendot
, seendigit
;
449 int radix
= 1 << bits
;
453 seendot
= seendigit
= false;
457 memset(mult
, 0, sizeof mult
);
459 while ((c
= *string
++) != '\0') {
465 "too many periods in floating-point constant");
468 } else if ((v
= hexval(c
)) < (unsigned int)radix
) {
469 if (!seendigit
&& v
) {
476 twopwr
= seendot
? twopwr
-bits
+l
: l
+1-bits
;
483 if (mp
> &mult
[MANT_WORDS
])
484 mp
= &mult
[MANT_WORDS
]; /* Guard slot */
496 } else if (c
== 'p' || c
== 'P') {
498 e
= read_exponent(string
, 20000);
503 } else if (c
== '_') {
507 "floating-point constant: `%c' is invalid character", c
);
513 memset(mant
, 0, 2 * MANT_WORDS
); /* Zero */
516 memcpy(mant
, mult
, 2 * MANT_WORDS
);
524 * Shift a mantissa to the right by i bits.
526 static void ieee_shr(uint16_t * mant
, int i
)
532 sr
= i
%16; sl
= 16-sr
;
537 for (j
= MANT_WORDS
-1; j
>= offs
; j
--)
538 mant
[j
] = mant
[j
-offs
];
540 n
= mant
[MANT_WORDS
-1-offs
] >> sr
;
541 for (j
= MANT_WORDS
-1; j
> offs
; j
--) {
543 mant
[j
] = (m
<< sl
) | n
;
552 #if defined(__i386__) || defined(__x86_64__)
553 #define put(a,b) (*(uint16_t *)(a) = (b))
555 #define put(a,b) (((a)[0] = (b)), ((a)[1] = (b) >> 8))
558 /* Set a bit, using *bigendian* bit numbering (0 = MSB) */
559 static void set_bit(uint16_t *mant
, int bit
)
561 mant
[bit
>> 4] |= 1 << (~bit
& 15);
564 /* Test a single bit */
565 static int test_bit(const uint16_t *mant
, int bit
)
567 return (mant
[bit
>> 4] >> (~bit
& 15)) & 1;
570 /* Report if the mantissa value is all zero */
571 static bool is_zero(const uint16_t *mant
)
575 for (i
= 0; i
< MANT_WORDS
; i
++)
582 /* Produce standard IEEE formats, with implicit or explicit integer
583 bit; this makes the following assumptions:
585 - the sign bit is the MSB, followed by the exponent,
586 followed by the integer bit if present.
587 - the sign bit plus exponent fit in 16 bits.
588 - the exponent bias is 2^(n-1)-1 for an n-bit exponent */
592 int mantissa
; /* Fractional bits in the mantissa */
593 int explicit; /* Explicit integer */
594 int exponent
; /* Bits in the exponent */
598 * The 16- and 128-bit formats are expected to be in IEEE 754r.
599 * AMD SSE5 uses the 16-bit format.
601 * The 32- and 64-bit formats are the original IEEE 754 formats.
603 * The 80-bit format is x87-specific, but widely used.
605 static const struct ieee_format ieee_16
= { 1, 10, 0, 5 };
606 static const struct ieee_format ieee_32
= { 2, 23, 0, 8 };
607 static const struct ieee_format ieee_64
= { 4, 52, 0, 11 };
608 static const struct ieee_format ieee_80
= { 5, 63, 1, 15 };
609 static const struct ieee_format ieee_128
= { 8, 112, 0, 15 };
611 /* Types of values we can generate */
621 static int to_float(const char *str
, int sign
, uint8_t * result
,
622 const struct ieee_format
*fmt
)
624 uint16_t mant
[MANT_WORDS
], *mp
;
625 int32_t exponent
= 0;
626 int32_t expmax
= 1 << (fmt
->exponent
- 1);
627 uint16_t one_mask
= 0x8000 >> ((fmt
->exponent
+fmt
->explicit) % 16);
628 int one_pos
= (fmt
->exponent
+fmt
->explicit)/16;
634 sign
= (sign
< 0 ? 0x8000 : 0);
640 case 'n': /* __nan__ */
642 case 'q': /* __qnan__ */
646 case 's': /* __snan__ */
650 case 'i': /* __infinity__ */
656 "internal error: unknown FP constant token `%s'\n", str
);
665 ok
= ieee_flconvert_bin(str
+2, 4, mant
, &exponent
);
669 ok
= ieee_flconvert_bin(str
+2, 3, mant
, &exponent
);
673 ok
= ieee_flconvert_bin(str
+2, 1, mant
, &exponent
);
677 ok
= ieee_flconvert(str
+2, mant
, &exponent
);
679 case '0': case '1': case '2': case '3': case '4':
680 case '5': case '6': case '7': case '8': case '9':
682 /* Leading zero was just a zero */
683 ok
= ieee_flconvert(str
, mant
, &exponent
);
687 "floating-point constant: invalid radix `%c'", str
[1]);
691 } else if (str
[0] == '$') {
692 ok
= ieee_flconvert_bin(str
+1, 4, mant
, &exponent
);
694 ok
= ieee_flconvert(str
, mant
, &exponent
);
699 } else if (mant
[0] & 0x8000) {
704 if (exponent
>= 2 - expmax
&& exponent
<= expmax
) {
706 } else if (exponent
< 2 - expmax
&&
707 exponent
>= 2 - expmax
- fmt
->mantissa
) {
709 } else if (exponent
> 0) {
711 error(ERR_WARNING
|ERR_WARN_FL_OVERFLOW
,
712 "overflow in floating-point constant");
717 error(ERR_WARNING
|ERR_WARN_FL_UNDERFLOW
,
718 "underflow in floating-point constant");
730 memset(mant
, 0, sizeof mant
);
735 shift
= -(exponent
+ expmax
- 2 - fmt
->exponent
)
737 ieee_shr(mant
, shift
);
738 ieee_round(sign
, mant
, fmt
->words
);
739 if (mant
[one_pos
] & one_mask
) {
740 /* One's position is set, we rounded up into normal range */
743 mant
[one_pos
] &= ~one_mask
; /* remove explicit one */
744 mant
[0] |= exponent
<< (15 - fmt
->exponent
);
746 if (daz
|| is_zero(mant
)) {
747 /* Flush denormals to zero */
749 error(ERR_WARNING
|ERR_WARN_FL_UNDERFLOW
,
750 "underflow in floating-point constant");
754 error(ERR_WARNING
|ERR_WARN_FL_DENORM
,
755 "denormal floating-point constant");
762 exponent
+= expmax
- 1;
763 ieee_shr(mant
, fmt
->exponent
+fmt
->explicit);
764 ieee_round(sign
, mant
, fmt
->words
);
765 /* did we scale up by one? */
766 if (test_bit(mant
, fmt
->exponent
+fmt
->explicit-1)) {
769 if (exponent
>= (expmax
<< 1)-1) {
771 error(ERR_WARNING
|ERR_WARN_FL_OVERFLOW
,
772 "overflow in floating-point constant");
779 mant
[one_pos
] &= ~one_mask
; /* remove explicit one */
780 mant
[0] |= exponent
<< (15 - fmt
->exponent
);
787 memset(mant
, 0, sizeof mant
);
788 mant
[0] = ((1 << fmt
->exponent
)-1) << (15 - fmt
->exponent
);
790 mant
[one_pos
] |= one_mask
;
792 set_bit(mant
, fmt
->exponent
+fmt
->explicit+1);
793 else if (type
== FL_SNAN
)
794 set_bit(mant
, fmt
->exponent
+fmt
->explicit+fmt
->mantissa
);
800 for (mp
= &mant
[fmt
->words
], i
= 0; i
< fmt
->words
; i
++) {
806 return 1; /* success */
809 int float_const(const char *number
, int32_t sign
, uint8_t * result
,
810 int bytes
, efunc err
)
816 return to_float(number
, sign
, result
, &ieee_16
);
818 return to_float(number
, sign
, result
, &ieee_32
);
820 return to_float(number
, sign
, result
, &ieee_64
);
822 return to_float(number
, sign
, result
, &ieee_80
);
824 return to_float(number
, sign
, result
, &ieee_128
);
826 error(ERR_PANIC
, "strange value %d passed to float_const", bytes
);
831 /* Set floating-point options */
832 int float_option(const char *option
)
834 if (!nasm_stricmp(option
, "daz")) {
837 } else if (!nasm_stricmp(option
, "nodaz")) {
840 } else if (!nasm_stricmp(option
, "near")) {
843 } else if (!nasm_stricmp(option
, "down")) {
846 } else if (!nasm_stricmp(option
, "up")) {
849 } else if (!nasm_stricmp(option
, "zero")) {
852 } else if (!nasm_stricmp(option
, "default")) {
857 return -1; /* Unknown option */