blob: 7e99f96cbd91055ae648407cc71631d77c2a7ddb [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 Anvinc65a2f62007-10-22 17:34:10 -0700427/* Returns a value >= 16 if not a valid hex digit */
428static unsigned int hexval(char c)
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700429{
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700430 unsigned int v = (unsigned char) c;
431
432 if (v >= '0' && v <= '9')
433 return v - '0';
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700434 else
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700435 return (v|0x20) - 'a' + 10;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700436}
437
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700438/* Handle floating-point numbers with radix 2^bits and binary exponent */
439static bool ieee_flconvert_bin(const char *string, int bits,
440 uint16_t * mant, int32_t * exponent)
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700441{
442 static const int log2tbl[16] =
H. Peter Anvin214f5492007-10-15 19:46:32 -0700443 { -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3 };
444 uint16_t mult[MANT_WORDS + 1], *mp;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700445 int ms;
446 int32_t twopwr;
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700447 bool seendot, seendigit;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700448 unsigned char c;
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700449 int radix = 1 << bits;
450 unsigned int v;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700451
452 twopwr = 0;
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700453 seendot = seendigit = false;
H. Peter Anvin82f9f632007-09-24 20:53:48 -0700454 ms = 0;
455 mp = NULL;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700456
457 memset(mult, 0, sizeof mult);
458
459 while ((c = *string++) != '\0') {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700460 if (c == '.') {
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700461 if (!seendot)
H. Peter Anvin6867acc2007-10-10 14:58:45 -0700462 seendot = true;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700463 else {
464 error(ERR_NONFATAL,
465 "too many periods in floating-point constant");
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700466 return false;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700467 }
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700468 } else if ((v = hexval(c)) < (unsigned int)radix) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700469 if (!seendigit && v) {
470 int l = log2tbl[v];
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700471
H. Peter Anvin214f5492007-10-15 19:46:32 -0700472 seendigit = 1;
473 mp = mult;
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700474 ms = 15-l;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700475
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700476 twopwr = seendot ? twopwr-bits+l : l+1-bits;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700477 }
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700478
H. Peter Anvin214f5492007-10-15 19:46:32 -0700479 if (seendigit) {
480 if (ms <= 0) {
481 *mp |= v >> -ms;
482 mp++;
483 if (mp > &mult[MANT_WORDS])
484 mp = &mult[MANT_WORDS]; /* Guard slot */
485 ms += 16;
486 }
487 *mp |= v << ms;
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700488 ms -= bits;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700489
H. Peter Anvin214f5492007-10-15 19:46:32 -0700490 if (!seendot)
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700491 twopwr += bits;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700492 } else {
493 if (seendot)
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700494 twopwr -= bits;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700495 }
496 } else if (c == 'p' || c == 'P') {
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700497 int32_t e;
H. Peter Anvin3b2ad1b2007-10-21 15:32:33 -0700498 e = read_exponent(string, 20000);
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700499 if (e == INT32_MAX)
500 return false;
501 twopwr += e;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700502 break;
H. Peter Anvin2ef4aac2007-10-19 13:10:46 -0700503 } else if (c == '_') {
504 /* ignore */
H. Peter Anvin214f5492007-10-15 19:46:32 -0700505 } else {
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700506 error(ERR_NONFATAL,
H. Peter Anvin214f5492007-10-15 19:46:32 -0700507 "floating-point constant: `%c' is invalid character", c);
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700508 return false;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700509 }
510 }
511
512 if (!seendigit) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700513 memset(mant, 0, 2 * MANT_WORDS); /* Zero */
514 *exponent = 0;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700515 } else {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700516 memcpy(mant, mult, 2 * MANT_WORDS);
517 *exponent = twopwr;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700518 }
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700519
520 return true;
H. Peter Anvinfe2177f2007-09-18 18:31:26 -0700521}
522
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000523/*
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700524 * Shift a mantissa to the right by i bits.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000525 */
H. Peter Anvin214f5492007-10-15 19:46:32 -0700526static void ieee_shr(uint16_t * mant, int i)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000527{
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700528 uint16_t n, m;
529 int j = 0;
530 int sr, sl, offs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000531
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700532 sr = i%16; sl = 16-sr;
533 offs = i/16;
534
535 if (sr == 0) {
536 if (offs)
537 for (j = MANT_WORDS-1; j >= offs; j--)
538 mant[j] = mant[j-offs];
539 } else {
540 n = mant[MANT_WORDS-1-offs] >> sr;
541 for (j = MANT_WORDS-1; j > offs; j--) {
542 m = mant[j-offs-1];
543 mant[j] = (m << sl) | n;
544 n = m >> sr;
545 }
546 mant[j--] = n;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000547 }
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700548 while (j >= 0)
549 mant[j--] = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000550}
551
H. Peter Anvin214f5492007-10-15 19:46:32 -0700552#if defined(__i386__) || defined(__x86_64__)
553#define put(a,b) (*(uint16_t *)(a) = (b))
554#else
555#define put(a,b) (((a)[0] = (b)), ((a)[1] = (b) >> 8))
556#endif
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000557
H. Peter Anvinf48bc6f2007-09-18 21:55:56 -0700558/* Set a bit, using *bigendian* bit numbering (0 = MSB) */
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700559static void set_bit(uint16_t *mant, int bit)
H. Peter Anvinf48bc6f2007-09-18 21:55:56 -0700560{
561 mant[bit >> 4] |= 1 << (~bit & 15);
562}
563
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700564/* Test a single bit */
H. Peter Anvinfab3a6c2007-10-16 11:48:07 -0700565static int test_bit(const uint16_t *mant, int bit)
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700566{
567 return (mant[bit >> 4] >> (~bit & 15)) & 1;
568}
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000569
H. Peter Anvinfab3a6c2007-10-16 11:48:07 -0700570/* Report if the mantissa value is all zero */
571static bool is_zero(const uint16_t *mant)
572{
573 int i;
574
575 for (i = 0; i < MANT_WORDS; i++)
576 if (mant[i])
577 return false;
578
579 return true;
580}
581
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700582/* Produce standard IEEE formats, with implicit or explicit integer
583 bit; this makes the following assumptions:
584
585 - the sign bit is the MSB, followed by the exponent,
586 followed by the integer bit if present.
H. Peter Anvine31747e2007-09-18 17:50:34 -0700587 - the sign bit plus exponent fit in 16 bits.
588 - the exponent bias is 2^(n-1)-1 for an n-bit exponent */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000589
H. Peter Anvine31747e2007-09-18 17:50:34 -0700590struct ieee_format {
591 int words;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700592 int mantissa; /* Fractional bits in the mantissa */
593 int explicit; /* Explicit integer */
H. Peter Anvin214f5492007-10-15 19:46:32 -0700594 int exponent; /* Bits in the exponent */
H. Peter Anvine31747e2007-09-18 17:50:34 -0700595};
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000596
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700597/*
598 * The 16- and 128-bit formats are expected to be in IEEE 754r.
599 * AMD SSE5 uses the 16-bit format.
600 *
601 * The 32- and 64-bit formats are the original IEEE 754 formats.
602 *
603 * The 80-bit format is x87-specific, but widely used.
604 */
605static const struct ieee_format ieee_16 = { 1, 10, 0, 5 };
606static const struct ieee_format ieee_32 = { 2, 23, 0, 8 };
607static const struct ieee_format ieee_64 = { 4, 52, 0, 11 };
608static const struct ieee_format ieee_80 = { 5, 63, 1, 15 };
609static const struct ieee_format ieee_128 = { 8, 112, 0, 15 };
H. Peter Anvine31747e2007-09-18 17:50:34 -0700610
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700611/* Types of values we can generate */
612enum floats {
613 FL_ZERO,
614 FL_DENORMAL,
615 FL_NORMAL,
616 FL_INFINITY,
617 FL_QNAN,
618 FL_SNAN
619};
620
H. Peter Anvin214f5492007-10-15 19:46:32 -0700621static int to_float(const char *str, int sign, uint8_t * result,
622 const struct ieee_format *fmt)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000623{
H. Peter Anvine31747e2007-09-18 17:50:34 -0700624 uint16_t mant[MANT_WORDS], *mp;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700625 int32_t exponent = 0;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700626 int32_t expmax = 1 << (fmt->exponent - 1);
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700627 uint16_t one_mask = 0x8000 >> ((fmt->exponent+fmt->explicit) % 16);
628 int one_pos = (fmt->exponent+fmt->explicit)/16;
H. Peter Anvine31747e2007-09-18 17:50:34 -0700629 int i;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700630 int shift;
631 enum floats type;
632 bool ok;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000633
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700634 sign = (sign < 0 ? 0x8000 : 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000635
H. Peter Anvinf48bc6f2007-09-18 21:55:56 -0700636 if (str[0] == '_') {
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700637 /* Special tokens */
H. Peter Anvine31747e2007-09-18 17:50:34 -0700638
H. Peter Anvin214f5492007-10-15 19:46:32 -0700639 switch (str[2]) {
640 case 'n': /* __nan__ */
641 case 'N':
642 case 'q': /* __qnan__ */
643 case 'Q':
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700644 type = FL_QNAN;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700645 break;
646 case 's': /* __snan__ */
647 case 'S':
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700648 type = FL_SNAN;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700649 break;
650 case 'i': /* __infinity__ */
651 case 'I':
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700652 type = FL_INFINITY;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700653 break;
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700654 default:
655 error(ERR_NONFATAL,
656 "internal error: unknown FP constant token `%s'\n", str);
657 type = FL_QNAN;
658 break;
H. Peter Anvin214f5492007-10-15 19:46:32 -0700659 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000660 } else {
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700661 if (str[0] == '0') {
662 switch (str[1]) {
663 case 'x': case 'X':
664 case 'h': case 'H':
665 ok = ieee_flconvert_bin(str+2, 4, mant, &exponent);
666 break;
667 case 'o': case 'O':
668 case 'q': case 'Q':
669 ok = ieee_flconvert_bin(str+2, 3, mant, &exponent);
670 break;
671 case 'b': case 'B':
672 case 'y': case 'Y':
673 ok = ieee_flconvert_bin(str+2, 1, mant, &exponent);
674 break;
675 case 'd': case 'D':
676 case 't': case 'T':
677 ok = ieee_flconvert(str+2, mant, &exponent);
678 break;
679 case '0': case '1': case '2': case '3': case '4':
680 case '5': case '6': case '7': case '8': case '9':
681 case '\0':
682 /* Leading zero was just a zero */
683 ok = ieee_flconvert(str, mant, &exponent);
684 break;
685 default:
686 error(ERR_NONFATAL,
687 "floating-point constant: invalid radix `%c'", str[1]);
688 ok = false;
689 break;
690 }
691 } else if (str[0] == '$') {
692 ok = ieee_flconvert_bin(str+1, 4, mant, &exponent);
693 } else {
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700694 ok = ieee_flconvert(str, mant, &exponent);
H. Peter Anvinc65a2f62007-10-22 17:34:10 -0700695 }
H. Peter Anvin214f5492007-10-15 19:46:32 -0700696
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700697 if (!ok) {
698 type = FL_QNAN;
699 } else if (mant[0] & 0x8000) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700700 /*
701 * Non-zero.
702 */
703 exponent--;
704 if (exponent >= 2 - expmax && exponent <= expmax) {
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700705 type = FL_NORMAL;
H. Peter Anvin125c8782007-10-16 11:32:58 -0700706 } else if (exponent < 2 - expmax &&
H. Peter Anvin214f5492007-10-15 19:46:32 -0700707 exponent >= 2 - expmax - fmt->mantissa) {
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700708 type = FL_DENORMAL;
709 } else if (exponent > 0) {
H. Peter Anvinfab3a6c2007-10-16 11:48:07 -0700710 if (pass0 == 1)
711 error(ERR_WARNING|ERR_WARN_FL_OVERFLOW,
712 "overflow in floating-point constant");
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700713 type = FL_INFINITY;
714 } else {
715 /* underflow */
H. Peter Anvinfab3a6c2007-10-16 11:48:07 -0700716 if (pass0 == 1)
717 error(ERR_WARNING|ERR_WARN_FL_UNDERFLOW,
718 "underflow in floating-point constant");
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700719 type = FL_ZERO;
720 }
721 } else {
722 /* Zero */
723 type = FL_ZERO;
724 }
725 }
H. Peter Anvin214f5492007-10-15 19:46:32 -0700726
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700727 switch (type) {
728 case FL_ZERO:
H. Peter Anvin125c8782007-10-16 11:32:58 -0700729 zero:
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700730 memset(mant, 0, sizeof mant);
731 break;
732
733 case FL_DENORMAL:
734 {
735 shift = -(exponent + expmax - 2 - fmt->exponent)
736 + fmt->explicit;
737 ieee_shr(mant, shift);
H. Peter Anvin125c8782007-10-16 11:32:58 -0700738 ieee_round(sign, mant, fmt->words);
739 if (mant[one_pos] & one_mask) {
740 /* One's position is set, we rounded up into normal range */
741 exponent = 1;
742 if (!fmt->explicit)
743 mant[one_pos] &= ~one_mask; /* remove explicit one */
744 mant[0] |= exponent << (15 - fmt->exponent);
H. Peter Anvinfab3a6c2007-10-16 11:48:07 -0700745 } else {
746 if (daz || is_zero(mant)) {
747 /* Flush denormals to zero */
748 if (pass0 == 1)
749 error(ERR_WARNING|ERR_WARN_FL_UNDERFLOW,
750 "underflow in floating-point constant");
751 goto zero;
752 } else {
753 if (pass0 == 1)
754 error(ERR_WARNING|ERR_WARN_FL_DENORM,
755 "denormal floating-point constant");
756 }
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700757 }
758 break;
759 }
760
761 case FL_NORMAL:
762 exponent += expmax - 1;
763 ieee_shr(mant, fmt->exponent+fmt->explicit);
764 ieee_round(sign, mant, fmt->words);
765 /* did we scale up by one? */
766 if (test_bit(mant, fmt->exponent+fmt->explicit-1)) {
767 ieee_shr(mant, 1);
768 exponent++;
H. Peter Anvinfab3a6c2007-10-16 11:48:07 -0700769 if (exponent >= (expmax << 1)-1) {
770 if (pass0 == 1)
771 error(ERR_WARNING|ERR_WARN_FL_OVERFLOW,
772 "overflow in floating-point constant");
H. Peter Anvin125c8782007-10-16 11:32:58 -0700773 type = FL_INFINITY;
774 goto overflow;
775 }
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700776 }
H. Peter Anvin70653092007-10-19 14:42:29 -0700777
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700778 if (!fmt->explicit)
779 mant[one_pos] &= ~one_mask; /* remove explicit one */
780 mant[0] |= exponent << (15 - fmt->exponent);
781 break;
782
783 case FL_INFINITY:
784 case FL_QNAN:
785 case FL_SNAN:
H. Peter Anvin125c8782007-10-16 11:32:58 -0700786 overflow:
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700787 memset(mant, 0, sizeof mant);
788 mant[0] = ((1 << fmt->exponent)-1) << (15 - fmt->exponent);
789 if (fmt->explicit)
790 mant[one_pos] |= one_mask;
791 if (type == FL_QNAN)
792 set_bit(mant, fmt->exponent+fmt->explicit+1);
793 else if (type == FL_SNAN)
794 set_bit(mant, fmt->exponent+fmt->explicit+fmt->mantissa);
795 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000796 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000797
H. Peter Anvine31747e2007-09-18 17:50:34 -0700798 mant[0] |= sign;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700799
H. Peter Anvine31747e2007-09-18 17:50:34 -0700800 for (mp = &mant[fmt->words], i = 0; i < fmt->words; i++) {
H. Peter Anvin214f5492007-10-15 19:46:32 -0700801 uint16_t m = *--mp;
802 put(result, m);
803 result += 2;
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700804 }
H. Peter Anvine31747e2007-09-18 17:50:34 -0700805
806 return 1; /* success */
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700807}
808
H. Peter Anvin214f5492007-10-15 19:46:32 -0700809int float_const(const char *number, int32_t sign, uint8_t * result,
810 int bytes, efunc err)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000811{
H. Peter Anvin214f5492007-10-15 19:46:32 -0700812 error = err;
813
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700814 switch (bytes) {
815 case 2:
H. Peter Anvin214f5492007-10-15 19:46:32 -0700816 return to_float(number, sign, result, &ieee_16);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700817 case 4:
H. Peter Anvin214f5492007-10-15 19:46:32 -0700818 return to_float(number, sign, result, &ieee_32);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700819 case 8:
H. Peter Anvin214f5492007-10-15 19:46:32 -0700820 return to_float(number, sign, result, &ieee_64);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700821 case 10:
H. Peter Anvin4da5b8c2007-10-16 10:32:57 -0700822 return to_float(number, sign, result, &ieee_80);
H. Peter Anvine31747e2007-09-18 17:50:34 -0700823 case 16:
H. Peter Anvin214f5492007-10-15 19:46:32 -0700824 return to_float(number, sign, result, &ieee_128);
H. Peter Anvin141d7cf2007-09-18 16:39:03 -0700825 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000826 error(ERR_PANIC, "strange value %d passed to float_const", bytes);
827 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000828 }
829}
H. Peter Anvinf6c9e652007-10-16 14:40:27 -0700830
831/* Set floating-point options */
832int float_option(const char *option)
833{
834 if (!nasm_stricmp(option, "daz")) {
835 daz = true;
836 return 0;
837 } else if (!nasm_stricmp(option, "nodaz")) {
838 daz = false;
839 return 0;
840 } else if (!nasm_stricmp(option, "near")) {
841 rc = FLOAT_RC_NEAR;
842 return 0;
843 } else if (!nasm_stricmp(option, "down")) {
844 rc = FLOAT_RC_DOWN;
845 return 0;
846 } else if (!nasm_stricmp(option, "up")) {
847 rc = FLOAT_RC_UP;
848 return 0;
849 } else if (!nasm_stricmp(option, "zero")) {
850 rc = FLOAT_RC_ZERO;
851 return 0;
852 } else if (!nasm_stricmp(option, "default")) {
853 rc = FLOAT_RC_NEAR;
854 daz = false;
855 return 0;
856 } else {
857 return -1; /* Unknown option */
858 }
859}