blob: 7d313a5917f42e549107e996b33d09d77af80b0d [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunov1598a232009-10-11 13:17:52 +04002 *
H. Peter Anvin3646e7d2017-04-05 21:47:20 -07003 * Copyright 1996-2017 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 {
H. Peter Anvinaf59af42018-07-06 03:11:52 -0700185 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400186 "invalid character in floating-point constant %s: '%c'",
187 "exponent", *string);
188 return INT32_MAX;
189 }
190 string++;
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700191 }
192
193 return neg ? -i : i;
194}
195
196/*
197 * ---------------------------------------------------------------------------
H. Peter Anvin214f5492007-10-15 19:46:32 -0700198 * convert
199 * ---------------------------------------------------------------------------
200 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700201static bool ieee_flconvert(const char *string, fp_limb *mant,
H. Peter Anvin214f5492007-10-15 19:46:32 -0700202 int32_t * exponent)
203{
204 char digits[MANT_DIGITS];
205 char *p, *q, *r;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700206 fp_limb mult[MANT_LIMBS], bit;
207 fp_limb *m;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700208 int32_t tenpwr, twopwr;
209 int32_t extratwos;
210 bool started, seendot, warned;
H. Peter Anvin136dcdb2007-11-12 18:25:24 -0800211
212 warned = false;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700213 p = digits;
214 tenpwr = 0;
H. Peter Anvinfab3a6c2007-10-16 11:48:07 -0700215 started = seendot = false;
H. Peter Anvin136dcdb2007-11-12 18:25:24 -0800216
H. Peter Anvin214f5492007-10-15 19:46:32 -0700217 while (*string && *string != 'E' && *string != 'e') {
218 if (*string == '.') {
219 if (!seendot) {
220 seendot = true;
221 } else {
H. Peter Anvinaf59af42018-07-06 03:11:52 -0700222 nasm_error(ERR_NONFATAL,
H. Peter Anvin214f5492007-10-15 19:46:32 -0700223 "too many periods in floating-point constant");
224 return false;
225 }
226 } else if (*string >= '0' && *string <= '9') {
227 if (*string == '0' && !started) {
228 if (seendot) {
229 tenpwr--;
230 }
231 } else {
232 started = true;
233 if (p < digits + sizeof(digits)) {
234 *p++ = *string - '0';
235 } else {
236 if (!warned) {
H. Peter Anvinaf59af42018-07-06 03:11:52 -0700237 nasm_error(ERR_WARNING|ERR_WARN_FL_TOOLONG|ERR_PASS2,
H. Peter Anvin214f5492007-10-15 19:46:32 -0700238 "floating-point constant significand contains "
239 "more than %i digits", MANT_DIGITS);
240 warned = true;
241 }
242 }
243 if (!seendot) {
244 tenpwr++;
245 }
246 }
247 } else if (*string == '_') {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700248 /* do nothing */
249 } else {
H. Peter Anvinaf59af42018-07-06 03:11:52 -0700250 nasm_error(ERR_NONFATAL|ERR_PASS2,
H. Peter Anvin214f5492007-10-15 19:46:32 -0700251 "invalid character in floating-point constant %s: '%c'",
252 "significand", *string);
253 return false;
254 }
255 string++;
256 }
H. Peter Anvin70653092007-10-19 14:42:29 -0700257
H. Peter Anvin214f5492007-10-15 19:46:32 -0700258 if (*string) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400259 int32_t e;
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700260
H. Peter Anvin214f5492007-10-15 19:46:32 -0700261 string++; /* eat the E */
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400262 e = read_exponent(string, 5000);
263 if (e == INT32_MAX)
264 return false;
265 tenpwr += e;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700266 }
267
268 /*
269 * At this point, the memory interval [digits,p) contains a
270 * series of decimal digits zzzzzzz, such that our number X
271 * satisfies X = 0.zzzzzzz * 10^tenpwr.
272 */
273 q = digits;
274 dprintf(("X = 0."));
275 while (q < p) {
276 dprintf(("%c", *q + '0'));
277 q++;
278 }
279 dprintf((" * 10^%i\n", tenpwr));
280
281 /*
282 * Now convert [digits,p) to our internal representation.
283 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700284 bit = LIMB_TOP_BIT;
285 for (m = mant; m < mant + MANT_LIMBS; m++) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700286 *m = 0;
287 }
288 m = mant;
289 q = digits;
290 started = false;
291 twopwr = 0;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700292 while (m < mant + MANT_LIMBS) {
293 fp_limb carry = 0;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700294 while (p > q && !p[-1]) {
295 p--;
296 }
297 if (p <= q) {
298 break;
299 }
300 for (r = p; r-- > q;) {
301 int32_t i;
302 i = 2 * *r + carry;
303 if (i >= 10) {
304 carry = 1;
305 i -= 10;
306 } else {
307 carry = 0;
308 }
309 *r = i;
310 }
311 if (carry) {
312 *m |= bit;
313 started = true;
314 }
315 if (started) {
316 if (bit == 1) {
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700317 bit = LIMB_TOP_BIT;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700318 m++;
319 } else {
320 bit >>= 1;
321 }
322 } else {
323 twopwr--;
324 }
325 }
326 twopwr += tenpwr;
327
328 /*
329 * At this point, the 'mant' array contains the first frac-
330 * tional places of a base-2^16 real number which when mul-
331 * tiplied by 2^twopwr and 5^tenpwr gives X.
332 */
333 dprintf(("X = " MANT_FMT " * 2^%i * 5^%i\n", MANT_ARG, twopwr,
334 tenpwr));
335
336 /*
337 * Now multiply 'mant' by 5^tenpwr.
338 */
339 if (tenpwr < 0) { /* mult = 5^-1 = 0.2 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700340 for (m = mult; m < mult + MANT_LIMBS - 1; m++) {
341 *m = LIMB_BYTE(0xcc);
H. Peter Anvin214f5492007-10-15 19:46:32 -0700342 }
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700343 mult[MANT_LIMBS - 1] = LIMB_BYTE(0xcc)+1;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700344 extratwos = -2;
345 tenpwr = -tenpwr;
346
347 /*
348 * If tenpwr was 1000...000b, then it becomes 1000...000b. See
349 * the "ANSI C" comment below for more details on that case.
350 *
351 * Because we already truncated tenpwr to +5000...-5000 inside
352 * the exponent parsing code, this shouldn't happen though.
353 */
354 } else if (tenpwr > 0) { /* mult = 5^+1 = 5.0 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700355 mult[0] = (fp_limb)5 << (LIMB_BITS-3); /* 0xA000... */
356 for (m = mult + 1; m < mult + MANT_LIMBS; m++) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700357 *m = 0;
358 }
359 extratwos = 3;
360 } else {
361 extratwos = 0;
362 }
363 while (tenpwr) {
364 dprintf(("loop=" MANT_FMT " * 2^%i * 5^%i (%i)\n", MANT_ARG,
365 twopwr, tenpwr, extratwos));
366 if (tenpwr & 1) {
367 dprintf(("mant*mult\n"));
368 twopwr += extratwos + float_multiply(mant, mult);
369 }
370 dprintf(("mult*mult\n"));
371 extratwos = extratwos * 2 + float_multiply(mult, mult);
372 tenpwr >>= 1;
373
374 /*
375 * In ANSI C, the result of right-shifting a signed integer is
376 * considered implementation-specific. To ensure that the loop
377 * terminates even if tenpwr was 1000...000b to begin with, we
378 * manually clear the MSB, in case a 1 was shifted in.
379 *
380 * Because we already truncated tenpwr to +5000...-5000 inside
381 * the exponent parsing code, this shouldn't matter; neverthe-
382 * less it is the right thing to do here.
383 */
384 tenpwr &= (uint32_t) - 1 >> 1;
385 }
386
387 /*
388 * At this point, the 'mant' array contains the first frac-
389 * tional places of a base-2^16 real number in [0.5,1) that
390 * when multiplied by 2^twopwr gives X. Or it contains zero
391 * of course. We are done.
392 */
393 *exponent = twopwr;
394 return true;
395}
396
397/*
398 * ---------------------------------------------------------------------------
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700399 * operations of specific bits
400 * ---------------------------------------------------------------------------
401 */
402
403/* Set a bit, using *bigendian* bit numbering (0 = MSB) */
404static void set_bit(fp_limb *mant, int bit)
405{
406 mant[bit/LIMB_BITS] |= LIMB_TOP_BIT >> (bit & (LIMB_BITS-1));
407}
408
409/* Test a single bit */
410static int test_bit(const fp_limb *mant, int bit)
411{
412 return (mant[bit/LIMB_BITS] >> (~bit & (LIMB_BITS-1))) & 1;
413}
414
415/* Report if the mantissa value is all zero */
416static bool is_zero(const fp_limb *mant)
417{
418 int i;
419
420 for (i = 0; i < MANT_LIMBS; i++)
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400421 if (mant[i])
422 return false;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700423
424 return true;
425}
426
427/*
428 * ---------------------------------------------------------------------------
H. Peter Anvin214f5492007-10-15 19:46:32 -0700429 * round a mantissa off after i words
430 * ---------------------------------------------------------------------------
431 */
432
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400433#define ROUND_COLLECT_BITS \
434 do { \
435 m = mant[i] & (2*bit-1); \
436 for (j = i+1; j < MANT_LIMBS; j++) \
437 m = m | mant[j]; \
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700438 } while (0)
H. Peter Anvin214f5492007-10-15 19:46:32 -0700439
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400440#define ROUND_ABS_DOWN \
441 do { \
442 mant[i] &= ~(bit-1); \
443 for (j = i+1; j < MANT_LIMBS; j++) \
444 mant[j] = 0; \
445 return false; \
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700446 } while (0)
H. Peter Anvin214f5492007-10-15 19:46:32 -0700447
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400448#define ROUND_ABS_UP \
449 do { \
450 mant[i] = (mant[i] & ~(bit-1)) + bit; \
451 for (j = i+1; j < MANT_LIMBS; j++) \
452 mant[j] = 0; \
453 while (i > 0 && !mant[i]) \
454 ++mant[--i]; \
455 return !mant[0]; \
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700456 } while (0)
H. Peter Anvin214f5492007-10-15 19:46:32 -0700457
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700458static bool ieee_round(bool minus, fp_limb *mant, int bits)
H. Peter Anvin214f5492007-10-15 19:46:32 -0700459{
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700460 fp_limb m = 0;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700461 int32_t j;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700462 int i = bits / LIMB_BITS;
463 int p = bits % LIMB_BITS;
464 fp_limb bit = LIMB_TOP_BIT >> p;
465
466 if (rc == FLOAT_RC_NEAR) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400467 if (mant[i] & bit) {
468 mant[i] &= ~bit;
469 ROUND_COLLECT_BITS;
470 mant[i] |= bit;
471 if (m) {
472 ROUND_ABS_UP;
473 } else {
474 if (test_bit(mant, bits-1)) {
475 ROUND_ABS_UP;
476 } else {
477 ROUND_ABS_DOWN;
478 }
479 }
480 } else {
481 ROUND_ABS_DOWN;
482 }
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700483 } else if (rc == FLOAT_RC_ZERO ||
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400484 rc == (minus ? FLOAT_RC_UP : FLOAT_RC_DOWN)) {
485 ROUND_ABS_DOWN;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700486 } else {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400487 /* rc == (minus ? FLOAT_RC_DOWN : FLOAT_RC_UP) */
488 /* Round toward +/- infinity */
489 ROUND_COLLECT_BITS;
490 if (m) {
491 ROUND_ABS_UP;
492 } else {
493 ROUND_ABS_DOWN;
494 }
H. Peter Anvin214f5492007-10-15 19:46:32 -0700495 }
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700496 return false;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700497}
498
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700499/* Returns a value >= 16 if not a valid hex digit */
500static unsigned int hexval(char c)
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700501{
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700502 unsigned int v = (unsigned char) c;
503
504 if (v >= '0' && v <= '9')
505 return v - '0';
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700506 else
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700507 return (v|0x20) - 'a' + 10;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700508}
509
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700510/* Handle floating-point numbers with radix 2^bits and binary exponent */
511static bool ieee_flconvert_bin(const char *string, int bits,
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400512 fp_limb *mant, int32_t *exponent)
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700513{
514 static const int log2tbl[16] =
H. Peter Anvin214f5492007-10-15 19:46:32 -0700515 { -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3 };
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700516 fp_limb mult[MANT_LIMBS + 1], *mp;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700517 int ms;
518 int32_t twopwr;
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700519 bool seendot, seendigit;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700520 unsigned char c;
H. Peter Anvin45f22922008-07-03 20:12:37 -0700521 const int radix = 1 << bits;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700522 fp_limb v;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700523
524 twopwr = 0;
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700525 seendot = seendigit = false;
H. Peter Anvin82f9f632007-09-24 20:53:48 -0700526 ms = 0;
527 mp = NULL;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700528
529 memset(mult, 0, sizeof mult);
530
531 while ((c = *string++) != '\0') {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700532 if (c == '.') {
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700533 if (!seendot)
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700534 seendot = true;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700535 else {
H. Peter Anvinaf59af42018-07-06 03:11:52 -0700536 nasm_error(ERR_NONFATAL,
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700537 "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) {
552 if (ms <= 0) {
553 *mp |= v >> -ms;
554 mp++;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700555 if (mp > &mult[MANT_LIMBS])
556 mp = &mult[MANT_LIMBS]; /* Guard slot */
557 ms += LIMB_BITS;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700558 }
Michael Bradshawfd143102018-11-21 11:03:13 -0800559 *mp |= v << (ms % (sizeof(fp_limb) * CHAR_BIT));
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700560 ms -= bits;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700561
H. Peter Anvin214f5492007-10-15 19:46:32 -0700562 if (!seendot)
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700563 twopwr += bits;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700564 } else {
565 if (seendot)
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700566 twopwr -= bits;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700567 }
568 } else if (c == 'p' || c == 'P') {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400569 int32_t e;
570 e = read_exponent(string, 20000);
571 if (e == INT32_MAX)
572 return false;
573 twopwr += e;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700574 break;
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400575 } else if (c == '_') {
576 /* ignore */
H. Peter Anvin214f5492007-10-15 19:46:32 -0700577 } else {
H. Peter Anvinaf59af42018-07-06 03:11:52 -0700578 nasm_error(ERR_NONFATAL,
H. Peter Anvin214f5492007-10-15 19:46:32 -0700579 "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) {
H. Peter Anvinaf59af42018-07-06 03:11:52 -0700678 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400679 "packed BCD requires an 80-bit format");
680 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) {
687 if (n == 9) {
H. Peter Anvinaf59af42018-07-06 03:11:52 -0700688 nasm_error(ERR_WARNING|ERR_PASS2,
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400689 "packed BCD truncated to 18 digits");
690 }
691 tv = c-'0';
692 } else {
693 if (n < 9)
694 *result++ = tv + ((c-'0') << 4);
695 n++;
696 tv = -1;
697 }
698 } else if (c == '_') {
699 /* do nothing */
700 } else {
H. Peter Anvinaf59af42018-07-06 03:11:52 -0700701 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400702 "invalid character `%c' in packed BCD constant", c);
703 return 0;
704 }
H. Peter Anvin45f22922008-07-03 20:12:37 -0700705 }
706 if (tv >= 0) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400707 if (n < 9)
708 *result++ = tv;
709 n++;
H. Peter Anvin45f22922008-07-03 20:12:37 -0700710 }
711 while (n < 9) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400712 *result++ = 0;
713 n++;
H. Peter Anvin45f22922008-07-03 20:12:37 -0700714 }
715 *result = (s < 0) ? 0x80 : 0;
716
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400717 return 1; /* success */
H. Peter Anvin45f22922008-07-03 20:12:37 -0700718}
719
720static int to_float(const char *str, int s, uint8_t *result,
H. Peter Anvin214f5492007-10-15 19:46:32 -0700721 const struct ieee_format *fmt)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000722{
H. Peter Anvinf7bd02a2008-01-21 16:19:52 -0800723 fp_limb mant[MANT_LIMBS];
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700724 int32_t exponent = 0;
H. Peter Anvin45f22922008-07-03 20:12:37 -0700725 const int32_t expmax = 1 << (fmt->exponent - 1);
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700726 fp_limb one_mask = LIMB_TOP_BIT >>
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400727 ((fmt->exponent+fmt->explicit) % LIMB_BITS);
H. Peter Anvin45f22922008-07-03 20:12:37 -0700728 const int one_pos = (fmt->exponent+fmt->explicit)/LIMB_BITS;
H. Peter Anvine31747e2007-09-18 17:50:34 -0700729 int i;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700730 int shift;
731 enum floats type;
732 bool ok;
H. Peter Anvin45f22922008-07-03 20:12:37 -0700733 const bool minus = s < 0;
734 const int bits = fmt->bytes * 8;
735 const char *strend;
736
737 if (!str[0]) {
H. Peter Anvind6d1b652016-03-03 14:36:01 -0800738 nasm_panic(0,
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400739 "internal errror: empty string passed to float_const");
740 return 0;
H. Peter Anvin45f22922008-07-03 20:12:37 -0700741 }
742
743 strend = strchr(str, '\0');
744 if (strend[-1] == 'P' || strend[-1] == 'p')
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400745 return to_packed_bcd(str, strend-2, s, result, fmt);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000746
H. Peter Anvinf48bc6f2007-09-18 21:55:56 -0700747 if (str[0] == '_') {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400748 /* Special tokens */
H. Peter Anvine31747e2007-09-18 17:50:34 -0700749
H. Peter Anvin214f5492007-10-15 19:46:32 -0700750 switch (str[2]) {
751 case 'n': /* __nan__ */
752 case 'N':
753 case 'q': /* __qnan__ */
754 case 'Q':
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400755 type = FL_QNAN;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700756 break;
757 case 's': /* __snan__ */
758 case 'S':
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400759 type = FL_SNAN;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700760 break;
761 case 'i': /* __infinity__ */
762 case 'I':
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400763 type = FL_INFINITY;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700764 break;
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400765 default:
H. Peter Anvinaf59af42018-07-06 03:11:52 -0700766 nasm_error(ERR_NONFATAL,
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400767 "internal error: unknown FP constant token `%s'\n", str);
768 type = FL_QNAN;
769 break;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700770 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000771 } else {
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700772 if (str[0] == '0') {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400773 switch (str[1]) {
774 case 'x': case 'X':
775 case 'h': case 'H':
776 ok = ieee_flconvert_bin(str+2, 4, mant, &exponent);
777 break;
778 case 'o': case 'O':
779 case 'q': case 'Q':
780 ok = ieee_flconvert_bin(str+2, 3, mant, &exponent);
781 break;
782 case 'b': case 'B':
783 case 'y': case 'Y':
784 ok = ieee_flconvert_bin(str+2, 1, mant, &exponent);
785 break;
786 case 'd': case 'D':
787 case 't': case 'T':
788 ok = ieee_flconvert(str+2, mant, &exponent);
789 break;
790 case 'p': case 'P':
791 return to_packed_bcd(str+2, strend-1, s, result, fmt);
792 default:
793 /* Leading zero was just a zero? */
794 ok = ieee_flconvert(str, mant, &exponent);
795 break;
796 }
797 } else if (str[0] == '$') {
798 ok = ieee_flconvert_bin(str+1, 4, mant, &exponent);
799 } else {
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700800 ok = ieee_flconvert(str, mant, &exponent);
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400801 }
H. Peter Anvin214f5492007-10-15 19:46:32 -0700802
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400803 if (!ok) {
804 type = FL_QNAN;
805 } else if (mant[0] & LIMB_TOP_BIT) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700806 /*
807 * Non-zero.
808 */
809 exponent--;
810 if (exponent >= 2 - expmax && exponent <= expmax) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400811 type = FL_NORMAL;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700812 } else if (exponent > 0) {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400813 if (pass0 == 1)
H. Peter Anvinaf59af42018-07-06 03:11:52 -0700814 nasm_error(ERR_WARNING|ERR_WARN_FL_OVERFLOW|ERR_PASS2,
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400815 "overflow in floating-point constant");
816 type = FL_INFINITY;
817 } else {
818 /* underflow or denormal; the denormal code handles
819 actual underflow. */
820 type = FL_DENORMAL;
821 }
822 } else {
823 /* Zero */
824 type = FL_ZERO;
825 }
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700826 }
H. Peter Anvin214f5492007-10-15 19:46:32 -0700827
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700828 switch (type) {
829 case FL_ZERO:
H. Peter Anvin125c8782007-10-16 11:32:58 -0700830 zero:
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400831 memset(mant, 0, sizeof mant);
832 break;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700833
834 case FL_DENORMAL:
835 {
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400836 shift = -(exponent + expmax - 2 - fmt->exponent)
837 + fmt->explicit;
838 ieee_shr(mant, shift);
839 ieee_round(minus, mant, bits);
840 if (mant[one_pos] & one_mask) {
841 /* One's position is set, we rounded up into normal range */
842 exponent = 1;
843 if (!fmt->explicit)
844 mant[one_pos] &= ~one_mask; /* remove explicit one */
845 mant[0] |= exponent << (LIMB_BITS-1 - fmt->exponent);
846 } else {
847 if (daz || is_zero(mant)) {
848 /* Flush denormals to zero */
H. Peter Anvinaf59af42018-07-06 03:11:52 -0700849 nasm_error(ERR_WARNING|ERR_WARN_FL_UNDERFLOW|ERR_PASS2,
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400850 "underflow in floating-point constant");
851 goto zero;
852 } else {
H. Peter Anvinaf59af42018-07-06 03:11:52 -0700853 nasm_error(ERR_WARNING|ERR_WARN_FL_DENORM|ERR_PASS2,
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400854 "denormal floating-point constant");
855 }
856 }
857 break;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700858 }
859
860 case FL_NORMAL:
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400861 exponent += expmax - 1;
862 ieee_shr(mant, fmt->exponent+fmt->explicit);
863 ieee_round(minus, mant, bits);
864 /* did we scale up by one? */
865 if (test_bit(mant, fmt->exponent+fmt->explicit-1)) {
866 ieee_shr(mant, 1);
867 exponent++;
868 if (exponent >= (expmax << 1)-1) {
H. Peter Anvinaf59af42018-07-06 03:11:52 -0700869 nasm_error(ERR_WARNING|ERR_WARN_FL_OVERFLOW|ERR_PASS2,
Cyrill Gorcunov1598a232009-10-11 13:17:52 +0400870 "overflow in floating-point constant");
871 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 Anvind6d1b652016-03-03 14:36:01 -0800921 nasm_panic(0, "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}