blob: 39bd3571318cb10e8b57f6c326c216d8f7836fef [file] [log] [blame]
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001/* float.c floating-point constant support for the Netwide Assembler
2 *
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the licence given in the file "Licence"
6 * distributed in the NASM archive.
7 *
8 * initial version 13/ix/96 by Simon Tatham
9 */
10
H. Peter Anvinfe501952007-10-02 21:53:51 -070011#include "compiler.h"
12
H. Peter Anvinfe2177f2007-09-18 18:31:26 -070013#include <ctype.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000014#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
Keith Kaniosb7a89542007-04-12 02:40:54 +000017#include <inttypes.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000018
19#include "nasm.h"
H. Peter Anvin214f5492007-10-15 19:46:32 -070020#include "float.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000021
22/*
H. Peter Anvin214f5492007-10-15 19:46:32 -070023 * -----------------
24 * local variables
25 * -----------------
26 */
27static efunc error;
28static bool daz = false; /* denormals as zero */
29static enum float_round rc = FLOAT_RC_NEAR; /* rounding control */
30
31/*
32 * -----------
33 * constants
34 * -----------
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000035 */
36
H. Peter Anvin2ce02742007-10-29 20:20:12 -070037/* "A limb is like a digit but bigger */
38typedef uint32_t fp_limb;
39typedef uint64_t fp_2limb;
40
41#define LIMB_BITS 32
42#define LIMB_BYTES (LIMB_BITS/8)
43#define LIMB_TOP_BIT ((fp_limb)1 << (LIMB_BITS-1))
44#define LIMB_MASK ((fp_limb)(~0))
45#define LIMB_ALL_BYTES ((fp_limb)0x01010101)
46#define LIMB_BYTE(x) ((x)*LIMB_ALL_BYTES)
47
48#if defined(__i386__) || defined(__x86_64__)
49#define put(a,b) (*(uint32_t *)(a) = (b))
50#else
51#define put(a,b) (((a)[0] = (b)), \
52 ((a)[1] = (b) >> 8), \
53 ((a)[2] = (b) >> 16), \
54 ((a)[3] = (b) >> 24))
55#endif
56
H. Peter Anvin214f5492007-10-15 19:46:32 -070057/* 112 bits + 64 bits for accuracy + 16 bits for rounding */
H. Peter Anvin2ce02742007-10-29 20:20:12 -070058#define MANT_LIMBS 6
H. Peter Anvin214f5492007-10-15 19:46:32 -070059
60/* 52 digits fit in 176 bits because 10^53 > 2^176 > 10^52 */
61#define MANT_DIGITS 52
62
H. Peter Anvin2ce02742007-10-29 20:20:12 -070063/* the format and the argument list depend on MANT_LIMBS */
64#define MANT_FMT "%08x_%08x_%08x_%08x_%08x_%08x"
H. Peter Anvin214f5492007-10-15 19:46:32 -070065#define MANT_ARG SOME_ARG(mant, 0)
66
67#define SOME_ARG(a,i) (a)[(i)+0], (a)[(i)+1], (a)[(i)+2], (a)[(i)+3], \
H. Peter Anvin2ce02742007-10-29 20:20:12 -070068 (a)[(i)+4], (a)[(i)+5]
H. Peter Anvin214f5492007-10-15 19:46:32 -070069
70/*
71 * ---------------------------------------------------------------------------
72 * emit a printf()-like debug message... but only if DEBUG_FLOAT was defined
73 * ---------------------------------------------------------------------------
74 */
75
76#ifdef DEBUG_FLOAT
77#define dprintf(x) printf x
78#else /* */
79#define dprintf(x) do { } while (0)
80#endif /* */
81
82/*
83 * ---------------------------------------------------------------------------
84 * multiply
85 * ---------------------------------------------------------------------------
86 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -070087static int float_multiply(fp_limb *to, fp_limb *from)
H. Peter Anvineba20a72002-04-30 20:53:55 +000088{
H. Peter Anvin2ce02742007-10-29 20:20:12 -070089 fp_2limb temp[MANT_LIMBS * 2];
90 int i, j;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000091
H. Peter Anvin70653092007-10-19 14:42:29 -070092 /*
H. Peter Anvin214f5492007-10-15 19:46:32 -070093 * guaranteed that top bit of 'from' is set -- so we only have
94 * to worry about _one_ bit shift to the left
95 */
96 dprintf(("%s=" MANT_FMT "\n", "mul1", SOME_ARG(to, 0)));
97 dprintf(("%s=" MANT_FMT "\n", "mul2", SOME_ARG(from, 0)));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000098
H. Peter Anvin214f5492007-10-15 19:46:32 -070099 memset(temp, 0, sizeof temp);
100
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700101 for (i = 0; i < MANT_LIMBS; i++) {
102 for (j = 0; j < MANT_LIMBS; j++) {
103 fp_2limb n;
104 n = (fp_2limb) to[i] * (fp_2limb) from[j];
105 temp[i + j] += n >> LIMB_BITS;
106 temp[i + j + 1] += (fp_limb)n;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107 }
H. Peter Anvin214f5492007-10-15 19:46:32 -0700108 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000109
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700110 for (i = MANT_LIMBS * 2; --i;) {
111 temp[i - 1] += temp[i] >> LIMB_BITS;
112 temp[i] &= LIMB_MASK;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000113 }
H. Peter Anvin214f5492007-10-15 19:46:32 -0700114
115 dprintf(("%s=" MANT_FMT "_" MANT_FMT "\n", "temp", SOME_ARG(temp, 0),
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700116 SOME_ARG(temp, MANT_LIMBS)));
H. Peter Anvin214f5492007-10-15 19:46:32 -0700117
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700118 if (temp[0] & LIMB_TOP_BIT) {
119 for (i = 0; i < MANT_LIMBS; i++) {
120 to[i] = temp[i] & LIMB_MASK;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700121 }
122 dprintf(("%s=" MANT_FMT " (%i)\n", "prod", SOME_ARG(to, 0), 0));
123 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000124 } else {
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700125 for (i = 0; i < MANT_LIMBS; i++) {
126 to[i] = (temp[i] << 1) + !!(temp[i + 1] & LIMB_TOP_BIT);
H. Peter Anvin214f5492007-10-15 19:46:32 -0700127 }
128 dprintf(("%s=" MANT_FMT " (%i)\n", "prod", SOME_ARG(to, 0), -1));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000129 return -1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000130 }
131}
132
H. Peter Anvin214f5492007-10-15 19:46:32 -0700133/*
134 * ---------------------------------------------------------------------------
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700135 * read an exponent; returns INT32_MAX on error
136 * ---------------------------------------------------------------------------
137 */
H. Peter Anvin3514ad02007-10-19 14:17:51 -0700138static int32_t read_exponent(const char *string, int32_t max)
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700139{
140 int32_t i = 0;
141 bool neg = false;
H. Peter Anvin70653092007-10-19 14:42:29 -0700142
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700143 if (*string == '+') {
144 string++;
145 } else if (*string == '-') {
146 neg = true;
147 string++;
148 }
149 while (*string) {
150 if (*string >= '0' && *string <= '9') {
151 i = (i * 10) + (*string - '0');
H. Peter Anvin70653092007-10-19 14:42:29 -0700152
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700153 /*
154 * To ensure that underflows and overflows are
155 * handled properly we must avoid wraparounds of
156 * the signed integer value that is used to hold
157 * the exponent. Therefore we cap the exponent at
158 * +/-5000, which is slightly more/less than
159 * what's required for normal and denormal numbers
160 * in single, double, and extended precision, but
161 * sufficient to avoid signed integer wraparound.
162 */
H. Peter Anvin3b2ad1b2007-10-21 15:32:33 -0700163 if (i > max)
164 i = max;
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700165 } else if (*string == '_') {
166 /* do nothing */
167 } else {
168 error(ERR_NONFATAL,
169 "invalid character in floating-point constant %s: '%c'",
170 "exponent", *string);
171 return INT32_MAX;
172 }
173 string++;
174 }
175
176 return neg ? -i : i;
177}
178
179/*
180 * ---------------------------------------------------------------------------
H. Peter Anvin214f5492007-10-15 19:46:32 -0700181 * convert
182 * ---------------------------------------------------------------------------
183 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700184static bool ieee_flconvert(const char *string, fp_limb *mant,
H. Peter Anvin214f5492007-10-15 19:46:32 -0700185 int32_t * exponent)
186{
187 char digits[MANT_DIGITS];
188 char *p, *q, *r;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700189 fp_limb mult[MANT_LIMBS], bit;
190 fp_limb *m;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700191 int32_t tenpwr, twopwr;
192 int32_t extratwos;
193 bool started, seendot, warned;
194 p = digits;
195 tenpwr = 0;
H. Peter Anvinfab3a6c2007-10-16 11:48:07 -0700196 started = seendot = false;
197 warned = (pass0 != 1);
H. Peter Anvin214f5492007-10-15 19:46:32 -0700198 while (*string && *string != 'E' && *string != 'e') {
199 if (*string == '.') {
200 if (!seendot) {
201 seendot = true;
202 } else {
203 error(ERR_NONFATAL,
204 "too many periods in floating-point constant");
205 return false;
206 }
207 } else if (*string >= '0' && *string <= '9') {
208 if (*string == '0' && !started) {
209 if (seendot) {
210 tenpwr--;
211 }
212 } else {
213 started = true;
214 if (p < digits + sizeof(digits)) {
215 *p++ = *string - '0';
216 } else {
217 if (!warned) {
H. Peter Anvin125c8782007-10-16 11:32:58 -0700218 error(ERR_WARNING|ERR_WARN_FL_TOOLONG,
H. Peter Anvin214f5492007-10-15 19:46:32 -0700219 "floating-point constant significand contains "
220 "more than %i digits", MANT_DIGITS);
221 warned = true;
222 }
223 }
224 if (!seendot) {
225 tenpwr++;
226 }
227 }
228 } else if (*string == '_') {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700229 /* do nothing */
230 } else {
231 error(ERR_NONFATAL,
232 "invalid character in floating-point constant %s: '%c'",
233 "significand", *string);
234 return false;
235 }
236 string++;
237 }
H. Peter Anvin70653092007-10-19 14:42:29 -0700238
H. Peter Anvin214f5492007-10-15 19:46:32 -0700239 if (*string) {
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700240 int32_t e;
241
H. Peter Anvin214f5492007-10-15 19:46:32 -0700242 string++; /* eat the E */
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700243 e = read_exponent(string, 5000);
244 if (e == INT32_MAX)
245 return false;
246 tenpwr += e;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700247 }
248
249 /*
250 * At this point, the memory interval [digits,p) contains a
251 * series of decimal digits zzzzzzz, such that our number X
252 * satisfies X = 0.zzzzzzz * 10^tenpwr.
253 */
254 q = digits;
255 dprintf(("X = 0."));
256 while (q < p) {
257 dprintf(("%c", *q + '0'));
258 q++;
259 }
260 dprintf((" * 10^%i\n", tenpwr));
261
262 /*
263 * Now convert [digits,p) to our internal representation.
264 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700265 bit = LIMB_TOP_BIT;
266 for (m = mant; m < mant + MANT_LIMBS; m++) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700267 *m = 0;
268 }
269 m = mant;
270 q = digits;
271 started = false;
272 twopwr = 0;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700273 while (m < mant + MANT_LIMBS) {
274 fp_limb carry = 0;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700275 while (p > q && !p[-1]) {
276 p--;
277 }
278 if (p <= q) {
279 break;
280 }
281 for (r = p; r-- > q;) {
282 int32_t i;
283 i = 2 * *r + carry;
284 if (i >= 10) {
285 carry = 1;
286 i -= 10;
287 } else {
288 carry = 0;
289 }
290 *r = i;
291 }
292 if (carry) {
293 *m |= bit;
294 started = true;
295 }
296 if (started) {
297 if (bit == 1) {
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700298 bit = LIMB_TOP_BIT;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700299 m++;
300 } else {
301 bit >>= 1;
302 }
303 } else {
304 twopwr--;
305 }
306 }
307 twopwr += tenpwr;
308
309 /*
310 * At this point, the 'mant' array contains the first frac-
311 * tional places of a base-2^16 real number which when mul-
312 * tiplied by 2^twopwr and 5^tenpwr gives X.
313 */
314 dprintf(("X = " MANT_FMT " * 2^%i * 5^%i\n", MANT_ARG, twopwr,
315 tenpwr));
316
317 /*
318 * Now multiply 'mant' by 5^tenpwr.
319 */
320 if (tenpwr < 0) { /* mult = 5^-1 = 0.2 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700321 for (m = mult; m < mult + MANT_LIMBS - 1; m++) {
322 *m = LIMB_BYTE(0xcc);
H. Peter Anvin214f5492007-10-15 19:46:32 -0700323 }
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700324 mult[MANT_LIMBS - 1] = LIMB_BYTE(0xcc)+1;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700325 extratwos = -2;
326 tenpwr = -tenpwr;
327
328 /*
329 * If tenpwr was 1000...000b, then it becomes 1000...000b. See
330 * the "ANSI C" comment below for more details on that case.
331 *
332 * Because we already truncated tenpwr to +5000...-5000 inside
333 * the exponent parsing code, this shouldn't happen though.
334 */
335 } else if (tenpwr > 0) { /* mult = 5^+1 = 5.0 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700336 mult[0] = (fp_limb)5 << (LIMB_BITS-3); /* 0xA000... */
337 for (m = mult + 1; m < mult + MANT_LIMBS; m++) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700338 *m = 0;
339 }
340 extratwos = 3;
341 } else {
342 extratwos = 0;
343 }
344 while (tenpwr) {
345 dprintf(("loop=" MANT_FMT " * 2^%i * 5^%i (%i)\n", MANT_ARG,
346 twopwr, tenpwr, extratwos));
347 if (tenpwr & 1) {
348 dprintf(("mant*mult\n"));
349 twopwr += extratwos + float_multiply(mant, mult);
350 }
351 dprintf(("mult*mult\n"));
352 extratwos = extratwos * 2 + float_multiply(mult, mult);
353 tenpwr >>= 1;
354
355 /*
356 * In ANSI C, the result of right-shifting a signed integer is
357 * considered implementation-specific. To ensure that the loop
358 * terminates even if tenpwr was 1000...000b to begin with, we
359 * manually clear the MSB, in case a 1 was shifted in.
360 *
361 * Because we already truncated tenpwr to +5000...-5000 inside
362 * the exponent parsing code, this shouldn't matter; neverthe-
363 * less it is the right thing to do here.
364 */
365 tenpwr &= (uint32_t) - 1 >> 1;
366 }
367
368 /*
369 * At this point, the 'mant' array contains the first frac-
370 * tional places of a base-2^16 real number in [0.5,1) that
371 * when multiplied by 2^twopwr gives X. Or it contains zero
372 * of course. We are done.
373 */
374 *exponent = twopwr;
375 return true;
376}
377
378/*
379 * ---------------------------------------------------------------------------
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700380 * operations of specific bits
381 * ---------------------------------------------------------------------------
382 */
383
384/* Set a bit, using *bigendian* bit numbering (0 = MSB) */
385static void set_bit(fp_limb *mant, int bit)
386{
387 mant[bit/LIMB_BITS] |= LIMB_TOP_BIT >> (bit & (LIMB_BITS-1));
388}
389
390/* Test a single bit */
391static int test_bit(const fp_limb *mant, int bit)
392{
393 return (mant[bit/LIMB_BITS] >> (~bit & (LIMB_BITS-1))) & 1;
394}
395
396/* Report if the mantissa value is all zero */
397static bool is_zero(const fp_limb *mant)
398{
399 int i;
400
401 for (i = 0; i < MANT_LIMBS; i++)
402 if (mant[i])
403 return false;
404
405 return true;
406}
407
408/*
409 * ---------------------------------------------------------------------------
H. Peter Anvin214f5492007-10-15 19:46:32 -0700410 * round a mantissa off after i words
411 * ---------------------------------------------------------------------------
412 */
413
414#define ROUND_COLLECT_BITS \
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700415 do { \
416 m = mant[i] & (2*bit-1); \
417 for (j = i+1; j < MANT_LIMBS; j++) \
418 m = m | mant[j]; \
419 } while (0)
H. Peter Anvin214f5492007-10-15 19:46:32 -0700420
421#define ROUND_ABS_DOWN \
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700422 do { \
423 mant[i] &= ~(bit-1); \
424 for (j = i+1; j < MANT_LIMBS; j++) \
425 mant[j] = 0; \
426 return false; \
427 } while (0)
H. Peter Anvin214f5492007-10-15 19:46:32 -0700428
429#define ROUND_ABS_UP \
430 do { \
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700431 mant[i] = (mant[i] & ~(bit-1)) + bit; \
432 for (j = i+1; j < MANT_LIMBS; j++) \
433 mant[j] = 0; \
434 while (i > 0 && !mant[i]) \
435 ++mant[--i]; \
436 return !mant[0]; \
437 } while (0)
H. Peter Anvin214f5492007-10-15 19:46:32 -0700438
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700439static bool ieee_round(bool minus, fp_limb *mant, int bits)
H. Peter Anvin214f5492007-10-15 19:46:32 -0700440{
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700441 fp_limb m = 0;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700442 int32_t j;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700443 int i = bits / LIMB_BITS;
444 int p = bits % LIMB_BITS;
445 fp_limb bit = LIMB_TOP_BIT >> p;
446
447 if (rc == FLOAT_RC_NEAR) {
448 if (mant[i] & bit) {
449 mant[i] &= ~bit;
450 ROUND_COLLECT_BITS;
451 mant[i] |= bit;
452 if (m) {
453 ROUND_ABS_UP;
454 } else {
455 if (test_bit(mant, bits-1)) {
456 ROUND_ABS_UP;
457 } else {
458 ROUND_ABS_DOWN;
459 }
460 }
461 } else {
462 ROUND_ABS_DOWN;
463 }
464 } else if (rc == FLOAT_RC_ZERO ||
465 rc == (minus ? FLOAT_RC_UP : FLOAT_RC_DOWN)) {
466 ROUND_ABS_DOWN;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700467 } else {
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700468 /* rc == (minus ? FLOAT_RC_DOWN : FLOAT_RC_UP) */
469 /* Round toward +/- infinity */
470 ROUND_COLLECT_BITS;
471 if (m) {
472 ROUND_ABS_UP;
473 } else {
474 ROUND_ABS_DOWN;
475 }
H. Peter Anvin214f5492007-10-15 19:46:32 -0700476 }
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700477 return false;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700478}
479
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700480/* Returns a value >= 16 if not a valid hex digit */
481static unsigned int hexval(char c)
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700482{
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700483 unsigned int v = (unsigned char) c;
484
485 if (v >= '0' && v <= '9')
486 return v - '0';
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700487 else
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700488 return (v|0x20) - 'a' + 10;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700489}
490
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700491/* Handle floating-point numbers with radix 2^bits and binary exponent */
492static bool ieee_flconvert_bin(const char *string, int bits,
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700493 fp_limb *mant, int32_t *exponent)
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700494{
495 static const int log2tbl[16] =
H. Peter Anvin214f5492007-10-15 19:46:32 -0700496 { -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3 };
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700497 fp_limb mult[MANT_LIMBS + 1], *mp;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700498 int ms;
499 int32_t twopwr;
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700500 bool seendot, seendigit;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700501 unsigned char c;
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700502 int radix = 1 << bits;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700503 fp_limb v;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700504
505 twopwr = 0;
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700506 seendot = seendigit = false;
H. Peter Anvin82f9f632007-09-24 20:53:48 -0700507 ms = 0;
508 mp = NULL;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700509
510 memset(mult, 0, sizeof mult);
511
512 while ((c = *string++) != '\0') {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700513 if (c == '.') {
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700514 if (!seendot)
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700515 seendot = true;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700516 else {
517 error(ERR_NONFATAL,
518 "too many periods in floating-point constant");
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700519 return false;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700520 }
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700521 } else if ((v = hexval(c)) < (unsigned int)radix) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700522 if (!seendigit && v) {
523 int l = log2tbl[v];
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700524
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700525 seendigit = true;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700526 mp = mult;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700527 ms = (LIMB_BITS-1)-l;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700528
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700529 twopwr = seendot ? twopwr-bits+l : l+1-bits;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700530 }
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700531
H. Peter Anvin214f5492007-10-15 19:46:32 -0700532 if (seendigit) {
533 if (ms <= 0) {
534 *mp |= v >> -ms;
535 mp++;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700536 if (mp > &mult[MANT_LIMBS])
537 mp = &mult[MANT_LIMBS]; /* Guard slot */
538 ms += LIMB_BITS;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700539 }
540 *mp |= v << ms;
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700541 ms -= bits;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700542
H. Peter Anvin214f5492007-10-15 19:46:32 -0700543 if (!seendot)
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700544 twopwr += bits;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700545 } else {
546 if (seendot)
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700547 twopwr -= bits;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700548 }
549 } else if (c == 'p' || c == 'P') {
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700550 int32_t e;
H. Peter Anvin3b2ad1b2007-10-21 15:32:33 -0700551 e = read_exponent(string, 20000);
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700552 if (e == INT32_MAX)
553 return false;
554 twopwr += e;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700555 break;
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700556 } else if (c == '_') {
557 /* ignore */
H. Peter Anvin214f5492007-10-15 19:46:32 -0700558 } else {
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700559 error(ERR_NONFATAL,
H. Peter Anvin214f5492007-10-15 19:46:32 -0700560 "floating-point constant: `%c' is invalid character", c);
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700561 return false;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700562 }
563 }
564
565 if (!seendigit) {
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700566 memset(mant, 0, sizeof mult); /* Zero */
H. Peter Anvin214f5492007-10-15 19:46:32 -0700567 *exponent = 0;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700568 } else {
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700569 memcpy(mant, mult, sizeof mult);
H. Peter Anvin214f5492007-10-15 19:46:32 -0700570 *exponent = twopwr;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700571 }
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700572
573 return true;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700574}
575
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000576/*
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700577 * Shift a mantissa to the right by i bits.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000578 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700579static void ieee_shr(fp_limb *mant, int i)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000580{
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700581 fp_limb n, m;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700582 int j = 0;
583 int sr, sl, offs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000584
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700585 sr = i % LIMB_BITS; sl = LIMB_BITS-sr;
586 offs = i/LIMB_BITS;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700587
588 if (sr == 0) {
589 if (offs)
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700590 for (j = MANT_LIMBS-1; j >= offs; j--)
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700591 mant[j] = mant[j-offs];
592 } else {
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700593 n = mant[MANT_LIMBS-1-offs] >> sr;
594 for (j = MANT_LIMBS-1; j > offs; j--) {
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700595 m = mant[j-offs-1];
596 mant[j] = (m << sl) | n;
597 n = m >> sr;
598 }
599 mant[j--] = n;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000600 }
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700601 while (j >= 0)
602 mant[j--] = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000603}
604
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700605/* Produce standard IEEE formats, with implicit or explicit integer
606 bit; this makes the following assumptions:
607
608 - the sign bit is the MSB, followed by the exponent,
609 followed by the integer bit if present.
H. Peter Anvine31747e2007-09-18 17:50:34 -0700610 - the sign bit plus exponent fit in 16 bits.
611 - the exponent bias is 2^(n-1)-1 for an n-bit exponent */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000612
H. Peter Anvine31747e2007-09-18 17:50:34 -0700613struct ieee_format {
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700614 int bytes;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700615 int mantissa; /* Fractional bits in the mantissa */
616 int explicit; /* Explicit integer */
H. Peter Anvin214f5492007-10-15 19:46:32 -0700617 int exponent; /* Bits in the exponent */
H. Peter Anvine31747e2007-09-18 17:50:34 -0700618};
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000619
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700620/*
621 * The 16- and 128-bit formats are expected to be in IEEE 754r.
622 * AMD SSE5 uses the 16-bit format.
623 *
624 * The 32- and 64-bit formats are the original IEEE 754 formats.
625 *
626 * The 80-bit format is x87-specific, but widely used.
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700627 *
628 * The 8-bit format appears to be the consensus 8-bit floating-point
629 * format. It is apparently used in graphics applications.
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700630 */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700631static const struct ieee_format ieee_8 = { 1, 3, 0, 4 };
632static const struct ieee_format ieee_16 = { 2, 10, 0, 5 };
633static const struct ieee_format ieee_32 = { 4, 23, 0, 8 };
634static const struct ieee_format ieee_64 = { 8, 52, 0, 11 };
635static const struct ieee_format ieee_80 = { 10, 63, 1, 15 };
636static const struct ieee_format ieee_128 = { 16, 112, 0, 15 };
H. Peter Anvine31747e2007-09-18 17:50:34 -0700637
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700638/* Types of values we can generate */
639enum floats {
640 FL_ZERO,
641 FL_DENORMAL,
642 FL_NORMAL,
643 FL_INFINITY,
644 FL_QNAN,
645 FL_SNAN
646};
647
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700648static int to_float(const char *str, int s, uint8_t * result,
H. Peter Anvin214f5492007-10-15 19:46:32 -0700649 const struct ieee_format *fmt)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000650{
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700651 fp_limb mant[MANT_LIMBS], *mp, m;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700652 int32_t exponent = 0;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700653 int32_t expmax = 1 << (fmt->exponent - 1);
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700654 fp_limb one_mask = LIMB_TOP_BIT >>
655 ((fmt->exponent+fmt->explicit) % LIMB_BITS);
656 int one_pos = (fmt->exponent+fmt->explicit)/LIMB_BITS;
H. Peter Anvine31747e2007-09-18 17:50:34 -0700657 int i;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700658 int shift;
659 enum floats type;
660 bool ok;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700661 bool minus = s < 0;
662 int bits = fmt->bytes * 8;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000663
H. Peter Anvinf48bc6f2007-09-18 21:55:56 -0700664 if (str[0] == '_') {
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700665 /* Special tokens */
H. Peter Anvine31747e2007-09-18 17:50:34 -0700666
H. Peter Anvin214f5492007-10-15 19:46:32 -0700667 switch (str[2]) {
668 case 'n': /* __nan__ */
669 case 'N':
670 case 'q': /* __qnan__ */
671 case 'Q':
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700672 type = FL_QNAN;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700673 break;
674 case 's': /* __snan__ */
675 case 'S':
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700676 type = FL_SNAN;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700677 break;
678 case 'i': /* __infinity__ */
679 case 'I':
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700680 type = FL_INFINITY;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700681 break;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700682 default:
683 error(ERR_NONFATAL,
684 "internal error: unknown FP constant token `%s'\n", str);
685 type = FL_QNAN;
686 break;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700687 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000688 } else {
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700689 if (str[0] == '0') {
690 switch (str[1]) {
691 case 'x': case 'X':
692 case 'h': case 'H':
693 ok = ieee_flconvert_bin(str+2, 4, mant, &exponent);
694 break;
695 case 'o': case 'O':
696 case 'q': case 'Q':
697 ok = ieee_flconvert_bin(str+2, 3, mant, &exponent);
698 break;
699 case 'b': case 'B':
700 case 'y': case 'Y':
701 ok = ieee_flconvert_bin(str+2, 1, mant, &exponent);
702 break;
703 case 'd': case 'D':
704 case 't': case 'T':
705 ok = ieee_flconvert(str+2, mant, &exponent);
706 break;
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700707 default:
H. Peter Anvinf41aef22007-10-22 19:37:36 -0700708 /* Leading zero was just a zero? */
709 ok = ieee_flconvert(str, mant, &exponent);
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700710 break;
711 }
712 } else if (str[0] == '$') {
713 ok = ieee_flconvert_bin(str+1, 4, mant, &exponent);
714 } else {
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700715 ok = ieee_flconvert(str, mant, &exponent);
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700716 }
H. Peter Anvin214f5492007-10-15 19:46:32 -0700717
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700718 if (!ok) {
719 type = FL_QNAN;
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700720 } else if (mant[0] & LIMB_TOP_BIT) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700721 /*
722 * Non-zero.
723 */
724 exponent--;
725 if (exponent >= 2 - expmax && exponent <= expmax) {
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700726 type = FL_NORMAL;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700727 } else if (exponent > 0) {
H. Peter Anvinfab3a6c2007-10-16 11:48:07 -0700728 if (pass0 == 1)
729 error(ERR_WARNING|ERR_WARN_FL_OVERFLOW,
730 "overflow in floating-point constant");
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700731 type = FL_INFINITY;
732 } else {
H. Peter Anvin4a63c202007-10-30 01:13:09 -0700733 /* underflow or denormal; the denormal code handles
734 actual underflow. */
735 type = FL_DENORMAL;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700736 }
737 } else {
738 /* Zero */
739 type = FL_ZERO;
740 }
741 }
H. Peter Anvin214f5492007-10-15 19:46:32 -0700742
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700743 switch (type) {
744 case FL_ZERO:
H. Peter Anvin125c8782007-10-16 11:32:58 -0700745 zero:
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700746 memset(mant, 0, sizeof mant);
747 break;
748
749 case FL_DENORMAL:
750 {
751 shift = -(exponent + expmax - 2 - fmt->exponent)
752 + fmt->explicit;
753 ieee_shr(mant, shift);
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700754 ieee_round(minus, mant, bits);
H. Peter Anvin125c8782007-10-16 11:32:58 -0700755 if (mant[one_pos] & one_mask) {
756 /* One's position is set, we rounded up into normal range */
757 exponent = 1;
758 if (!fmt->explicit)
759 mant[one_pos] &= ~one_mask; /* remove explicit one */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700760 mant[0] |= exponent << (LIMB_BITS-1 - fmt->exponent);
H. Peter Anvinfab3a6c2007-10-16 11:48:07 -0700761 } else {
762 if (daz || is_zero(mant)) {
763 /* Flush denormals to zero */
764 if (pass0 == 1)
765 error(ERR_WARNING|ERR_WARN_FL_UNDERFLOW,
766 "underflow in floating-point constant");
767 goto zero;
768 } else {
769 if (pass0 == 1)
770 error(ERR_WARNING|ERR_WARN_FL_DENORM,
771 "denormal floating-point constant");
772 }
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700773 }
774 break;
775 }
776
777 case FL_NORMAL:
778 exponent += expmax - 1;
779 ieee_shr(mant, fmt->exponent+fmt->explicit);
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700780 ieee_round(minus, mant, bits);
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700781 /* did we scale up by one? */
782 if (test_bit(mant, fmt->exponent+fmt->explicit-1)) {
783 ieee_shr(mant, 1);
784 exponent++;
H. Peter Anvinfab3a6c2007-10-16 11:48:07 -0700785 if (exponent >= (expmax << 1)-1) {
786 if (pass0 == 1)
787 error(ERR_WARNING|ERR_WARN_FL_OVERFLOW,
788 "overflow in floating-point constant");
H. Peter Anvin125c8782007-10-16 11:32:58 -0700789 type = FL_INFINITY;
790 goto overflow;
791 }
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700792 }
H. Peter Anvin70653092007-10-19 14:42:29 -0700793
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700794 if (!fmt->explicit)
795 mant[one_pos] &= ~one_mask; /* remove explicit one */
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700796 mant[0] |= exponent << (LIMB_BITS-1 - fmt->exponent);
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700797 break;
798
799 case FL_INFINITY:
800 case FL_QNAN:
801 case FL_SNAN:
H. Peter Anvin125c8782007-10-16 11:32:58 -0700802 overflow:
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700803 memset(mant, 0, sizeof mant);
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700804 mant[0] = (((fp_limb)1 << fmt->exponent)-1)
805 << (LIMB_BITS-1 - fmt->exponent);
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700806 if (fmt->explicit)
807 mant[one_pos] |= one_mask;
808 if (type == FL_QNAN)
809 set_bit(mant, fmt->exponent+fmt->explicit+1);
810 else if (type == FL_SNAN)
811 set_bit(mant, fmt->exponent+fmt->explicit+fmt->mantissa);
812 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000813 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000814
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700815 mant[0] |= minus ? LIMB_TOP_BIT : 0;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700816
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700817 m = mant[fmt->bytes/LIMB_BYTES];
818 for (i = LIMB_BYTES-(fmt->bytes % LIMB_BYTES); i < LIMB_BYTES; i++)
819 *result++ = m >> (i*8);
820
821 for (mp = &mant[fmt->bytes/LIMB_BYTES], i = 0;
822 i < fmt->bytes; i += LIMB_BYTES) {
823 m = *--mp;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700824 put(result, m);
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700825 result += LIMB_BYTES;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700826 }
H. Peter Anvine31747e2007-09-18 17:50:34 -0700827
828 return 1; /* success */
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700829}
830
H. Peter Anvin214f5492007-10-15 19:46:32 -0700831int float_const(const char *number, int32_t sign, uint8_t * result,
832 int bytes, efunc err)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000833{
H. Peter Anvin214f5492007-10-15 19:46:32 -0700834 error = err;
835
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700836 switch (bytes) {
H. Peter Anvin2ce02742007-10-29 20:20:12 -0700837 case 1:
838 return to_float(number, sign, result, &ieee_8);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700839 case 2:
H. Peter Anvin214f5492007-10-15 19:46:32 -0700840 return to_float(number, sign, result, &ieee_16);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700841 case 4:
H. Peter Anvin214f5492007-10-15 19:46:32 -0700842 return to_float(number, sign, result, &ieee_32);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700843 case 8:
H. Peter Anvin214f5492007-10-15 19:46:32 -0700844 return to_float(number, sign, result, &ieee_64);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700845 case 10:
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700846 return to_float(number, sign, result, &ieee_80);
H. Peter Anvine31747e2007-09-18 17:50:34 -0700847 case 16:
H. Peter Anvin214f5492007-10-15 19:46:32 -0700848 return to_float(number, sign, result, &ieee_128);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700849 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000850 error(ERR_PANIC, "strange value %d passed to float_const", bytes);
851 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000852 }
853}
H. Peter Anvinf6c9e652007-10-16 14:40:27 -0700854
855/* Set floating-point options */
856int float_option(const char *option)
857{
858 if (!nasm_stricmp(option, "daz")) {
859 daz = true;
860 return 0;
861 } else if (!nasm_stricmp(option, "nodaz")) {
862 daz = false;
863 return 0;
864 } else if (!nasm_stricmp(option, "near")) {
865 rc = FLOAT_RC_NEAR;
866 return 0;
867 } else if (!nasm_stricmp(option, "down")) {
868 rc = FLOAT_RC_DOWN;
869 return 0;
870 } else if (!nasm_stricmp(option, "up")) {
871 rc = FLOAT_RC_UP;
872 return 0;
873 } else if (!nasm_stricmp(option, "zero")) {
874 rc = FLOAT_RC_ZERO;
875 return 0;
876 } else if (!nasm_stricmp(option, "default")) {
877 rc = FLOAT_RC_NEAR;
878 daz = false;
879 return 0;
880 } else {
881 return -1; /* Unknown option */
882 }
883}