blob: adc6afbf0b7529fa2f48f123a1faf1c873a955c6 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunov1598a232009-10-11 13:17:52 +04002 *
H. Peter Anvin79a070e2018-11-26 14:17:40 -08003 * Copyright 1996-2018 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000010 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070011 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunov1598a232009-10-11 13:17:52 +040017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * float.c floating-point constant support for the Netwide Assembler
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 */
37
H. Peter Anvinfe501952007-10-02 21:53:51 -070038#include "compiler.h"
39
H. Peter Anvinc2f3f262018-12-27 12:37:25 -080040#include "nctype.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000041
42#include "nasm.h"
H. Peter Anvin (Intel)6e9554f2020-06-14 23:21:44 -070043#include "floats.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080044#include "error.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000045
46/*
H. Peter Anvin214f5492007-10-15 19:46:32 -070047 * -----------------
48 * local variables
49 * -----------------
50 */
H. Peter Anvin214f5492007-10-15 19:46:32 -070051static bool daz = false; /* denormals as zero */
52static enum float_round rc = FLOAT_RC_NEAR; /* rounding control */
53
54/*
55 * -----------
56 * constants
57 * -----------
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000058 */
59
H. Peter Anvin2ce02742007-10-29 20:20:12 -070060/* "A limb is like a digit but bigger */
61typedef uint32_t fp_limb;
62typedef uint64_t fp_2limb;
63
Cyrill Gorcunov1598a232009-10-11 13:17:52 +040064#define LIMB_BITS 32
H. Peter Anvin2ce02742007-10-29 20:20:12 -070065#define LIMB_BYTES (LIMB_BITS/8)
Cyrill Gorcunov1598a232009-10-11 13:17:52 +040066#define LIMB_TOP_BIT ((fp_limb)1 << (LIMB_BITS-1))
67#define LIMB_MASK ((fp_limb)(~0))
68#define LIMB_ALL_BYTES ((fp_limb)0x01010101)
69#define LIMB_BYTE(x) ((x)*LIMB_ALL_BYTES)
H. Peter Anvin2ce02742007-10-29 20:20:12 -070070
H. Peter Anvin214f5492007-10-15 19:46:32 -070071/* 112 bits + 64 bits for accuracy + 16 bits for rounding */
H. Peter Anvin2ce02742007-10-29 20:20:12 -070072#define MANT_LIMBS 6
H. Peter Anvin214f5492007-10-15 19:46:32 -070073
74/* 52 digits fit in 176 bits because 10^53 > 2^176 > 10^52 */
75#define MANT_DIGITS 52
76
H. Peter Anvin2ce02742007-10-29 20:20:12 -070077/* the format and the argument list depend on MANT_LIMBS */
78#define MANT_FMT "%08x_%08x_%08x_%08x_%08x_%08x"
H. Peter Anvin214f5492007-10-15 19:46:32 -070079#define MANT_ARG SOME_ARG(mant, 0)
80
Cyrill Gorcunov1598a232009-10-11 13:17:52 +040081#define SOME_ARG(a,i) (a)[(i)+0], (a)[(i)+1], (a)[(i)+2], \
82 (a)[(i)+3], (a)[(i)+4], (a)[(i)+5]
H. Peter Anvin214f5492007-10-15 19:46:32 -070083
84/*
85 * ---------------------------------------------------------------------------
86 * emit a printf()-like debug message... but only if DEBUG_FLOAT was defined
87 * ---------------------------------------------------------------------------
88 */
89
90#ifdef DEBUG_FLOAT
91#define dprintf(x) printf x
Cyrill Gorcunov1598a232009-10-11 13:17:52 +040092#else
H. Peter Anvin214f5492007-10-15 19:46:32 -070093#define dprintf(x) do { } while (0)
Cyrill Gorcunov1598a232009-10-11 13:17:52 +040094#endif
H. Peter Anvin214f5492007-10-15 19:46:32 -070095
96/*
97 * ---------------------------------------------------------------------------
98 * multiply
99 * ---------------------------------------------------------------------------
100 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700101static int float_multiply(fp_limb *to, fp_limb *from)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000102{
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700103 fp_2limb temp[MANT_LIMBS * 2];
104 int i, j;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000105
H. Peter Anvin70653092007-10-19 14:42:29 -0700106 /*
H. Peter Anvin214f5492007-10-15 19:46:32 -0700107 * guaranteed that top bit of 'from' is set -- so we only have
108 * to worry about _one_ bit shift to the left
109 */
110 dprintf(("%s=" MANT_FMT "\n", "mul1", SOME_ARG(to, 0)));
111 dprintf(("%s=" MANT_FMT "\n", "mul2", SOME_ARG(from, 0)));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000112
H. Peter Anvin214f5492007-10-15 19:46:32 -0700113 memset(temp, 0, sizeof temp);
114
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700115 for (i = 0; i < MANT_LIMBS; i++) {
116 for (j = 0; j < MANT_LIMBS; j++) {
117 fp_2limb n;
118 n = (fp_2limb) to[i] * (fp_2limb) from[j];
119 temp[i + j] += n >> LIMB_BITS;
120 temp[i + j + 1] += (fp_limb)n;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000121 }
H. Peter Anvin214f5492007-10-15 19:46:32 -0700122 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000123
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700124 for (i = MANT_LIMBS * 2; --i;) {
125 temp[i - 1] += temp[i] >> LIMB_BITS;
126 temp[i] &= LIMB_MASK;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000127 }
H. Peter Anvin214f5492007-10-15 19:46:32 -0700128
129 dprintf(("%s=" MANT_FMT "_" MANT_FMT "\n", "temp", SOME_ARG(temp, 0),
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700130 SOME_ARG(temp, MANT_LIMBS)));
H. Peter Anvin214f5492007-10-15 19:46:32 -0700131
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700132 if (temp[0] & LIMB_TOP_BIT) {
133 for (i = 0; i < MANT_LIMBS; i++) {
134 to[i] = temp[i] & LIMB_MASK;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700135 }
136 dprintf(("%s=" MANT_FMT " (%i)\n", "prod", SOME_ARG(to, 0), 0));
137 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000138 } else {
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700139 for (i = 0; i < MANT_LIMBS; i++) {
140 to[i] = (temp[i] << 1) + !!(temp[i + 1] & LIMB_TOP_BIT);
H. Peter Anvin214f5492007-10-15 19:46:32 -0700141 }
142 dprintf(("%s=" MANT_FMT " (%i)\n", "prod", SOME_ARG(to, 0), -1));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000143 return -1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000144 }
145}
146
H. Peter Anvin214f5492007-10-15 19:46:32 -0700147/*
148 * ---------------------------------------------------------------------------
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700149 * read an exponent; returns INT32_MAX on error
150 * ---------------------------------------------------------------------------
151 */
H. Peter Anvin3514ad02007-10-19 14:17:51 -0700152static int32_t read_exponent(const char *string, int32_t max)
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700153{
154 int32_t i = 0;
155 bool neg = false;
H. Peter Anvin70653092007-10-19 14:42:29 -0700156
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700157 if (*string == '+') {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400158 string++;
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700159 } else if (*string == '-') {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400160 neg = true;
161 string++;
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700162 }
163 while (*string) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400164 if (*string >= '0' && *string <= '9') {
165 i = (i * 10) + (*string - '0');
H. Peter Anvin70653092007-10-19 14:42:29 -0700166
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400167 /*
168 * To ensure that underflows and overflows are
169 * handled properly we must avoid wraparounds of
170 * the signed integer value that is used to hold
171 * the exponent. Therefore we cap the exponent at
172 * +/-5000, which is slightly more/less than
173 * what's required for normal and denormal numbers
174 * in single, double, and extended precision, but
175 * sufficient to avoid signed integer wraparound.
176 */
177 if (i > max)
178 i = max;
179 } else if (*string == '_') {
180 /* do nothing */
181 } else {
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300182 nasm_nonfatal("invalid character in floating-point constant %s: '%c'",
183 "exponent", *string);
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400184 return INT32_MAX;
185 }
186 string++;
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700187 }
188
189 return neg ? -i : i;
190}
191
192/*
193 * ---------------------------------------------------------------------------
H. Peter Anvin214f5492007-10-15 19:46:32 -0700194 * convert
195 * ---------------------------------------------------------------------------
196 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700197static bool ieee_flconvert(const char *string, fp_limb *mant,
H. Peter Anvin214f5492007-10-15 19:46:32 -0700198 int32_t * exponent)
199{
200 char digits[MANT_DIGITS];
201 char *p, *q, *r;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700202 fp_limb mult[MANT_LIMBS], bit;
203 fp_limb *m;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700204 int32_t tenpwr, twopwr;
205 int32_t extratwos;
206 bool started, seendot, warned;
H. Peter Anvin136dcdb2007-11-12 18:25:24 -0800207
208 warned = false;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700209 p = digits;
210 tenpwr = 0;
H. Peter Anvinfab3a6c2007-10-16 11:48:07 -0700211 started = seendot = false;
H. Peter Anvin136dcdb2007-11-12 18:25:24 -0800212
H. Peter Anvin214f5492007-10-15 19:46:32 -0700213 while (*string && *string != 'E' && *string != 'e') {
214 if (*string == '.') {
215 if (!seendot) {
216 seendot = true;
217 } else {
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300218 nasm_nonfatal("too many periods in floating-point constant");
H. Peter Anvin214f5492007-10-15 19:46:32 -0700219 return false;
220 }
221 } else if (*string >= '0' && *string <= '9') {
222 if (*string == '0' && !started) {
223 if (seendot) {
224 tenpwr--;
225 }
226 } else {
227 started = true;
228 if (p < digits + sizeof(digits)) {
229 *p++ = *string - '0';
230 } else {
231 if (!warned) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800232 /*!
233 *!float-toolong [on] too many digits in floating-point number
234 *! warns about too many digits in floating-point numbers.
235 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800236 nasm_warn(WARN_FLOAT_TOOLONG|ERR_PASS2,
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300237 "floating-point constant significand contains "
238 "more than %i digits", MANT_DIGITS);
H. Peter Anvin214f5492007-10-15 19:46:32 -0700239 warned = true;
240 }
241 }
242 if (!seendot) {
243 tenpwr++;
244 }
245 }
246 } else if (*string == '_') {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700247 /* do nothing */
248 } else {
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300249 nasm_nonfatalf(ERR_PASS2,
250 "invalid character in floating-point constant %s: '%c'",
251 "significand", *string);
H. Peter Anvin214f5492007-10-15 19:46:32 -0700252 return false;
253 }
254 string++;
255 }
H. Peter Anvin70653092007-10-19 14:42:29 -0700256
H. Peter Anvin214f5492007-10-15 19:46:32 -0700257 if (*string) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400258 int32_t e;
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700259
H. Peter Anvin214f5492007-10-15 19:46:32 -0700260 string++; /* eat the E */
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400261 e = read_exponent(string, 5000);
262 if (e == INT32_MAX)
263 return false;
264 tenpwr += e;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700265 }
266
267 /*
268 * At this point, the memory interval [digits,p) contains a
269 * series of decimal digits zzzzzzz, such that our number X
270 * satisfies X = 0.zzzzzzz * 10^tenpwr.
271 */
272 q = digits;
273 dprintf(("X = 0."));
274 while (q < p) {
275 dprintf(("%c", *q + '0'));
276 q++;
277 }
278 dprintf((" * 10^%i\n", tenpwr));
279
280 /*
281 * Now convert [digits,p) to our internal representation.
282 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700283 bit = LIMB_TOP_BIT;
284 for (m = mant; m < mant + MANT_LIMBS; m++) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700285 *m = 0;
286 }
287 m = mant;
288 q = digits;
289 started = false;
290 twopwr = 0;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700291 while (m < mant + MANT_LIMBS) {
292 fp_limb carry = 0;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700293 while (p > q && !p[-1]) {
294 p--;
295 }
296 if (p <= q) {
297 break;
298 }
299 for (r = p; r-- > q;) {
300 int32_t i;
301 i = 2 * *r + carry;
302 if (i >= 10) {
303 carry = 1;
304 i -= 10;
305 } else {
306 carry = 0;
307 }
308 *r = i;
309 }
310 if (carry) {
311 *m |= bit;
312 started = true;
313 }
314 if (started) {
315 if (bit == 1) {
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700316 bit = LIMB_TOP_BIT;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700317 m++;
318 } else {
319 bit >>= 1;
320 }
321 } else {
322 twopwr--;
323 }
324 }
325 twopwr += tenpwr;
326
327 /*
328 * At this point, the 'mant' array contains the first frac-
329 * tional places of a base-2^16 real number which when mul-
330 * tiplied by 2^twopwr and 5^tenpwr gives X.
331 */
332 dprintf(("X = " MANT_FMT " * 2^%i * 5^%i\n", MANT_ARG, twopwr,
333 tenpwr));
334
335 /*
336 * Now multiply 'mant' by 5^tenpwr.
337 */
338 if (tenpwr < 0) { /* mult = 5^-1 = 0.2 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700339 for (m = mult; m < mult + MANT_LIMBS - 1; m++) {
340 *m = LIMB_BYTE(0xcc);
H. Peter Anvin214f5492007-10-15 19:46:32 -0700341 }
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700342 mult[MANT_LIMBS - 1] = LIMB_BYTE(0xcc)+1;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700343 extratwos = -2;
344 tenpwr = -tenpwr;
345
346 /*
347 * If tenpwr was 1000...000b, then it becomes 1000...000b. See
348 * the "ANSI C" comment below for more details on that case.
349 *
350 * Because we already truncated tenpwr to +5000...-5000 inside
351 * the exponent parsing code, this shouldn't happen though.
352 */
353 } else if (tenpwr > 0) { /* mult = 5^+1 = 5.0 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700354 mult[0] = (fp_limb)5 << (LIMB_BITS-3); /* 0xA000... */
355 for (m = mult + 1; m < mult + MANT_LIMBS; m++) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700356 *m = 0;
357 }
358 extratwos = 3;
359 } else {
360 extratwos = 0;
361 }
362 while (tenpwr) {
363 dprintf(("loop=" MANT_FMT " * 2^%i * 5^%i (%i)\n", MANT_ARG,
364 twopwr, tenpwr, extratwos));
365 if (tenpwr & 1) {
366 dprintf(("mant*mult\n"));
367 twopwr += extratwos + float_multiply(mant, mult);
368 }
369 dprintf(("mult*mult\n"));
370 extratwos = extratwos * 2 + float_multiply(mult, mult);
371 tenpwr >>= 1;
372
373 /*
374 * In ANSI C, the result of right-shifting a signed integer is
375 * considered implementation-specific. To ensure that the loop
376 * terminates even if tenpwr was 1000...000b to begin with, we
377 * manually clear the MSB, in case a 1 was shifted in.
378 *
379 * Because we already truncated tenpwr to +5000...-5000 inside
380 * the exponent parsing code, this shouldn't matter; neverthe-
381 * less it is the right thing to do here.
382 */
383 tenpwr &= (uint32_t) - 1 >> 1;
384 }
385
386 /*
387 * At this point, the 'mant' array contains the first frac-
388 * tional places of a base-2^16 real number in [0.5,1) that
389 * when multiplied by 2^twopwr gives X. Or it contains zero
390 * of course. We are done.
391 */
392 *exponent = twopwr;
393 return true;
394}
395
396/*
397 * ---------------------------------------------------------------------------
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700398 * operations of specific bits
399 * ---------------------------------------------------------------------------
400 */
401
402/* Set a bit, using *bigendian* bit numbering (0 = MSB) */
403static void set_bit(fp_limb *mant, int bit)
404{
405 mant[bit/LIMB_BITS] |= LIMB_TOP_BIT >> (bit & (LIMB_BITS-1));
406}
407
408/* Test a single bit */
409static int test_bit(const fp_limb *mant, int bit)
410{
411 return (mant[bit/LIMB_BITS] >> (~bit & (LIMB_BITS-1))) & 1;
412}
413
414/* Report if the mantissa value is all zero */
415static bool is_zero(const fp_limb *mant)
416{
417 int i;
418
419 for (i = 0; i < MANT_LIMBS; i++)
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400420 if (mant[i])
421 return false;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700422
423 return true;
424}
425
426/*
427 * ---------------------------------------------------------------------------
H. Peter Anvin214f5492007-10-15 19:46:32 -0700428 * round a mantissa off after i words
429 * ---------------------------------------------------------------------------
430 */
431
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400432#define ROUND_COLLECT_BITS \
433 do { \
434 m = mant[i] & (2*bit-1); \
435 for (j = i+1; j < MANT_LIMBS; j++) \
436 m = m | mant[j]; \
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700437 } while (0)
H. Peter Anvin214f5492007-10-15 19:46:32 -0700438
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400439#define ROUND_ABS_DOWN \
440 do { \
441 mant[i] &= ~(bit-1); \
442 for (j = i+1; j < MANT_LIMBS; j++) \
443 mant[j] = 0; \
444 return false; \
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700445 } while (0)
H. Peter Anvin214f5492007-10-15 19:46:32 -0700446
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400447#define ROUND_ABS_UP \
448 do { \
449 mant[i] = (mant[i] & ~(bit-1)) + bit; \
450 for (j = i+1; j < MANT_LIMBS; j++) \
451 mant[j] = 0; \
452 while (i > 0 && !mant[i]) \
453 ++mant[--i]; \
454 return !mant[0]; \
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700455 } while (0)
H. Peter Anvin214f5492007-10-15 19:46:32 -0700456
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700457static bool ieee_round(bool minus, fp_limb *mant, int bits)
H. Peter Anvin214f5492007-10-15 19:46:32 -0700458{
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700459 fp_limb m = 0;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700460 int32_t j;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700461 int i = bits / LIMB_BITS;
462 int p = bits % LIMB_BITS;
463 fp_limb bit = LIMB_TOP_BIT >> p;
464
465 if (rc == FLOAT_RC_NEAR) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400466 if (mant[i] & bit) {
467 mant[i] &= ~bit;
468 ROUND_COLLECT_BITS;
469 mant[i] |= bit;
470 if (m) {
471 ROUND_ABS_UP;
472 } else {
473 if (test_bit(mant, bits-1)) {
474 ROUND_ABS_UP;
475 } else {
476 ROUND_ABS_DOWN;
477 }
478 }
479 } else {
480 ROUND_ABS_DOWN;
481 }
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700482 } else if (rc == FLOAT_RC_ZERO ||
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400483 rc == (minus ? FLOAT_RC_UP : FLOAT_RC_DOWN)) {
484 ROUND_ABS_DOWN;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700485 } else {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400486 /* rc == (minus ? FLOAT_RC_DOWN : FLOAT_RC_UP) */
487 /* Round toward +/- infinity */
488 ROUND_COLLECT_BITS;
489 if (m) {
490 ROUND_ABS_UP;
491 } else {
492 ROUND_ABS_DOWN;
493 }
H. Peter Anvin214f5492007-10-15 19:46:32 -0700494 }
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700495 return false;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700496}
497
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700498/* Returns a value >= 16 if not a valid hex digit */
499static unsigned int hexval(char c)
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700500{
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700501 unsigned int v = (unsigned char) c;
502
503 if (v >= '0' && v <= '9')
504 return v - '0';
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700505 else
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700506 return (v|0x20) - 'a' + 10;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700507}
508
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700509/* Handle floating-point numbers with radix 2^bits and binary exponent */
510static bool ieee_flconvert_bin(const char *string, int bits,
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400511 fp_limb *mant, int32_t *exponent)
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700512{
513 static const int log2tbl[16] =
H. Peter Anvin214f5492007-10-15 19:46:32 -0700514 { -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3 };
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700515 fp_limb mult[MANT_LIMBS + 1], *mp;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700516 int ms;
517 int32_t twopwr;
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700518 bool seendot, seendigit;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700519 unsigned char c;
H. Peter Anvin45f22922008-07-03 20:12:37 -0700520 const int radix = 1 << bits;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700521 fp_limb v;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700522
523 twopwr = 0;
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700524 seendot = seendigit = false;
H. Peter Anvin82f9f632007-09-24 20:53:48 -0700525 ms = 0;
526 mp = NULL;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700527
528 memset(mult, 0, sizeof mult);
529
530 while ((c = *string++) != '\0') {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700531 if (c == '.') {
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700532 if (!seendot)
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700533 seendot = true;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700534 else {
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300535 nasm_nonfatal("too many periods in floating-point constant");
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700536 return false;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700537 }
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700538 } else if ((v = hexval(c)) < (unsigned int)radix) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700539 if (!seendigit && v) {
540 int l = log2tbl[v];
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700541
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700542 seendigit = true;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700543 mp = mult;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700544 ms = (LIMB_BITS-1)-l;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700545
H. Peter Anvin3646e7d2017-04-05 21:47:20 -0700546 twopwr += l+1-bits;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700547 }
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700548
H. Peter Anvin214f5492007-10-15 19:46:32 -0700549 if (seendigit) {
H. Peter Anvin79a070e2018-11-26 14:17:40 -0800550 if (ms < 0) {
551 /* Cast to fp_2limb as ms == -LIMB_BITS is possible. */
552 *mp |= (fp_2limb)v >> -ms;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700553 mp++;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700554 if (mp > &mult[MANT_LIMBS])
555 mp = &mult[MANT_LIMBS]; /* Guard slot */
556 ms += LIMB_BITS;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700557 }
H. Peter Anvin79a070e2018-11-26 14:17:40 -0800558 *mp |= v << ms;
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700559 ms -= bits;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700560
H. Peter Anvin214f5492007-10-15 19:46:32 -0700561 if (!seendot)
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700562 twopwr += bits;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700563 } else {
564 if (seendot)
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700565 twopwr -= bits;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700566 }
567 } else if (c == 'p' || c == 'P') {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400568 int32_t e;
569 e = read_exponent(string, 20000);
570 if (e == INT32_MAX)
571 return false;
572 twopwr += e;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700573 break;
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400574 } else if (c == '_') {
575 /* ignore */
H. Peter Anvin214f5492007-10-15 19:46:32 -0700576 } else {
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300577 nasm_nonfatal("floating-point constant: `%c' is invalid character", c);
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700578 return false;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700579 }
580 }
581
582 if (!seendigit) {
H. Peter Anvin5aa689f2008-07-03 20:11:30 -0700583 memset(mant, 0, MANT_LIMBS*sizeof(fp_limb)); /* Zero */
H. Peter Anvin214f5492007-10-15 19:46:32 -0700584 *exponent = 0;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700585 } else {
H. Peter Anvin5aa689f2008-07-03 20:11:30 -0700586 memcpy(mant, mult, MANT_LIMBS*sizeof(fp_limb));
H. Peter Anvin214f5492007-10-15 19:46:32 -0700587 *exponent = twopwr;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700588 }
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700589
590 return true;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700591}
592
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000593/*
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700594 * Shift a mantissa to the right by i bits.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000595 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700596static void ieee_shr(fp_limb *mant, int i)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000597{
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700598 fp_limb n, m;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700599 int j = 0;
600 int sr, sl, offs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000601
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700602 sr = i % LIMB_BITS; sl = LIMB_BITS-sr;
603 offs = i/LIMB_BITS;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700604
605 if (sr == 0) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400606 if (offs)
607 for (j = MANT_LIMBS-1; j >= offs; j--)
608 mant[j] = mant[j-offs];
Adam Majerc7c28352018-07-05 17:40:24 +0200609 } else if (MANT_LIMBS-1-offs < 0) {
610 j = MANT_LIMBS-1;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700611 } else {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400612 n = mant[MANT_LIMBS-1-offs] >> sr;
613 for (j = MANT_LIMBS-1; j > offs; j--) {
614 m = mant[j-offs-1];
615 mant[j] = (m << sl) | n;
616 n = m >> sr;
617 }
618 mant[j--] = n;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000619 }
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700620 while (j >= 0)
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400621 mant[j--] = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000622}
623
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700624/* Produce standard IEEE formats, with implicit or explicit integer
625 bit; this makes the following assumptions:
626
627 - the sign bit is the MSB, followed by the exponent,
628 followed by the integer bit if present.
H. Peter Anvine31747e2007-09-18 17:50:34 -0700629 - the sign bit plus exponent fit in 16 bits.
630 - the exponent bias is 2^(n-1)-1 for an n-bit exponent */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000631
H. Peter Anvine31747e2007-09-18 17:50:34 -0700632struct ieee_format {
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700633 int bytes;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700634 int mantissa; /* Fractional bits in the mantissa */
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400635 int explicit; /* Explicit integer */
H. Peter Anvin214f5492007-10-15 19:46:32 -0700636 int exponent; /* Bits in the exponent */
H. Peter Anvine31747e2007-09-18 17:50:34 -0700637};
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000638
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700639/*
640 * The 16- and 128-bit formats are expected to be in IEEE 754r.
641 * AMD SSE5 uses the 16-bit format.
642 *
643 * The 32- and 64-bit formats are the original IEEE 754 formats.
644 *
645 * The 80-bit format is x87-specific, but widely used.
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700646 *
647 * The 8-bit format appears to be the consensus 8-bit floating-point
648 * format. It is apparently used in graphics applications.
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700649 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700650static const struct ieee_format ieee_8 = { 1, 3, 0, 4 };
651static const struct ieee_format ieee_16 = { 2, 10, 0, 5 };
652static const struct ieee_format ieee_32 = { 4, 23, 0, 8 };
653static const struct ieee_format ieee_64 = { 8, 52, 0, 11 };
654static const struct ieee_format ieee_80 = { 10, 63, 1, 15 };
655static const struct ieee_format ieee_128 = { 16, 112, 0, 15 };
H. Peter Anvine31747e2007-09-18 17:50:34 -0700656
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700657/* Types of values we can generate */
658enum floats {
659 FL_ZERO,
660 FL_DENORMAL,
661 FL_NORMAL,
662 FL_INFINITY,
663 FL_QNAN,
664 FL_SNAN
665};
666
H. Peter Anvin63ebf162008-07-03 20:16:07 -0700667static int to_packed_bcd(const char *str, const char *p,
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400668 int s, uint8_t *result,
669 const struct ieee_format *fmt)
H. Peter Anvin45f22922008-07-03 20:12:37 -0700670{
671 int n = 0;
672 char c;
673 int tv = -1;
H. Peter Anvin45f22922008-07-03 20:12:37 -0700674
675 if (fmt != &ieee_80) {
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300676 nasm_nonfatal("packed BCD requires an 80-bit format");
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400677 return 0;
H. Peter Anvin45f22922008-07-03 20:12:37 -0700678 }
679
680 while (p >= str) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400681 c = *p--;
682 if (c >= '0' && c <= '9') {
683 if (tv < 0) {
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300684 if (n == 9)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -0800685 nasm_warn(WARN_OTHER|ERR_PASS2, "packed BCD truncated to 18 digits");
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400686 tv = c-'0';
687 } else {
688 if (n < 9)
689 *result++ = tv + ((c-'0') << 4);
690 n++;
691 tv = -1;
692 }
693 } else if (c == '_') {
694 /* do nothing */
695 } else {
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300696 nasm_nonfatal("invalid character `%c' in packed BCD constant", c);
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400697 return 0;
698 }
H. Peter Anvin45f22922008-07-03 20:12:37 -0700699 }
700 if (tv >= 0) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400701 if (n < 9)
702 *result++ = tv;
703 n++;
H. Peter Anvin45f22922008-07-03 20:12:37 -0700704 }
705 while (n < 9) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400706 *result++ = 0;
707 n++;
H. Peter Anvin45f22922008-07-03 20:12:37 -0700708 }
709 *result = (s < 0) ? 0x80 : 0;
710
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400711 return 1; /* success */
H. Peter Anvin45f22922008-07-03 20:12:37 -0700712}
713
714static int to_float(const char *str, int s, uint8_t *result,
H. Peter Anvin214f5492007-10-15 19:46:32 -0700715 const struct ieee_format *fmt)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000716{
H. Peter Anvinf7bd02a2008-01-21 16:19:52 -0800717 fp_limb mant[MANT_LIMBS];
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700718 int32_t exponent = 0;
H. Peter Anvin45f22922008-07-03 20:12:37 -0700719 const int32_t expmax = 1 << (fmt->exponent - 1);
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700720 fp_limb one_mask = LIMB_TOP_BIT >>
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400721 ((fmt->exponent+fmt->explicit) % LIMB_BITS);
H. Peter Anvin45f22922008-07-03 20:12:37 -0700722 const int one_pos = (fmt->exponent+fmt->explicit)/LIMB_BITS;
H. Peter Anvine31747e2007-09-18 17:50:34 -0700723 int i;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700724 int shift;
725 enum floats type;
726 bool ok;
H. Peter Anvin45f22922008-07-03 20:12:37 -0700727 const bool minus = s < 0;
728 const int bits = fmt->bytes * 8;
729 const char *strend;
730
H. Peter Anvinc5136902018-06-15 18:20:17 -0700731 nasm_assert(str[0]);
H. Peter Anvin45f22922008-07-03 20:12:37 -0700732
733 strend = strchr(str, '\0');
734 if (strend[-1] == 'P' || strend[-1] == 'p')
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400735 return to_packed_bcd(str, strend-2, s, result, fmt);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000736
H. Peter Anvinf48bc6f2007-09-18 21:55:56 -0700737 if (str[0] == '_') {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400738 /* Special tokens */
H. Peter Anvine31747e2007-09-18 17:50:34 -0700739
H. Peter Anvind2354082019-08-27 16:38:48 -0700740 switch (str[3]) {
741 case 'n': /* __?nan?__ */
H. Peter Anvin214f5492007-10-15 19:46:32 -0700742 case 'N':
H. Peter Anvind2354082019-08-27 16:38:48 -0700743 case 'q': /* __?qnan?__ */
H. Peter Anvin214f5492007-10-15 19:46:32 -0700744 case 'Q':
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400745 type = FL_QNAN;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700746 break;
H. Peter Anvind2354082019-08-27 16:38:48 -0700747 case 's': /* __?snan?__ */
H. Peter Anvin214f5492007-10-15 19:46:32 -0700748 case 'S':
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400749 type = FL_SNAN;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700750 break;
H. Peter Anvind2354082019-08-27 16:38:48 -0700751 case 'i': /* __?infinity?__ */
H. Peter Anvin214f5492007-10-15 19:46:32 -0700752 case 'I':
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400753 type = FL_INFINITY;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700754 break;
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400755 default:
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300756 nasm_nonfatal("internal error: unknown FP constant token `%s'", str);
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400757 type = FL_QNAN;
758 break;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700759 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000760 } else {
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700761 if (str[0] == '0') {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400762 switch (str[1]) {
763 case 'x': case 'X':
764 case 'h': case 'H':
765 ok = ieee_flconvert_bin(str+2, 4, mant, &exponent);
766 break;
767 case 'o': case 'O':
768 case 'q': case 'Q':
769 ok = ieee_flconvert_bin(str+2, 3, mant, &exponent);
770 break;
771 case 'b': case 'B':
772 case 'y': case 'Y':
773 ok = ieee_flconvert_bin(str+2, 1, mant, &exponent);
774 break;
775 case 'd': case 'D':
776 case 't': case 'T':
777 ok = ieee_flconvert(str+2, mant, &exponent);
778 break;
779 case 'p': case 'P':
780 return to_packed_bcd(str+2, strend-1, s, result, fmt);
781 default:
782 /* Leading zero was just a zero? */
783 ok = ieee_flconvert(str, mant, &exponent);
784 break;
785 }
786 } else if (str[0] == '$') {
787 ok = ieee_flconvert_bin(str+1, 4, mant, &exponent);
788 } else {
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700789 ok = ieee_flconvert(str, mant, &exponent);
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400790 }
H. Peter Anvin214f5492007-10-15 19:46:32 -0700791
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400792 if (!ok) {
793 type = FL_QNAN;
794 } else if (mant[0] & LIMB_TOP_BIT) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700795 /*
796 * Non-zero.
797 */
798 exponent--;
799 if (exponent >= 2 - expmax && exponent <= expmax) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400800 type = FL_NORMAL;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700801 } else if (exponent > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800802 nasm_warn(WARN_FLOAT_OVERFLOW|ERR_PASS2,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800803 "overflow in floating-point constant");
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400804 type = FL_INFINITY;
805 } else {
806 /* underflow or denormal; the denormal code handles
807 actual underflow. */
808 type = FL_DENORMAL;
809 }
810 } else {
811 /* Zero */
812 type = FL_ZERO;
813 }
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700814 }
H. Peter Anvin214f5492007-10-15 19:46:32 -0700815
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700816 switch (type) {
817 case FL_ZERO:
H. Peter Anvin125c8782007-10-16 11:32:58 -0700818 zero:
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400819 memset(mant, 0, sizeof mant);
820 break;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700821
822 case FL_DENORMAL:
823 {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400824 shift = -(exponent + expmax - 2 - fmt->exponent)
825 + fmt->explicit;
826 ieee_shr(mant, shift);
827 ieee_round(minus, mant, bits);
828 if (mant[one_pos] & one_mask) {
829 /* One's position is set, we rounded up into normal range */
830 exponent = 1;
831 if (!fmt->explicit)
832 mant[one_pos] &= ~one_mask; /* remove explicit one */
833 mant[0] |= exponent << (LIMB_BITS-1 - fmt->exponent);
834 } else {
835 if (daz || is_zero(mant)) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800836 /*!
837 *!float-underflow [off] floating point underflow
838 *! warns about floating point underflow (a nonzero
839 *! constant rounded to zero.)
840 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800841 nasm_warn(WARN_FLOAT_UNDERFLOW|ERR_PASS2,
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300842 "underflow in floating-point constant");
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400843 goto zero;
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800844 } else {
845 /*!
846 *!float-denorm [off] floating point denormal
847 *! warns about denormal floating point constants.
848 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800849 nasm_warn(WARN_FLOAT_DENORM|ERR_PASS2,
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300850 "denormal floating-point constant");
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800851 }
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400852 }
853 break;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700854 }
855
856 case FL_NORMAL:
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400857 exponent += expmax - 1;
858 ieee_shr(mant, fmt->exponent+fmt->explicit);
859 ieee_round(minus, mant, bits);
860 /* did we scale up by one? */
861 if (test_bit(mant, fmt->exponent+fmt->explicit-1)) {
862 ieee_shr(mant, 1);
863 exponent++;
864 if (exponent >= (expmax << 1)-1) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800865 /*!
866 *!float-overflow [on] floating point overflow
867 *! warns about floating point underflow.
868 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800869 nasm_warn(WARN_FLOAT_OVERFLOW|ERR_PASS2,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800870 "overflow in floating-point constant");
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400871 type = FL_INFINITY;
872 goto overflow;
873 }
874 }
H. Peter Anvin70653092007-10-19 14:42:29 -0700875
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400876 if (!fmt->explicit)
877 mant[one_pos] &= ~one_mask; /* remove explicit one */
878 mant[0] |= exponent << (LIMB_BITS-1 - fmt->exponent);
879 break;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700880
881 case FL_INFINITY:
882 case FL_QNAN:
883 case FL_SNAN:
H. Peter Anvin125c8782007-10-16 11:32:58 -0700884 overflow:
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400885 memset(mant, 0, sizeof mant);
886 mant[0] = (((fp_limb)1 << fmt->exponent)-1)
887 << (LIMB_BITS-1 - fmt->exponent);
888 if (fmt->explicit)
889 mant[one_pos] |= one_mask;
890 if (type == FL_QNAN)
891 set_bit(mant, fmt->exponent+fmt->explicit+1);
892 else if (type == FL_SNAN)
893 set_bit(mant, fmt->exponent+fmt->explicit+fmt->mantissa);
894 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000895 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000896
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700897 mant[0] |= minus ? LIMB_TOP_BIT : 0;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700898
H. Peter Anvinf7bd02a2008-01-21 16:19:52 -0800899 for (i = fmt->bytes - 1; i >= 0; i--)
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400900 *result++ = mant[i/LIMB_BYTES] >> (((LIMB_BYTES-1)-(i%LIMB_BYTES))*8);
H. Peter Anvine31747e2007-09-18 17:50:34 -0700901
902 return 1; /* success */
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700903}
904
H. Peter Anvin215186f2016-02-17 20:27:41 -0800905int float_const(const char *number, int sign, uint8_t *result, int bytes)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000906{
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700907 switch (bytes) {
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700908 case 1:
909 return to_float(number, sign, result, &ieee_8);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700910 case 2:
H. Peter Anvin214f5492007-10-15 19:46:32 -0700911 return to_float(number, sign, result, &ieee_16);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700912 case 4:
H. Peter Anvin214f5492007-10-15 19:46:32 -0700913 return to_float(number, sign, result, &ieee_32);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700914 case 8:
H. Peter Anvin214f5492007-10-15 19:46:32 -0700915 return to_float(number, sign, result, &ieee_64);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700916 case 10:
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700917 return to_float(number, sign, result, &ieee_80);
H. Peter Anvine31747e2007-09-18 17:50:34 -0700918 case 16:
H. Peter Anvin214f5492007-10-15 19:46:32 -0700919 return to_float(number, sign, result, &ieee_128);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700920 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -0700921 nasm_panic("strange value %d passed to float_const", bytes);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000922 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000923 }
924}
H. Peter Anvinf6c9e652007-10-16 14:40:27 -0700925
926/* Set floating-point options */
927int float_option(const char *option)
928{
929 if (!nasm_stricmp(option, "daz")) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400930 daz = true;
931 return 0;
H. Peter Anvinf6c9e652007-10-16 14:40:27 -0700932 } else if (!nasm_stricmp(option, "nodaz")) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400933 daz = false;
934 return 0;
H. Peter Anvinf6c9e652007-10-16 14:40:27 -0700935 } else if (!nasm_stricmp(option, "near")) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400936 rc = FLOAT_RC_NEAR;
937 return 0;
H. Peter Anvinf6c9e652007-10-16 14:40:27 -0700938 } else if (!nasm_stricmp(option, "down")) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400939 rc = FLOAT_RC_DOWN;
940 return 0;
H. Peter Anvinf6c9e652007-10-16 14:40:27 -0700941 } else if (!nasm_stricmp(option, "up")) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400942 rc = FLOAT_RC_UP;
943 return 0;
H. Peter Anvinf6c9e652007-10-16 14:40:27 -0700944 } else if (!nasm_stricmp(option, "zero")) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400945 rc = FLOAT_RC_ZERO;
946 return 0;
H. Peter Anvinf6c9e652007-10-16 14:40:27 -0700947 } else if (!nasm_stricmp(option, "default")) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400948 rc = FLOAT_RC_NEAR;
949 daz = false;
950 return 0;
H. Peter Anvinf6c9e652007-10-16 14:40:27 -0700951 } else {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400952 return -1; /* Unknown option */
H. Peter Anvinf6c9e652007-10-16 14:40:27 -0700953 }
954}