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 static int hexval(char c
)
429 if (c
>= '0' && c
<= '9')
431 else if (c
>= 'a' && c
<= 'f')
437 static bool ieee_flconvert_hex(const char *string
, uint16_t * mant
,
440 static const int log2tbl
[16] =
441 { -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3 };
442 uint16_t mult
[MANT_WORDS
+ 1], *mp
;
445 int seendot
, seendigit
;
449 seendot
= seendigit
= 0;
453 memset(mult
, 0, sizeof mult
);
455 while ((c
= *string
++) != '\0') {
461 "too many periods in floating-point constant");
464 } else if (isxdigit(c
)) {
467 if (!seendigit
&& v
) {
474 twopwr
= seendot
? twopwr
- 4 + l
: l
- 3;
481 if (mp
> &mult
[MANT_WORDS
])
482 mp
= &mult
[MANT_WORDS
]; /* Guard slot */
494 } else if (c
== 'p' || c
== 'P') {
496 e
= read_exponent(string
, 20000);
501 } else if (c
== '_') {
505 "floating-point constant: `%c' is invalid character", c
);
511 memset(mant
, 0, 2 * MANT_WORDS
); /* Zero */
514 memcpy(mant
, mult
, 2 * MANT_WORDS
);
522 * Shift a mantissa to the right by i bits.
524 static void ieee_shr(uint16_t * mant
, int i
)
530 sr
= i
%16; sl
= 16-sr
;
535 for (j
= MANT_WORDS
-1; j
>= offs
; j
--)
536 mant
[j
] = mant
[j
-offs
];
538 n
= mant
[MANT_WORDS
-1-offs
] >> sr
;
539 for (j
= MANT_WORDS
-1; j
> offs
; j
--) {
541 mant
[j
] = (m
<< sl
) | n
;
550 #if defined(__i386__) || defined(__x86_64__)
551 #define put(a,b) (*(uint16_t *)(a) = (b))
553 #define put(a,b) (((a)[0] = (b)), ((a)[1] = (b) >> 8))
556 /* Set a bit, using *bigendian* bit numbering (0 = MSB) */
557 static void set_bit(uint16_t *mant
, int bit
)
559 mant
[bit
>> 4] |= 1 << (~bit
& 15);
562 /* Test a single bit */
563 static int test_bit(const uint16_t *mant
, int bit
)
565 return (mant
[bit
>> 4] >> (~bit
& 15)) & 1;
568 /* Report if the mantissa value is all zero */
569 static bool is_zero(const uint16_t *mant
)
573 for (i
= 0; i
< MANT_WORDS
; i
++)
580 /* Produce standard IEEE formats, with implicit or explicit integer
581 bit; this makes the following assumptions:
583 - the sign bit is the MSB, followed by the exponent,
584 followed by the integer bit if present.
585 - the sign bit plus exponent fit in 16 bits.
586 - the exponent bias is 2^(n-1)-1 for an n-bit exponent */
590 int mantissa
; /* Fractional bits in the mantissa */
591 int explicit; /* Explicit integer */
592 int exponent
; /* Bits in the exponent */
596 * The 16- and 128-bit formats are expected to be in IEEE 754r.
597 * AMD SSE5 uses the 16-bit format.
599 * The 32- and 64-bit formats are the original IEEE 754 formats.
601 * The 80-bit format is x87-specific, but widely used.
603 static const struct ieee_format ieee_16
= { 1, 10, 0, 5 };
604 static const struct ieee_format ieee_32
= { 2, 23, 0, 8 };
605 static const struct ieee_format ieee_64
= { 4, 52, 0, 11 };
606 static const struct ieee_format ieee_80
= { 5, 63, 1, 15 };
607 static const struct ieee_format ieee_128
= { 8, 112, 0, 15 };
609 /* Types of values we can generate */
619 static int to_float(const char *str
, int sign
, uint8_t * result
,
620 const struct ieee_format
*fmt
)
622 uint16_t mant
[MANT_WORDS
], *mp
;
623 int32_t exponent
= 0;
624 int32_t expmax
= 1 << (fmt
->exponent
- 1);
625 uint16_t one_mask
= 0x8000 >> ((fmt
->exponent
+fmt
->explicit) % 16);
626 int one_pos
= (fmt
->exponent
+fmt
->explicit)/16;
632 sign
= (sign
< 0 ? 0x8000 : 0);
638 case 'n': /* __nan__ */
640 case 'q': /* __qnan__ */
644 case 's': /* __snan__ */
648 case 'i': /* __infinity__ */
654 "internal error: unknown FP constant token `%s'\n", str
);
659 if (str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
660 ok
= ieee_flconvert_hex(str
+ 2, mant
, &exponent
);
661 else if (str
[0] == '$')
662 ok
= ieee_flconvert_hex(str
+ 1, mant
, &exponent
);
664 ok
= ieee_flconvert(str
, mant
, &exponent
);
668 } else if (mant
[0] & 0x8000) {
673 if (exponent
>= 2 - expmax
&& exponent
<= expmax
) {
675 } else if (exponent
< 2 - expmax
&&
676 exponent
>= 2 - expmax
- fmt
->mantissa
) {
678 } else if (exponent
> 0) {
680 error(ERR_WARNING
|ERR_WARN_FL_OVERFLOW
,
681 "overflow in floating-point constant");
686 error(ERR_WARNING
|ERR_WARN_FL_UNDERFLOW
,
687 "underflow in floating-point constant");
699 memset(mant
, 0, sizeof mant
);
704 shift
= -(exponent
+ expmax
- 2 - fmt
->exponent
)
706 ieee_shr(mant
, shift
);
707 ieee_round(sign
, mant
, fmt
->words
);
708 if (mant
[one_pos
] & one_mask
) {
709 /* One's position is set, we rounded up into normal range */
712 mant
[one_pos
] &= ~one_mask
; /* remove explicit one */
713 mant
[0] |= exponent
<< (15 - fmt
->exponent
);
715 if (daz
|| is_zero(mant
)) {
716 /* Flush denormals to zero */
718 error(ERR_WARNING
|ERR_WARN_FL_UNDERFLOW
,
719 "underflow in floating-point constant");
723 error(ERR_WARNING
|ERR_WARN_FL_DENORM
,
724 "denormal floating-point constant");
731 exponent
+= expmax
- 1;
732 ieee_shr(mant
, fmt
->exponent
+fmt
->explicit);
733 ieee_round(sign
, mant
, fmt
->words
);
734 /* did we scale up by one? */
735 if (test_bit(mant
, fmt
->exponent
+fmt
->explicit-1)) {
738 if (exponent
>= (expmax
<< 1)-1) {
740 error(ERR_WARNING
|ERR_WARN_FL_OVERFLOW
,
741 "overflow in floating-point constant");
748 mant
[one_pos
] &= ~one_mask
; /* remove explicit one */
749 mant
[0] |= exponent
<< (15 - fmt
->exponent
);
756 memset(mant
, 0, sizeof mant
);
757 mant
[0] = ((1 << fmt
->exponent
)-1) << (15 - fmt
->exponent
);
759 mant
[one_pos
] |= one_mask
;
761 set_bit(mant
, fmt
->exponent
+fmt
->explicit+1);
762 else if (type
== FL_SNAN
)
763 set_bit(mant
, fmt
->exponent
+fmt
->explicit+fmt
->mantissa
);
769 for (mp
= &mant
[fmt
->words
], i
= 0; i
< fmt
->words
; i
++) {
775 return 1; /* success */
778 int float_const(const char *number
, int32_t sign
, uint8_t * result
,
779 int bytes
, efunc err
)
785 return to_float(number
, sign
, result
, &ieee_16
);
787 return to_float(number
, sign
, result
, &ieee_32
);
789 return to_float(number
, sign
, result
, &ieee_64
);
791 return to_float(number
, sign
, result
, &ieee_80
);
793 return to_float(number
, sign
, result
, &ieee_128
);
795 error(ERR_PANIC
, "strange value %d passed to float_const", bytes
);
800 /* Set floating-point options */
801 int float_option(const char *option
)
803 if (!nasm_stricmp(option
, "daz")) {
806 } else if (!nasm_stricmp(option
, "nodaz")) {
809 } else if (!nasm_stricmp(option
, "near")) {
812 } else if (!nasm_stricmp(option
, "down")) {
815 } else if (!nasm_stricmp(option
, "up")) {
818 } else if (!nasm_stricmp(option
, "zero")) {
821 } else if (!nasm_stricmp(option
, "default")) {
826 return -1; /* Unknown option */