blob: f51654a6187597b5447c07fe627ed375a90e66ff [file] [log] [blame]
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001/* float.c floating-point constant support for the Netwide Assembler
2 *
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
Beroset095e6a22007-12-29 09:44:23 -05005 * redistributable under the license given in the file "LICENSE"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 * distributed in the NASM archive.
7 *
8 * initial version 13/ix/96 by Simon Tatham
9 */
10
H. Peter Anvinfe501952007-10-02 21:53:51 -070011#include "compiler.h"
12
H. Peter Anvinfe2177f2007-09-18 18:31:26 -070013#include <ctype.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000014#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000017#include <inttypes.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000018
19#include "nasm.h"
H. Peter Anvin214f5492007-10-15 19:46:32 -070020#include "float.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000021
22/*
H. Peter Anvin214f5492007-10-15 19:46:32 -070023 * -----------------
24 * local variables
25 * -----------------
26 */
27static efunc error;
28static bool daz = false; /* denormals as zero */
29static enum float_round rc = FLOAT_RC_NEAR; /* rounding control */
30
31/*
32 * -----------
33 * constants
34 * -----------
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000035 */
36
H. Peter Anvin2ce02742007-10-29 20:20:12 -070037/* "A limb is like a digit but bigger */
38typedef uint32_t fp_limb;
39typedef uint64_t fp_2limb;
40
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)
47
H. Peter Anvin214f5492007-10-15 19:46:32 -070048/* 112 bits + 64 bits for accuracy + 16 bits for rounding */
H. Peter Anvin2ce02742007-10-29 20:20:12 -070049#define MANT_LIMBS 6
H. Peter Anvin214f5492007-10-15 19:46:32 -070050
51/* 52 digits fit in 176 bits because 10^53 > 2^176 > 10^52 */
52#define MANT_DIGITS 52
53
H. Peter Anvin2ce02742007-10-29 20:20:12 -070054/* the format and the argument list depend on MANT_LIMBS */
55#define MANT_FMT "%08x_%08x_%08x_%08x_%08x_%08x"
H. Peter Anvin214f5492007-10-15 19:46:32 -070056#define MANT_ARG SOME_ARG(mant, 0)
57
58#define SOME_ARG(a,i) (a)[(i)+0], (a)[(i)+1], (a)[(i)+2], (a)[(i)+3], \
H. Peter Anvin2ce02742007-10-29 20:20:12 -070059 (a)[(i)+4], (a)[(i)+5]
H. Peter Anvin214f5492007-10-15 19:46:32 -070060
61/*
62 * ---------------------------------------------------------------------------
63 * emit a printf()-like debug message... but only if DEBUG_FLOAT was defined
64 * ---------------------------------------------------------------------------
65 */
66
67#ifdef DEBUG_FLOAT
68#define dprintf(x) printf x
69#else /* */
70#define dprintf(x) do { } while (0)
71#endif /* */
72
73/*
74 * ---------------------------------------------------------------------------
75 * multiply
76 * ---------------------------------------------------------------------------
77 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -070078static int float_multiply(fp_limb *to, fp_limb *from)
H. Peter Anvineba20a72002-04-30 20:53:55 +000079{
H. Peter Anvin2ce02742007-10-29 20:20:12 -070080 fp_2limb temp[MANT_LIMBS * 2];
81 int i, j;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000082
H. Peter Anvin70653092007-10-19 14:42:29 -070083 /*
H. Peter Anvin214f5492007-10-15 19:46:32 -070084 * guaranteed that top bit of 'from' is set -- so we only have
85 * to worry about _one_ bit shift to the left
86 */
87 dprintf(("%s=" MANT_FMT "\n", "mul1", SOME_ARG(to, 0)));
88 dprintf(("%s=" MANT_FMT "\n", "mul2", SOME_ARG(from, 0)));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000089
H. Peter Anvin214f5492007-10-15 19:46:32 -070090 memset(temp, 0, sizeof temp);
91
H. Peter Anvin2ce02742007-10-29 20:20:12 -070092 for (i = 0; i < MANT_LIMBS; i++) {
93 for (j = 0; j < MANT_LIMBS; j++) {
94 fp_2limb n;
95 n = (fp_2limb) to[i] * (fp_2limb) from[j];
96 temp[i + j] += n >> LIMB_BITS;
97 temp[i + j + 1] += (fp_limb)n;
H. Peter Anvine2c80182005-01-15 22:15:51 +000098 }
H. Peter Anvin214f5492007-10-15 19:46:32 -070099 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000100
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700101 for (i = MANT_LIMBS * 2; --i;) {
102 temp[i - 1] += temp[i] >> LIMB_BITS;
103 temp[i] &= LIMB_MASK;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000104 }
H. Peter Anvin214f5492007-10-15 19:46:32 -0700105
106 dprintf(("%s=" MANT_FMT "_" MANT_FMT "\n", "temp", SOME_ARG(temp, 0),
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700107 SOME_ARG(temp, MANT_LIMBS)));
H. Peter Anvin214f5492007-10-15 19:46:32 -0700108
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700109 if (temp[0] & LIMB_TOP_BIT) {
110 for (i = 0; i < MANT_LIMBS; i++) {
111 to[i] = temp[i] & LIMB_MASK;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700112 }
113 dprintf(("%s=" MANT_FMT " (%i)\n", "prod", SOME_ARG(to, 0), 0));
114 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000115 } else {
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700116 for (i = 0; i < MANT_LIMBS; i++) {
117 to[i] = (temp[i] << 1) + !!(temp[i + 1] & LIMB_TOP_BIT);
H. Peter Anvin214f5492007-10-15 19:46:32 -0700118 }
119 dprintf(("%s=" MANT_FMT " (%i)\n", "prod", SOME_ARG(to, 0), -1));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000120 return -1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000121 }
122}
123
H. Peter Anvin214f5492007-10-15 19:46:32 -0700124/*
125 * ---------------------------------------------------------------------------
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700126 * read an exponent; returns INT32_MAX on error
127 * ---------------------------------------------------------------------------
128 */
H. Peter Anvin3514ad02007-10-19 14:17:51 -0700129static int32_t read_exponent(const char *string, int32_t max)
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700130{
131 int32_t i = 0;
132 bool neg = false;
H. Peter Anvin70653092007-10-19 14:42:29 -0700133
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700134 if (*string == '+') {
135 string++;
136 } else if (*string == '-') {
137 neg = true;
138 string++;
139 }
140 while (*string) {
141 if (*string >= '0' && *string <= '9') {
142 i = (i * 10) + (*string - '0');
H. Peter Anvin70653092007-10-19 14:42:29 -0700143
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700144 /*
145 * To ensure that underflows and overflows are
146 * handled properly we must avoid wraparounds of
147 * the signed integer value that is used to hold
148 * the exponent. Therefore we cap the exponent at
149 * +/-5000, which is slightly more/less than
150 * what's required for normal and denormal numbers
151 * in single, double, and extended precision, but
152 * sufficient to avoid signed integer wraparound.
153 */
H. Peter Anvin3b2ad1b2007-10-21 15:32:33 -0700154 if (i > max)
155 i = max;
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700156 } else if (*string == '_') {
157 /* do nothing */
158 } else {
H. Peter Anvin136dcdb2007-11-12 18:25:24 -0800159 error(ERR_NONFATAL|ERR_PASS1,
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700160 "invalid character in floating-point constant %s: '%c'",
161 "exponent", *string);
162 return INT32_MAX;
163 }
164 string++;
165 }
166
167 return neg ? -i : i;
168}
169
170/*
171 * ---------------------------------------------------------------------------
H. Peter Anvin214f5492007-10-15 19:46:32 -0700172 * convert
173 * ---------------------------------------------------------------------------
174 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700175static bool ieee_flconvert(const char *string, fp_limb *mant,
H. Peter Anvin214f5492007-10-15 19:46:32 -0700176 int32_t * exponent)
177{
178 char digits[MANT_DIGITS];
179 char *p, *q, *r;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700180 fp_limb mult[MANT_LIMBS], bit;
181 fp_limb *m;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700182 int32_t tenpwr, twopwr;
183 int32_t extratwos;
184 bool started, seendot, warned;
H. Peter Anvin136dcdb2007-11-12 18:25:24 -0800185
186 warned = false;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700187 p = digits;
188 tenpwr = 0;
H. Peter Anvinfab3a6c2007-10-16 11:48:07 -0700189 started = seendot = false;
H. Peter Anvin136dcdb2007-11-12 18:25:24 -0800190
H. Peter Anvin214f5492007-10-15 19:46:32 -0700191 while (*string && *string != 'E' && *string != 'e') {
192 if (*string == '.') {
193 if (!seendot) {
194 seendot = true;
195 } else {
H. Peter Anvin136dcdb2007-11-12 18:25:24 -0800196 error(ERR_NONFATAL|ERR_PASS1,
H. Peter Anvin214f5492007-10-15 19:46:32 -0700197 "too many periods in floating-point constant");
198 return false;
199 }
200 } else if (*string >= '0' && *string <= '9') {
201 if (*string == '0' && !started) {
202 if (seendot) {
203 tenpwr--;
204 }
205 } else {
206 started = true;
207 if (p < digits + sizeof(digits)) {
208 *p++ = *string - '0';
209 } else {
210 if (!warned) {
H. Peter Anvin136dcdb2007-11-12 18:25:24 -0800211 error(ERR_WARNING|ERR_WARN_FL_TOOLONG|ERR_PASS1,
H. Peter Anvin214f5492007-10-15 19:46:32 -0700212 "floating-point constant significand contains "
213 "more than %i digits", MANT_DIGITS);
214 warned = true;
215 }
216 }
217 if (!seendot) {
218 tenpwr++;
219 }
220 }
221 } else if (*string == '_') {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700222 /* do nothing */
223 } else {
H. Peter Anvin136dcdb2007-11-12 18:25:24 -0800224 error(ERR_NONFATAL|ERR_PASS1,
H. Peter Anvin214f5492007-10-15 19:46:32 -0700225 "invalid character in floating-point constant %s: '%c'",
226 "significand", *string);
227 return false;
228 }
229 string++;
230 }
H. Peter Anvin70653092007-10-19 14:42:29 -0700231
H. Peter Anvin214f5492007-10-15 19:46:32 -0700232 if (*string) {
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700233 int32_t e;
234
H. Peter Anvin214f5492007-10-15 19:46:32 -0700235 string++; /* eat the E */
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700236 e = read_exponent(string, 5000);
237 if (e == INT32_MAX)
238 return false;
239 tenpwr += e;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700240 }
241
242 /*
243 * At this point, the memory interval [digits,p) contains a
244 * series of decimal digits zzzzzzz, such that our number X
245 * satisfies X = 0.zzzzzzz * 10^tenpwr.
246 */
247 q = digits;
248 dprintf(("X = 0."));
249 while (q < p) {
250 dprintf(("%c", *q + '0'));
251 q++;
252 }
253 dprintf((" * 10^%i\n", tenpwr));
254
255 /*
256 * Now convert [digits,p) to our internal representation.
257 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700258 bit = LIMB_TOP_BIT;
259 for (m = mant; m < mant + MANT_LIMBS; m++) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700260 *m = 0;
261 }
262 m = mant;
263 q = digits;
264 started = false;
265 twopwr = 0;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700266 while (m < mant + MANT_LIMBS) {
267 fp_limb carry = 0;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700268 while (p > q && !p[-1]) {
269 p--;
270 }
271 if (p <= q) {
272 break;
273 }
274 for (r = p; r-- > q;) {
275 int32_t i;
276 i = 2 * *r + carry;
277 if (i >= 10) {
278 carry = 1;
279 i -= 10;
280 } else {
281 carry = 0;
282 }
283 *r = i;
284 }
285 if (carry) {
286 *m |= bit;
287 started = true;
288 }
289 if (started) {
290 if (bit == 1) {
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700291 bit = LIMB_TOP_BIT;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700292 m++;
293 } else {
294 bit >>= 1;
295 }
296 } else {
297 twopwr--;
298 }
299 }
300 twopwr += tenpwr;
301
302 /*
303 * At this point, the 'mant' array contains the first frac-
304 * tional places of a base-2^16 real number which when mul-
305 * tiplied by 2^twopwr and 5^tenpwr gives X.
306 */
307 dprintf(("X = " MANT_FMT " * 2^%i * 5^%i\n", MANT_ARG, twopwr,
308 tenpwr));
309
310 /*
311 * Now multiply 'mant' by 5^tenpwr.
312 */
313 if (tenpwr < 0) { /* mult = 5^-1 = 0.2 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700314 for (m = mult; m < mult + MANT_LIMBS - 1; m++) {
315 *m = LIMB_BYTE(0xcc);
H. Peter Anvin214f5492007-10-15 19:46:32 -0700316 }
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700317 mult[MANT_LIMBS - 1] = LIMB_BYTE(0xcc)+1;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700318 extratwos = -2;
319 tenpwr = -tenpwr;
320
321 /*
322 * If tenpwr was 1000...000b, then it becomes 1000...000b. See
323 * the "ANSI C" comment below for more details on that case.
324 *
325 * Because we already truncated tenpwr to +5000...-5000 inside
326 * the exponent parsing code, this shouldn't happen though.
327 */
328 } else if (tenpwr > 0) { /* mult = 5^+1 = 5.0 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700329 mult[0] = (fp_limb)5 << (LIMB_BITS-3); /* 0xA000... */
330 for (m = mult + 1; m < mult + MANT_LIMBS; m++) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700331 *m = 0;
332 }
333 extratwos = 3;
334 } else {
335 extratwos = 0;
336 }
337 while (tenpwr) {
338 dprintf(("loop=" MANT_FMT " * 2^%i * 5^%i (%i)\n", MANT_ARG,
339 twopwr, tenpwr, extratwos));
340 if (tenpwr & 1) {
341 dprintf(("mant*mult\n"));
342 twopwr += extratwos + float_multiply(mant, mult);
343 }
344 dprintf(("mult*mult\n"));
345 extratwos = extratwos * 2 + float_multiply(mult, mult);
346 tenpwr >>= 1;
347
348 /*
349 * In ANSI C, the result of right-shifting a signed integer is
350 * considered implementation-specific. To ensure that the loop
351 * terminates even if tenpwr was 1000...000b to begin with, we
352 * manually clear the MSB, in case a 1 was shifted in.
353 *
354 * Because we already truncated tenpwr to +5000...-5000 inside
355 * the exponent parsing code, this shouldn't matter; neverthe-
356 * less it is the right thing to do here.
357 */
358 tenpwr &= (uint32_t) - 1 >> 1;
359 }
360
361 /*
362 * At this point, the 'mant' array contains the first frac-
363 * tional places of a base-2^16 real number in [0.5,1) that
364 * when multiplied by 2^twopwr gives X. Or it contains zero
365 * of course. We are done.
366 */
367 *exponent = twopwr;
368 return true;
369}
370
371/*
372 * ---------------------------------------------------------------------------
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700373 * operations of specific bits
374 * ---------------------------------------------------------------------------
375 */
376
377/* Set a bit, using *bigendian* bit numbering (0 = MSB) */
378static void set_bit(fp_limb *mant, int bit)
379{
380 mant[bit/LIMB_BITS] |= LIMB_TOP_BIT >> (bit & (LIMB_BITS-1));
381}
382
383/* Test a single bit */
384static int test_bit(const fp_limb *mant, int bit)
385{
386 return (mant[bit/LIMB_BITS] >> (~bit & (LIMB_BITS-1))) & 1;
387}
388
389/* Report if the mantissa value is all zero */
390static bool is_zero(const fp_limb *mant)
391{
392 int i;
393
394 for (i = 0; i < MANT_LIMBS; i++)
395 if (mant[i])
396 return false;
397
398 return true;
399}
400
401/*
402 * ---------------------------------------------------------------------------
H. Peter Anvin214f5492007-10-15 19:46:32 -0700403 * round a mantissa off after i words
404 * ---------------------------------------------------------------------------
405 */
406
407#define ROUND_COLLECT_BITS \
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700408 do { \
409 m = mant[i] & (2*bit-1); \
410 for (j = i+1; j < MANT_LIMBS; j++) \
411 m = m | mant[j]; \
412 } while (0)
H. Peter Anvin214f5492007-10-15 19:46:32 -0700413
414#define ROUND_ABS_DOWN \
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700415 do { \
416 mant[i] &= ~(bit-1); \
417 for (j = i+1; j < MANT_LIMBS; j++) \
418 mant[j] = 0; \
419 return false; \
420 } while (0)
H. Peter Anvin214f5492007-10-15 19:46:32 -0700421
422#define ROUND_ABS_UP \
423 do { \
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700424 mant[i] = (mant[i] & ~(bit-1)) + bit; \
425 for (j = i+1; j < MANT_LIMBS; j++) \
426 mant[j] = 0; \
427 while (i > 0 && !mant[i]) \
428 ++mant[--i]; \
429 return !mant[0]; \
430 } while (0)
H. Peter Anvin214f5492007-10-15 19:46:32 -0700431
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700432static bool ieee_round(bool minus, fp_limb *mant, int bits)
H. Peter Anvin214f5492007-10-15 19:46:32 -0700433{
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700434 fp_limb m = 0;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700435 int32_t j;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700436 int i = bits / LIMB_BITS;
437 int p = bits % LIMB_BITS;
438 fp_limb bit = LIMB_TOP_BIT >> p;
439
440 if (rc == FLOAT_RC_NEAR) {
441 if (mant[i] & bit) {
442 mant[i] &= ~bit;
443 ROUND_COLLECT_BITS;
444 mant[i] |= bit;
445 if (m) {
446 ROUND_ABS_UP;
447 } else {
448 if (test_bit(mant, bits-1)) {
449 ROUND_ABS_UP;
450 } else {
451 ROUND_ABS_DOWN;
452 }
453 }
454 } else {
455 ROUND_ABS_DOWN;
456 }
457 } else if (rc == FLOAT_RC_ZERO ||
458 rc == (minus ? FLOAT_RC_UP : FLOAT_RC_DOWN)) {
459 ROUND_ABS_DOWN;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700460 } else {
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700461 /* rc == (minus ? FLOAT_RC_DOWN : FLOAT_RC_UP) */
462 /* Round toward +/- infinity */
463 ROUND_COLLECT_BITS;
464 if (m) {
465 ROUND_ABS_UP;
466 } else {
467 ROUND_ABS_DOWN;
468 }
H. Peter Anvin214f5492007-10-15 19:46:32 -0700469 }
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700470 return false;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700471}
472
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700473/* Returns a value >= 16 if not a valid hex digit */
474static unsigned int hexval(char c)
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700475{
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700476 unsigned int v = (unsigned char) c;
477
478 if (v >= '0' && v <= '9')
479 return v - '0';
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700480 else
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700481 return (v|0x20) - 'a' + 10;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700482}
483
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700484/* Handle floating-point numbers with radix 2^bits and binary exponent */
485static bool ieee_flconvert_bin(const char *string, int bits,
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700486 fp_limb *mant, int32_t *exponent)
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700487{
488 static const int log2tbl[16] =
H. Peter Anvin214f5492007-10-15 19:46:32 -0700489 { -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3 };
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700490 fp_limb mult[MANT_LIMBS + 1], *mp;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700491 int ms;
492 int32_t twopwr;
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700493 bool seendot, seendigit;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700494 unsigned char c;
H. Peter Anvin45f22922008-07-03 20:12:37 -0700495 const int radix = 1 << bits;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700496 fp_limb v;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700497
498 twopwr = 0;
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700499 seendot = seendigit = false;
H. Peter Anvin82f9f632007-09-24 20:53:48 -0700500 ms = 0;
501 mp = NULL;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700502
503 memset(mult, 0, sizeof mult);
504
505 while ((c = *string++) != '\0') {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700506 if (c == '.') {
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700507 if (!seendot)
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700508 seendot = true;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700509 else {
H. Peter Anvin136dcdb2007-11-12 18:25:24 -0800510 error(ERR_NONFATAL|ERR_PASS1,
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700511 "too many periods in floating-point constant");
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700512 return false;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700513 }
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700514 } else if ((v = hexval(c)) < (unsigned int)radix) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700515 if (!seendigit && v) {
516 int l = log2tbl[v];
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700517
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700518 seendigit = true;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700519 mp = mult;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700520 ms = (LIMB_BITS-1)-l;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700521
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700522 twopwr = seendot ? twopwr-bits+l : l+1-bits;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700523 }
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700524
H. Peter Anvin214f5492007-10-15 19:46:32 -0700525 if (seendigit) {
526 if (ms <= 0) {
527 *mp |= v >> -ms;
528 mp++;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700529 if (mp > &mult[MANT_LIMBS])
530 mp = &mult[MANT_LIMBS]; /* Guard slot */
531 ms += LIMB_BITS;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700532 }
533 *mp |= v << ms;
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700534 ms -= bits;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700535
H. Peter Anvin214f5492007-10-15 19:46:32 -0700536 if (!seendot)
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700537 twopwr += bits;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700538 } else {
539 if (seendot)
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700540 twopwr -= bits;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700541 }
542 } else if (c == 'p' || c == 'P') {
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700543 int32_t e;
H. Peter Anvin3b2ad1b2007-10-21 15:32:33 -0700544 e = read_exponent(string, 20000);
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700545 if (e == INT32_MAX)
546 return false;
547 twopwr += e;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700548 break;
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700549 } else if (c == '_') {
550 /* ignore */
H. Peter Anvin214f5492007-10-15 19:46:32 -0700551 } else {
H. Peter Anvin136dcdb2007-11-12 18:25:24 -0800552 error(ERR_NONFATAL|ERR_PASS1,
H. Peter Anvin214f5492007-10-15 19:46:32 -0700553 "floating-point constant: `%c' is invalid character", c);
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700554 return false;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700555 }
556 }
557
558 if (!seendigit) {
H. Peter Anvin5aa689f2008-07-03 20:11:30 -0700559 memset(mant, 0, MANT_LIMBS*sizeof(fp_limb)); /* Zero */
H. Peter Anvin214f5492007-10-15 19:46:32 -0700560 *exponent = 0;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700561 } else {
H. Peter Anvin5aa689f2008-07-03 20:11:30 -0700562 memcpy(mant, mult, MANT_LIMBS*sizeof(fp_limb));
H. Peter Anvin214f5492007-10-15 19:46:32 -0700563 *exponent = twopwr;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700564 }
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700565
566 return true;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700567}
568
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000569/*
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700570 * Shift a mantissa to the right by i bits.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000571 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700572static void ieee_shr(fp_limb *mant, int i)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000573{
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700574 fp_limb n, m;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700575 int j = 0;
576 int sr, sl, offs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000577
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700578 sr = i % LIMB_BITS; sl = LIMB_BITS-sr;
579 offs = i/LIMB_BITS;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700580
581 if (sr == 0) {
582 if (offs)
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700583 for (j = MANT_LIMBS-1; j >= offs; j--)
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700584 mant[j] = mant[j-offs];
585 } else {
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700586 n = mant[MANT_LIMBS-1-offs] >> sr;
587 for (j = MANT_LIMBS-1; j > offs; j--) {
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700588 m = mant[j-offs-1];
589 mant[j] = (m << sl) | n;
590 n = m >> sr;
591 }
592 mant[j--] = n;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000593 }
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700594 while (j >= 0)
595 mant[j--] = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000596}
597
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700598/* Produce standard IEEE formats, with implicit or explicit integer
599 bit; this makes the following assumptions:
600
601 - the sign bit is the MSB, followed by the exponent,
602 followed by the integer bit if present.
H. Peter Anvine31747e2007-09-18 17:50:34 -0700603 - the sign bit plus exponent fit in 16 bits.
604 - the exponent bias is 2^(n-1)-1 for an n-bit exponent */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000605
H. Peter Anvine31747e2007-09-18 17:50:34 -0700606struct ieee_format {
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700607 int bytes;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700608 int mantissa; /* Fractional bits in the mantissa */
609 int explicit; /* Explicit integer */
H. Peter Anvin214f5492007-10-15 19:46:32 -0700610 int exponent; /* Bits in the exponent */
H. Peter Anvine31747e2007-09-18 17:50:34 -0700611};
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000612
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700613/*
614 * The 16- and 128-bit formats are expected to be in IEEE 754r.
615 * AMD SSE5 uses the 16-bit format.
616 *
617 * The 32- and 64-bit formats are the original IEEE 754 formats.
618 *
619 * The 80-bit format is x87-specific, but widely used.
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700620 *
621 * The 8-bit format appears to be the consensus 8-bit floating-point
622 * format. It is apparently used in graphics applications.
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700623 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700624static const struct ieee_format ieee_8 = { 1, 3, 0, 4 };
625static const struct ieee_format ieee_16 = { 2, 10, 0, 5 };
626static const struct ieee_format ieee_32 = { 4, 23, 0, 8 };
627static const struct ieee_format ieee_64 = { 8, 52, 0, 11 };
628static const struct ieee_format ieee_80 = { 10, 63, 1, 15 };
629static const struct ieee_format ieee_128 = { 16, 112, 0, 15 };
H. Peter Anvine31747e2007-09-18 17:50:34 -0700630
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700631/* Types of values we can generate */
632enum floats {
633 FL_ZERO,
634 FL_DENORMAL,
635 FL_NORMAL,
636 FL_INFINITY,
637 FL_QNAN,
638 FL_SNAN
639};
640
H. Peter Anvin63ebf162008-07-03 20:16:07 -0700641static int to_packed_bcd(const char *str, const char *p,
H. Peter Anvin45f22922008-07-03 20:12:37 -0700642 int s, uint8_t *result,
643 const struct ieee_format *fmt)
644{
645 int n = 0;
646 char c;
647 int tv = -1;
H. Peter Anvin45f22922008-07-03 20:12:37 -0700648
649 if (fmt != &ieee_80) {
650 error(ERR_NONFATAL|ERR_PASS1,
651 "packed BCD requires an 80-bit format");
652 return 0;
653 }
654
655 while (p >= str) {
656 c = *p--;
657 if (c >= '0' && c <= '9') {
658 if (tv < 0) {
659 if (n == 9) {
660 error(ERR_WARNING|ERR_PASS1,
661 "packed BCD truncated to 18 digits");
662 }
663 tv = c-'0';
664 } else {
665 if (n < 9)
666 *result++ = tv + ((c-'0') << 4);
667 n++;
668 tv = -1;
669 }
670 } else if (c == '_') {
671 /* do nothing */
672 } else {
673 error(ERR_NONFATAL|ERR_PASS1,
674 "invalid character `%c' in packed BCD constant", c);
675 return 0;
676 }
677 }
678 if (tv >= 0) {
679 if (n < 9)
680 *result++ = tv;
681 n++;
682 }
683 while (n < 9) {
684 *result++ = 0;
685 n++;
686 }
687 *result = (s < 0) ? 0x80 : 0;
688
689 return 1; /* success */
690}
691
692static int to_float(const char *str, int s, uint8_t *result,
H. Peter Anvin214f5492007-10-15 19:46:32 -0700693 const struct ieee_format *fmt)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000694{
H. Peter Anvinf7bd02a2008-01-21 16:19:52 -0800695 fp_limb mant[MANT_LIMBS];
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700696 int32_t exponent = 0;
H. Peter Anvin45f22922008-07-03 20:12:37 -0700697 const int32_t expmax = 1 << (fmt->exponent - 1);
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700698 fp_limb one_mask = LIMB_TOP_BIT >>
699 ((fmt->exponent+fmt->explicit) % LIMB_BITS);
H. Peter Anvin45f22922008-07-03 20:12:37 -0700700 const int one_pos = (fmt->exponent+fmt->explicit)/LIMB_BITS;
H. Peter Anvine31747e2007-09-18 17:50:34 -0700701 int i;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700702 int shift;
703 enum floats type;
704 bool ok;
H. Peter Anvin45f22922008-07-03 20:12:37 -0700705 const bool minus = s < 0;
706 const int bits = fmt->bytes * 8;
707 const char *strend;
708
709 if (!str[0]) {
710 error(ERR_PANIC,
711 "internal errror: empty string passed to float_const");
712 return 0;
713 }
714
715 strend = strchr(str, '\0');
716 if (strend[-1] == 'P' || strend[-1] == 'p')
H. Peter Anvin63ebf162008-07-03 20:16:07 -0700717 return to_packed_bcd(str, strend-2, s, result, fmt);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000718
H. Peter Anvinf48bc6f2007-09-18 21:55:56 -0700719 if (str[0] == '_') {
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700720 /* Special tokens */
H. Peter Anvine31747e2007-09-18 17:50:34 -0700721
H. Peter Anvin214f5492007-10-15 19:46:32 -0700722 switch (str[2]) {
723 case 'n': /* __nan__ */
724 case 'N':
725 case 'q': /* __qnan__ */
726 case 'Q':
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700727 type = FL_QNAN;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700728 break;
729 case 's': /* __snan__ */
730 case 'S':
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700731 type = FL_SNAN;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700732 break;
733 case 'i': /* __infinity__ */
734 case 'I':
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700735 type = FL_INFINITY;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700736 break;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700737 default:
H. Peter Anvin136dcdb2007-11-12 18:25:24 -0800738 error(ERR_NONFATAL|ERR_PASS1,
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700739 "internal error: unknown FP constant token `%s'\n", str);
740 type = FL_QNAN;
741 break;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700742 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000743 } else {
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700744 if (str[0] == '0') {
745 switch (str[1]) {
746 case 'x': case 'X':
747 case 'h': case 'H':
748 ok = ieee_flconvert_bin(str+2, 4, mant, &exponent);
749 break;
750 case 'o': case 'O':
751 case 'q': case 'Q':
752 ok = ieee_flconvert_bin(str+2, 3, mant, &exponent);
753 break;
754 case 'b': case 'B':
755 case 'y': case 'Y':
756 ok = ieee_flconvert_bin(str+2, 1, mant, &exponent);
757 break;
758 case 'd': case 'D':
759 case 't': case 'T':
760 ok = ieee_flconvert(str+2, mant, &exponent);
761 break;
H. Peter Anvin63ebf162008-07-03 20:16:07 -0700762 case 'p': case 'P':
763 return to_packed_bcd(str+2, strend-1, s, result, fmt);
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700764 default:
H. Peter Anvinf41aef22007-10-22 19:37:36 -0700765 /* Leading zero was just a zero? */
766 ok = ieee_flconvert(str, mant, &exponent);
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700767 break;
768 }
769 } else if (str[0] == '$') {
770 ok = ieee_flconvert_bin(str+1, 4, mant, &exponent);
771 } else {
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700772 ok = ieee_flconvert(str, mant, &exponent);
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700773 }
H. Peter Anvin214f5492007-10-15 19:46:32 -0700774
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700775 if (!ok) {
776 type = FL_QNAN;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700777 } else if (mant[0] & LIMB_TOP_BIT) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700778 /*
779 * Non-zero.
780 */
781 exponent--;
782 if (exponent >= 2 - expmax && exponent <= expmax) {
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700783 type = FL_NORMAL;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700784 } else if (exponent > 0) {
H. Peter Anvinfab3a6c2007-10-16 11:48:07 -0700785 if (pass0 == 1)
H. Peter Anvin136dcdb2007-11-12 18:25:24 -0800786 error(ERR_WARNING|ERR_WARN_FL_OVERFLOW|ERR_PASS1,
H. Peter Anvinfab3a6c2007-10-16 11:48:07 -0700787 "overflow in floating-point constant");
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700788 type = FL_INFINITY;
789 } else {
H. Peter Anvin4a63c202007-10-30 01:13:09 -0700790 /* underflow or denormal; the denormal code handles
791 actual underflow. */
792 type = FL_DENORMAL;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700793 }
794 } else {
795 /* Zero */
796 type = FL_ZERO;
797 }
798 }
H. Peter Anvin214f5492007-10-15 19:46:32 -0700799
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700800 switch (type) {
801 case FL_ZERO:
H. Peter Anvin125c8782007-10-16 11:32:58 -0700802 zero:
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700803 memset(mant, 0, sizeof mant);
804 break;
805
806 case FL_DENORMAL:
807 {
808 shift = -(exponent + expmax - 2 - fmt->exponent)
809 + fmt->explicit;
810 ieee_shr(mant, shift);
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700811 ieee_round(minus, mant, bits);
H. Peter Anvin125c8782007-10-16 11:32:58 -0700812 if (mant[one_pos] & one_mask) {
813 /* One's position is set, we rounded up into normal range */
814 exponent = 1;
815 if (!fmt->explicit)
816 mant[one_pos] &= ~one_mask; /* remove explicit one */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700817 mant[0] |= exponent << (LIMB_BITS-1 - fmt->exponent);
H. Peter Anvinfab3a6c2007-10-16 11:48:07 -0700818 } else {
819 if (daz || is_zero(mant)) {
820 /* Flush denormals to zero */
H. Peter Anvin136dcdb2007-11-12 18:25:24 -0800821 error(ERR_WARNING|ERR_WARN_FL_UNDERFLOW|ERR_PASS1,
822 "underflow in floating-point constant");
H. Peter Anvinfab3a6c2007-10-16 11:48:07 -0700823 goto zero;
824 } else {
H. Peter Anvin136dcdb2007-11-12 18:25:24 -0800825 error(ERR_WARNING|ERR_WARN_FL_DENORM|ERR_PASS1,
826 "denormal floating-point constant");
H. Peter Anvinfab3a6c2007-10-16 11:48:07 -0700827 }
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700828 }
829 break;
830 }
831
832 case FL_NORMAL:
833 exponent += expmax - 1;
834 ieee_shr(mant, fmt->exponent+fmt->explicit);
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700835 ieee_round(minus, mant, bits);
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700836 /* did we scale up by one? */
837 if (test_bit(mant, fmt->exponent+fmt->explicit-1)) {
838 ieee_shr(mant, 1);
839 exponent++;
H. Peter Anvinfab3a6c2007-10-16 11:48:07 -0700840 if (exponent >= (expmax << 1)-1) {
H. Peter Anvin136dcdb2007-11-12 18:25:24 -0800841 error(ERR_WARNING|ERR_WARN_FL_OVERFLOW|ERR_PASS1,
H. Peter Anvinfab3a6c2007-10-16 11:48:07 -0700842 "overflow in floating-point constant");
H. Peter Anvin125c8782007-10-16 11:32:58 -0700843 type = FL_INFINITY;
844 goto overflow;
845 }
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700846 }
H. Peter Anvin70653092007-10-19 14:42:29 -0700847
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700848 if (!fmt->explicit)
849 mant[one_pos] &= ~one_mask; /* remove explicit one */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700850 mant[0] |= exponent << (LIMB_BITS-1 - fmt->exponent);
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700851 break;
852
853 case FL_INFINITY:
854 case FL_QNAN:
855 case FL_SNAN:
H. Peter Anvin125c8782007-10-16 11:32:58 -0700856 overflow:
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700857 memset(mant, 0, sizeof mant);
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700858 mant[0] = (((fp_limb)1 << fmt->exponent)-1)
859 << (LIMB_BITS-1 - fmt->exponent);
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700860 if (fmt->explicit)
861 mant[one_pos] |= one_mask;
862 if (type == FL_QNAN)
863 set_bit(mant, fmt->exponent+fmt->explicit+1);
864 else if (type == FL_SNAN)
865 set_bit(mant, fmt->exponent+fmt->explicit+fmt->mantissa);
866 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000867 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000868
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700869 mant[0] |= minus ? LIMB_TOP_BIT : 0;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700870
H. Peter Anvinf7bd02a2008-01-21 16:19:52 -0800871 for (i = fmt->bytes - 1; i >= 0; i--)
H. Peter Anvin5a99f4f2008-01-25 08:11:23 -0800872 *result++ = mant[i/LIMB_BYTES] >> (((LIMB_BYTES-1)-(i%LIMB_BYTES))*8);
H. Peter Anvine31747e2007-09-18 17:50:34 -0700873
874 return 1; /* success */
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700875}
876
H. Peter Anvin45f22922008-07-03 20:12:37 -0700877int float_const(const char *number, int sign, uint8_t *result,
H. Peter Anvin214f5492007-10-15 19:46:32 -0700878 int bytes, efunc err)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000879{
H. Peter Anvin214f5492007-10-15 19:46:32 -0700880 error = err;
881
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700882 switch (bytes) {
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700883 case 1:
884 return to_float(number, sign, result, &ieee_8);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700885 case 2:
H. Peter Anvin214f5492007-10-15 19:46:32 -0700886 return to_float(number, sign, result, &ieee_16);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700887 case 4:
H. Peter Anvin214f5492007-10-15 19:46:32 -0700888 return to_float(number, sign, result, &ieee_32);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700889 case 8:
H. Peter Anvin214f5492007-10-15 19:46:32 -0700890 return to_float(number, sign, result, &ieee_64);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700891 case 10:
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700892 return to_float(number, sign, result, &ieee_80);
H. Peter Anvine31747e2007-09-18 17:50:34 -0700893 case 16:
H. Peter Anvin214f5492007-10-15 19:46:32 -0700894 return to_float(number, sign, result, &ieee_128);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700895 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000896 error(ERR_PANIC, "strange value %d passed to float_const", bytes);
897 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000898 }
899}
H. Peter Anvinf6c9e652007-10-16 14:40:27 -0700900
901/* Set floating-point options */
902int float_option(const char *option)
903{
904 if (!nasm_stricmp(option, "daz")) {
905 daz = true;
906 return 0;
907 } else if (!nasm_stricmp(option, "nodaz")) {
908 daz = false;
909 return 0;
910 } else if (!nasm_stricmp(option, "near")) {
911 rc = FLOAT_RC_NEAR;
912 return 0;
913 } else if (!nasm_stricmp(option, "down")) {
914 rc = FLOAT_RC_DOWN;
915 return 0;
916 } else if (!nasm_stricmp(option, "up")) {
917 rc = FLOAT_RC_UP;
918 return 0;
919 } else if (!nasm_stricmp(option, "zero")) {
920 rc = FLOAT_RC_ZERO;
921 return 0;
922 } else if (!nasm_stricmp(option, "default")) {
923 rc = FLOAT_RC_NEAR;
924 daz = false;
925 return 0;
926 } else {
927 return -1; /* Unknown option */
928 }
929}