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 * ---------------------------------------------------------------------------
117 * ---------------------------------------------------------------------------
119 static bool ieee_flconvert(const char *string
, uint16_t * mant
,
122 char digits
[MANT_DIGITS
];
124 uint16_t mult
[MANT_WORDS
], bit
;
126 int32_t tenpwr
, twopwr
;
128 bool started
, seendot
, warned
;
131 started
= seendot
= false;
132 warned
= (pass0
!= 1);
133 while (*string
&& *string
!= 'E' && *string
!= 'e') {
134 if (*string
== '.') {
139 "too many periods in floating-point constant");
142 } else if (*string
>= '0' && *string
<= '9') {
143 if (*string
== '0' && !started
) {
149 if (p
< digits
+ sizeof(digits
)) {
150 *p
++ = *string
- '0';
153 error(ERR_WARNING
|ERR_WARN_FL_TOOLONG
,
154 "floating-point constant significand contains "
155 "more than %i digits", MANT_DIGITS
);
163 } else if (*string
== '_') {
168 "invalid character in floating-point constant %s: '%c'",
169 "significand", *string
);
177 string
++; /* eat the E */
178 if (*string
== '+') {
180 } else if (*string
== '-') {
185 if (*string
>= '0' && *string
<= '9') {
186 i
= (i
* 10) + (*string
- '0');
189 * To ensure that underflows and overflows are
190 * handled properly we must avoid wraparounds of
191 * the signed integer value that is used to hold
192 * the exponent. Therefore we cap the exponent at
193 * +/-5000, which is slightly more/less than
194 * what's required for normal and denormal numbers
195 * in single, double, and extended precision, but
196 * sufficient to avoid signed integer wraparound.
201 } else if (*string
== '_') {
206 "invalid character in floating-point constant %s: '%c'",
207 "exponent", *string
);
219 * At this point, the memory interval [digits,p) contains a
220 * series of decimal digits zzzzzzz, such that our number X
221 * satisfies X = 0.zzzzzzz * 10^tenpwr.
226 dprintf(("%c", *q
+ '0'));
229 dprintf((" * 10^%i\n", tenpwr
));
232 * Now convert [digits,p) to our internal representation.
235 for (m
= mant
; m
< mant
+ MANT_WORDS
; m
++) {
242 while (m
< mant
+ MANT_WORDS
) {
244 while (p
> q
&& !p
[-1]) {
250 for (r
= p
; r
-- > q
;) {
279 * At this point, the 'mant' array contains the first frac-
280 * tional places of a base-2^16 real number which when mul-
281 * tiplied by 2^twopwr and 5^tenpwr gives X.
283 dprintf(("X = " MANT_FMT
" * 2^%i * 5^%i\n", MANT_ARG
, twopwr
,
287 * Now multiply 'mant' by 5^tenpwr.
289 if (tenpwr
< 0) { /* mult = 5^-1 = 0.2 */
290 for (m
= mult
; m
< mult
+ MANT_WORDS
- 1; m
++) {
293 mult
[MANT_WORDS
- 1] = 0xCCCD;
298 * If tenpwr was 1000...000b, then it becomes 1000...000b. See
299 * the "ANSI C" comment below for more details on that case.
301 * Because we already truncated tenpwr to +5000...-5000 inside
302 * the exponent parsing code, this shouldn't happen though.
304 } else if (tenpwr
> 0) { /* mult = 5^+1 = 5.0 */
306 for (m
= mult
+ 1; m
< mult
+ MANT_WORDS
; m
++) {
314 dprintf(("loop=" MANT_FMT
" * 2^%i * 5^%i (%i)\n", MANT_ARG
,
315 twopwr
, tenpwr
, extratwos
));
317 dprintf(("mant*mult\n"));
318 twopwr
+= extratwos
+ float_multiply(mant
, mult
);
320 dprintf(("mult*mult\n"));
321 extratwos
= extratwos
* 2 + float_multiply(mult
, mult
);
325 * In ANSI C, the result of right-shifting a signed integer is
326 * considered implementation-specific. To ensure that the loop
327 * terminates even if tenpwr was 1000...000b to begin with, we
328 * manually clear the MSB, in case a 1 was shifted in.
330 * Because we already truncated tenpwr to +5000...-5000 inside
331 * the exponent parsing code, this shouldn't matter; neverthe-
332 * less it is the right thing to do here.
334 tenpwr
&= (uint32_t) - 1 >> 1;
338 * At this point, the 'mant' array contains the first frac-
339 * tional places of a base-2^16 real number in [0.5,1) that
340 * when multiplied by 2^twopwr gives X. Or it contains zero
341 * of course. We are done.
348 * ---------------------------------------------------------------------------
349 * round a mantissa off after i words
350 * ---------------------------------------------------------------------------
353 #define ROUND_COLLECT_BITS \
354 for (j = i; j < MANT_WORDS; j++) { \
358 #define ROUND_ABS_DOWN \
359 for (j = i; j < MANT_WORDS; j++) { \
363 #define ROUND_ABS_UP \
367 } while (i > 0 && !mant[i]); \
368 return (!i && !mant[i]);
370 static bool ieee_round(int sign
, uint16_t * mant
, int32_t i
)
374 if ((sign
== 0x0000) || (sign
== 0x8000)) {
375 if (rc
== FLOAT_RC_NEAR
) {
376 if (mant
[i
] & 0x8000) {
383 if (mant
[i
- 1] & 1) {
392 } else if (((sign
== 0x0000) && (rc
== FLOAT_RC_DOWN
))
393 || ((sign
== 0x8000) && (rc
== FLOAT_RC_UP
))) {
398 } else if (((sign
== 0x0000) && (rc
== FLOAT_RC_UP
))
399 || ((sign
== 0x8000) && (rc
== FLOAT_RC_DOWN
))) {
404 } else if (rc
== FLOAT_RC_ZERO
) {
407 error(ERR_PANIC
, "float_round() can't handle rc=%i", rc
);
410 error(ERR_PANIC
, "float_round() can't handle sign=%i", sign
);
415 static int hexval(char c
)
417 if (c
>= '0' && c
<= '9')
419 else if (c
>= 'a' && c
<= 'f')
425 static bool ieee_flconvert_hex(const char *string
, uint16_t * mant
,
428 static const int log2tbl
[16] =
429 { -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3 };
430 uint16_t mult
[MANT_WORDS
+ 1], *mp
;
433 int seendot
, seendigit
;
437 seendot
= seendigit
= 0;
441 memset(mult
, 0, sizeof mult
);
443 while ((c
= *string
++) != '\0') {
449 "too many periods in floating-point constant");
452 } else if (isxdigit(c
)) {
455 if (!seendigit
&& v
) {
462 twopwr
= seendot
? twopwr
- 4 + l
: l
- 3;
469 if (mp
> &mult
[MANT_WORDS
])
470 mp
= &mult
[MANT_WORDS
]; /* Guard slot */
482 } else if (c
== 'p' || c
== 'P') {
483 twopwr
+= atoi(string
);
487 "floating-point constant: `%c' is invalid character", c
);
493 memset(mant
, 0, 2 * MANT_WORDS
); /* Zero */
496 memcpy(mant
, mult
, 2 * MANT_WORDS
);
504 * Shift a mantissa to the right by i bits.
506 static void ieee_shr(uint16_t * mant
, int i
)
512 sr
= i
%16; sl
= 16-sr
;
517 for (j
= MANT_WORDS
-1; j
>= offs
; j
--)
518 mant
[j
] = mant
[j
-offs
];
520 n
= mant
[MANT_WORDS
-1-offs
] >> sr
;
521 for (j
= MANT_WORDS
-1; j
> offs
; j
--) {
523 mant
[j
] = (m
<< sl
) | n
;
532 #if defined(__i386__) || defined(__x86_64__)
533 #define put(a,b) (*(uint16_t *)(a) = (b))
535 #define put(a,b) (((a)[0] = (b)), ((a)[1] = (b) >> 8))
538 /* Set a bit, using *bigendian* bit numbering (0 = MSB) */
539 static void set_bit(uint16_t *mant
, int bit
)
541 mant
[bit
>> 4] |= 1 << (~bit
& 15);
544 /* Test a single bit */
545 static int test_bit(const uint16_t *mant
, int bit
)
547 return (mant
[bit
>> 4] >> (~bit
& 15)) & 1;
550 /* Report if the mantissa value is all zero */
551 static bool is_zero(const uint16_t *mant
)
555 for (i
= 0; i
< MANT_WORDS
; i
++)
562 /* Produce standard IEEE formats, with implicit or explicit integer
563 bit; this makes the following assumptions:
565 - the sign bit is the MSB, followed by the exponent,
566 followed by the integer bit if present.
567 - the sign bit plus exponent fit in 16 bits.
568 - the exponent bias is 2^(n-1)-1 for an n-bit exponent */
572 int mantissa
; /* Fractional bits in the mantissa */
573 int explicit; /* Explicit integer */
574 int exponent
; /* Bits in the exponent */
578 * The 16- and 128-bit formats are expected to be in IEEE 754r.
579 * AMD SSE5 uses the 16-bit format.
581 * The 32- and 64-bit formats are the original IEEE 754 formats.
583 * The 80-bit format is x87-specific, but widely used.
585 static const struct ieee_format ieee_16
= { 1, 10, 0, 5 };
586 static const struct ieee_format ieee_32
= { 2, 23, 0, 8 };
587 static const struct ieee_format ieee_64
= { 4, 52, 0, 11 };
588 static const struct ieee_format ieee_80
= { 5, 63, 1, 15 };
589 static const struct ieee_format ieee_128
= { 8, 112, 0, 15 };
591 /* Types of values we can generate */
601 static int to_float(const char *str
, int sign
, uint8_t * result
,
602 const struct ieee_format
*fmt
)
604 uint16_t mant
[MANT_WORDS
], *mp
;
605 int32_t exponent
= 0;
606 int32_t expmax
= 1 << (fmt
->exponent
- 1);
607 uint16_t one_mask
= 0x8000 >> ((fmt
->exponent
+fmt
->explicit) % 16);
608 int one_pos
= (fmt
->exponent
+fmt
->explicit)/16;
614 sign
= (sign
< 0 ? 0x8000 : 0);
620 case 'n': /* __nan__ */
622 case 'q': /* __qnan__ */
626 case 's': /* __snan__ */
630 case 'i': /* __infinity__ */
636 "internal error: unknown FP constant token `%s'\n", str
);
641 if (str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
642 ok
= ieee_flconvert_hex(str
+ 2, mant
, &exponent
);
644 ok
= ieee_flconvert(str
, mant
, &exponent
);
648 } else if (mant
[0] & 0x8000) {
653 if (exponent
>= 2 - expmax
&& exponent
<= expmax
) {
655 } else if (exponent
< 2 - expmax
&&
656 exponent
>= 2 - expmax
- fmt
->mantissa
) {
658 } else if (exponent
> 0) {
660 error(ERR_WARNING
|ERR_WARN_FL_OVERFLOW
,
661 "overflow in floating-point constant");
666 error(ERR_WARNING
|ERR_WARN_FL_UNDERFLOW
,
667 "underflow in floating-point constant");
679 memset(mant
, 0, sizeof mant
);
684 shift
= -(exponent
+ expmax
- 2 - fmt
->exponent
)
686 ieee_shr(mant
, shift
);
687 ieee_round(sign
, mant
, fmt
->words
);
688 if (mant
[one_pos
] & one_mask
) {
689 /* One's position is set, we rounded up into normal range */
692 mant
[one_pos
] &= ~one_mask
; /* remove explicit one */
693 mant
[0] |= exponent
<< (15 - fmt
->exponent
);
695 if (daz
|| is_zero(mant
)) {
696 /* Flush denormals to zero */
698 error(ERR_WARNING
|ERR_WARN_FL_UNDERFLOW
,
699 "underflow in floating-point constant");
703 error(ERR_WARNING
|ERR_WARN_FL_DENORM
,
704 "denormal floating-point constant");
711 exponent
+= expmax
- 1;
712 ieee_shr(mant
, fmt
->exponent
+fmt
->explicit);
713 ieee_round(sign
, mant
, fmt
->words
);
714 /* did we scale up by one? */
715 if (test_bit(mant
, fmt
->exponent
+fmt
->explicit-1)) {
718 if (exponent
>= (expmax
<< 1)-1) {
720 error(ERR_WARNING
|ERR_WARN_FL_OVERFLOW
,
721 "overflow in floating-point constant");
728 mant
[one_pos
] &= ~one_mask
; /* remove explicit one */
729 mant
[0] |= exponent
<< (15 - fmt
->exponent
);
736 memset(mant
, 0, sizeof mant
);
737 mant
[0] = ((1 << fmt
->exponent
)-1) << (15 - fmt
->exponent
);
739 mant
[one_pos
] |= one_mask
;
741 set_bit(mant
, fmt
->exponent
+fmt
->explicit+1);
742 else if (type
== FL_SNAN
)
743 set_bit(mant
, fmt
->exponent
+fmt
->explicit+fmt
->mantissa
);
749 for (mp
= &mant
[fmt
->words
], i
= 0; i
< fmt
->words
; i
++) {
755 return 1; /* success */
758 int float_const(const char *number
, int32_t sign
, uint8_t * result
,
759 int bytes
, efunc err
)
765 return to_float(number
, sign
, result
, &ieee_16
);
767 return to_float(number
, sign
, result
, &ieee_32
);
769 return to_float(number
, sign
, result
, &ieee_64
);
771 return to_float(number
, sign
, result
, &ieee_80
);
773 return to_float(number
, sign
, result
, &ieee_128
);
775 error(ERR_PANIC
, "strange value %d passed to float_const", bytes
);
780 /* Set floating-point options */
781 int float_option(const char *option
)
783 if (!nasm_stricmp(option
, "daz")) {
786 } else if (!nasm_stricmp(option
, "nodaz")) {
789 } else if (!nasm_stricmp(option
, "near")) {
792 } else if (!nasm_stricmp(option
, "down")) {
795 } else if (!nasm_stricmp(option
, "up")) {
798 } else if (!nasm_stricmp(option
, "zero")) {
801 } else if (!nasm_stricmp(option
, "default")) {
806 return -1; /* Unknown option */