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 /* "A limb is like a digit but bigger */
38 typedef uint32_t fp_limb
;
39 typedef uint64_t fp_2limb
;
42 #define LIMB_BYTES (LIMB_BITS/8)
43 #define LIMB_TOP_BIT ((fp_limb)1 << (LIMB_BITS-1))
44 #define LIMB_MASK ((fp_limb)(~0))
45 #define LIMB_ALL_BYTES ((fp_limb)0x01010101)
46 #define LIMB_BYTE(x) ((x)*LIMB_ALL_BYTES)
48 #if defined(__i386__) || defined(__x86_64__)
49 #define put(a,b) (*(uint32_t *)(a) = (b))
51 #define put(a,b) (((a)[0] = (b)), \
52 ((a)[1] = (b) >> 8), \
53 ((a)[2] = (b) >> 16), \
57 /* 112 bits + 64 bits for accuracy + 16 bits for rounding */
60 /* 52 digits fit in 176 bits because 10^53 > 2^176 > 10^52 */
61 #define MANT_DIGITS 52
63 /* the format and the argument list depend on MANT_LIMBS */
64 #define MANT_FMT "%08x_%08x_%08x_%08x_%08x_%08x"
65 #define MANT_ARG SOME_ARG(mant, 0)
67 #define SOME_ARG(a,i) (a)[(i)+0], (a)[(i)+1], (a)[(i)+2], (a)[(i)+3], \
68 (a)[(i)+4], (a)[(i)+5]
71 * ---------------------------------------------------------------------------
72 * emit a printf()-like debug message... but only if DEBUG_FLOAT was defined
73 * ---------------------------------------------------------------------------
77 #define dprintf(x) printf x
79 #define dprintf(x) do { } while (0)
83 * ---------------------------------------------------------------------------
85 * ---------------------------------------------------------------------------
87 static int float_multiply(fp_limb
*to
, fp_limb
*from
)
89 fp_2limb temp
[MANT_LIMBS
* 2];
93 * guaranteed that top bit of 'from' is set -- so we only have
94 * to worry about _one_ bit shift to the left
96 dprintf(("%s=" MANT_FMT
"\n", "mul1", SOME_ARG(to
, 0)));
97 dprintf(("%s=" MANT_FMT
"\n", "mul2", SOME_ARG(from
, 0)));
99 memset(temp
, 0, sizeof temp
);
101 for (i
= 0; i
< MANT_LIMBS
; i
++) {
102 for (j
= 0; j
< MANT_LIMBS
; j
++) {
104 n
= (fp_2limb
) to
[i
] * (fp_2limb
) from
[j
];
105 temp
[i
+ j
] += n
>> LIMB_BITS
;
106 temp
[i
+ j
+ 1] += (fp_limb
)n
;
110 for (i
= MANT_LIMBS
* 2; --i
;) {
111 temp
[i
- 1] += temp
[i
] >> LIMB_BITS
;
112 temp
[i
] &= LIMB_MASK
;
115 dprintf(("%s=" MANT_FMT
"_" MANT_FMT
"\n", "temp", SOME_ARG(temp
, 0),
116 SOME_ARG(temp
, MANT_LIMBS
)));
118 if (temp
[0] & LIMB_TOP_BIT
) {
119 for (i
= 0; i
< MANT_LIMBS
; i
++) {
120 to
[i
] = temp
[i
] & LIMB_MASK
;
122 dprintf(("%s=" MANT_FMT
" (%i)\n", "prod", SOME_ARG(to
, 0), 0));
125 for (i
= 0; i
< MANT_LIMBS
; i
++) {
126 to
[i
] = (temp
[i
] << 1) + !!(temp
[i
+ 1] & LIMB_TOP_BIT
);
128 dprintf(("%s=" MANT_FMT
" (%i)\n", "prod", SOME_ARG(to
, 0), -1));
134 * ---------------------------------------------------------------------------
135 * read an exponent; returns INT32_MAX on error
136 * ---------------------------------------------------------------------------
138 static int32_t read_exponent(const char *string
, int32_t max
)
143 if (*string
== '+') {
145 } else if (*string
== '-') {
150 if (*string
>= '0' && *string
<= '9') {
151 i
= (i
* 10) + (*string
- '0');
154 * To ensure that underflows and overflows are
155 * handled properly we must avoid wraparounds of
156 * the signed integer value that is used to hold
157 * the exponent. Therefore we cap the exponent at
158 * +/-5000, which is slightly more/less than
159 * what's required for normal and denormal numbers
160 * in single, double, and extended precision, but
161 * sufficient to avoid signed integer wraparound.
165 } else if (*string
== '_') {
169 "invalid character in floating-point constant %s: '%c'",
170 "exponent", *string
);
180 * ---------------------------------------------------------------------------
182 * ---------------------------------------------------------------------------
184 static bool ieee_flconvert(const char *string
, fp_limb
*mant
,
187 char digits
[MANT_DIGITS
];
189 fp_limb mult
[MANT_LIMBS
], bit
;
191 int32_t tenpwr
, twopwr
;
193 bool started
, seendot
, warned
;
196 started
= seendot
= false;
197 warned
= (pass0
!= 1);
198 while (*string
&& *string
!= 'E' && *string
!= 'e') {
199 if (*string
== '.') {
204 "too many periods in floating-point constant");
207 } else if (*string
>= '0' && *string
<= '9') {
208 if (*string
== '0' && !started
) {
214 if (p
< digits
+ sizeof(digits
)) {
215 *p
++ = *string
- '0';
218 error(ERR_WARNING
|ERR_WARN_FL_TOOLONG
,
219 "floating-point constant significand contains "
220 "more than %i digits", MANT_DIGITS
);
228 } else if (*string
== '_') {
232 "invalid character in floating-point constant %s: '%c'",
233 "significand", *string
);
242 string
++; /* eat the E */
243 e
= read_exponent(string
, 5000);
250 * At this point, the memory interval [digits,p) contains a
251 * series of decimal digits zzzzzzz, such that our number X
252 * satisfies X = 0.zzzzzzz * 10^tenpwr.
257 dprintf(("%c", *q
+ '0'));
260 dprintf((" * 10^%i\n", tenpwr
));
263 * Now convert [digits,p) to our internal representation.
266 for (m
= mant
; m
< mant
+ MANT_LIMBS
; m
++) {
273 while (m
< mant
+ MANT_LIMBS
) {
275 while (p
> q
&& !p
[-1]) {
281 for (r
= p
; r
-- > q
;) {
310 * At this point, the 'mant' array contains the first frac-
311 * tional places of a base-2^16 real number which when mul-
312 * tiplied by 2^twopwr and 5^tenpwr gives X.
314 dprintf(("X = " MANT_FMT
" * 2^%i * 5^%i\n", MANT_ARG
, twopwr
,
318 * Now multiply 'mant' by 5^tenpwr.
320 if (tenpwr
< 0) { /* mult = 5^-1 = 0.2 */
321 for (m
= mult
; m
< mult
+ MANT_LIMBS
- 1; m
++) {
322 *m
= LIMB_BYTE(0xcc);
324 mult
[MANT_LIMBS
- 1] = LIMB_BYTE(0xcc)+1;
329 * If tenpwr was 1000...000b, then it becomes 1000...000b. See
330 * the "ANSI C" comment below for more details on that case.
332 * Because we already truncated tenpwr to +5000...-5000 inside
333 * the exponent parsing code, this shouldn't happen though.
335 } else if (tenpwr
> 0) { /* mult = 5^+1 = 5.0 */
336 mult
[0] = (fp_limb
)5 << (LIMB_BITS
-3); /* 0xA000... */
337 for (m
= mult
+ 1; m
< mult
+ MANT_LIMBS
; m
++) {
345 dprintf(("loop=" MANT_FMT
" * 2^%i * 5^%i (%i)\n", MANT_ARG
,
346 twopwr
, tenpwr
, extratwos
));
348 dprintf(("mant*mult\n"));
349 twopwr
+= extratwos
+ float_multiply(mant
, mult
);
351 dprintf(("mult*mult\n"));
352 extratwos
= extratwos
* 2 + float_multiply(mult
, mult
);
356 * In ANSI C, the result of right-shifting a signed integer is
357 * considered implementation-specific. To ensure that the loop
358 * terminates even if tenpwr was 1000...000b to begin with, we
359 * manually clear the MSB, in case a 1 was shifted in.
361 * Because we already truncated tenpwr to +5000...-5000 inside
362 * the exponent parsing code, this shouldn't matter; neverthe-
363 * less it is the right thing to do here.
365 tenpwr
&= (uint32_t) - 1 >> 1;
369 * At this point, the 'mant' array contains the first frac-
370 * tional places of a base-2^16 real number in [0.5,1) that
371 * when multiplied by 2^twopwr gives X. Or it contains zero
372 * of course. We are done.
379 * ---------------------------------------------------------------------------
380 * operations of specific bits
381 * ---------------------------------------------------------------------------
384 /* Set a bit, using *bigendian* bit numbering (0 = MSB) */
385 static void set_bit(fp_limb
*mant
, int bit
)
387 mant
[bit
/LIMB_BITS
] |= LIMB_TOP_BIT
>> (bit
& (LIMB_BITS
-1));
390 /* Test a single bit */
391 static int test_bit(const fp_limb
*mant
, int bit
)
393 return (mant
[bit
/LIMB_BITS
] >> (~bit
& (LIMB_BITS
-1))) & 1;
396 /* Report if the mantissa value is all zero */
397 static bool is_zero(const fp_limb
*mant
)
401 for (i
= 0; i
< MANT_LIMBS
; i
++)
409 * ---------------------------------------------------------------------------
410 * round a mantissa off after i words
411 * ---------------------------------------------------------------------------
414 #define ROUND_COLLECT_BITS \
416 m = mant[i] & (2*bit-1); \
417 for (j = i+1; j < MANT_LIMBS; j++) \
421 #define ROUND_ABS_DOWN \
423 mant[i] &= ~(bit-1); \
424 for (j = i+1; j < MANT_LIMBS; j++) \
429 #define ROUND_ABS_UP \
431 mant[i] = (mant[i] & ~(bit-1)) + bit; \
432 for (j = i+1; j < MANT_LIMBS; j++) \
434 while (i > 0 && !mant[i]) \
439 static bool ieee_round(bool minus
, fp_limb
*mant
, int bits
)
443 int i
= bits
/ LIMB_BITS
;
444 int p
= bits
% LIMB_BITS
;
445 fp_limb bit
= LIMB_TOP_BIT
>> p
;
447 if (rc
== FLOAT_RC_NEAR
) {
455 if (test_bit(mant
, bits
-1)) {
464 } else if (rc
== FLOAT_RC_ZERO
||
465 rc
== (minus
? FLOAT_RC_UP
: FLOAT_RC_DOWN
)) {
468 /* rc == (minus ? FLOAT_RC_DOWN : FLOAT_RC_UP) */
469 /* Round toward +/- infinity */
480 /* Returns a value >= 16 if not a valid hex digit */
481 static unsigned int hexval(char c
)
483 unsigned int v
= (unsigned char) c
;
485 if (v
>= '0' && v
<= '9')
488 return (v
|0x20) - 'a' + 10;
491 /* Handle floating-point numbers with radix 2^bits and binary exponent */
492 static bool ieee_flconvert_bin(const char *string
, int bits
,
493 fp_limb
*mant
, int32_t *exponent
)
495 static const int log2tbl
[16] =
496 { -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3 };
497 fp_limb mult
[MANT_LIMBS
+ 1], *mp
;
500 bool seendot
, seendigit
;
502 int radix
= 1 << bits
;
506 seendot
= seendigit
= false;
510 memset(mult
, 0, sizeof mult
);
512 while ((c
= *string
++) != '\0') {
518 "too many periods in floating-point constant");
521 } else if ((v
= hexval(c
)) < (unsigned int)radix
) {
522 if (!seendigit
&& v
) {
527 ms
= (LIMB_BITS
-1)-l
;
529 twopwr
= seendot
? twopwr
-bits
+l
: l
+1-bits
;
536 if (mp
> &mult
[MANT_LIMBS
])
537 mp
= &mult
[MANT_LIMBS
]; /* Guard slot */
549 } else if (c
== 'p' || c
== 'P') {
551 e
= read_exponent(string
, 20000);
556 } else if (c
== '_') {
560 "floating-point constant: `%c' is invalid character", c
);
566 memset(mant
, 0, sizeof mult
); /* Zero */
569 memcpy(mant
, mult
, sizeof mult
);
577 * Shift a mantissa to the right by i bits.
579 static void ieee_shr(fp_limb
*mant
, int i
)
585 sr
= i
% LIMB_BITS
; sl
= LIMB_BITS
-sr
;
590 for (j
= MANT_LIMBS
-1; j
>= offs
; j
--)
591 mant
[j
] = mant
[j
-offs
];
593 n
= mant
[MANT_LIMBS
-1-offs
] >> sr
;
594 for (j
= MANT_LIMBS
-1; j
> offs
; j
--) {
596 mant
[j
] = (m
<< sl
) | n
;
605 /* Produce standard IEEE formats, with implicit or explicit integer
606 bit; this makes the following assumptions:
608 - the sign bit is the MSB, followed by the exponent,
609 followed by the integer bit if present.
610 - the sign bit plus exponent fit in 16 bits.
611 - the exponent bias is 2^(n-1)-1 for an n-bit exponent */
615 int mantissa
; /* Fractional bits in the mantissa */
616 int explicit; /* Explicit integer */
617 int exponent
; /* Bits in the exponent */
621 * The 16- and 128-bit formats are expected to be in IEEE 754r.
622 * AMD SSE5 uses the 16-bit format.
624 * The 32- and 64-bit formats are the original IEEE 754 formats.
626 * The 80-bit format is x87-specific, but widely used.
628 * The 8-bit format appears to be the consensus 8-bit floating-point
629 * format. It is apparently used in graphics applications.
631 static const struct ieee_format ieee_8
= { 1, 3, 0, 4 };
632 static const struct ieee_format ieee_16
= { 2, 10, 0, 5 };
633 static const struct ieee_format ieee_32
= { 4, 23, 0, 8 };
634 static const struct ieee_format ieee_64
= { 8, 52, 0, 11 };
635 static const struct ieee_format ieee_80
= { 10, 63, 1, 15 };
636 static const struct ieee_format ieee_128
= { 16, 112, 0, 15 };
638 /* Types of values we can generate */
648 static int to_float(const char *str
, int s
, uint8_t * result
,
649 const struct ieee_format
*fmt
)
651 fp_limb mant
[MANT_LIMBS
], *mp
, m
;
652 int32_t exponent
= 0;
653 int32_t expmax
= 1 << (fmt
->exponent
- 1);
654 fp_limb one_mask
= LIMB_TOP_BIT
>>
655 ((fmt
->exponent
+fmt
->explicit) % LIMB_BITS
);
656 int one_pos
= (fmt
->exponent
+fmt
->explicit)/LIMB_BITS
;
662 int bits
= fmt
->bytes
* 8;
668 case 'n': /* __nan__ */
670 case 'q': /* __qnan__ */
674 case 's': /* __snan__ */
678 case 'i': /* __infinity__ */
684 "internal error: unknown FP constant token `%s'\n", str
);
693 ok
= ieee_flconvert_bin(str
+2, 4, mant
, &exponent
);
697 ok
= ieee_flconvert_bin(str
+2, 3, mant
, &exponent
);
701 ok
= ieee_flconvert_bin(str
+2, 1, mant
, &exponent
);
705 ok
= ieee_flconvert(str
+2, mant
, &exponent
);
708 /* Leading zero was just a zero? */
709 ok
= ieee_flconvert(str
, mant
, &exponent
);
712 } else if (str
[0] == '$') {
713 ok
= ieee_flconvert_bin(str
+1, 4, mant
, &exponent
);
715 ok
= ieee_flconvert(str
, mant
, &exponent
);
720 } else if (mant
[0] & LIMB_TOP_BIT
) {
725 if (exponent
>= 2 - expmax
&& exponent
<= expmax
) {
727 } else if (exponent
> 0) {
729 error(ERR_WARNING
|ERR_WARN_FL_OVERFLOW
,
730 "overflow in floating-point constant");
733 /* underflow or denormal; the denormal code handles
746 memset(mant
, 0, sizeof mant
);
751 shift
= -(exponent
+ expmax
- 2 - fmt
->exponent
)
753 ieee_shr(mant
, shift
);
754 ieee_round(minus
, mant
, bits
);
755 if (mant
[one_pos
] & one_mask
) {
756 /* One's position is set, we rounded up into normal range */
759 mant
[one_pos
] &= ~one_mask
; /* remove explicit one */
760 mant
[0] |= exponent
<< (LIMB_BITS
-1 - fmt
->exponent
);
762 if (daz
|| is_zero(mant
)) {
763 /* Flush denormals to zero */
765 error(ERR_WARNING
|ERR_WARN_FL_UNDERFLOW
,
766 "underflow in floating-point constant");
770 error(ERR_WARNING
|ERR_WARN_FL_DENORM
,
771 "denormal floating-point constant");
778 exponent
+= expmax
- 1;
779 ieee_shr(mant
, fmt
->exponent
+fmt
->explicit);
780 ieee_round(minus
, mant
, bits
);
781 /* did we scale up by one? */
782 if (test_bit(mant
, fmt
->exponent
+fmt
->explicit-1)) {
785 if (exponent
>= (expmax
<< 1)-1) {
787 error(ERR_WARNING
|ERR_WARN_FL_OVERFLOW
,
788 "overflow in floating-point constant");
795 mant
[one_pos
] &= ~one_mask
; /* remove explicit one */
796 mant
[0] |= exponent
<< (LIMB_BITS
-1 - fmt
->exponent
);
803 memset(mant
, 0, sizeof mant
);
804 mant
[0] = (((fp_limb
)1 << fmt
->exponent
)-1)
805 << (LIMB_BITS
-1 - fmt
->exponent
);
807 mant
[one_pos
] |= one_mask
;
809 set_bit(mant
, fmt
->exponent
+fmt
->explicit+1);
810 else if (type
== FL_SNAN
)
811 set_bit(mant
, fmt
->exponent
+fmt
->explicit+fmt
->mantissa
);
815 mant
[0] |= minus
? LIMB_TOP_BIT
: 0;
817 m
= mant
[fmt
->bytes
/LIMB_BYTES
];
818 for (i
= LIMB_BYTES
-(fmt
->bytes
% LIMB_BYTES
); i
< LIMB_BYTES
; i
++)
819 *result
++ = m
>> (i
*8);
821 for (mp
= &mant
[fmt
->bytes
/LIMB_BYTES
], i
= 0;
822 i
< fmt
->bytes
; i
+= LIMB_BYTES
) {
825 result
+= LIMB_BYTES
;
828 return 1; /* success */
831 int float_const(const char *number
, int32_t sign
, uint8_t * result
,
832 int bytes
, efunc err
)
838 return to_float(number
, sign
, result
, &ieee_8
);
840 return to_float(number
, sign
, result
, &ieee_16
);
842 return to_float(number
, sign
, result
, &ieee_32
);
844 return to_float(number
, sign
, result
, &ieee_64
);
846 return to_float(number
, sign
, result
, &ieee_80
);
848 return to_float(number
, sign
, result
, &ieee_128
);
850 error(ERR_PANIC
, "strange value %d passed to float_const", bytes
);
855 /* Set floating-point options */
856 int float_option(const char *option
)
858 if (!nasm_stricmp(option
, "daz")) {
861 } else if (!nasm_stricmp(option
, "nodaz")) {
864 } else if (!nasm_stricmp(option
, "near")) {
867 } else if (!nasm_stricmp(option
, "down")) {
870 } else if (!nasm_stricmp(option
, "up")) {
873 } else if (!nasm_stricmp(option
, "zero")) {
876 } else if (!nasm_stricmp(option
, "default")) {
881 return -1; /* Unknown option */