blob: 3d38944787c18581a43fa4ead4132f9e5473b157 [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#include <stdio.h>
42#include <stdlib.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000043
44#include "nasm.h"
H. Peter Anvin214f5492007-10-15 19:46:32 -070045#include "float.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080046#include "error.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000047
48/*
H. Peter Anvin214f5492007-10-15 19:46:32 -070049 * -----------------
50 * local variables
51 * -----------------
52 */
H. Peter Anvin214f5492007-10-15 19:46:32 -070053static bool daz = false; /* denormals as zero */
54static enum float_round rc = FLOAT_RC_NEAR; /* rounding control */
55
56/*
57 * -----------
58 * constants
59 * -----------
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000060 */
61
H. Peter Anvin2ce02742007-10-29 20:20:12 -070062/* "A limb is like a digit but bigger */
63typedef uint32_t fp_limb;
64typedef uint64_t fp_2limb;
65
Cyrill Gorcunov1598a232009-10-11 13:17:52 +040066#define LIMB_BITS 32
H. Peter Anvin2ce02742007-10-29 20:20:12 -070067#define LIMB_BYTES (LIMB_BITS/8)
Cyrill Gorcunov1598a232009-10-11 13:17:52 +040068#define LIMB_TOP_BIT ((fp_limb)1 << (LIMB_BITS-1))
69#define LIMB_MASK ((fp_limb)(~0))
70#define LIMB_ALL_BYTES ((fp_limb)0x01010101)
71#define LIMB_BYTE(x) ((x)*LIMB_ALL_BYTES)
H. Peter Anvin2ce02742007-10-29 20:20:12 -070072
H. Peter Anvin214f5492007-10-15 19:46:32 -070073/* 112 bits + 64 bits for accuracy + 16 bits for rounding */
H. Peter Anvin2ce02742007-10-29 20:20:12 -070074#define MANT_LIMBS 6
H. Peter Anvin214f5492007-10-15 19:46:32 -070075
76/* 52 digits fit in 176 bits because 10^53 > 2^176 > 10^52 */
77#define MANT_DIGITS 52
78
H. Peter Anvin2ce02742007-10-29 20:20:12 -070079/* the format and the argument list depend on MANT_LIMBS */
80#define MANT_FMT "%08x_%08x_%08x_%08x_%08x_%08x"
H. Peter Anvin214f5492007-10-15 19:46:32 -070081#define MANT_ARG SOME_ARG(mant, 0)
82
Cyrill Gorcunov1598a232009-10-11 13:17:52 +040083#define SOME_ARG(a,i) (a)[(i)+0], (a)[(i)+1], (a)[(i)+2], \
84 (a)[(i)+3], (a)[(i)+4], (a)[(i)+5]
H. Peter Anvin214f5492007-10-15 19:46:32 -070085
86/*
87 * ---------------------------------------------------------------------------
88 * emit a printf()-like debug message... but only if DEBUG_FLOAT was defined
89 * ---------------------------------------------------------------------------
90 */
91
92#ifdef DEBUG_FLOAT
93#define dprintf(x) printf x
Cyrill Gorcunov1598a232009-10-11 13:17:52 +040094#else
H. Peter Anvin214f5492007-10-15 19:46:32 -070095#define dprintf(x) do { } while (0)
Cyrill Gorcunov1598a232009-10-11 13:17:52 +040096#endif
H. Peter Anvin214f5492007-10-15 19:46:32 -070097
98/*
99 * ---------------------------------------------------------------------------
100 * multiply
101 * ---------------------------------------------------------------------------
102 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700103static int float_multiply(fp_limb *to, fp_limb *from)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000104{
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700105 fp_2limb temp[MANT_LIMBS * 2];
106 int i, j;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000107
H. Peter Anvin70653092007-10-19 14:42:29 -0700108 /*
H. Peter Anvin214f5492007-10-15 19:46:32 -0700109 * guaranteed that top bit of 'from' is set -- so we only have
110 * to worry about _one_ bit shift to the left
111 */
112 dprintf(("%s=" MANT_FMT "\n", "mul1", SOME_ARG(to, 0)));
113 dprintf(("%s=" MANT_FMT "\n", "mul2", SOME_ARG(from, 0)));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000114
H. Peter Anvin214f5492007-10-15 19:46:32 -0700115 memset(temp, 0, sizeof temp);
116
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700117 for (i = 0; i < MANT_LIMBS; i++) {
118 for (j = 0; j < MANT_LIMBS; j++) {
119 fp_2limb n;
120 n = (fp_2limb) to[i] * (fp_2limb) from[j];
121 temp[i + j] += n >> LIMB_BITS;
122 temp[i + j + 1] += (fp_limb)n;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000123 }
H. Peter Anvin214f5492007-10-15 19:46:32 -0700124 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000125
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700126 for (i = MANT_LIMBS * 2; --i;) {
127 temp[i - 1] += temp[i] >> LIMB_BITS;
128 temp[i] &= LIMB_MASK;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000129 }
H. Peter Anvin214f5492007-10-15 19:46:32 -0700130
131 dprintf(("%s=" MANT_FMT "_" MANT_FMT "\n", "temp", SOME_ARG(temp, 0),
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700132 SOME_ARG(temp, MANT_LIMBS)));
H. Peter Anvin214f5492007-10-15 19:46:32 -0700133
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700134 if (temp[0] & LIMB_TOP_BIT) {
135 for (i = 0; i < MANT_LIMBS; i++) {
136 to[i] = temp[i] & LIMB_MASK;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700137 }
138 dprintf(("%s=" MANT_FMT " (%i)\n", "prod", SOME_ARG(to, 0), 0));
139 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000140 } else {
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700141 for (i = 0; i < MANT_LIMBS; i++) {
142 to[i] = (temp[i] << 1) + !!(temp[i + 1] & LIMB_TOP_BIT);
H. Peter Anvin214f5492007-10-15 19:46:32 -0700143 }
144 dprintf(("%s=" MANT_FMT " (%i)\n", "prod", SOME_ARG(to, 0), -1));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000145 return -1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000146 }
147}
148
H. Peter Anvin214f5492007-10-15 19:46:32 -0700149/*
150 * ---------------------------------------------------------------------------
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700151 * read an exponent; returns INT32_MAX on error
152 * ---------------------------------------------------------------------------
153 */
H. Peter Anvin3514ad02007-10-19 14:17:51 -0700154static int32_t read_exponent(const char *string, int32_t max)
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700155{
156 int32_t i = 0;
157 bool neg = false;
H. Peter Anvin70653092007-10-19 14:42:29 -0700158
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700159 if (*string == '+') {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400160 string++;
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700161 } else if (*string == '-') {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400162 neg = true;
163 string++;
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700164 }
165 while (*string) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400166 if (*string >= '0' && *string <= '9') {
167 i = (i * 10) + (*string - '0');
H. Peter Anvin70653092007-10-19 14:42:29 -0700168
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400169 /*
170 * To ensure that underflows and overflows are
171 * handled properly we must avoid wraparounds of
172 * the signed integer value that is used to hold
173 * the exponent. Therefore we cap the exponent at
174 * +/-5000, which is slightly more/less than
175 * what's required for normal and denormal numbers
176 * in single, double, and extended precision, but
177 * sufficient to avoid signed integer wraparound.
178 */
179 if (i > max)
180 i = max;
181 } else if (*string == '_') {
182 /* do nothing */
183 } else {
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300184 nasm_nonfatal("invalid character in floating-point constant %s: '%c'",
185 "exponent", *string);
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400186 return INT32_MAX;
187 }
188 string++;
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700189 }
190
191 return neg ? -i : i;
192}
193
194/*
195 * ---------------------------------------------------------------------------
H. Peter Anvin214f5492007-10-15 19:46:32 -0700196 * convert
197 * ---------------------------------------------------------------------------
198 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700199static bool ieee_flconvert(const char *string, fp_limb *mant,
H. Peter Anvin214f5492007-10-15 19:46:32 -0700200 int32_t * exponent)
201{
202 char digits[MANT_DIGITS];
203 char *p, *q, *r;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700204 fp_limb mult[MANT_LIMBS], bit;
205 fp_limb *m;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700206 int32_t tenpwr, twopwr;
207 int32_t extratwos;
208 bool started, seendot, warned;
H. Peter Anvin136dcdb2007-11-12 18:25:24 -0800209
210 warned = false;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700211 p = digits;
212 tenpwr = 0;
H. Peter Anvinfab3a6c2007-10-16 11:48:07 -0700213 started = seendot = false;
H. Peter Anvin136dcdb2007-11-12 18:25:24 -0800214
H. Peter Anvin214f5492007-10-15 19:46:32 -0700215 while (*string && *string != 'E' && *string != 'e') {
216 if (*string == '.') {
217 if (!seendot) {
218 seendot = true;
219 } else {
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300220 nasm_nonfatal("too many periods in floating-point constant");
H. Peter Anvin214f5492007-10-15 19:46:32 -0700221 return false;
222 }
223 } else if (*string >= '0' && *string <= '9') {
224 if (*string == '0' && !started) {
225 if (seendot) {
226 tenpwr--;
227 }
228 } else {
229 started = true;
230 if (p < digits + sizeof(digits)) {
231 *p++ = *string - '0';
232 } else {
233 if (!warned) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800234 /*!
235 *!float-toolong [on] too many digits in floating-point number
236 *! warns about too many digits in floating-point numbers.
237 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800238 nasm_warn(WARN_FLOAT_TOOLONG|ERR_PASS2,
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300239 "floating-point constant significand contains "
240 "more than %i digits", MANT_DIGITS);
H. Peter Anvin214f5492007-10-15 19:46:32 -0700241 warned = true;
242 }
243 }
244 if (!seendot) {
245 tenpwr++;
246 }
247 }
248 } else if (*string == '_') {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700249 /* do nothing */
250 } else {
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300251 nasm_nonfatalf(ERR_PASS2,
252 "invalid character in floating-point constant %s: '%c'",
253 "significand", *string);
H. Peter Anvin214f5492007-10-15 19:46:32 -0700254 return false;
255 }
256 string++;
257 }
H. Peter Anvin70653092007-10-19 14:42:29 -0700258
H. Peter Anvin214f5492007-10-15 19:46:32 -0700259 if (*string) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400260 int32_t e;
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700261
H. Peter Anvin214f5492007-10-15 19:46:32 -0700262 string++; /* eat the E */
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400263 e = read_exponent(string, 5000);
264 if (e == INT32_MAX)
265 return false;
266 tenpwr += e;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700267 }
268
269 /*
270 * At this point, the memory interval [digits,p) contains a
271 * series of decimal digits zzzzzzz, such that our number X
272 * satisfies X = 0.zzzzzzz * 10^tenpwr.
273 */
274 q = digits;
275 dprintf(("X = 0."));
276 while (q < p) {
277 dprintf(("%c", *q + '0'));
278 q++;
279 }
280 dprintf((" * 10^%i\n", tenpwr));
281
282 /*
283 * Now convert [digits,p) to our internal representation.
284 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700285 bit = LIMB_TOP_BIT;
286 for (m = mant; m < mant + MANT_LIMBS; m++) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700287 *m = 0;
288 }
289 m = mant;
290 q = digits;
291 started = false;
292 twopwr = 0;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700293 while (m < mant + MANT_LIMBS) {
294 fp_limb carry = 0;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700295 while (p > q && !p[-1]) {
296 p--;
297 }
298 if (p <= q) {
299 break;
300 }
301 for (r = p; r-- > q;) {
302 int32_t i;
303 i = 2 * *r + carry;
304 if (i >= 10) {
305 carry = 1;
306 i -= 10;
307 } else {
308 carry = 0;
309 }
310 *r = i;
311 }
312 if (carry) {
313 *m |= bit;
314 started = true;
315 }
316 if (started) {
317 if (bit == 1) {
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700318 bit = LIMB_TOP_BIT;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700319 m++;
320 } else {
321 bit >>= 1;
322 }
323 } else {
324 twopwr--;
325 }
326 }
327 twopwr += tenpwr;
328
329 /*
330 * At this point, the 'mant' array contains the first frac-
331 * tional places of a base-2^16 real number which when mul-
332 * tiplied by 2^twopwr and 5^tenpwr gives X.
333 */
334 dprintf(("X = " MANT_FMT " * 2^%i * 5^%i\n", MANT_ARG, twopwr,
335 tenpwr));
336
337 /*
338 * Now multiply 'mant' by 5^tenpwr.
339 */
340 if (tenpwr < 0) { /* mult = 5^-1 = 0.2 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700341 for (m = mult; m < mult + MANT_LIMBS - 1; m++) {
342 *m = LIMB_BYTE(0xcc);
H. Peter Anvin214f5492007-10-15 19:46:32 -0700343 }
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700344 mult[MANT_LIMBS - 1] = LIMB_BYTE(0xcc)+1;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700345 extratwos = -2;
346 tenpwr = -tenpwr;
347
348 /*
349 * If tenpwr was 1000...000b, then it becomes 1000...000b. See
350 * the "ANSI C" comment below for more details on that case.
351 *
352 * Because we already truncated tenpwr to +5000...-5000 inside
353 * the exponent parsing code, this shouldn't happen though.
354 */
355 } else if (tenpwr > 0) { /* mult = 5^+1 = 5.0 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700356 mult[0] = (fp_limb)5 << (LIMB_BITS-3); /* 0xA000... */
357 for (m = mult + 1; m < mult + MANT_LIMBS; m++) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700358 *m = 0;
359 }
360 extratwos = 3;
361 } else {
362 extratwos = 0;
363 }
364 while (tenpwr) {
365 dprintf(("loop=" MANT_FMT " * 2^%i * 5^%i (%i)\n", MANT_ARG,
366 twopwr, tenpwr, extratwos));
367 if (tenpwr & 1) {
368 dprintf(("mant*mult\n"));
369 twopwr += extratwos + float_multiply(mant, mult);
370 }
371 dprintf(("mult*mult\n"));
372 extratwos = extratwos * 2 + float_multiply(mult, mult);
373 tenpwr >>= 1;
374
375 /*
376 * In ANSI C, the result of right-shifting a signed integer is
377 * considered implementation-specific. To ensure that the loop
378 * terminates even if tenpwr was 1000...000b to begin with, we
379 * manually clear the MSB, in case a 1 was shifted in.
380 *
381 * Because we already truncated tenpwr to +5000...-5000 inside
382 * the exponent parsing code, this shouldn't matter; neverthe-
383 * less it is the right thing to do here.
384 */
385 tenpwr &= (uint32_t) - 1 >> 1;
386 }
387
388 /*
389 * At this point, the 'mant' array contains the first frac-
390 * tional places of a base-2^16 real number in [0.5,1) that
391 * when multiplied by 2^twopwr gives X. Or it contains zero
392 * of course. We are done.
393 */
394 *exponent = twopwr;
395 return true;
396}
397
398/*
399 * ---------------------------------------------------------------------------
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700400 * operations of specific bits
401 * ---------------------------------------------------------------------------
402 */
403
404/* Set a bit, using *bigendian* bit numbering (0 = MSB) */
405static void set_bit(fp_limb *mant, int bit)
406{
407 mant[bit/LIMB_BITS] |= LIMB_TOP_BIT >> (bit & (LIMB_BITS-1));
408}
409
410/* Test a single bit */
411static int test_bit(const fp_limb *mant, int bit)
412{
413 return (mant[bit/LIMB_BITS] >> (~bit & (LIMB_BITS-1))) & 1;
414}
415
416/* Report if the mantissa value is all zero */
417static bool is_zero(const fp_limb *mant)
418{
419 int i;
420
421 for (i = 0; i < MANT_LIMBS; i++)
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400422 if (mant[i])
423 return false;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700424
425 return true;
426}
427
428/*
429 * ---------------------------------------------------------------------------
H. Peter Anvin214f5492007-10-15 19:46:32 -0700430 * round a mantissa off after i words
431 * ---------------------------------------------------------------------------
432 */
433
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400434#define ROUND_COLLECT_BITS \
435 do { \
436 m = mant[i] & (2*bit-1); \
437 for (j = i+1; j < MANT_LIMBS; j++) \
438 m = m | mant[j]; \
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700439 } while (0)
H. Peter Anvin214f5492007-10-15 19:46:32 -0700440
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400441#define ROUND_ABS_DOWN \
442 do { \
443 mant[i] &= ~(bit-1); \
444 for (j = i+1; j < MANT_LIMBS; j++) \
445 mant[j] = 0; \
446 return false; \
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700447 } while (0)
H. Peter Anvin214f5492007-10-15 19:46:32 -0700448
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400449#define ROUND_ABS_UP \
450 do { \
451 mant[i] = (mant[i] & ~(bit-1)) + bit; \
452 for (j = i+1; j < MANT_LIMBS; j++) \
453 mant[j] = 0; \
454 while (i > 0 && !mant[i]) \
455 ++mant[--i]; \
456 return !mant[0]; \
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700457 } while (0)
H. Peter Anvin214f5492007-10-15 19:46:32 -0700458
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700459static bool ieee_round(bool minus, fp_limb *mant, int bits)
H. Peter Anvin214f5492007-10-15 19:46:32 -0700460{
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700461 fp_limb m = 0;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700462 int32_t j;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700463 int i = bits / LIMB_BITS;
464 int p = bits % LIMB_BITS;
465 fp_limb bit = LIMB_TOP_BIT >> p;
466
467 if (rc == FLOAT_RC_NEAR) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400468 if (mant[i] & bit) {
469 mant[i] &= ~bit;
470 ROUND_COLLECT_BITS;
471 mant[i] |= bit;
472 if (m) {
473 ROUND_ABS_UP;
474 } else {
475 if (test_bit(mant, bits-1)) {
476 ROUND_ABS_UP;
477 } else {
478 ROUND_ABS_DOWN;
479 }
480 }
481 } else {
482 ROUND_ABS_DOWN;
483 }
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700484 } else if (rc == FLOAT_RC_ZERO ||
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400485 rc == (minus ? FLOAT_RC_UP : FLOAT_RC_DOWN)) {
486 ROUND_ABS_DOWN;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700487 } else {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400488 /* rc == (minus ? FLOAT_RC_DOWN : FLOAT_RC_UP) */
489 /* Round toward +/- infinity */
490 ROUND_COLLECT_BITS;
491 if (m) {
492 ROUND_ABS_UP;
493 } else {
494 ROUND_ABS_DOWN;
495 }
H. Peter Anvin214f5492007-10-15 19:46:32 -0700496 }
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700497 return false;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700498}
499
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700500/* Returns a value >= 16 if not a valid hex digit */
501static unsigned int hexval(char c)
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700502{
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700503 unsigned int v = (unsigned char) c;
504
505 if (v >= '0' && v <= '9')
506 return v - '0';
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700507 else
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700508 return (v|0x20) - 'a' + 10;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700509}
510
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700511/* Handle floating-point numbers with radix 2^bits and binary exponent */
512static bool ieee_flconvert_bin(const char *string, int bits,
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400513 fp_limb *mant, int32_t *exponent)
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700514{
515 static const int log2tbl[16] =
H. Peter Anvin214f5492007-10-15 19:46:32 -0700516 { -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3 };
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700517 fp_limb mult[MANT_LIMBS + 1], *mp;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700518 int ms;
519 int32_t twopwr;
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700520 bool seendot, seendigit;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700521 unsigned char c;
H. Peter Anvin45f22922008-07-03 20:12:37 -0700522 const int radix = 1 << bits;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700523 fp_limb v;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700524
525 twopwr = 0;
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700526 seendot = seendigit = false;
H. Peter Anvin82f9f632007-09-24 20:53:48 -0700527 ms = 0;
528 mp = NULL;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700529
530 memset(mult, 0, sizeof mult);
531
532 while ((c = *string++) != '\0') {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700533 if (c == '.') {
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700534 if (!seendot)
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700535 seendot = true;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700536 else {
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300537 nasm_nonfatal("too many periods in floating-point constant");
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700538 return false;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700539 }
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700540 } else if ((v = hexval(c)) < (unsigned int)radix) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700541 if (!seendigit && v) {
542 int l = log2tbl[v];
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700543
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700544 seendigit = true;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700545 mp = mult;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700546 ms = (LIMB_BITS-1)-l;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700547
H. Peter Anvin3646e7d2017-04-05 21:47:20 -0700548 twopwr += l+1-bits;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700549 }
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700550
H. Peter Anvin214f5492007-10-15 19:46:32 -0700551 if (seendigit) {
H. Peter Anvin79a070e2018-11-26 14:17:40 -0800552 if (ms < 0) {
553 /* Cast to fp_2limb as ms == -LIMB_BITS is possible. */
554 *mp |= (fp_2limb)v >> -ms;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700555 mp++;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700556 if (mp > &mult[MANT_LIMBS])
557 mp = &mult[MANT_LIMBS]; /* Guard slot */
558 ms += LIMB_BITS;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700559 }
H. Peter Anvin79a070e2018-11-26 14:17:40 -0800560 *mp |= v << ms;
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700561 ms -= bits;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700562
H. Peter Anvin214f5492007-10-15 19:46:32 -0700563 if (!seendot)
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700564 twopwr += bits;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700565 } else {
566 if (seendot)
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700567 twopwr -= bits;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700568 }
569 } else if (c == 'p' || c == 'P') {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400570 int32_t e;
571 e = read_exponent(string, 20000);
572 if (e == INT32_MAX)
573 return false;
574 twopwr += e;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700575 break;
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400576 } else if (c == '_') {
577 /* ignore */
H. Peter Anvin214f5492007-10-15 19:46:32 -0700578 } else {
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300579 nasm_nonfatal("floating-point constant: `%c' is invalid character", c);
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700580 return false;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700581 }
582 }
583
584 if (!seendigit) {
H. Peter Anvin5aa689f2008-07-03 20:11:30 -0700585 memset(mant, 0, MANT_LIMBS*sizeof(fp_limb)); /* Zero */
H. Peter Anvin214f5492007-10-15 19:46:32 -0700586 *exponent = 0;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700587 } else {
H. Peter Anvin5aa689f2008-07-03 20:11:30 -0700588 memcpy(mant, mult, MANT_LIMBS*sizeof(fp_limb));
H. Peter Anvin214f5492007-10-15 19:46:32 -0700589 *exponent = twopwr;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700590 }
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700591
592 return true;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700593}
594
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000595/*
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700596 * Shift a mantissa to the right by i bits.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000597 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700598static void ieee_shr(fp_limb *mant, int i)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000599{
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700600 fp_limb n, m;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700601 int j = 0;
602 int sr, sl, offs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000603
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700604 sr = i % LIMB_BITS; sl = LIMB_BITS-sr;
605 offs = i/LIMB_BITS;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700606
607 if (sr == 0) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400608 if (offs)
609 for (j = MANT_LIMBS-1; j >= offs; j--)
610 mant[j] = mant[j-offs];
Adam Majerc7c28352018-07-05 17:40:24 +0200611 } else if (MANT_LIMBS-1-offs < 0) {
612 j = MANT_LIMBS-1;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700613 } else {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400614 n = mant[MANT_LIMBS-1-offs] >> sr;
615 for (j = MANT_LIMBS-1; j > offs; j--) {
616 m = mant[j-offs-1];
617 mant[j] = (m << sl) | n;
618 n = m >> sr;
619 }
620 mant[j--] = n;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000621 }
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700622 while (j >= 0)
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400623 mant[j--] = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000624}
625
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700626/* Produce standard IEEE formats, with implicit or explicit integer
627 bit; this makes the following assumptions:
628
629 - the sign bit is the MSB, followed by the exponent,
630 followed by the integer bit if present.
H. Peter Anvine31747e2007-09-18 17:50:34 -0700631 - the sign bit plus exponent fit in 16 bits.
632 - the exponent bias is 2^(n-1)-1 for an n-bit exponent */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000633
H. Peter Anvine31747e2007-09-18 17:50:34 -0700634struct ieee_format {
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700635 int bytes;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700636 int mantissa; /* Fractional bits in the mantissa */
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400637 int explicit; /* Explicit integer */
H. Peter Anvin214f5492007-10-15 19:46:32 -0700638 int exponent; /* Bits in the exponent */
H. Peter Anvine31747e2007-09-18 17:50:34 -0700639};
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000640
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700641/*
642 * The 16- and 128-bit formats are expected to be in IEEE 754r.
643 * AMD SSE5 uses the 16-bit format.
644 *
645 * The 32- and 64-bit formats are the original IEEE 754 formats.
646 *
647 * The 80-bit format is x87-specific, but widely used.
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700648 *
649 * The 8-bit format appears to be the consensus 8-bit floating-point
650 * format. It is apparently used in graphics applications.
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700651 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700652static const struct ieee_format ieee_8 = { 1, 3, 0, 4 };
653static const struct ieee_format ieee_16 = { 2, 10, 0, 5 };
654static const struct ieee_format ieee_32 = { 4, 23, 0, 8 };
655static const struct ieee_format ieee_64 = { 8, 52, 0, 11 };
656static const struct ieee_format ieee_80 = { 10, 63, 1, 15 };
657static const struct ieee_format ieee_128 = { 16, 112, 0, 15 };
H. Peter Anvine31747e2007-09-18 17:50:34 -0700658
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700659/* Types of values we can generate */
660enum floats {
661 FL_ZERO,
662 FL_DENORMAL,
663 FL_NORMAL,
664 FL_INFINITY,
665 FL_QNAN,
666 FL_SNAN
667};
668
H. Peter Anvin63ebf162008-07-03 20:16:07 -0700669static int to_packed_bcd(const char *str, const char *p,
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400670 int s, uint8_t *result,
671 const struct ieee_format *fmt)
H. Peter Anvin45f22922008-07-03 20:12:37 -0700672{
673 int n = 0;
674 char c;
675 int tv = -1;
H. Peter Anvin45f22922008-07-03 20:12:37 -0700676
677 if (fmt != &ieee_80) {
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300678 nasm_nonfatal("packed BCD requires an 80-bit format");
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400679 return 0;
H. Peter Anvin45f22922008-07-03 20:12:37 -0700680 }
681
682 while (p >= str) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400683 c = *p--;
684 if (c >= '0' && c <= '9') {
685 if (tv < 0) {
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300686 if (n == 9)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -0800687 nasm_warn(WARN_OTHER|ERR_PASS2, "packed BCD truncated to 18 digits");
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400688 tv = c-'0';
689 } else {
690 if (n < 9)
691 *result++ = tv + ((c-'0') << 4);
692 n++;
693 tv = -1;
694 }
695 } else if (c == '_') {
696 /* do nothing */
697 } else {
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300698 nasm_nonfatal("invalid character `%c' in packed BCD constant", c);
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400699 return 0;
700 }
H. Peter Anvin45f22922008-07-03 20:12:37 -0700701 }
702 if (tv >= 0) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400703 if (n < 9)
704 *result++ = tv;
705 n++;
H. Peter Anvin45f22922008-07-03 20:12:37 -0700706 }
707 while (n < 9) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400708 *result++ = 0;
709 n++;
H. Peter Anvin45f22922008-07-03 20:12:37 -0700710 }
711 *result = (s < 0) ? 0x80 : 0;
712
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400713 return 1; /* success */
H. Peter Anvin45f22922008-07-03 20:12:37 -0700714}
715
716static int to_float(const char *str, int s, uint8_t *result,
H. Peter Anvin214f5492007-10-15 19:46:32 -0700717 const struct ieee_format *fmt)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000718{
H. Peter Anvinf7bd02a2008-01-21 16:19:52 -0800719 fp_limb mant[MANT_LIMBS];
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700720 int32_t exponent = 0;
H. Peter Anvin45f22922008-07-03 20:12:37 -0700721 const int32_t expmax = 1 << (fmt->exponent - 1);
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700722 fp_limb one_mask = LIMB_TOP_BIT >>
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400723 ((fmt->exponent+fmt->explicit) % LIMB_BITS);
H. Peter Anvin45f22922008-07-03 20:12:37 -0700724 const int one_pos = (fmt->exponent+fmt->explicit)/LIMB_BITS;
H. Peter Anvine31747e2007-09-18 17:50:34 -0700725 int i;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700726 int shift;
727 enum floats type;
728 bool ok;
H. Peter Anvin45f22922008-07-03 20:12:37 -0700729 const bool minus = s < 0;
730 const int bits = fmt->bytes * 8;
731 const char *strend;
732
H. Peter Anvinc5136902018-06-15 18:20:17 -0700733 nasm_assert(str[0]);
H. Peter Anvin45f22922008-07-03 20:12:37 -0700734
735 strend = strchr(str, '\0');
736 if (strend[-1] == 'P' || strend[-1] == 'p')
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400737 return to_packed_bcd(str, strend-2, s, result, fmt);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000738
H. Peter Anvinf48bc6f2007-09-18 21:55:56 -0700739 if (str[0] == '_') {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400740 /* Special tokens */
H. Peter Anvine31747e2007-09-18 17:50:34 -0700741
H. Peter Anvin214f5492007-10-15 19:46:32 -0700742 switch (str[2]) {
743 case 'n': /* __nan__ */
744 case 'N':
745 case 'q': /* __qnan__ */
746 case 'Q':
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400747 type = FL_QNAN;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700748 break;
749 case 's': /* __snan__ */
750 case 'S':
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400751 type = FL_SNAN;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700752 break;
753 case 'i': /* __infinity__ */
754 case 'I':
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400755 type = FL_INFINITY;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700756 break;
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400757 default:
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300758 nasm_nonfatal("internal error: unknown FP constant token `%s'", str);
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400759 type = FL_QNAN;
760 break;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700761 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000762 } else {
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700763 if (str[0] == '0') {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400764 switch (str[1]) {
765 case 'x': case 'X':
766 case 'h': case 'H':
767 ok = ieee_flconvert_bin(str+2, 4, mant, &exponent);
768 break;
769 case 'o': case 'O':
770 case 'q': case 'Q':
771 ok = ieee_flconvert_bin(str+2, 3, mant, &exponent);
772 break;
773 case 'b': case 'B':
774 case 'y': case 'Y':
775 ok = ieee_flconvert_bin(str+2, 1, mant, &exponent);
776 break;
777 case 'd': case 'D':
778 case 't': case 'T':
779 ok = ieee_flconvert(str+2, mant, &exponent);
780 break;
781 case 'p': case 'P':
782 return to_packed_bcd(str+2, strend-1, s, result, fmt);
783 default:
784 /* Leading zero was just a zero? */
785 ok = ieee_flconvert(str, mant, &exponent);
786 break;
787 }
788 } else if (str[0] == '$') {
789 ok = ieee_flconvert_bin(str+1, 4, mant, &exponent);
790 } else {
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700791 ok = ieee_flconvert(str, mant, &exponent);
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400792 }
H. Peter Anvin214f5492007-10-15 19:46:32 -0700793
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400794 if (!ok) {
795 type = FL_QNAN;
796 } else if (mant[0] & LIMB_TOP_BIT) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700797 /*
798 * Non-zero.
799 */
800 exponent--;
801 if (exponent >= 2 - expmax && exponent <= expmax) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400802 type = FL_NORMAL;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700803 } else if (exponent > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800804 nasm_warn(WARN_FLOAT_OVERFLOW|ERR_PASS2,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800805 "overflow in floating-point constant");
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400806 type = FL_INFINITY;
807 } else {
808 /* underflow or denormal; the denormal code handles
809 actual underflow. */
810 type = FL_DENORMAL;
811 }
812 } else {
813 /* Zero */
814 type = FL_ZERO;
815 }
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700816 }
H. Peter Anvin214f5492007-10-15 19:46:32 -0700817
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700818 switch (type) {
819 case FL_ZERO:
H. Peter Anvin125c8782007-10-16 11:32:58 -0700820 zero:
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400821 memset(mant, 0, sizeof mant);
822 break;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700823
824 case FL_DENORMAL:
825 {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400826 shift = -(exponent + expmax - 2 - fmt->exponent)
827 + fmt->explicit;
828 ieee_shr(mant, shift);
829 ieee_round(minus, mant, bits);
830 if (mant[one_pos] & one_mask) {
831 /* One's position is set, we rounded up into normal range */
832 exponent = 1;
833 if (!fmt->explicit)
834 mant[one_pos] &= ~one_mask; /* remove explicit one */
835 mant[0] |= exponent << (LIMB_BITS-1 - fmt->exponent);
836 } else {
837 if (daz || is_zero(mant)) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800838 /*!
839 *!float-underflow [off] floating point underflow
840 *! warns about floating point underflow (a nonzero
841 *! constant rounded to zero.)
842 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800843 nasm_warn(WARN_FLOAT_UNDERFLOW|ERR_PASS2,
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300844 "underflow in floating-point constant");
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400845 goto zero;
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800846 } else {
847 /*!
848 *!float-denorm [off] floating point denormal
849 *! warns about denormal floating point constants.
850 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800851 nasm_warn(WARN_FLOAT_DENORM|ERR_PASS2,
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300852 "denormal floating-point constant");
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800853 }
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400854 }
855 break;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700856 }
857
858 case FL_NORMAL:
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400859 exponent += expmax - 1;
860 ieee_shr(mant, fmt->exponent+fmt->explicit);
861 ieee_round(minus, mant, bits);
862 /* did we scale up by one? */
863 if (test_bit(mant, fmt->exponent+fmt->explicit-1)) {
864 ieee_shr(mant, 1);
865 exponent++;
866 if (exponent >= (expmax << 1)-1) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800867 /*!
868 *!float-overflow [on] floating point overflow
869 *! warns about floating point underflow.
870 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800871 nasm_warn(WARN_FLOAT_OVERFLOW|ERR_PASS2,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800872 "overflow in floating-point constant");
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400873 type = FL_INFINITY;
874 goto overflow;
875 }
876 }
H. Peter Anvin70653092007-10-19 14:42:29 -0700877
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400878 if (!fmt->explicit)
879 mant[one_pos] &= ~one_mask; /* remove explicit one */
880 mant[0] |= exponent << (LIMB_BITS-1 - fmt->exponent);
881 break;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700882
883 case FL_INFINITY:
884 case FL_QNAN:
885 case FL_SNAN:
H. Peter Anvin125c8782007-10-16 11:32:58 -0700886 overflow:
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400887 memset(mant, 0, sizeof mant);
888 mant[0] = (((fp_limb)1 << fmt->exponent)-1)
889 << (LIMB_BITS-1 - fmt->exponent);
890 if (fmt->explicit)
891 mant[one_pos] |= one_mask;
892 if (type == FL_QNAN)
893 set_bit(mant, fmt->exponent+fmt->explicit+1);
894 else if (type == FL_SNAN)
895 set_bit(mant, fmt->exponent+fmt->explicit+fmt->mantissa);
896 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000897 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000898
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700899 mant[0] |= minus ? LIMB_TOP_BIT : 0;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700900
H. Peter Anvinf7bd02a2008-01-21 16:19:52 -0800901 for (i = fmt->bytes - 1; i >= 0; i--)
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400902 *result++ = mant[i/LIMB_BYTES] >> (((LIMB_BYTES-1)-(i%LIMB_BYTES))*8);
H. Peter Anvine31747e2007-09-18 17:50:34 -0700903
904 return 1; /* success */
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700905}
906
H. Peter Anvin215186f2016-02-17 20:27:41 -0800907int float_const(const char *number, int sign, uint8_t *result, int bytes)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000908{
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700909 switch (bytes) {
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700910 case 1:
911 return to_float(number, sign, result, &ieee_8);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700912 case 2:
H. Peter Anvin214f5492007-10-15 19:46:32 -0700913 return to_float(number, sign, result, &ieee_16);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700914 case 4:
H. Peter Anvin214f5492007-10-15 19:46:32 -0700915 return to_float(number, sign, result, &ieee_32);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700916 case 8:
H. Peter Anvin214f5492007-10-15 19:46:32 -0700917 return to_float(number, sign, result, &ieee_64);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700918 case 10:
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700919 return to_float(number, sign, result, &ieee_80);
H. Peter Anvine31747e2007-09-18 17:50:34 -0700920 case 16:
H. Peter Anvin214f5492007-10-15 19:46:32 -0700921 return to_float(number, sign, result, &ieee_128);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700922 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -0700923 nasm_panic("strange value %d passed to float_const", bytes);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000924 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000925 }
926}
H. Peter Anvinf6c9e652007-10-16 14:40:27 -0700927
928/* Set floating-point options */
929int float_option(const char *option)
930{
931 if (!nasm_stricmp(option, "daz")) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400932 daz = true;
933 return 0;
H. Peter Anvinf6c9e652007-10-16 14:40:27 -0700934 } else if (!nasm_stricmp(option, "nodaz")) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400935 daz = false;
936 return 0;
H. Peter Anvinf6c9e652007-10-16 14:40:27 -0700937 } else if (!nasm_stricmp(option, "near")) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400938 rc = FLOAT_RC_NEAR;
939 return 0;
H. Peter Anvinf6c9e652007-10-16 14:40:27 -0700940 } else if (!nasm_stricmp(option, "down")) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400941 rc = FLOAT_RC_DOWN;
942 return 0;
H. Peter Anvinf6c9e652007-10-16 14:40:27 -0700943 } else if (!nasm_stricmp(option, "up")) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400944 rc = FLOAT_RC_UP;
945 return 0;
H. Peter Anvinf6c9e652007-10-16 14:40:27 -0700946 } else if (!nasm_stricmp(option, "zero")) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400947 rc = FLOAT_RC_ZERO;
948 return 0;
H. Peter Anvinf6c9e652007-10-16 14:40:27 -0700949 } else if (!nasm_stricmp(option, "default")) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400950 rc = FLOAT_RC_NEAR;
951 daz = false;
952 return 0;
H. Peter Anvinf6c9e652007-10-16 14:40:27 -0700953 } else {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400954 return -1; /* Unknown option */
H. Peter Anvinf6c9e652007-10-16 14:40:27 -0700955 }
956}