NASM 0.99.06
[nasm.git] / float.c
blob39bd3571318cb10e8b57f6c326c216d8f7836fef
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
9 */
11 #include "compiler.h"
13 #include <ctype.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <inttypes.h>
19 #include "nasm.h"
20 #include "float.h"
23 * -----------------
24 * local variables
25 * -----------------
27 static efunc error;
28 static bool daz = false; /* denormals as zero */
29 static enum float_round rc = FLOAT_RC_NEAR; /* rounding control */
32 * -----------
33 * constants
34 * -----------
37 /* "A limb is like a digit but bigger */
38 typedef uint32_t fp_limb;
39 typedef uint64_t fp_2limb;
41 #define LIMB_BITS 32
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))
50 #else
51 #define put(a,b) (((a)[0] = (b)), \
52 ((a)[1] = (b) >> 8), \
53 ((a)[2] = (b) >> 16), \
54 ((a)[3] = (b) >> 24))
55 #endif
57 /* 112 bits + 64 bits for accuracy + 16 bits for rounding */
58 #define MANT_LIMBS 6
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 * ---------------------------------------------------------------------------
76 #ifdef DEBUG_FLOAT
77 #define dprintf(x) printf x
78 #else /* */
79 #define dprintf(x) do { } while (0)
80 #endif /* */
83 * ---------------------------------------------------------------------------
84 * multiply
85 * ---------------------------------------------------------------------------
87 static int float_multiply(fp_limb *to, fp_limb *from)
89 fp_2limb temp[MANT_LIMBS * 2];
90 int i, j;
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++) {
103 fp_2limb n;
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));
123 return 0;
124 } else {
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));
129 return -1;
134 * ---------------------------------------------------------------------------
135 * read an exponent; returns INT32_MAX on error
136 * ---------------------------------------------------------------------------
138 static int32_t read_exponent(const char *string, int32_t max)
140 int32_t i = 0;
141 bool neg = false;
143 if (*string == '+') {
144 string++;
145 } else if (*string == '-') {
146 neg = true;
147 string++;
149 while (*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.
163 if (i > max)
164 i = max;
165 } else if (*string == '_') {
166 /* do nothing */
167 } else {
168 error(ERR_NONFATAL,
169 "invalid character in floating-point constant %s: '%c'",
170 "exponent", *string);
171 return INT32_MAX;
173 string++;
176 return neg ? -i : i;
180 * ---------------------------------------------------------------------------
181 * convert
182 * ---------------------------------------------------------------------------
184 static bool ieee_flconvert(const char *string, fp_limb *mant,
185 int32_t * exponent)
187 char digits[MANT_DIGITS];
188 char *p, *q, *r;
189 fp_limb mult[MANT_LIMBS], bit;
190 fp_limb *m;
191 int32_t tenpwr, twopwr;
192 int32_t extratwos;
193 bool started, seendot, warned;
194 p = digits;
195 tenpwr = 0;
196 started = seendot = false;
197 warned = (pass0 != 1);
198 while (*string && *string != 'E' && *string != 'e') {
199 if (*string == '.') {
200 if (!seendot) {
201 seendot = true;
202 } else {
203 error(ERR_NONFATAL,
204 "too many periods in floating-point constant");
205 return false;
207 } else if (*string >= '0' && *string <= '9') {
208 if (*string == '0' && !started) {
209 if (seendot) {
210 tenpwr--;
212 } else {
213 started = true;
214 if (p < digits + sizeof(digits)) {
215 *p++ = *string - '0';
216 } else {
217 if (!warned) {
218 error(ERR_WARNING|ERR_WARN_FL_TOOLONG,
219 "floating-point constant significand contains "
220 "more than %i digits", MANT_DIGITS);
221 warned = true;
224 if (!seendot) {
225 tenpwr++;
228 } else if (*string == '_') {
229 /* do nothing */
230 } else {
231 error(ERR_NONFATAL,
232 "invalid character in floating-point constant %s: '%c'",
233 "significand", *string);
234 return false;
236 string++;
239 if (*string) {
240 int32_t e;
242 string++; /* eat the E */
243 e = read_exponent(string, 5000);
244 if (e == INT32_MAX)
245 return false;
246 tenpwr += e;
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.
254 q = digits;
255 dprintf(("X = 0."));
256 while (q < p) {
257 dprintf(("%c", *q + '0'));
258 q++;
260 dprintf((" * 10^%i\n", tenpwr));
263 * Now convert [digits,p) to our internal representation.
265 bit = LIMB_TOP_BIT;
266 for (m = mant; m < mant + MANT_LIMBS; m++) {
267 *m = 0;
269 m = mant;
270 q = digits;
271 started = false;
272 twopwr = 0;
273 while (m < mant + MANT_LIMBS) {
274 fp_limb carry = 0;
275 while (p > q && !p[-1]) {
276 p--;
278 if (p <= q) {
279 break;
281 for (r = p; r-- > q;) {
282 int32_t i;
283 i = 2 * *r + carry;
284 if (i >= 10) {
285 carry = 1;
286 i -= 10;
287 } else {
288 carry = 0;
290 *r = i;
292 if (carry) {
293 *m |= bit;
294 started = true;
296 if (started) {
297 if (bit == 1) {
298 bit = LIMB_TOP_BIT;
299 m++;
300 } else {
301 bit >>= 1;
303 } else {
304 twopwr--;
307 twopwr += tenpwr;
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,
315 tenpwr));
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;
325 extratwos = -2;
326 tenpwr = -tenpwr;
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++) {
338 *m = 0;
340 extratwos = 3;
341 } else {
342 extratwos = 0;
344 while (tenpwr) {
345 dprintf(("loop=" MANT_FMT " * 2^%i * 5^%i (%i)\n", MANT_ARG,
346 twopwr, tenpwr, extratwos));
347 if (tenpwr & 1) {
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);
353 tenpwr >>= 1;
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.
374 *exponent = twopwr;
375 return true;
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)
399 int i;
401 for (i = 0; i < MANT_LIMBS; i++)
402 if (mant[i])
403 return false;
405 return true;
409 * ---------------------------------------------------------------------------
410 * round a mantissa off after i words
411 * ---------------------------------------------------------------------------
414 #define ROUND_COLLECT_BITS \
415 do { \
416 m = mant[i] & (2*bit-1); \
417 for (j = i+1; j < MANT_LIMBS; j++) \
418 m = m | mant[j]; \
419 } while (0)
421 #define ROUND_ABS_DOWN \
422 do { \
423 mant[i] &= ~(bit-1); \
424 for (j = i+1; j < MANT_LIMBS; j++) \
425 mant[j] = 0; \
426 return false; \
427 } while (0)
429 #define ROUND_ABS_UP \
430 do { \
431 mant[i] = (mant[i] & ~(bit-1)) + bit; \
432 for (j = i+1; j < MANT_LIMBS; j++) \
433 mant[j] = 0; \
434 while (i > 0 && !mant[i]) \
435 ++mant[--i]; \
436 return !mant[0]; \
437 } while (0)
439 static bool ieee_round(bool minus, fp_limb *mant, int bits)
441 fp_limb m = 0;
442 int32_t j;
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) {
448 if (mant[i] & bit) {
449 mant[i] &= ~bit;
450 ROUND_COLLECT_BITS;
451 mant[i] |= bit;
452 if (m) {
453 ROUND_ABS_UP;
454 } else {
455 if (test_bit(mant, bits-1)) {
456 ROUND_ABS_UP;
457 } else {
458 ROUND_ABS_DOWN;
461 } else {
462 ROUND_ABS_DOWN;
464 } else if (rc == FLOAT_RC_ZERO ||
465 rc == (minus ? FLOAT_RC_UP : FLOAT_RC_DOWN)) {
466 ROUND_ABS_DOWN;
467 } else {
468 /* rc == (minus ? FLOAT_RC_DOWN : FLOAT_RC_UP) */
469 /* Round toward +/- infinity */
470 ROUND_COLLECT_BITS;
471 if (m) {
472 ROUND_ABS_UP;
473 } else {
474 ROUND_ABS_DOWN;
477 return false;
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')
486 return v - '0';
487 else
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;
498 int ms;
499 int32_t twopwr;
500 bool seendot, seendigit;
501 unsigned char c;
502 int radix = 1 << bits;
503 fp_limb v;
505 twopwr = 0;
506 seendot = seendigit = false;
507 ms = 0;
508 mp = NULL;
510 memset(mult, 0, sizeof mult);
512 while ((c = *string++) != '\0') {
513 if (c == '.') {
514 if (!seendot)
515 seendot = true;
516 else {
517 error(ERR_NONFATAL,
518 "too many periods in floating-point constant");
519 return false;
521 } else if ((v = hexval(c)) < (unsigned int)radix) {
522 if (!seendigit && v) {
523 int l = log2tbl[v];
525 seendigit = true;
526 mp = mult;
527 ms = (LIMB_BITS-1)-l;
529 twopwr = seendot ? twopwr-bits+l : l+1-bits;
532 if (seendigit) {
533 if (ms <= 0) {
534 *mp |= v >> -ms;
535 mp++;
536 if (mp > &mult[MANT_LIMBS])
537 mp = &mult[MANT_LIMBS]; /* Guard slot */
538 ms += LIMB_BITS;
540 *mp |= v << ms;
541 ms -= bits;
543 if (!seendot)
544 twopwr += bits;
545 } else {
546 if (seendot)
547 twopwr -= bits;
549 } else if (c == 'p' || c == 'P') {
550 int32_t e;
551 e = read_exponent(string, 20000);
552 if (e == INT32_MAX)
553 return false;
554 twopwr += e;
555 break;
556 } else if (c == '_') {
557 /* ignore */
558 } else {
559 error(ERR_NONFATAL,
560 "floating-point constant: `%c' is invalid character", c);
561 return false;
565 if (!seendigit) {
566 memset(mant, 0, sizeof mult); /* Zero */
567 *exponent = 0;
568 } else {
569 memcpy(mant, mult, sizeof mult);
570 *exponent = twopwr;
573 return true;
577 * Shift a mantissa to the right by i bits.
579 static void ieee_shr(fp_limb *mant, int i)
581 fp_limb n, m;
582 int j = 0;
583 int sr, sl, offs;
585 sr = i % LIMB_BITS; sl = LIMB_BITS-sr;
586 offs = i/LIMB_BITS;
588 if (sr == 0) {
589 if (offs)
590 for (j = MANT_LIMBS-1; j >= offs; j--)
591 mant[j] = mant[j-offs];
592 } else {
593 n = mant[MANT_LIMBS-1-offs] >> sr;
594 for (j = MANT_LIMBS-1; j > offs; j--) {
595 m = mant[j-offs-1];
596 mant[j] = (m << sl) | n;
597 n = m >> sr;
599 mant[j--] = n;
601 while (j >= 0)
602 mant[j--] = 0;
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 */
613 struct ieee_format {
614 int bytes;
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 */
639 enum floats {
640 FL_ZERO,
641 FL_DENORMAL,
642 FL_NORMAL,
643 FL_INFINITY,
644 FL_QNAN,
645 FL_SNAN
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;
657 int i;
658 int shift;
659 enum floats type;
660 bool ok;
661 bool minus = s < 0;
662 int bits = fmt->bytes * 8;
664 if (str[0] == '_') {
665 /* Special tokens */
667 switch (str[2]) {
668 case 'n': /* __nan__ */
669 case 'N':
670 case 'q': /* __qnan__ */
671 case 'Q':
672 type = FL_QNAN;
673 break;
674 case 's': /* __snan__ */
675 case 'S':
676 type = FL_SNAN;
677 break;
678 case 'i': /* __infinity__ */
679 case 'I':
680 type = FL_INFINITY;
681 break;
682 default:
683 error(ERR_NONFATAL,
684 "internal error: unknown FP constant token `%s'\n", str);
685 type = FL_QNAN;
686 break;
688 } else {
689 if (str[0] == '0') {
690 switch (str[1]) {
691 case 'x': case 'X':
692 case 'h': case 'H':
693 ok = ieee_flconvert_bin(str+2, 4, mant, &exponent);
694 break;
695 case 'o': case 'O':
696 case 'q': case 'Q':
697 ok = ieee_flconvert_bin(str+2, 3, mant, &exponent);
698 break;
699 case 'b': case 'B':
700 case 'y': case 'Y':
701 ok = ieee_flconvert_bin(str+2, 1, mant, &exponent);
702 break;
703 case 'd': case 'D':
704 case 't': case 'T':
705 ok = ieee_flconvert(str+2, mant, &exponent);
706 break;
707 default:
708 /* Leading zero was just a zero? */
709 ok = ieee_flconvert(str, mant, &exponent);
710 break;
712 } else if (str[0] == '$') {
713 ok = ieee_flconvert_bin(str+1, 4, mant, &exponent);
714 } else {
715 ok = ieee_flconvert(str, mant, &exponent);
718 if (!ok) {
719 type = FL_QNAN;
720 } else if (mant[0] & LIMB_TOP_BIT) {
722 * Non-zero.
724 exponent--;
725 if (exponent >= 2 - expmax && exponent <= expmax) {
726 type = FL_NORMAL;
727 } else if (exponent > 0) {
728 if (pass0 == 1)
729 error(ERR_WARNING|ERR_WARN_FL_OVERFLOW,
730 "overflow in floating-point constant");
731 type = FL_INFINITY;
732 } else {
733 /* underflow or denormal; the denormal code handles
734 actual underflow. */
735 type = FL_DENORMAL;
737 } else {
738 /* Zero */
739 type = FL_ZERO;
743 switch (type) {
744 case FL_ZERO:
745 zero:
746 memset(mant, 0, sizeof mant);
747 break;
749 case FL_DENORMAL:
751 shift = -(exponent + expmax - 2 - fmt->exponent)
752 + fmt->explicit;
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 */
757 exponent = 1;
758 if (!fmt->explicit)
759 mant[one_pos] &= ~one_mask; /* remove explicit one */
760 mant[0] |= exponent << (LIMB_BITS-1 - fmt->exponent);
761 } else {
762 if (daz || is_zero(mant)) {
763 /* Flush denormals to zero */
764 if (pass0 == 1)
765 error(ERR_WARNING|ERR_WARN_FL_UNDERFLOW,
766 "underflow in floating-point constant");
767 goto zero;
768 } else {
769 if (pass0 == 1)
770 error(ERR_WARNING|ERR_WARN_FL_DENORM,
771 "denormal floating-point constant");
774 break;
777 case FL_NORMAL:
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)) {
783 ieee_shr(mant, 1);
784 exponent++;
785 if (exponent >= (expmax << 1)-1) {
786 if (pass0 == 1)
787 error(ERR_WARNING|ERR_WARN_FL_OVERFLOW,
788 "overflow in floating-point constant");
789 type = FL_INFINITY;
790 goto overflow;
794 if (!fmt->explicit)
795 mant[one_pos] &= ~one_mask; /* remove explicit one */
796 mant[0] |= exponent << (LIMB_BITS-1 - fmt->exponent);
797 break;
799 case FL_INFINITY:
800 case FL_QNAN:
801 case FL_SNAN:
802 overflow:
803 memset(mant, 0, sizeof mant);
804 mant[0] = (((fp_limb)1 << fmt->exponent)-1)
805 << (LIMB_BITS-1 - fmt->exponent);
806 if (fmt->explicit)
807 mant[one_pos] |= one_mask;
808 if (type == FL_QNAN)
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);
812 break;
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) {
823 m = *--mp;
824 put(result, m);
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)
834 error = err;
836 switch (bytes) {
837 case 1:
838 return to_float(number, sign, result, &ieee_8);
839 case 2:
840 return to_float(number, sign, result, &ieee_16);
841 case 4:
842 return to_float(number, sign, result, &ieee_32);
843 case 8:
844 return to_float(number, sign, result, &ieee_64);
845 case 10:
846 return to_float(number, sign, result, &ieee_80);
847 case 16:
848 return to_float(number, sign, result, &ieee_128);
849 default:
850 error(ERR_PANIC, "strange value %d passed to float_const", bytes);
851 return 0;
855 /* Set floating-point options */
856 int float_option(const char *option)
858 if (!nasm_stricmp(option, "daz")) {
859 daz = true;
860 return 0;
861 } else if (!nasm_stricmp(option, "nodaz")) {
862 daz = false;
863 return 0;
864 } else if (!nasm_stricmp(option, "near")) {
865 rc = FLOAT_RC_NEAR;
866 return 0;
867 } else if (!nasm_stricmp(option, "down")) {
868 rc = FLOAT_RC_DOWN;
869 return 0;
870 } else if (!nasm_stricmp(option, "up")) {
871 rc = FLOAT_RC_UP;
872 return 0;
873 } else if (!nasm_stricmp(option, "zero")) {
874 rc = FLOAT_RC_ZERO;
875 return 0;
876 } else if (!nasm_stricmp(option, "default")) {
877 rc = FLOAT_RC_NEAR;
878 daz = false;
879 return 0;
880 } else {
881 return -1; /* Unknown option */