blob: 187b9e5e018225709c4e34b5d96611a2a83fe555 [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 Anvinfe2177f2007-09-18 18:31:26 -070040#include <ctype.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000041#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44
45#include "nasm.h"
H. Peter Anvin214f5492007-10-15 19:46:32 -070046#include "float.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080047#include "error.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000048
49/*
H. Peter Anvin214f5492007-10-15 19:46:32 -070050 * -----------------
51 * local variables
52 * -----------------
53 */
H. Peter Anvin214f5492007-10-15 19:46:32 -070054static bool daz = false; /* denormals as zero */
55static enum float_round rc = FLOAT_RC_NEAR; /* rounding control */
56
57/*
58 * -----------
59 * constants
60 * -----------
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000061 */
62
H. Peter Anvin2ce02742007-10-29 20:20:12 -070063/* "A limb is like a digit but bigger */
64typedef uint32_t fp_limb;
65typedef uint64_t fp_2limb;
66
Cyrill Gorcunov1598a232009-10-11 13:17:52 +040067#define LIMB_BITS 32
H. Peter Anvin2ce02742007-10-29 20:20:12 -070068#define LIMB_BYTES (LIMB_BITS/8)
Cyrill Gorcunov1598a232009-10-11 13:17:52 +040069#define LIMB_TOP_BIT ((fp_limb)1 << (LIMB_BITS-1))
70#define LIMB_MASK ((fp_limb)(~0))
71#define LIMB_ALL_BYTES ((fp_limb)0x01010101)
72#define LIMB_BYTE(x) ((x)*LIMB_ALL_BYTES)
H. Peter Anvin2ce02742007-10-29 20:20:12 -070073
H. Peter Anvin214f5492007-10-15 19:46:32 -070074/* 112 bits + 64 bits for accuracy + 16 bits for rounding */
H. Peter Anvin2ce02742007-10-29 20:20:12 -070075#define MANT_LIMBS 6
H. Peter Anvin214f5492007-10-15 19:46:32 -070076
77/* 52 digits fit in 176 bits because 10^53 > 2^176 > 10^52 */
78#define MANT_DIGITS 52
79
H. Peter Anvin2ce02742007-10-29 20:20:12 -070080/* the format and the argument list depend on MANT_LIMBS */
81#define MANT_FMT "%08x_%08x_%08x_%08x_%08x_%08x"
H. Peter Anvin214f5492007-10-15 19:46:32 -070082#define MANT_ARG SOME_ARG(mant, 0)
83
Cyrill Gorcunov1598a232009-10-11 13:17:52 +040084#define SOME_ARG(a,i) (a)[(i)+0], (a)[(i)+1], (a)[(i)+2], \
85 (a)[(i)+3], (a)[(i)+4], (a)[(i)+5]
H. Peter Anvin214f5492007-10-15 19:46:32 -070086
87/*
88 * ---------------------------------------------------------------------------
89 * emit a printf()-like debug message... but only if DEBUG_FLOAT was defined
90 * ---------------------------------------------------------------------------
91 */
92
93#ifdef DEBUG_FLOAT
94#define dprintf(x) printf x
Cyrill Gorcunov1598a232009-10-11 13:17:52 +040095#else
H. Peter Anvin214f5492007-10-15 19:46:32 -070096#define dprintf(x) do { } while (0)
Cyrill Gorcunov1598a232009-10-11 13:17:52 +040097#endif
H. Peter Anvin214f5492007-10-15 19:46:32 -070098
99/*
100 * ---------------------------------------------------------------------------
101 * multiply
102 * ---------------------------------------------------------------------------
103 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700104static int float_multiply(fp_limb *to, fp_limb *from)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000105{
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700106 fp_2limb temp[MANT_LIMBS * 2];
107 int i, j;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000108
H. Peter Anvin70653092007-10-19 14:42:29 -0700109 /*
H. Peter Anvin214f5492007-10-15 19:46:32 -0700110 * guaranteed that top bit of 'from' is set -- so we only have
111 * to worry about _one_ bit shift to the left
112 */
113 dprintf(("%s=" MANT_FMT "\n", "mul1", SOME_ARG(to, 0)));
114 dprintf(("%s=" MANT_FMT "\n", "mul2", SOME_ARG(from, 0)));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000115
H. Peter Anvin214f5492007-10-15 19:46:32 -0700116 memset(temp, 0, sizeof temp);
117
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700118 for (i = 0; i < MANT_LIMBS; i++) {
119 for (j = 0; j < MANT_LIMBS; j++) {
120 fp_2limb n;
121 n = (fp_2limb) to[i] * (fp_2limb) from[j];
122 temp[i + j] += n >> LIMB_BITS;
123 temp[i + j + 1] += (fp_limb)n;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000124 }
H. Peter Anvin214f5492007-10-15 19:46:32 -0700125 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000126
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700127 for (i = MANT_LIMBS * 2; --i;) {
128 temp[i - 1] += temp[i] >> LIMB_BITS;
129 temp[i] &= LIMB_MASK;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000130 }
H. Peter Anvin214f5492007-10-15 19:46:32 -0700131
132 dprintf(("%s=" MANT_FMT "_" MANT_FMT "\n", "temp", SOME_ARG(temp, 0),
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700133 SOME_ARG(temp, MANT_LIMBS)));
H. Peter Anvin214f5492007-10-15 19:46:32 -0700134
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700135 if (temp[0] & LIMB_TOP_BIT) {
136 for (i = 0; i < MANT_LIMBS; i++) {
137 to[i] = temp[i] & LIMB_MASK;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700138 }
139 dprintf(("%s=" MANT_FMT " (%i)\n", "prod", SOME_ARG(to, 0), 0));
140 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000141 } else {
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700142 for (i = 0; i < MANT_LIMBS; i++) {
143 to[i] = (temp[i] << 1) + !!(temp[i + 1] & LIMB_TOP_BIT);
H. Peter Anvin214f5492007-10-15 19:46:32 -0700144 }
145 dprintf(("%s=" MANT_FMT " (%i)\n", "prod", SOME_ARG(to, 0), -1));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000146 return -1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000147 }
148}
149
H. Peter Anvin214f5492007-10-15 19:46:32 -0700150/*
151 * ---------------------------------------------------------------------------
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700152 * read an exponent; returns INT32_MAX on error
153 * ---------------------------------------------------------------------------
154 */
H. Peter Anvin3514ad02007-10-19 14:17:51 -0700155static int32_t read_exponent(const char *string, int32_t max)
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700156{
157 int32_t i = 0;
158 bool neg = false;
H. Peter Anvin70653092007-10-19 14:42:29 -0700159
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700160 if (*string == '+') {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400161 string++;
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700162 } else if (*string == '-') {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400163 neg = true;
164 string++;
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700165 }
166 while (*string) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400167 if (*string >= '0' && *string <= '9') {
168 i = (i * 10) + (*string - '0');
H. Peter Anvin70653092007-10-19 14:42:29 -0700169
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400170 /*
171 * To ensure that underflows and overflows are
172 * handled properly we must avoid wraparounds of
173 * the signed integer value that is used to hold
174 * the exponent. Therefore we cap the exponent at
175 * +/-5000, which is slightly more/less than
176 * what's required for normal and denormal numbers
177 * in single, double, and extended precision, but
178 * sufficient to avoid signed integer wraparound.
179 */
180 if (i > max)
181 i = max;
182 } else if (*string == '_') {
183 /* do nothing */
184 } else {
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300185 nasm_nonfatal("invalid character in floating-point constant %s: '%c'",
186 "exponent", *string);
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400187 return INT32_MAX;
188 }
189 string++;
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700190 }
191
192 return neg ? -i : i;
193}
194
195/*
196 * ---------------------------------------------------------------------------
H. Peter Anvin214f5492007-10-15 19:46:32 -0700197 * convert
198 * ---------------------------------------------------------------------------
199 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700200static bool ieee_flconvert(const char *string, fp_limb *mant,
H. Peter Anvin214f5492007-10-15 19:46:32 -0700201 int32_t * exponent)
202{
203 char digits[MANT_DIGITS];
204 char *p, *q, *r;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700205 fp_limb mult[MANT_LIMBS], bit;
206 fp_limb *m;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700207 int32_t tenpwr, twopwr;
208 int32_t extratwos;
209 bool started, seendot, warned;
H. Peter Anvin136dcdb2007-11-12 18:25:24 -0800210
211 warned = false;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700212 p = digits;
213 tenpwr = 0;
H. Peter Anvinfab3a6c2007-10-16 11:48:07 -0700214 started = seendot = false;
H. Peter Anvin136dcdb2007-11-12 18:25:24 -0800215
H. Peter Anvin214f5492007-10-15 19:46:32 -0700216 while (*string && *string != 'E' && *string != 'e') {
217 if (*string == '.') {
218 if (!seendot) {
219 seendot = true;
220 } else {
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300221 nasm_nonfatal("too many periods in floating-point constant");
H. Peter Anvin214f5492007-10-15 19:46:32 -0700222 return false;
223 }
224 } else if (*string >= '0' && *string <= '9') {
225 if (*string == '0' && !started) {
226 if (seendot) {
227 tenpwr--;
228 }
229 } else {
230 started = true;
231 if (p < digits + sizeof(digits)) {
232 *p++ = *string - '0';
233 } else {
234 if (!warned) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800235 /*!
236 *!float-toolong [on] too many digits in floating-point number
237 *! warns about too many digits in floating-point numbers.
238 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800239 nasm_warn(WARN_FLOAT_TOOLONG|ERR_PASS2,
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300240 "floating-point constant significand contains "
241 "more than %i digits", MANT_DIGITS);
H. Peter Anvin214f5492007-10-15 19:46:32 -0700242 warned = true;
243 }
244 }
245 if (!seendot) {
246 tenpwr++;
247 }
248 }
249 } else if (*string == '_') {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700250 /* do nothing */
251 } else {
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300252 nasm_nonfatalf(ERR_PASS2,
253 "invalid character in floating-point constant %s: '%c'",
254 "significand", *string);
H. Peter Anvin214f5492007-10-15 19:46:32 -0700255 return false;
256 }
257 string++;
258 }
H. Peter Anvin70653092007-10-19 14:42:29 -0700259
H. Peter Anvin214f5492007-10-15 19:46:32 -0700260 if (*string) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400261 int32_t e;
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700262
H. Peter Anvin214f5492007-10-15 19:46:32 -0700263 string++; /* eat the E */
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400264 e = read_exponent(string, 5000);
265 if (e == INT32_MAX)
266 return false;
267 tenpwr += e;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700268 }
269
270 /*
271 * At this point, the memory interval [digits,p) contains a
272 * series of decimal digits zzzzzzz, such that our number X
273 * satisfies X = 0.zzzzzzz * 10^tenpwr.
274 */
275 q = digits;
276 dprintf(("X = 0."));
277 while (q < p) {
278 dprintf(("%c", *q + '0'));
279 q++;
280 }
281 dprintf((" * 10^%i\n", tenpwr));
282
283 /*
284 * Now convert [digits,p) to our internal representation.
285 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700286 bit = LIMB_TOP_BIT;
287 for (m = mant; m < mant + MANT_LIMBS; m++) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700288 *m = 0;
289 }
290 m = mant;
291 q = digits;
292 started = false;
293 twopwr = 0;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700294 while (m < mant + MANT_LIMBS) {
295 fp_limb carry = 0;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700296 while (p > q && !p[-1]) {
297 p--;
298 }
299 if (p <= q) {
300 break;
301 }
302 for (r = p; r-- > q;) {
303 int32_t i;
304 i = 2 * *r + carry;
305 if (i >= 10) {
306 carry = 1;
307 i -= 10;
308 } else {
309 carry = 0;
310 }
311 *r = i;
312 }
313 if (carry) {
314 *m |= bit;
315 started = true;
316 }
317 if (started) {
318 if (bit == 1) {
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700319 bit = LIMB_TOP_BIT;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700320 m++;
321 } else {
322 bit >>= 1;
323 }
324 } else {
325 twopwr--;
326 }
327 }
328 twopwr += tenpwr;
329
330 /*
331 * At this point, the 'mant' array contains the first frac-
332 * tional places of a base-2^16 real number which when mul-
333 * tiplied by 2^twopwr and 5^tenpwr gives X.
334 */
335 dprintf(("X = " MANT_FMT " * 2^%i * 5^%i\n", MANT_ARG, twopwr,
336 tenpwr));
337
338 /*
339 * Now multiply 'mant' by 5^tenpwr.
340 */
341 if (tenpwr < 0) { /* mult = 5^-1 = 0.2 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700342 for (m = mult; m < mult + MANT_LIMBS - 1; m++) {
343 *m = LIMB_BYTE(0xcc);
H. Peter Anvin214f5492007-10-15 19:46:32 -0700344 }
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700345 mult[MANT_LIMBS - 1] = LIMB_BYTE(0xcc)+1;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700346 extratwos = -2;
347 tenpwr = -tenpwr;
348
349 /*
350 * If tenpwr was 1000...000b, then it becomes 1000...000b. See
351 * the "ANSI C" comment below for more details on that case.
352 *
353 * Because we already truncated tenpwr to +5000...-5000 inside
354 * the exponent parsing code, this shouldn't happen though.
355 */
356 } else if (tenpwr > 0) { /* mult = 5^+1 = 5.0 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700357 mult[0] = (fp_limb)5 << (LIMB_BITS-3); /* 0xA000... */
358 for (m = mult + 1; m < mult + MANT_LIMBS; m++) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700359 *m = 0;
360 }
361 extratwos = 3;
362 } else {
363 extratwos = 0;
364 }
365 while (tenpwr) {
366 dprintf(("loop=" MANT_FMT " * 2^%i * 5^%i (%i)\n", MANT_ARG,
367 twopwr, tenpwr, extratwos));
368 if (tenpwr & 1) {
369 dprintf(("mant*mult\n"));
370 twopwr += extratwos + float_multiply(mant, mult);
371 }
372 dprintf(("mult*mult\n"));
373 extratwos = extratwos * 2 + float_multiply(mult, mult);
374 tenpwr >>= 1;
375
376 /*
377 * In ANSI C, the result of right-shifting a signed integer is
378 * considered implementation-specific. To ensure that the loop
379 * terminates even if tenpwr was 1000...000b to begin with, we
380 * manually clear the MSB, in case a 1 was shifted in.
381 *
382 * Because we already truncated tenpwr to +5000...-5000 inside
383 * the exponent parsing code, this shouldn't matter; neverthe-
384 * less it is the right thing to do here.
385 */
386 tenpwr &= (uint32_t) - 1 >> 1;
387 }
388
389 /*
390 * At this point, the 'mant' array contains the first frac-
391 * tional places of a base-2^16 real number in [0.5,1) that
392 * when multiplied by 2^twopwr gives X. Or it contains zero
393 * of course. We are done.
394 */
395 *exponent = twopwr;
396 return true;
397}
398
399/*
400 * ---------------------------------------------------------------------------
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700401 * operations of specific bits
402 * ---------------------------------------------------------------------------
403 */
404
405/* Set a bit, using *bigendian* bit numbering (0 = MSB) */
406static void set_bit(fp_limb *mant, int bit)
407{
408 mant[bit/LIMB_BITS] |= LIMB_TOP_BIT >> (bit & (LIMB_BITS-1));
409}
410
411/* Test a single bit */
412static int test_bit(const fp_limb *mant, int bit)
413{
414 return (mant[bit/LIMB_BITS] >> (~bit & (LIMB_BITS-1))) & 1;
415}
416
417/* Report if the mantissa value is all zero */
418static bool is_zero(const fp_limb *mant)
419{
420 int i;
421
422 for (i = 0; i < MANT_LIMBS; i++)
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400423 if (mant[i])
424 return false;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700425
426 return true;
427}
428
429/*
430 * ---------------------------------------------------------------------------
H. Peter Anvin214f5492007-10-15 19:46:32 -0700431 * round a mantissa off after i words
432 * ---------------------------------------------------------------------------
433 */
434
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400435#define ROUND_COLLECT_BITS \
436 do { \
437 m = mant[i] & (2*bit-1); \
438 for (j = i+1; j < MANT_LIMBS; j++) \
439 m = m | mant[j]; \
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700440 } while (0)
H. Peter Anvin214f5492007-10-15 19:46:32 -0700441
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400442#define ROUND_ABS_DOWN \
443 do { \
444 mant[i] &= ~(bit-1); \
445 for (j = i+1; j < MANT_LIMBS; j++) \
446 mant[j] = 0; \
447 return false; \
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700448 } while (0)
H. Peter Anvin214f5492007-10-15 19:46:32 -0700449
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400450#define ROUND_ABS_UP \
451 do { \
452 mant[i] = (mant[i] & ~(bit-1)) + bit; \
453 for (j = i+1; j < MANT_LIMBS; j++) \
454 mant[j] = 0; \
455 while (i > 0 && !mant[i]) \
456 ++mant[--i]; \
457 return !mant[0]; \
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700458 } while (0)
H. Peter Anvin214f5492007-10-15 19:46:32 -0700459
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700460static bool ieee_round(bool minus, fp_limb *mant, int bits)
H. Peter Anvin214f5492007-10-15 19:46:32 -0700461{
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700462 fp_limb m = 0;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700463 int32_t j;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700464 int i = bits / LIMB_BITS;
465 int p = bits % LIMB_BITS;
466 fp_limb bit = LIMB_TOP_BIT >> p;
467
468 if (rc == FLOAT_RC_NEAR) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400469 if (mant[i] & bit) {
470 mant[i] &= ~bit;
471 ROUND_COLLECT_BITS;
472 mant[i] |= bit;
473 if (m) {
474 ROUND_ABS_UP;
475 } else {
476 if (test_bit(mant, bits-1)) {
477 ROUND_ABS_UP;
478 } else {
479 ROUND_ABS_DOWN;
480 }
481 }
482 } else {
483 ROUND_ABS_DOWN;
484 }
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700485 } else if (rc == FLOAT_RC_ZERO ||
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400486 rc == (minus ? FLOAT_RC_UP : FLOAT_RC_DOWN)) {
487 ROUND_ABS_DOWN;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700488 } else {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400489 /* rc == (minus ? FLOAT_RC_DOWN : FLOAT_RC_UP) */
490 /* Round toward +/- infinity */
491 ROUND_COLLECT_BITS;
492 if (m) {
493 ROUND_ABS_UP;
494 } else {
495 ROUND_ABS_DOWN;
496 }
H. Peter Anvin214f5492007-10-15 19:46:32 -0700497 }
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700498 return false;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700499}
500
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700501/* Returns a value >= 16 if not a valid hex digit */
502static unsigned int hexval(char c)
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700503{
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700504 unsigned int v = (unsigned char) c;
505
506 if (v >= '0' && v <= '9')
507 return v - '0';
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700508 else
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700509 return (v|0x20) - 'a' + 10;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700510}
511
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700512/* Handle floating-point numbers with radix 2^bits and binary exponent */
513static bool ieee_flconvert_bin(const char *string, int bits,
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400514 fp_limb *mant, int32_t *exponent)
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700515{
516 static const int log2tbl[16] =
H. Peter Anvin214f5492007-10-15 19:46:32 -0700517 { -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3 };
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700518 fp_limb mult[MANT_LIMBS + 1], *mp;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700519 int ms;
520 int32_t twopwr;
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700521 bool seendot, seendigit;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700522 unsigned char c;
H. Peter Anvin45f22922008-07-03 20:12:37 -0700523 const int radix = 1 << bits;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700524 fp_limb v;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700525
526 twopwr = 0;
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700527 seendot = seendigit = false;
H. Peter Anvin82f9f632007-09-24 20:53:48 -0700528 ms = 0;
529 mp = NULL;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700530
531 memset(mult, 0, sizeof mult);
532
533 while ((c = *string++) != '\0') {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700534 if (c == '.') {
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700535 if (!seendot)
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700536 seendot = true;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700537 else {
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300538 nasm_nonfatal("too many periods in floating-point constant");
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700539 return false;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700540 }
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700541 } else if ((v = hexval(c)) < (unsigned int)radix) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700542 if (!seendigit && v) {
543 int l = log2tbl[v];
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700544
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700545 seendigit = true;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700546 mp = mult;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700547 ms = (LIMB_BITS-1)-l;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700548
H. Peter Anvin3646e7d2017-04-05 21:47:20 -0700549 twopwr += l+1-bits;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700550 }
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700551
H. Peter Anvin214f5492007-10-15 19:46:32 -0700552 if (seendigit) {
H. Peter Anvin79a070e2018-11-26 14:17:40 -0800553 if (ms < 0) {
554 /* Cast to fp_2limb as ms == -LIMB_BITS is possible. */
555 *mp |= (fp_2limb)v >> -ms;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700556 mp++;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700557 if (mp > &mult[MANT_LIMBS])
558 mp = &mult[MANT_LIMBS]; /* Guard slot */
559 ms += LIMB_BITS;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700560 }
H. Peter Anvin79a070e2018-11-26 14:17:40 -0800561 *mp |= v << ms;
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700562 ms -= bits;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700563
H. Peter Anvin214f5492007-10-15 19:46:32 -0700564 if (!seendot)
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700565 twopwr += bits;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700566 } else {
567 if (seendot)
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700568 twopwr -= bits;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700569 }
570 } else if (c == 'p' || c == 'P') {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400571 int32_t e;
572 e = read_exponent(string, 20000);
573 if (e == INT32_MAX)
574 return false;
575 twopwr += e;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700576 break;
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400577 } else if (c == '_') {
578 /* ignore */
H. Peter Anvin214f5492007-10-15 19:46:32 -0700579 } else {
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300580 nasm_nonfatal("floating-point constant: `%c' is invalid character", c);
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700581 return false;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700582 }
583 }
584
585 if (!seendigit) {
H. Peter Anvin5aa689f2008-07-03 20:11:30 -0700586 memset(mant, 0, MANT_LIMBS*sizeof(fp_limb)); /* Zero */
H. Peter Anvin214f5492007-10-15 19:46:32 -0700587 *exponent = 0;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700588 } else {
H. Peter Anvin5aa689f2008-07-03 20:11:30 -0700589 memcpy(mant, mult, MANT_LIMBS*sizeof(fp_limb));
H. Peter Anvin214f5492007-10-15 19:46:32 -0700590 *exponent = twopwr;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700591 }
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700592
593 return true;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700594}
595
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000596/*
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700597 * Shift a mantissa to the right by i bits.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000598 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700599static void ieee_shr(fp_limb *mant, int i)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000600{
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700601 fp_limb n, m;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700602 int j = 0;
603 int sr, sl, offs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000604
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700605 sr = i % LIMB_BITS; sl = LIMB_BITS-sr;
606 offs = i/LIMB_BITS;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700607
608 if (sr == 0) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400609 if (offs)
610 for (j = MANT_LIMBS-1; j >= offs; j--)
611 mant[j] = mant[j-offs];
Adam Majerc7c28352018-07-05 17:40:24 +0200612 } else if (MANT_LIMBS-1-offs < 0) {
613 j = MANT_LIMBS-1;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700614 } else {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400615 n = mant[MANT_LIMBS-1-offs] >> sr;
616 for (j = MANT_LIMBS-1; j > offs; j--) {
617 m = mant[j-offs-1];
618 mant[j] = (m << sl) | n;
619 n = m >> sr;
620 }
621 mant[j--] = n;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000622 }
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700623 while (j >= 0)
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400624 mant[j--] = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000625}
626
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700627/* Produce standard IEEE formats, with implicit or explicit integer
628 bit; this makes the following assumptions:
629
630 - the sign bit is the MSB, followed by the exponent,
631 followed by the integer bit if present.
H. Peter Anvine31747e2007-09-18 17:50:34 -0700632 - the sign bit plus exponent fit in 16 bits.
633 - the exponent bias is 2^(n-1)-1 for an n-bit exponent */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000634
H. Peter Anvine31747e2007-09-18 17:50:34 -0700635struct ieee_format {
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700636 int bytes;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700637 int mantissa; /* Fractional bits in the mantissa */
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400638 int explicit; /* Explicit integer */
H. Peter Anvin214f5492007-10-15 19:46:32 -0700639 int exponent; /* Bits in the exponent */
H. Peter Anvine31747e2007-09-18 17:50:34 -0700640};
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000641
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700642/*
643 * The 16- and 128-bit formats are expected to be in IEEE 754r.
644 * AMD SSE5 uses the 16-bit format.
645 *
646 * The 32- and 64-bit formats are the original IEEE 754 formats.
647 *
648 * The 80-bit format is x87-specific, but widely used.
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700649 *
650 * The 8-bit format appears to be the consensus 8-bit floating-point
651 * format. It is apparently used in graphics applications.
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700652 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700653static const struct ieee_format ieee_8 = { 1, 3, 0, 4 };
654static const struct ieee_format ieee_16 = { 2, 10, 0, 5 };
655static const struct ieee_format ieee_32 = { 4, 23, 0, 8 };
656static const struct ieee_format ieee_64 = { 8, 52, 0, 11 };
657static const struct ieee_format ieee_80 = { 10, 63, 1, 15 };
658static const struct ieee_format ieee_128 = { 16, 112, 0, 15 };
H. Peter Anvine31747e2007-09-18 17:50:34 -0700659
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700660/* Types of values we can generate */
661enum floats {
662 FL_ZERO,
663 FL_DENORMAL,
664 FL_NORMAL,
665 FL_INFINITY,
666 FL_QNAN,
667 FL_SNAN
668};
669
H. Peter Anvin63ebf162008-07-03 20:16:07 -0700670static int to_packed_bcd(const char *str, const char *p,
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400671 int s, uint8_t *result,
672 const struct ieee_format *fmt)
H. Peter Anvin45f22922008-07-03 20:12:37 -0700673{
674 int n = 0;
675 char c;
676 int tv = -1;
H. Peter Anvin45f22922008-07-03 20:12:37 -0700677
678 if (fmt != &ieee_80) {
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300679 nasm_nonfatal("packed BCD requires an 80-bit format");
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400680 return 0;
H. Peter Anvin45f22922008-07-03 20:12:37 -0700681 }
682
683 while (p >= str) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400684 c = *p--;
685 if (c >= '0' && c <= '9') {
686 if (tv < 0) {
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300687 if (n == 9)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -0800688 nasm_warn(WARN_OTHER|ERR_PASS2, "packed BCD truncated to 18 digits");
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400689 tv = c-'0';
690 } else {
691 if (n < 9)
692 *result++ = tv + ((c-'0') << 4);
693 n++;
694 tv = -1;
695 }
696 } else if (c == '_') {
697 /* do nothing */
698 } else {
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300699 nasm_nonfatal("invalid character `%c' in packed BCD constant", c);
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400700 return 0;
701 }
H. Peter Anvin45f22922008-07-03 20:12:37 -0700702 }
703 if (tv >= 0) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400704 if (n < 9)
705 *result++ = tv;
706 n++;
H. Peter Anvin45f22922008-07-03 20:12:37 -0700707 }
708 while (n < 9) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400709 *result++ = 0;
710 n++;
H. Peter Anvin45f22922008-07-03 20:12:37 -0700711 }
712 *result = (s < 0) ? 0x80 : 0;
713
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400714 return 1; /* success */
H. Peter Anvin45f22922008-07-03 20:12:37 -0700715}
716
717static int to_float(const char *str, int s, uint8_t *result,
H. Peter Anvin214f5492007-10-15 19:46:32 -0700718 const struct ieee_format *fmt)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000719{
H. Peter Anvinf7bd02a2008-01-21 16:19:52 -0800720 fp_limb mant[MANT_LIMBS];
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700721 int32_t exponent = 0;
H. Peter Anvin45f22922008-07-03 20:12:37 -0700722 const int32_t expmax = 1 << (fmt->exponent - 1);
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700723 fp_limb one_mask = LIMB_TOP_BIT >>
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400724 ((fmt->exponent+fmt->explicit) % LIMB_BITS);
H. Peter Anvin45f22922008-07-03 20:12:37 -0700725 const int one_pos = (fmt->exponent+fmt->explicit)/LIMB_BITS;
H. Peter Anvine31747e2007-09-18 17:50:34 -0700726 int i;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700727 int shift;
728 enum floats type;
729 bool ok;
H. Peter Anvin45f22922008-07-03 20:12:37 -0700730 const bool minus = s < 0;
731 const int bits = fmt->bytes * 8;
732 const char *strend;
733
H. Peter Anvinc5136902018-06-15 18:20:17 -0700734 nasm_assert(str[0]);
H. Peter Anvin45f22922008-07-03 20:12:37 -0700735
736 strend = strchr(str, '\0');
737 if (strend[-1] == 'P' || strend[-1] == 'p')
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400738 return to_packed_bcd(str, strend-2, s, result, fmt);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000739
H. Peter Anvinf48bc6f2007-09-18 21:55:56 -0700740 if (str[0] == '_') {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400741 /* Special tokens */
H. Peter Anvine31747e2007-09-18 17:50:34 -0700742
H. Peter Anvin214f5492007-10-15 19:46:32 -0700743 switch (str[2]) {
744 case 'n': /* __nan__ */
745 case 'N':
746 case 'q': /* __qnan__ */
747 case 'Q':
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400748 type = FL_QNAN;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700749 break;
750 case 's': /* __snan__ */
751 case 'S':
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400752 type = FL_SNAN;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700753 break;
754 case 'i': /* __infinity__ */
755 case 'I':
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400756 type = FL_INFINITY;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700757 break;
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400758 default:
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300759 nasm_nonfatal("internal error: unknown FP constant token `%s'", str);
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400760 type = FL_QNAN;
761 break;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700762 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000763 } else {
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700764 if (str[0] == '0') {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400765 switch (str[1]) {
766 case 'x': case 'X':
767 case 'h': case 'H':
768 ok = ieee_flconvert_bin(str+2, 4, mant, &exponent);
769 break;
770 case 'o': case 'O':
771 case 'q': case 'Q':
772 ok = ieee_flconvert_bin(str+2, 3, mant, &exponent);
773 break;
774 case 'b': case 'B':
775 case 'y': case 'Y':
776 ok = ieee_flconvert_bin(str+2, 1, mant, &exponent);
777 break;
778 case 'd': case 'D':
779 case 't': case 'T':
780 ok = ieee_flconvert(str+2, mant, &exponent);
781 break;
782 case 'p': case 'P':
783 return to_packed_bcd(str+2, strend-1, s, result, fmt);
784 default:
785 /* Leading zero was just a zero? */
786 ok = ieee_flconvert(str, mant, &exponent);
787 break;
788 }
789 } else if (str[0] == '$') {
790 ok = ieee_flconvert_bin(str+1, 4, mant, &exponent);
791 } else {
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700792 ok = ieee_flconvert(str, mant, &exponent);
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400793 }
H. Peter Anvin214f5492007-10-15 19:46:32 -0700794
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400795 if (!ok) {
796 type = FL_QNAN;
797 } else if (mant[0] & LIMB_TOP_BIT) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700798 /*
799 * Non-zero.
800 */
801 exponent--;
802 if (exponent >= 2 - expmax && exponent <= expmax) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400803 type = FL_NORMAL;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700804 } else if (exponent > 0) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800805 nasm_warn(WARN_FLOAT_OVERFLOW|ERR_PASS2,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800806 "overflow in floating-point constant");
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400807 type = FL_INFINITY;
808 } else {
809 /* underflow or denormal; the denormal code handles
810 actual underflow. */
811 type = FL_DENORMAL;
812 }
813 } else {
814 /* Zero */
815 type = FL_ZERO;
816 }
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700817 }
H. Peter Anvin214f5492007-10-15 19:46:32 -0700818
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700819 switch (type) {
820 case FL_ZERO:
H. Peter Anvin125c8782007-10-16 11:32:58 -0700821 zero:
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400822 memset(mant, 0, sizeof mant);
823 break;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700824
825 case FL_DENORMAL:
826 {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400827 shift = -(exponent + expmax - 2 - fmt->exponent)
828 + fmt->explicit;
829 ieee_shr(mant, shift);
830 ieee_round(minus, mant, bits);
831 if (mant[one_pos] & one_mask) {
832 /* One's position is set, we rounded up into normal range */
833 exponent = 1;
834 if (!fmt->explicit)
835 mant[one_pos] &= ~one_mask; /* remove explicit one */
836 mant[0] |= exponent << (LIMB_BITS-1 - fmt->exponent);
837 } else {
838 if (daz || is_zero(mant)) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800839 /*!
840 *!float-underflow [off] floating point underflow
841 *! warns about floating point underflow (a nonzero
842 *! constant rounded to zero.)
843 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800844 nasm_warn(WARN_FLOAT_UNDERFLOW|ERR_PASS2,
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300845 "underflow in floating-point constant");
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400846 goto zero;
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800847 } else {
848 /*!
849 *!float-denorm [off] floating point denormal
850 *! warns about denormal floating point constants.
851 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800852 nasm_warn(WARN_FLOAT_DENORM|ERR_PASS2,
Cyrill Gorcunov194f9332018-12-01 20:01:40 +0300853 "denormal floating-point constant");
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800854 }
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400855 }
856 break;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700857 }
858
859 case FL_NORMAL:
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400860 exponent += expmax - 1;
861 ieee_shr(mant, fmt->exponent+fmt->explicit);
862 ieee_round(minus, mant, bits);
863 /* did we scale up by one? */
864 if (test_bit(mant, fmt->exponent+fmt->explicit-1)) {
865 ieee_shr(mant, 1);
866 exponent++;
867 if (exponent >= (expmax << 1)-1) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800868 /*!
869 *!float-overflow [on] floating point overflow
870 *! warns about floating point underflow.
871 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800872 nasm_warn(WARN_FLOAT_OVERFLOW|ERR_PASS2,
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800873 "overflow in floating-point constant");
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400874 type = FL_INFINITY;
875 goto overflow;
876 }
877 }
H. Peter Anvin70653092007-10-19 14:42:29 -0700878
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400879 if (!fmt->explicit)
880 mant[one_pos] &= ~one_mask; /* remove explicit one */
881 mant[0] |= exponent << (LIMB_BITS-1 - fmt->exponent);
882 break;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700883
884 case FL_INFINITY:
885 case FL_QNAN:
886 case FL_SNAN:
H. Peter Anvin125c8782007-10-16 11:32:58 -0700887 overflow:
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400888 memset(mant, 0, sizeof mant);
889 mant[0] = (((fp_limb)1 << fmt->exponent)-1)
890 << (LIMB_BITS-1 - fmt->exponent);
891 if (fmt->explicit)
892 mant[one_pos] |= one_mask;
893 if (type == FL_QNAN)
894 set_bit(mant, fmt->exponent+fmt->explicit+1);
895 else if (type == FL_SNAN)
896 set_bit(mant, fmt->exponent+fmt->explicit+fmt->mantissa);
897 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000898 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000899
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700900 mant[0] |= minus ? LIMB_TOP_BIT : 0;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700901
H. Peter Anvinf7bd02a2008-01-21 16:19:52 -0800902 for (i = fmt->bytes - 1; i >= 0; i--)
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400903 *result++ = mant[i/LIMB_BYTES] >> (((LIMB_BYTES-1)-(i%LIMB_BYTES))*8);
H. Peter Anvine31747e2007-09-18 17:50:34 -0700904
905 return 1; /* success */
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700906}
907
H. Peter Anvin215186f2016-02-17 20:27:41 -0800908int float_const(const char *number, int sign, uint8_t *result, int bytes)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000909{
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700910 switch (bytes) {
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700911 case 1:
912 return to_float(number, sign, result, &ieee_8);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700913 case 2:
H. Peter Anvin214f5492007-10-15 19:46:32 -0700914 return to_float(number, sign, result, &ieee_16);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700915 case 4:
H. Peter Anvin214f5492007-10-15 19:46:32 -0700916 return to_float(number, sign, result, &ieee_32);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700917 case 8:
H. Peter Anvin214f5492007-10-15 19:46:32 -0700918 return to_float(number, sign, result, &ieee_64);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700919 case 10:
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700920 return to_float(number, sign, result, &ieee_80);
H. Peter Anvine31747e2007-09-18 17:50:34 -0700921 case 16:
H. Peter Anvin214f5492007-10-15 19:46:32 -0700922 return to_float(number, sign, result, &ieee_128);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700923 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -0700924 nasm_panic("strange value %d passed to float_const", bytes);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000925 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000926 }
927}
H. Peter Anvinf6c9e652007-10-16 14:40:27 -0700928
929/* Set floating-point options */
930int float_option(const char *option)
931{
932 if (!nasm_stricmp(option, "daz")) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400933 daz = true;
934 return 0;
H. Peter Anvinf6c9e652007-10-16 14:40:27 -0700935 } else if (!nasm_stricmp(option, "nodaz")) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400936 daz = false;
937 return 0;
H. Peter Anvinf6c9e652007-10-16 14:40:27 -0700938 } else if (!nasm_stricmp(option, "near")) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400939 rc = FLOAT_RC_NEAR;
940 return 0;
H. Peter Anvinf6c9e652007-10-16 14:40:27 -0700941 } else if (!nasm_stricmp(option, "down")) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400942 rc = FLOAT_RC_DOWN;
943 return 0;
H. Peter Anvinf6c9e652007-10-16 14:40:27 -0700944 } else if (!nasm_stricmp(option, "up")) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400945 rc = FLOAT_RC_UP;
946 return 0;
H. Peter Anvinf6c9e652007-10-16 14:40:27 -0700947 } else if (!nasm_stricmp(option, "zero")) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400948 rc = FLOAT_RC_ZERO;
949 return 0;
H. Peter Anvinf6c9e652007-10-16 14:40:27 -0700950 } else if (!nasm_stricmp(option, "default")) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400951 rc = FLOAT_RC_NEAR;
952 daz = false;
953 return 0;
H. Peter Anvinf6c9e652007-10-16 14:40:27 -0700954 } else {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400955 return -1; /* Unknown option */
H. Peter Anvinf6c9e652007-10-16 14:40:27 -0700956 }
957}