blob: a2df323dd9b1c55d632eb8f33b2929920b32eb4f [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 Anvin214f5492007-10-15 19:46:32 -070037/* 112 bits + 64 bits for accuracy + 16 bits for rounding */
38#define MANT_WORDS 12
39
40/* 52 digits fit in 176 bits because 10^53 > 2^176 > 10^52 */
41#define MANT_DIGITS 52
42
43/* the format and the argument list depend on MANT_WORDS */
44#define MANT_FMT "%04x%04x_%04x%04x_%04x%04x_%04x%04x_%04x%04x_%04x%04x"
45#define MANT_ARG SOME_ARG(mant, 0)
46
47#define SOME_ARG(a,i) (a)[(i)+0], (a)[(i)+1], (a)[(i)+2], (a)[(i)+3], \
48 (a)[(i)+4], (a)[(i)+5], (a)[(i)+6], (a)[(i)+7], (a)[(i)+8], \
49 (a)[(i)+9], (a)[(i)+10], (a)[(i)+11]
50
51/*
52 * ---------------------------------------------------------------------------
53 * emit a printf()-like debug message... but only if DEBUG_FLOAT was defined
54 * ---------------------------------------------------------------------------
55 */
56
57#ifdef DEBUG_FLOAT
58#define dprintf(x) printf x
59#else /* */
60#define dprintf(x) do { } while (0)
61#endif /* */
62
63/*
64 * ---------------------------------------------------------------------------
65 * multiply
66 * ---------------------------------------------------------------------------
67 */
68static int float_multiply(uint16_t * to, uint16_t * from)
H. Peter Anvineba20a72002-04-30 20:53:55 +000069{
Keith Kaniosb7a89542007-04-12 02:40:54 +000070 uint32_t temp[MANT_WORDS * 2];
H. Peter Anvin214f5492007-10-15 19:46:32 -070071 int32_t i, j;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000072
H. Peter Anvin70653092007-10-19 14:42:29 -070073 /*
H. Peter Anvin214f5492007-10-15 19:46:32 -070074 * guaranteed that top bit of 'from' is set -- so we only have
75 * to worry about _one_ bit shift to the left
76 */
77 dprintf(("%s=" MANT_FMT "\n", "mul1", SOME_ARG(to, 0)));
78 dprintf(("%s=" MANT_FMT "\n", "mul2", SOME_ARG(from, 0)));
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000079
H. Peter Anvin214f5492007-10-15 19:46:32 -070080 memset(temp, 0, sizeof temp);
81
82 for (i = 0; i < MANT_WORDS; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +000083 for (j = 0; j < MANT_WORDS; j++) {
Keith Kaniosb7a89542007-04-12 02:40:54 +000084 uint32_t n;
H. Peter Anvin214f5492007-10-15 19:46:32 -070085 n = (uint32_t) to[i] * (uint32_t) from[j];
H. Peter Anvine2c80182005-01-15 22:15:51 +000086 temp[i + j] += n >> 16;
87 temp[i + j + 1] += n & 0xFFFF;
88 }
H. Peter Anvin214f5492007-10-15 19:46:32 -070089 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000090
H. Peter Anvine2c80182005-01-15 22:15:51 +000091 for (i = MANT_WORDS * 2; --i;) {
92 temp[i - 1] += temp[i] >> 16;
93 temp[i] &= 0xFFFF;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000094 }
H. Peter Anvin214f5492007-10-15 19:46:32 -070095
96 dprintf(("%s=" MANT_FMT "_" MANT_FMT "\n", "temp", SOME_ARG(temp, 0),
97 SOME_ARG(temp, MANT_WORDS)));
98
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000099 if (temp[0] & 0x8000) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700100 for (i = 0; i < MANT_WORDS; i++) {
101 to[i] = temp[i] & 0xFFFF;
102 }
103 dprintf(("%s=" MANT_FMT " (%i)\n", "prod", SOME_ARG(to, 0), 0));
104 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000105 } else {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700106 for (i = 0; i < MANT_WORDS; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000107 to[i] = (temp[i] << 1) + !!(temp[i + 1] & 0x8000);
H. Peter Anvin214f5492007-10-15 19:46:32 -0700108 }
109 dprintf(("%s=" MANT_FMT " (%i)\n", "prod", SOME_ARG(to, 0), -1));
H. Peter Anvine2c80182005-01-15 22:15:51 +0000110 return -1;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000111 }
112}
113
H. Peter Anvin214f5492007-10-15 19:46:32 -0700114/*
115 * ---------------------------------------------------------------------------
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700116 * read an exponent; returns INT32_MAX on error
117 * ---------------------------------------------------------------------------
118 */
H. Peter Anvin3514ad02007-10-19 14:17:51 -0700119static int32_t read_exponent(const char *string, int32_t max)
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700120{
121 int32_t i = 0;
122 bool neg = false;
H. Peter Anvin70653092007-10-19 14:42:29 -0700123
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700124 if (*string == '+') {
125 string++;
126 } else if (*string == '-') {
127 neg = true;
128 string++;
129 }
130 while (*string) {
131 if (*string >= '0' && *string <= '9') {
132 i = (i * 10) + (*string - '0');
H. Peter Anvin70653092007-10-19 14:42:29 -0700133
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700134 /*
135 * To ensure that underflows and overflows are
136 * handled properly we must avoid wraparounds of
137 * the signed integer value that is used to hold
138 * the exponent. Therefore we cap the exponent at
139 * +/-5000, which is slightly more/less than
140 * what's required for normal and denormal numbers
141 * in single, double, and extended precision, but
142 * sufficient to avoid signed integer wraparound.
143 */
H. Peter Anvin3b2ad1b2007-10-21 15:32:33 -0700144 if (i > max)
145 i = max;
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700146 } else if (*string == '_') {
147 /* do nothing */
148 } else {
149 error(ERR_NONFATAL,
150 "invalid character in floating-point constant %s: '%c'",
151 "exponent", *string);
152 return INT32_MAX;
153 }
154 string++;
155 }
156
157 return neg ? -i : i;
158}
159
160/*
161 * ---------------------------------------------------------------------------
H. Peter Anvin214f5492007-10-15 19:46:32 -0700162 * convert
163 * ---------------------------------------------------------------------------
164 */
165static bool ieee_flconvert(const char *string, uint16_t * mant,
166 int32_t * exponent)
167{
168 char digits[MANT_DIGITS];
169 char *p, *q, *r;
170 uint16_t mult[MANT_WORDS], bit;
171 uint16_t *m;
172 int32_t tenpwr, twopwr;
173 int32_t extratwos;
174 bool started, seendot, warned;
175 p = digits;
176 tenpwr = 0;
H. Peter Anvinfab3a6c2007-10-16 11:48:07 -0700177 started = seendot = false;
178 warned = (pass0 != 1);
H. Peter Anvin214f5492007-10-15 19:46:32 -0700179 while (*string && *string != 'E' && *string != 'e') {
180 if (*string == '.') {
181 if (!seendot) {
182 seendot = true;
183 } else {
184 error(ERR_NONFATAL,
185 "too many periods in floating-point constant");
186 return false;
187 }
188 } else if (*string >= '0' && *string <= '9') {
189 if (*string == '0' && !started) {
190 if (seendot) {
191 tenpwr--;
192 }
193 } else {
194 started = true;
195 if (p < digits + sizeof(digits)) {
196 *p++ = *string - '0';
197 } else {
198 if (!warned) {
H. Peter Anvin125c8782007-10-16 11:32:58 -0700199 error(ERR_WARNING|ERR_WARN_FL_TOOLONG,
H. Peter Anvin214f5492007-10-15 19:46:32 -0700200 "floating-point constant significand contains "
201 "more than %i digits", MANT_DIGITS);
202 warned = true;
203 }
204 }
205 if (!seendot) {
206 tenpwr++;
207 }
208 }
209 } else if (*string == '_') {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700210 /* do nothing */
211 } else {
212 error(ERR_NONFATAL,
213 "invalid character in floating-point constant %s: '%c'",
214 "significand", *string);
215 return false;
216 }
217 string++;
218 }
H. Peter Anvin70653092007-10-19 14:42:29 -0700219
H. Peter Anvin214f5492007-10-15 19:46:32 -0700220 if (*string) {
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700221 int32_t e;
222
H. Peter Anvin214f5492007-10-15 19:46:32 -0700223 string++; /* eat the E */
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700224 e = read_exponent(string, 5000);
225 if (e == INT32_MAX)
226 return false;
227 tenpwr += e;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700228 }
229
230 /*
231 * At this point, the memory interval [digits,p) contains a
232 * series of decimal digits zzzzzzz, such that our number X
233 * satisfies X = 0.zzzzzzz * 10^tenpwr.
234 */
235 q = digits;
236 dprintf(("X = 0."));
237 while (q < p) {
238 dprintf(("%c", *q + '0'));
239 q++;
240 }
241 dprintf((" * 10^%i\n", tenpwr));
242
243 /*
244 * Now convert [digits,p) to our internal representation.
245 */
246 bit = 0x8000;
247 for (m = mant; m < mant + MANT_WORDS; m++) {
248 *m = 0;
249 }
250 m = mant;
251 q = digits;
252 started = false;
253 twopwr = 0;
254 while (m < mant + MANT_WORDS) {
255 uint16_t carry = 0;
256 while (p > q && !p[-1]) {
257 p--;
258 }
259 if (p <= q) {
260 break;
261 }
262 for (r = p; r-- > q;) {
263 int32_t i;
264 i = 2 * *r + carry;
265 if (i >= 10) {
266 carry = 1;
267 i -= 10;
268 } else {
269 carry = 0;
270 }
271 *r = i;
272 }
273 if (carry) {
274 *m |= bit;
275 started = true;
276 }
277 if (started) {
278 if (bit == 1) {
279 bit = 0x8000;
280 m++;
281 } else {
282 bit >>= 1;
283 }
284 } else {
285 twopwr--;
286 }
287 }
288 twopwr += tenpwr;
289
290 /*
291 * At this point, the 'mant' array contains the first frac-
292 * tional places of a base-2^16 real number which when mul-
293 * tiplied by 2^twopwr and 5^tenpwr gives X.
294 */
295 dprintf(("X = " MANT_FMT " * 2^%i * 5^%i\n", MANT_ARG, twopwr,
296 tenpwr));
297
298 /*
299 * Now multiply 'mant' by 5^tenpwr.
300 */
301 if (tenpwr < 0) { /* mult = 5^-1 = 0.2 */
302 for (m = mult; m < mult + MANT_WORDS - 1; m++) {
303 *m = 0xCCCC;
304 }
305 mult[MANT_WORDS - 1] = 0xCCCD;
306 extratwos = -2;
307 tenpwr = -tenpwr;
308
309 /*
310 * If tenpwr was 1000...000b, then it becomes 1000...000b. See
311 * the "ANSI C" comment below for more details on that case.
312 *
313 * Because we already truncated tenpwr to +5000...-5000 inside
314 * the exponent parsing code, this shouldn't happen though.
315 */
316 } else if (tenpwr > 0) { /* mult = 5^+1 = 5.0 */
317 mult[0] = 0xA000;
318 for (m = mult + 1; m < mult + MANT_WORDS; m++) {
319 *m = 0;
320 }
321 extratwos = 3;
322 } else {
323 extratwos = 0;
324 }
325 while (tenpwr) {
326 dprintf(("loop=" MANT_FMT " * 2^%i * 5^%i (%i)\n", MANT_ARG,
327 twopwr, tenpwr, extratwos));
328 if (tenpwr & 1) {
329 dprintf(("mant*mult\n"));
330 twopwr += extratwos + float_multiply(mant, mult);
331 }
332 dprintf(("mult*mult\n"));
333 extratwos = extratwos * 2 + float_multiply(mult, mult);
334 tenpwr >>= 1;
335
336 /*
337 * In ANSI C, the result of right-shifting a signed integer is
338 * considered implementation-specific. To ensure that the loop
339 * terminates even if tenpwr was 1000...000b to begin with, we
340 * manually clear the MSB, in case a 1 was shifted in.
341 *
342 * Because we already truncated tenpwr to +5000...-5000 inside
343 * the exponent parsing code, this shouldn't matter; neverthe-
344 * less it is the right thing to do here.
345 */
346 tenpwr &= (uint32_t) - 1 >> 1;
347 }
348
349 /*
350 * At this point, the 'mant' array contains the first frac-
351 * tional places of a base-2^16 real number in [0.5,1) that
352 * when multiplied by 2^twopwr gives X. Or it contains zero
353 * of course. We are done.
354 */
355 *exponent = twopwr;
356 return true;
357}
358
359/*
360 * ---------------------------------------------------------------------------
361 * round a mantissa off after i words
362 * ---------------------------------------------------------------------------
363 */
364
365#define ROUND_COLLECT_BITS \
366 for (j = i; j < MANT_WORDS; j++) { \
367 m = m | mant[j]; \
368 }
369
370#define ROUND_ABS_DOWN \
371 for (j = i; j < MANT_WORDS; j++) { \
372 mant[j] = 0x0000; \
373 }
374
375#define ROUND_ABS_UP \
376 do { \
377 ++mant[--i]; \
378 mant[i] &= 0xFFFF; \
379 } while (i > 0 && !mant[i]); \
380 return (!i && !mant[i]);
381
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700382static bool ieee_round(int sign, uint16_t * mant, int32_t i)
H. Peter Anvin214f5492007-10-15 19:46:32 -0700383{
384 uint16_t m = 0;
385 int32_t j;
386 if ((sign == 0x0000) || (sign == 0x8000)) {
387 if (rc == FLOAT_RC_NEAR) {
388 if (mant[i] & 0x8000) {
389 mant[i] &= 0x7FFF;
390 ROUND_COLLECT_BITS;
391 mant[i] |= 0x8000;
392 if (m) {
393 ROUND_ABS_UP;
394 } else {
395 if (mant[i - 1] & 1) {
396 ROUND_ABS_UP;
397 } else {
398 ROUND_ABS_DOWN;
399 }
400 }
401 } else {
402 ROUND_ABS_DOWN;
403 }
404 } else if (((sign == 0x0000) && (rc == FLOAT_RC_DOWN))
405 || ((sign == 0x8000) && (rc == FLOAT_RC_UP))) {
406 ROUND_COLLECT_BITS;
407 if (m) {
408 ROUND_ABS_DOWN;
409 }
410 } else if (((sign == 0x0000) && (rc == FLOAT_RC_UP))
411 || ((sign == 0x8000) && (rc == FLOAT_RC_DOWN))) {
412 ROUND_COLLECT_BITS;
413 if (m) {
414 ROUND_ABS_UP;
415 }
416 } else if (rc == FLOAT_RC_ZERO) {
417 ROUND_ABS_DOWN;
418 } else {
419 error(ERR_PANIC, "float_round() can't handle rc=%i", rc);
420 }
421 } else {
422 error(ERR_PANIC, "float_round() can't handle sign=%i", sign);
423 }
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700424 return false;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700425}
426
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700427static int hexval(char c)
428{
429 if (c >= '0' && c <= '9')
H. Peter Anvin214f5492007-10-15 19:46:32 -0700430 return c - '0';
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700431 else if (c >= 'a' && c <= 'f')
H. Peter Anvin214f5492007-10-15 19:46:32 -0700432 return c - 'a' + 10;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700433 else
H. Peter Anvin214f5492007-10-15 19:46:32 -0700434 return c - 'A' + 10;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700435}
436
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700437static bool ieee_flconvert_hex(const char *string, uint16_t * mant,
H. Peter Anvin214f5492007-10-15 19:46:32 -0700438 int32_t * exponent)
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700439{
440 static const int log2tbl[16] =
H. Peter Anvin214f5492007-10-15 19:46:32 -0700441 { -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3 };
442 uint16_t mult[MANT_WORDS + 1], *mp;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700443 int ms;
444 int32_t twopwr;
445 int seendot, seendigit;
446 unsigned char c;
447
448 twopwr = 0;
449 seendot = seendigit = 0;
H. Peter Anvin82f9f632007-09-24 20:53:48 -0700450 ms = 0;
451 mp = NULL;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700452
453 memset(mult, 0, sizeof mult);
454
455 while ((c = *string++) != '\0') {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700456 if (c == '.') {
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700457 if (!seendot)
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700458 seendot = true;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700459 else {
460 error(ERR_NONFATAL,
461 "too many periods in floating-point constant");
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700462 return false;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700463 }
H. Peter Anvin214f5492007-10-15 19:46:32 -0700464 } else if (isxdigit(c)) {
465 int v = hexval(c);
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700466
H. Peter Anvin214f5492007-10-15 19:46:32 -0700467 if (!seendigit && v) {
468 int l = log2tbl[v];
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700469
H. Peter Anvin214f5492007-10-15 19:46:32 -0700470 seendigit = 1;
471 mp = mult;
472 ms = 15 - l;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700473
H. Peter Anvin214f5492007-10-15 19:46:32 -0700474 twopwr = seendot ? twopwr - 4 + l : l - 3;
475 }
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700476
H. Peter Anvin214f5492007-10-15 19:46:32 -0700477 if (seendigit) {
478 if (ms <= 0) {
479 *mp |= v >> -ms;
480 mp++;
481 if (mp > &mult[MANT_WORDS])
482 mp = &mult[MANT_WORDS]; /* Guard slot */
483 ms += 16;
484 }
485 *mp |= v << ms;
486 ms -= 4;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700487
H. Peter Anvin214f5492007-10-15 19:46:32 -0700488 if (!seendot)
489 twopwr += 4;
490 } else {
491 if (seendot)
492 twopwr -= 4;
493 }
494 } else if (c == 'p' || c == 'P') {
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700495 int32_t e;
H. Peter Anvin3b2ad1b2007-10-21 15:32:33 -0700496 e = read_exponent(string, 20000);
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700497 if (e == INT32_MAX)
498 return false;
499 twopwr += e;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700500 break;
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700501 } else if (c == '_') {
502 /* ignore */
H. Peter Anvin214f5492007-10-15 19:46:32 -0700503 } else {
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700504 error(ERR_NONFATAL,
H. Peter Anvin214f5492007-10-15 19:46:32 -0700505 "floating-point constant: `%c' is invalid character", c);
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700506 return false;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700507 }
508 }
509
510 if (!seendigit) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700511 memset(mant, 0, 2 * MANT_WORDS); /* Zero */
512 *exponent = 0;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700513 } else {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700514 memcpy(mant, mult, 2 * MANT_WORDS);
515 *exponent = twopwr;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700516 }
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700517
518 return true;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700519}
520
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000521/*
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700522 * Shift a mantissa to the right by i bits.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000523 */
H. Peter Anvin214f5492007-10-15 19:46:32 -0700524static void ieee_shr(uint16_t * mant, int i)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000525{
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700526 uint16_t n, m;
527 int j = 0;
528 int sr, sl, offs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000529
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700530 sr = i%16; sl = 16-sr;
531 offs = i/16;
532
533 if (sr == 0) {
534 if (offs)
535 for (j = MANT_WORDS-1; j >= offs; j--)
536 mant[j] = mant[j-offs];
537 } else {
538 n = mant[MANT_WORDS-1-offs] >> sr;
539 for (j = MANT_WORDS-1; j > offs; j--) {
540 m = mant[j-offs-1];
541 mant[j] = (m << sl) | n;
542 n = m >> sr;
543 }
544 mant[j--] = n;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000545 }
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700546 while (j >= 0)
547 mant[j--] = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000548}
549
H. Peter Anvin214f5492007-10-15 19:46:32 -0700550#if defined(__i386__) || defined(__x86_64__)
551#define put(a,b) (*(uint16_t *)(a) = (b))
552#else
553#define put(a,b) (((a)[0] = (b)), ((a)[1] = (b) >> 8))
554#endif
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000555
H. Peter Anvinf48bc6f2007-09-18 21:55:56 -0700556/* Set a bit, using *bigendian* bit numbering (0 = MSB) */
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700557static void set_bit(uint16_t *mant, int bit)
H. Peter Anvinf48bc6f2007-09-18 21:55:56 -0700558{
559 mant[bit >> 4] |= 1 << (~bit & 15);
560}
561
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700562/* Test a single bit */
H. Peter Anvinfab3a6c2007-10-16 11:48:07 -0700563static int test_bit(const uint16_t *mant, int bit)
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700564{
565 return (mant[bit >> 4] >> (~bit & 15)) & 1;
566}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000567
H. Peter Anvinfab3a6c2007-10-16 11:48:07 -0700568/* Report if the mantissa value is all zero */
569static bool is_zero(const uint16_t *mant)
570{
571 int i;
572
573 for (i = 0; i < MANT_WORDS; i++)
574 if (mant[i])
575 return false;
576
577 return true;
578}
579
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700580/* Produce standard IEEE formats, with implicit or explicit integer
581 bit; this makes the following assumptions:
582
583 - the sign bit is the MSB, followed by the exponent,
584 followed by the integer bit if present.
H. Peter Anvine31747e2007-09-18 17:50:34 -0700585 - the sign bit plus exponent fit in 16 bits.
586 - the exponent bias is 2^(n-1)-1 for an n-bit exponent */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000587
H. Peter Anvine31747e2007-09-18 17:50:34 -0700588struct ieee_format {
589 int words;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700590 int mantissa; /* Fractional bits in the mantissa */
591 int explicit; /* Explicit integer */
H. Peter Anvin214f5492007-10-15 19:46:32 -0700592 int exponent; /* Bits in the exponent */
H. Peter Anvine31747e2007-09-18 17:50:34 -0700593};
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000594
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700595/*
596 * The 16- and 128-bit formats are expected to be in IEEE 754r.
597 * AMD SSE5 uses the 16-bit format.
598 *
599 * The 32- and 64-bit formats are the original IEEE 754 formats.
600 *
601 * The 80-bit format is x87-specific, but widely used.
602 */
603static const struct ieee_format ieee_16 = { 1, 10, 0, 5 };
604static const struct ieee_format ieee_32 = { 2, 23, 0, 8 };
605static const struct ieee_format ieee_64 = { 4, 52, 0, 11 };
606static const struct ieee_format ieee_80 = { 5, 63, 1, 15 };
607static const struct ieee_format ieee_128 = { 8, 112, 0, 15 };
H. Peter Anvine31747e2007-09-18 17:50:34 -0700608
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700609/* Types of values we can generate */
610enum floats {
611 FL_ZERO,
612 FL_DENORMAL,
613 FL_NORMAL,
614 FL_INFINITY,
615 FL_QNAN,
616 FL_SNAN
617};
618
H. Peter Anvin214f5492007-10-15 19:46:32 -0700619static int to_float(const char *str, int sign, uint8_t * result,
620 const struct ieee_format *fmt)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000621{
H. Peter Anvine31747e2007-09-18 17:50:34 -0700622 uint16_t mant[MANT_WORDS], *mp;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700623 int32_t exponent = 0;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700624 int32_t expmax = 1 << (fmt->exponent - 1);
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700625 uint16_t one_mask = 0x8000 >> ((fmt->exponent+fmt->explicit) % 16);
626 int one_pos = (fmt->exponent+fmt->explicit)/16;
H. Peter Anvine31747e2007-09-18 17:50:34 -0700627 int i;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700628 int shift;
629 enum floats type;
630 bool ok;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000631
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700632 sign = (sign < 0 ? 0x8000 : 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000633
H. Peter Anvinf48bc6f2007-09-18 21:55:56 -0700634 if (str[0] == '_') {
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700635 /* Special tokens */
H. Peter Anvine31747e2007-09-18 17:50:34 -0700636
H. Peter Anvin214f5492007-10-15 19:46:32 -0700637 switch (str[2]) {
638 case 'n': /* __nan__ */
639 case 'N':
640 case 'q': /* __qnan__ */
641 case 'Q':
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700642 type = FL_QNAN;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700643 break;
644 case 's': /* __snan__ */
645 case 'S':
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700646 type = FL_SNAN;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700647 break;
648 case 'i': /* __infinity__ */
649 case 'I':
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700650 type = FL_INFINITY;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700651 break;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700652 default:
653 error(ERR_NONFATAL,
654 "internal error: unknown FP constant token `%s'\n", str);
655 type = FL_QNAN;
656 break;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700657 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000658 } else {
H. Peter Anvinbea0bbb2007-10-22 16:53:48 -0700659 if (str[0] == '0' &&
660 (str[1] == 'x' || str[1] == 'X' || str[1] == 'h' || str[1] == 'H'))
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700661 ok = ieee_flconvert_hex(str + 2, mant, &exponent);
H. Peter Anvin449e04b2007-10-19 18:33:57 -0700662 else if (str[0] == '$')
663 ok = ieee_flconvert_hex(str + 1, mant, &exponent);
H. Peter Anvin214f5492007-10-15 19:46:32 -0700664 else
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700665 ok = ieee_flconvert(str, mant, &exponent);
H. Peter Anvin214f5492007-10-15 19:46:32 -0700666
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700667 if (!ok) {
668 type = FL_QNAN;
669 } else if (mant[0] & 0x8000) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700670 /*
671 * Non-zero.
672 */
673 exponent--;
674 if (exponent >= 2 - expmax && exponent <= expmax) {
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700675 type = FL_NORMAL;
H. Peter Anvin125c8782007-10-16 11:32:58 -0700676 } else if (exponent < 2 - expmax &&
H. Peter Anvin214f5492007-10-15 19:46:32 -0700677 exponent >= 2 - expmax - fmt->mantissa) {
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700678 type = FL_DENORMAL;
679 } else if (exponent > 0) {
H. Peter Anvinfab3a6c2007-10-16 11:48:07 -0700680 if (pass0 == 1)
681 error(ERR_WARNING|ERR_WARN_FL_OVERFLOW,
682 "overflow in floating-point constant");
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700683 type = FL_INFINITY;
684 } else {
685 /* underflow */
H. Peter Anvinfab3a6c2007-10-16 11:48:07 -0700686 if (pass0 == 1)
687 error(ERR_WARNING|ERR_WARN_FL_UNDERFLOW,
688 "underflow in floating-point constant");
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700689 type = FL_ZERO;
690 }
691 } else {
692 /* Zero */
693 type = FL_ZERO;
694 }
695 }
H. Peter Anvin214f5492007-10-15 19:46:32 -0700696
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700697 switch (type) {
698 case FL_ZERO:
H. Peter Anvin125c8782007-10-16 11:32:58 -0700699 zero:
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700700 memset(mant, 0, sizeof mant);
701 break;
702
703 case FL_DENORMAL:
704 {
705 shift = -(exponent + expmax - 2 - fmt->exponent)
706 + fmt->explicit;
707 ieee_shr(mant, shift);
H. Peter Anvin125c8782007-10-16 11:32:58 -0700708 ieee_round(sign, mant, fmt->words);
709 if (mant[one_pos] & one_mask) {
710 /* One's position is set, we rounded up into normal range */
711 exponent = 1;
712 if (!fmt->explicit)
713 mant[one_pos] &= ~one_mask; /* remove explicit one */
714 mant[0] |= exponent << (15 - fmt->exponent);
H. Peter Anvinfab3a6c2007-10-16 11:48:07 -0700715 } else {
716 if (daz || is_zero(mant)) {
717 /* Flush denormals to zero */
718 if (pass0 == 1)
719 error(ERR_WARNING|ERR_WARN_FL_UNDERFLOW,
720 "underflow in floating-point constant");
721 goto zero;
722 } else {
723 if (pass0 == 1)
724 error(ERR_WARNING|ERR_WARN_FL_DENORM,
725 "denormal floating-point constant");
726 }
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700727 }
728 break;
729 }
730
731 case FL_NORMAL:
732 exponent += expmax - 1;
733 ieee_shr(mant, fmt->exponent+fmt->explicit);
734 ieee_round(sign, mant, fmt->words);
735 /* did we scale up by one? */
736 if (test_bit(mant, fmt->exponent+fmt->explicit-1)) {
737 ieee_shr(mant, 1);
738 exponent++;
H. Peter Anvinfab3a6c2007-10-16 11:48:07 -0700739 if (exponent >= (expmax << 1)-1) {
740 if (pass0 == 1)
741 error(ERR_WARNING|ERR_WARN_FL_OVERFLOW,
742 "overflow in floating-point constant");
H. Peter Anvin125c8782007-10-16 11:32:58 -0700743 type = FL_INFINITY;
744 goto overflow;
745 }
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700746 }
H. Peter Anvin70653092007-10-19 14:42:29 -0700747
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700748 if (!fmt->explicit)
749 mant[one_pos] &= ~one_mask; /* remove explicit one */
750 mant[0] |= exponent << (15 - fmt->exponent);
751 break;
752
753 case FL_INFINITY:
754 case FL_QNAN:
755 case FL_SNAN:
H. Peter Anvin125c8782007-10-16 11:32:58 -0700756 overflow:
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700757 memset(mant, 0, sizeof mant);
758 mant[0] = ((1 << fmt->exponent)-1) << (15 - fmt->exponent);
759 if (fmt->explicit)
760 mant[one_pos] |= one_mask;
761 if (type == FL_QNAN)
762 set_bit(mant, fmt->exponent+fmt->explicit+1);
763 else if (type == FL_SNAN)
764 set_bit(mant, fmt->exponent+fmt->explicit+fmt->mantissa);
765 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000766 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000767
H. Peter Anvine31747e2007-09-18 17:50:34 -0700768 mant[0] |= sign;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700769
H. Peter Anvine31747e2007-09-18 17:50:34 -0700770 for (mp = &mant[fmt->words], i = 0; i < fmt->words; i++) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700771 uint16_t m = *--mp;
772 put(result, m);
773 result += 2;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700774 }
H. Peter Anvine31747e2007-09-18 17:50:34 -0700775
776 return 1; /* success */
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700777}
778
H. Peter Anvin214f5492007-10-15 19:46:32 -0700779int float_const(const char *number, int32_t sign, uint8_t * result,
780 int bytes, efunc err)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000781{
H. Peter Anvin214f5492007-10-15 19:46:32 -0700782 error = err;
783
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700784 switch (bytes) {
785 case 2:
H. Peter Anvin214f5492007-10-15 19:46:32 -0700786 return to_float(number, sign, result, &ieee_16);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700787 case 4:
H. Peter Anvin214f5492007-10-15 19:46:32 -0700788 return to_float(number, sign, result, &ieee_32);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700789 case 8:
H. Peter Anvin214f5492007-10-15 19:46:32 -0700790 return to_float(number, sign, result, &ieee_64);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700791 case 10:
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700792 return to_float(number, sign, result, &ieee_80);
H. Peter Anvine31747e2007-09-18 17:50:34 -0700793 case 16:
H. Peter Anvin214f5492007-10-15 19:46:32 -0700794 return to_float(number, sign, result, &ieee_128);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700795 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000796 error(ERR_PANIC, "strange value %d passed to float_const", bytes);
797 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000798 }
799}
H. Peter Anvinf6c9e652007-10-16 14:40:27 -0700800
801/* Set floating-point options */
802int float_option(const char *option)
803{
804 if (!nasm_stricmp(option, "daz")) {
805 daz = true;
806 return 0;
807 } else if (!nasm_stricmp(option, "nodaz")) {
808 daz = false;
809 return 0;
810 } else if (!nasm_stricmp(option, "near")) {
811 rc = FLOAT_RC_NEAR;
812 return 0;
813 } else if (!nasm_stricmp(option, "down")) {
814 rc = FLOAT_RC_DOWN;
815 return 0;
816 } else if (!nasm_stricmp(option, "up")) {
817 rc = FLOAT_RC_UP;
818 return 0;
819 } else if (!nasm_stricmp(option, "zero")) {
820 rc = FLOAT_RC_ZERO;
821 return 0;
822 } else if (!nasm_stricmp(option, "default")) {
823 rc = FLOAT_RC_NEAR;
824 daz = false;
825 return 0;
826 } else {
827 return -1; /* Unknown option */
828 }
829}