float.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. /* ----------------------------------------------------------------------- *
  2. *
  3. * Copyright 1996-2018 The NASM Authors - All Rights Reserved
  4. * See the file AUTHORS included with the NASM distribution for
  5. * the specific copyright holders.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following
  9. * conditions are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials provided
  16. * with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  19. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  20. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  30. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. * ----------------------------------------------------------------------- */
  33. /*
  34. * float.c floating-point constant support for the Netwide Assembler
  35. */
  36. #include "compiler.h"
  37. #include <ctype.h>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include "nasm.h"
  42. #include "float.h"
  43. #include "error.h"
  44. /*
  45. * -----------------
  46. * local variables
  47. * -----------------
  48. */
  49. static bool daz = false; /* denormals as zero */
  50. static enum float_round rc = FLOAT_RC_NEAR; /* rounding control */
  51. /*
  52. * -----------
  53. * constants
  54. * -----------
  55. */
  56. /* "A limb is like a digit but bigger */
  57. typedef uint32_t fp_limb;
  58. typedef uint64_t fp_2limb;
  59. #define LIMB_BITS 32
  60. #define LIMB_BYTES (LIMB_BITS/8)
  61. #define LIMB_TOP_BIT ((fp_limb)1 << (LIMB_BITS-1))
  62. #define LIMB_MASK ((fp_limb)(~0))
  63. #define LIMB_ALL_BYTES ((fp_limb)0x01010101)
  64. #define LIMB_BYTE(x) ((x)*LIMB_ALL_BYTES)
  65. /* 112 bits + 64 bits for accuracy + 16 bits for rounding */
  66. #define MANT_LIMBS 6
  67. /* 52 digits fit in 176 bits because 10^53 > 2^176 > 10^52 */
  68. #define MANT_DIGITS 52
  69. /* the format and the argument list depend on MANT_LIMBS */
  70. #define MANT_FMT "%08x_%08x_%08x_%08x_%08x_%08x"
  71. #define MANT_ARG SOME_ARG(mant, 0)
  72. #define SOME_ARG(a,i) (a)[(i)+0], (a)[(i)+1], (a)[(i)+2], \
  73. (a)[(i)+3], (a)[(i)+4], (a)[(i)+5]
  74. /*
  75. * ---------------------------------------------------------------------------
  76. * emit a printf()-like debug message... but only if DEBUG_FLOAT was defined
  77. * ---------------------------------------------------------------------------
  78. */
  79. #ifdef DEBUG_FLOAT
  80. #define dprintf(x) printf x
  81. #else
  82. #define dprintf(x) do { } while (0)
  83. #endif
  84. /*
  85. * ---------------------------------------------------------------------------
  86. * multiply
  87. * ---------------------------------------------------------------------------
  88. */
  89. static int float_multiply(fp_limb *to, fp_limb *from)
  90. {
  91. fp_2limb temp[MANT_LIMBS * 2];
  92. int i, j;
  93. /*
  94. * guaranteed that top bit of 'from' is set -- so we only have
  95. * to worry about _one_ bit shift to the left
  96. */
  97. dprintf(("%s=" MANT_FMT "\n", "mul1", SOME_ARG(to, 0)));
  98. dprintf(("%s=" MANT_FMT "\n", "mul2", SOME_ARG(from, 0)));
  99. memset(temp, 0, sizeof temp);
  100. for (i = 0; i < MANT_LIMBS; i++) {
  101. for (j = 0; j < MANT_LIMBS; j++) {
  102. fp_2limb n;
  103. n = (fp_2limb) to[i] * (fp_2limb) from[j];
  104. temp[i + j] += n >> LIMB_BITS;
  105. temp[i + j + 1] += (fp_limb)n;
  106. }
  107. }
  108. for (i = MANT_LIMBS * 2; --i;) {
  109. temp[i - 1] += temp[i] >> LIMB_BITS;
  110. temp[i] &= LIMB_MASK;
  111. }
  112. dprintf(("%s=" MANT_FMT "_" MANT_FMT "\n", "temp", SOME_ARG(temp, 0),
  113. SOME_ARG(temp, MANT_LIMBS)));
  114. if (temp[0] & LIMB_TOP_BIT) {
  115. for (i = 0; i < MANT_LIMBS; i++) {
  116. to[i] = temp[i] & LIMB_MASK;
  117. }
  118. dprintf(("%s=" MANT_FMT " (%i)\n", "prod", SOME_ARG(to, 0), 0));
  119. return 0;
  120. } else {
  121. for (i = 0; i < MANT_LIMBS; i++) {
  122. to[i] = (temp[i] << 1) + !!(temp[i + 1] & LIMB_TOP_BIT);
  123. }
  124. dprintf(("%s=" MANT_FMT " (%i)\n", "prod", SOME_ARG(to, 0), -1));
  125. return -1;
  126. }
  127. }
  128. /*
  129. * ---------------------------------------------------------------------------
  130. * read an exponent; returns INT32_MAX on error
  131. * ---------------------------------------------------------------------------
  132. */
  133. static int32_t read_exponent(const char *string, int32_t max)
  134. {
  135. int32_t i = 0;
  136. bool neg = false;
  137. if (*string == '+') {
  138. string++;
  139. } else if (*string == '-') {
  140. neg = true;
  141. string++;
  142. }
  143. while (*string) {
  144. if (*string >= '0' && *string <= '9') {
  145. i = (i * 10) + (*string - '0');
  146. /*
  147. * To ensure that underflows and overflows are
  148. * handled properly we must avoid wraparounds of
  149. * the signed integer value that is used to hold
  150. * the exponent. Therefore we cap the exponent at
  151. * +/-5000, which is slightly more/less than
  152. * what's required for normal and denormal numbers
  153. * in single, double, and extended precision, but
  154. * sufficient to avoid signed integer wraparound.
  155. */
  156. if (i > max)
  157. i = max;
  158. } else if (*string == '_') {
  159. /* do nothing */
  160. } else {
  161. nasm_error(ERR_NONFATAL,
  162. "invalid character in floating-point constant %s: '%c'",
  163. "exponent", *string);
  164. return INT32_MAX;
  165. }
  166. string++;
  167. }
  168. return neg ? -i : i;
  169. }
  170. /*
  171. * ---------------------------------------------------------------------------
  172. * convert
  173. * ---------------------------------------------------------------------------
  174. */
  175. static bool ieee_flconvert(const char *string, fp_limb *mant,
  176. int32_t * exponent)
  177. {
  178. char digits[MANT_DIGITS];
  179. char *p, *q, *r;
  180. fp_limb mult[MANT_LIMBS], bit;
  181. fp_limb *m;
  182. int32_t tenpwr, twopwr;
  183. int32_t extratwos;
  184. bool started, seendot, warned;
  185. warned = false;
  186. p = digits;
  187. tenpwr = 0;
  188. started = seendot = false;
  189. while (*string && *string != 'E' && *string != 'e') {
  190. if (*string == '.') {
  191. if (!seendot) {
  192. seendot = true;
  193. } else {
  194. nasm_error(ERR_NONFATAL,
  195. "too many periods in floating-point constant");
  196. return false;
  197. }
  198. } else if (*string >= '0' && *string <= '9') {
  199. if (*string == '0' && !started) {
  200. if (seendot) {
  201. tenpwr--;
  202. }
  203. } else {
  204. started = true;
  205. if (p < digits + sizeof(digits)) {
  206. *p++ = *string - '0';
  207. } else {
  208. if (!warned) {
  209. nasm_error(ERR_WARNING|WARN_FL_TOOLONG|ERR_PASS2,
  210. "floating-point constant significand contains "
  211. "more than %i digits", MANT_DIGITS);
  212. warned = true;
  213. }
  214. }
  215. if (!seendot) {
  216. tenpwr++;
  217. }
  218. }
  219. } else if (*string == '_') {
  220. /* do nothing */
  221. } else {
  222. nasm_error(ERR_NONFATAL|ERR_PASS2,
  223. "invalid character in floating-point constant %s: '%c'",
  224. "significand", *string);
  225. return false;
  226. }
  227. string++;
  228. }
  229. if (*string) {
  230. int32_t e;
  231. string++; /* eat the E */
  232. e = read_exponent(string, 5000);
  233. if (e == INT32_MAX)
  234. return false;
  235. tenpwr += e;
  236. }
  237. /*
  238. * At this point, the memory interval [digits,p) contains a
  239. * series of decimal digits zzzzzzz, such that our number X
  240. * satisfies X = 0.zzzzzzz * 10^tenpwr.
  241. */
  242. q = digits;
  243. dprintf(("X = 0."));
  244. while (q < p) {
  245. dprintf(("%c", *q + '0'));
  246. q++;
  247. }
  248. dprintf((" * 10^%i\n", tenpwr));
  249. /*
  250. * Now convert [digits,p) to our internal representation.
  251. */
  252. bit = LIMB_TOP_BIT;
  253. for (m = mant; m < mant + MANT_LIMBS; m++) {
  254. *m = 0;
  255. }
  256. m = mant;
  257. q = digits;
  258. started = false;
  259. twopwr = 0;
  260. while (m < mant + MANT_LIMBS) {
  261. fp_limb carry = 0;
  262. while (p > q && !p[-1]) {
  263. p--;
  264. }
  265. if (p <= q) {
  266. break;
  267. }
  268. for (r = p; r-- > q;) {
  269. int32_t i;
  270. i = 2 * *r + carry;
  271. if (i >= 10) {
  272. carry = 1;
  273. i -= 10;
  274. } else {
  275. carry = 0;
  276. }
  277. *r = i;
  278. }
  279. if (carry) {
  280. *m |= bit;
  281. started = true;
  282. }
  283. if (started) {
  284. if (bit == 1) {
  285. bit = LIMB_TOP_BIT;
  286. m++;
  287. } else {
  288. bit >>= 1;
  289. }
  290. } else {
  291. twopwr--;
  292. }
  293. }
  294. twopwr += tenpwr;
  295. /*
  296. * At this point, the 'mant' array contains the first frac-
  297. * tional places of a base-2^16 real number which when mul-
  298. * tiplied by 2^twopwr and 5^tenpwr gives X.
  299. */
  300. dprintf(("X = " MANT_FMT " * 2^%i * 5^%i\n", MANT_ARG, twopwr,
  301. tenpwr));
  302. /*
  303. * Now multiply 'mant' by 5^tenpwr.
  304. */
  305. if (tenpwr < 0) { /* mult = 5^-1 = 0.2 */
  306. for (m = mult; m < mult + MANT_LIMBS - 1; m++) {
  307. *m = LIMB_BYTE(0xcc);
  308. }
  309. mult[MANT_LIMBS - 1] = LIMB_BYTE(0xcc)+1;
  310. extratwos = -2;
  311. tenpwr = -tenpwr;
  312. /*
  313. * If tenpwr was 1000...000b, then it becomes 1000...000b. See
  314. * the "ANSI C" comment below for more details on that case.
  315. *
  316. * Because we already truncated tenpwr to +5000...-5000 inside
  317. * the exponent parsing code, this shouldn't happen though.
  318. */
  319. } else if (tenpwr > 0) { /* mult = 5^+1 = 5.0 */
  320. mult[0] = (fp_limb)5 << (LIMB_BITS-3); /* 0xA000... */
  321. for (m = mult + 1; m < mult + MANT_LIMBS; m++) {
  322. *m = 0;
  323. }
  324. extratwos = 3;
  325. } else {
  326. extratwos = 0;
  327. }
  328. while (tenpwr) {
  329. dprintf(("loop=" MANT_FMT " * 2^%i * 5^%i (%i)\n", MANT_ARG,
  330. twopwr, tenpwr, extratwos));
  331. if (tenpwr & 1) {
  332. dprintf(("mant*mult\n"));
  333. twopwr += extratwos + float_multiply(mant, mult);
  334. }
  335. dprintf(("mult*mult\n"));
  336. extratwos = extratwos * 2 + float_multiply(mult, mult);
  337. tenpwr >>= 1;
  338. /*
  339. * In ANSI C, the result of right-shifting a signed integer is
  340. * considered implementation-specific. To ensure that the loop
  341. * terminates even if tenpwr was 1000...000b to begin with, we
  342. * manually clear the MSB, in case a 1 was shifted in.
  343. *
  344. * Because we already truncated tenpwr to +5000...-5000 inside
  345. * the exponent parsing code, this shouldn't matter; neverthe-
  346. * less it is the right thing to do here.
  347. */
  348. tenpwr &= (uint32_t) - 1 >> 1;
  349. }
  350. /*
  351. * At this point, the 'mant' array contains the first frac-
  352. * tional places of a base-2^16 real number in [0.5,1) that
  353. * when multiplied by 2^twopwr gives X. Or it contains zero
  354. * of course. We are done.
  355. */
  356. *exponent = twopwr;
  357. return true;
  358. }
  359. /*
  360. * ---------------------------------------------------------------------------
  361. * operations of specific bits
  362. * ---------------------------------------------------------------------------
  363. */
  364. /* Set a bit, using *bigendian* bit numbering (0 = MSB) */
  365. static void set_bit(fp_limb *mant, int bit)
  366. {
  367. mant[bit/LIMB_BITS] |= LIMB_TOP_BIT >> (bit & (LIMB_BITS-1));
  368. }
  369. /* Test a single bit */
  370. static int test_bit(const fp_limb *mant, int bit)
  371. {
  372. return (mant[bit/LIMB_BITS] >> (~bit & (LIMB_BITS-1))) & 1;
  373. }
  374. /* Report if the mantissa value is all zero */
  375. static bool is_zero(const fp_limb *mant)
  376. {
  377. int i;
  378. for (i = 0; i < MANT_LIMBS; i++)
  379. if (mant[i])
  380. return false;
  381. return true;
  382. }
  383. /*
  384. * ---------------------------------------------------------------------------
  385. * round a mantissa off after i words
  386. * ---------------------------------------------------------------------------
  387. */
  388. #define ROUND_COLLECT_BITS \
  389. do { \
  390. m = mant[i] & (2*bit-1); \
  391. for (j = i+1; j < MANT_LIMBS; j++) \
  392. m = m | mant[j]; \
  393. } while (0)
  394. #define ROUND_ABS_DOWN \
  395. do { \
  396. mant[i] &= ~(bit-1); \
  397. for (j = i+1; j < MANT_LIMBS; j++) \
  398. mant[j] = 0; \
  399. return false; \
  400. } while (0)
  401. #define ROUND_ABS_UP \
  402. do { \
  403. mant[i] = (mant[i] & ~(bit-1)) + bit; \
  404. for (j = i+1; j < MANT_LIMBS; j++) \
  405. mant[j] = 0; \
  406. while (i > 0 && !mant[i]) \
  407. ++mant[--i]; \
  408. return !mant[0]; \
  409. } while (0)
  410. static bool ieee_round(bool minus, fp_limb *mant, int bits)
  411. {
  412. fp_limb m = 0;
  413. int32_t j;
  414. int i = bits / LIMB_BITS;
  415. int p = bits % LIMB_BITS;
  416. fp_limb bit = LIMB_TOP_BIT >> p;
  417. if (rc == FLOAT_RC_NEAR) {
  418. if (mant[i] & bit) {
  419. mant[i] &= ~bit;
  420. ROUND_COLLECT_BITS;
  421. mant[i] |= bit;
  422. if (m) {
  423. ROUND_ABS_UP;
  424. } else {
  425. if (test_bit(mant, bits-1)) {
  426. ROUND_ABS_UP;
  427. } else {
  428. ROUND_ABS_DOWN;
  429. }
  430. }
  431. } else {
  432. ROUND_ABS_DOWN;
  433. }
  434. } else if (rc == FLOAT_RC_ZERO ||
  435. rc == (minus ? FLOAT_RC_UP : FLOAT_RC_DOWN)) {
  436. ROUND_ABS_DOWN;
  437. } else {
  438. /* rc == (minus ? FLOAT_RC_DOWN : FLOAT_RC_UP) */
  439. /* Round toward +/- infinity */
  440. ROUND_COLLECT_BITS;
  441. if (m) {
  442. ROUND_ABS_UP;
  443. } else {
  444. ROUND_ABS_DOWN;
  445. }
  446. }
  447. return false;
  448. }
  449. /* Returns a value >= 16 if not a valid hex digit */
  450. static unsigned int hexval(char c)
  451. {
  452. unsigned int v = (unsigned char) c;
  453. if (v >= '0' && v <= '9')
  454. return v - '0';
  455. else
  456. return (v|0x20) - 'a' + 10;
  457. }
  458. /* Handle floating-point numbers with radix 2^bits and binary exponent */
  459. static bool ieee_flconvert_bin(const char *string, int bits,
  460. fp_limb *mant, int32_t *exponent)
  461. {
  462. static const int log2tbl[16] =
  463. { -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3 };
  464. fp_limb mult[MANT_LIMBS + 1], *mp;
  465. int ms;
  466. int32_t twopwr;
  467. bool seendot, seendigit;
  468. unsigned char c;
  469. const int radix = 1 << bits;
  470. fp_limb v;
  471. twopwr = 0;
  472. seendot = seendigit = false;
  473. ms = 0;
  474. mp = NULL;
  475. memset(mult, 0, sizeof mult);
  476. while ((c = *string++) != '\0') {
  477. if (c == '.') {
  478. if (!seendot)
  479. seendot = true;
  480. else {
  481. nasm_error(ERR_NONFATAL,
  482. "too many periods in floating-point constant");
  483. return false;
  484. }
  485. } else if ((v = hexval(c)) < (unsigned int)radix) {
  486. if (!seendigit && v) {
  487. int l = log2tbl[v];
  488. seendigit = true;
  489. mp = mult;
  490. ms = (LIMB_BITS-1)-l;
  491. twopwr += l+1-bits;
  492. }
  493. if (seendigit) {
  494. if (ms < 0) {
  495. /* Cast to fp_2limb as ms == -LIMB_BITS is possible. */
  496. *mp |= (fp_2limb)v >> -ms;
  497. mp++;
  498. if (mp > &mult[MANT_LIMBS])
  499. mp = &mult[MANT_LIMBS]; /* Guard slot */
  500. ms += LIMB_BITS;
  501. }
  502. *mp |= v << ms;
  503. ms -= bits;
  504. if (!seendot)
  505. twopwr += bits;
  506. } else {
  507. if (seendot)
  508. twopwr -= bits;
  509. }
  510. } else if (c == 'p' || c == 'P') {
  511. int32_t e;
  512. e = read_exponent(string, 20000);
  513. if (e == INT32_MAX)
  514. return false;
  515. twopwr += e;
  516. break;
  517. } else if (c == '_') {
  518. /* ignore */
  519. } else {
  520. nasm_error(ERR_NONFATAL,
  521. "floating-point constant: `%c' is invalid character", c);
  522. return false;
  523. }
  524. }
  525. if (!seendigit) {
  526. memset(mant, 0, MANT_LIMBS*sizeof(fp_limb)); /* Zero */
  527. *exponent = 0;
  528. } else {
  529. memcpy(mant, mult, MANT_LIMBS*sizeof(fp_limb));
  530. *exponent = twopwr;
  531. }
  532. return true;
  533. }
  534. /*
  535. * Shift a mantissa to the right by i bits.
  536. */
  537. static void ieee_shr(fp_limb *mant, int i)
  538. {
  539. fp_limb n, m;
  540. int j = 0;
  541. int sr, sl, offs;
  542. sr = i % LIMB_BITS; sl = LIMB_BITS-sr;
  543. offs = i/LIMB_BITS;
  544. if (sr == 0) {
  545. if (offs)
  546. for (j = MANT_LIMBS-1; j >= offs; j--)
  547. mant[j] = mant[j-offs];
  548. } else if (MANT_LIMBS-1-offs < 0) {
  549. j = MANT_LIMBS-1;
  550. } else {
  551. n = mant[MANT_LIMBS-1-offs] >> sr;
  552. for (j = MANT_LIMBS-1; j > offs; j--) {
  553. m = mant[j-offs-1];
  554. mant[j] = (m << sl) | n;
  555. n = m >> sr;
  556. }
  557. mant[j--] = n;
  558. }
  559. while (j >= 0)
  560. mant[j--] = 0;
  561. }
  562. /* Produce standard IEEE formats, with implicit or explicit integer
  563. bit; this makes the following assumptions:
  564. - the sign bit is the MSB, followed by the exponent,
  565. followed by the integer bit if present.
  566. - the sign bit plus exponent fit in 16 bits.
  567. - the exponent bias is 2^(n-1)-1 for an n-bit exponent */
  568. struct ieee_format {
  569. int bytes;
  570. int mantissa; /* Fractional bits in the mantissa */
  571. int explicit; /* Explicit integer */
  572. int exponent; /* Bits in the exponent */
  573. };
  574. /*
  575. * The 16- and 128-bit formats are expected to be in IEEE 754r.
  576. * AMD SSE5 uses the 16-bit format.
  577. *
  578. * The 32- and 64-bit formats are the original IEEE 754 formats.
  579. *
  580. * The 80-bit format is x87-specific, but widely used.
  581. *
  582. * The 8-bit format appears to be the consensus 8-bit floating-point
  583. * format. It is apparently used in graphics applications.
  584. */
  585. static const struct ieee_format ieee_8 = { 1, 3, 0, 4 };
  586. static const struct ieee_format ieee_16 = { 2, 10, 0, 5 };
  587. static const struct ieee_format ieee_32 = { 4, 23, 0, 8 };
  588. static const struct ieee_format ieee_64 = { 8, 52, 0, 11 };
  589. static const struct ieee_format ieee_80 = { 10, 63, 1, 15 };
  590. static const struct ieee_format ieee_128 = { 16, 112, 0, 15 };
  591. /* Types of values we can generate */
  592. enum floats {
  593. FL_ZERO,
  594. FL_DENORMAL,
  595. FL_NORMAL,
  596. FL_INFINITY,
  597. FL_QNAN,
  598. FL_SNAN
  599. };
  600. static int to_packed_bcd(const char *str, const char *p,
  601. int s, uint8_t *result,
  602. const struct ieee_format *fmt)
  603. {
  604. int n = 0;
  605. char c;
  606. int tv = -1;
  607. if (fmt != &ieee_80) {
  608. nasm_error(ERR_NONFATAL,
  609. "packed BCD requires an 80-bit format");
  610. return 0;
  611. }
  612. while (p >= str) {
  613. c = *p--;
  614. if (c >= '0' && c <= '9') {
  615. if (tv < 0) {
  616. if (n == 9) {
  617. nasm_error(ERR_WARNING|ERR_PASS2,
  618. "packed BCD truncated to 18 digits");
  619. }
  620. tv = c-'0';
  621. } else {
  622. if (n < 9)
  623. *result++ = tv + ((c-'0') << 4);
  624. n++;
  625. tv = -1;
  626. }
  627. } else if (c == '_') {
  628. /* do nothing */
  629. } else {
  630. nasm_error(ERR_NONFATAL,
  631. "invalid character `%c' in packed BCD constant", c);
  632. return 0;
  633. }
  634. }
  635. if (tv >= 0) {
  636. if (n < 9)
  637. *result++ = tv;
  638. n++;
  639. }
  640. while (n < 9) {
  641. *result++ = 0;
  642. n++;
  643. }
  644. *result = (s < 0) ? 0x80 : 0;
  645. return 1; /* success */
  646. }
  647. static int to_float(const char *str, int s, uint8_t *result,
  648. const struct ieee_format *fmt)
  649. {
  650. fp_limb mant[MANT_LIMBS];
  651. int32_t exponent = 0;
  652. const int32_t expmax = 1 << (fmt->exponent - 1);
  653. fp_limb one_mask = LIMB_TOP_BIT >>
  654. ((fmt->exponent+fmt->explicit) % LIMB_BITS);
  655. const int one_pos = (fmt->exponent+fmt->explicit)/LIMB_BITS;
  656. int i;
  657. int shift;
  658. enum floats type;
  659. bool ok;
  660. const bool minus = s < 0;
  661. const int bits = fmt->bytes * 8;
  662. const char *strend;
  663. if (!str[0]) {
  664. nasm_panic(0,
  665. "internal errror: empty string passed to float_const");
  666. return 0;
  667. }
  668. strend = strchr(str, '\0');
  669. if (strend[-1] == 'P' || strend[-1] == 'p')
  670. return to_packed_bcd(str, strend-2, s, result, fmt);
  671. if (str[0] == '_') {
  672. /* Special tokens */
  673. switch (str[2]) {
  674. case 'n': /* __nan__ */
  675. case 'N':
  676. case 'q': /* __qnan__ */
  677. case 'Q':
  678. type = FL_QNAN;
  679. break;
  680. case 's': /* __snan__ */
  681. case 'S':
  682. type = FL_SNAN;
  683. break;
  684. case 'i': /* __infinity__ */
  685. case 'I':
  686. type = FL_INFINITY;
  687. break;
  688. default:
  689. nasm_error(ERR_NONFATAL,
  690. "internal error: unknown FP constant token `%s'\n", str);
  691. type = FL_QNAN;
  692. break;
  693. }
  694. } else {
  695. if (str[0] == '0') {
  696. switch (str[1]) {
  697. case 'x': case 'X':
  698. case 'h': case 'H':
  699. ok = ieee_flconvert_bin(str+2, 4, mant, &exponent);
  700. break;
  701. case 'o': case 'O':
  702. case 'q': case 'Q':
  703. ok = ieee_flconvert_bin(str+2, 3, mant, &exponent);
  704. break;
  705. case 'b': case 'B':
  706. case 'y': case 'Y':
  707. ok = ieee_flconvert_bin(str+2, 1, mant, &exponent);
  708. break;
  709. case 'd': case 'D':
  710. case 't': case 'T':
  711. ok = ieee_flconvert(str+2, mant, &exponent);
  712. break;
  713. case 'p': case 'P':
  714. return to_packed_bcd(str+2, strend-1, s, result, fmt);
  715. default:
  716. /* Leading zero was just a zero? */
  717. ok = ieee_flconvert(str, mant, &exponent);
  718. break;
  719. }
  720. } else if (str[0] == '$') {
  721. ok = ieee_flconvert_bin(str+1, 4, mant, &exponent);
  722. } else {
  723. ok = ieee_flconvert(str, mant, &exponent);
  724. }
  725. if (!ok) {
  726. type = FL_QNAN;
  727. } else if (mant[0] & LIMB_TOP_BIT) {
  728. /*
  729. * Non-zero.
  730. */
  731. exponent--;
  732. if (exponent >= 2 - expmax && exponent <= expmax) {
  733. type = FL_NORMAL;
  734. } else if (exponent > 0) {
  735. if (pass0 == 1)
  736. nasm_error(ERR_WARNING|WARN_FL_OVERFLOW|ERR_PASS2,
  737. "overflow in floating-point constant");
  738. type = FL_INFINITY;
  739. } else {
  740. /* underflow or denormal; the denormal code handles
  741. actual underflow. */
  742. type = FL_DENORMAL;
  743. }
  744. } else {
  745. /* Zero */
  746. type = FL_ZERO;
  747. }
  748. }
  749. switch (type) {
  750. case FL_ZERO:
  751. zero:
  752. memset(mant, 0, sizeof mant);
  753. break;
  754. case FL_DENORMAL:
  755. {
  756. shift = -(exponent + expmax - 2 - fmt->exponent)
  757. + fmt->explicit;
  758. ieee_shr(mant, shift);
  759. ieee_round(minus, mant, bits);
  760. if (mant[one_pos] & one_mask) {
  761. /* One's position is set, we rounded up into normal range */
  762. exponent = 1;
  763. if (!fmt->explicit)
  764. mant[one_pos] &= ~one_mask; /* remove explicit one */
  765. mant[0] |= exponent << (LIMB_BITS-1 - fmt->exponent);
  766. } else {
  767. if (daz || is_zero(mant)) {
  768. /* Flush denormals to zero */
  769. nasm_error(ERR_WARNING|WARN_FL_UNDERFLOW|ERR_PASS2,
  770. "underflow in floating-point constant");
  771. goto zero;
  772. } else {
  773. nasm_error(ERR_WARNING|WARN_FL_DENORM|ERR_PASS2,
  774. "denormal floating-point constant");
  775. }
  776. }
  777. break;
  778. }
  779. case FL_NORMAL:
  780. exponent += expmax - 1;
  781. ieee_shr(mant, fmt->exponent+fmt->explicit);
  782. ieee_round(minus, mant, bits);
  783. /* did we scale up by one? */
  784. if (test_bit(mant, fmt->exponent+fmt->explicit-1)) {
  785. ieee_shr(mant, 1);
  786. exponent++;
  787. if (exponent >= (expmax << 1)-1) {
  788. nasm_error(ERR_WARNING|WARN_FL_OVERFLOW|ERR_PASS2,
  789. "overflow in floating-point constant");
  790. type = FL_INFINITY;
  791. goto overflow;
  792. }
  793. }
  794. if (!fmt->explicit)
  795. mant[one_pos] &= ~one_mask; /* remove explicit one */
  796. mant[0] |= exponent << (LIMB_BITS-1 - fmt->exponent);
  797. break;
  798. case FL_INFINITY:
  799. case FL_QNAN:
  800. case FL_SNAN:
  801. overflow:
  802. memset(mant, 0, sizeof mant);
  803. mant[0] = (((fp_limb)1 << fmt->exponent)-1)
  804. << (LIMB_BITS-1 - fmt->exponent);
  805. if (fmt->explicit)
  806. mant[one_pos] |= one_mask;
  807. if (type == FL_QNAN)
  808. set_bit(mant, fmt->exponent+fmt->explicit+1);
  809. else if (type == FL_SNAN)
  810. set_bit(mant, fmt->exponent+fmt->explicit+fmt->mantissa);
  811. break;
  812. }
  813. mant[0] |= minus ? LIMB_TOP_BIT : 0;
  814. for (i = fmt->bytes - 1; i >= 0; i--)
  815. *result++ = mant[i/LIMB_BYTES] >> (((LIMB_BYTES-1)-(i%LIMB_BYTES))*8);
  816. return 1; /* success */
  817. }
  818. int float_const(const char *number, int sign, uint8_t *result, int bytes)
  819. {
  820. switch (bytes) {
  821. case 1:
  822. return to_float(number, sign, result, &ieee_8);
  823. case 2:
  824. return to_float(number, sign, result, &ieee_16);
  825. case 4:
  826. return to_float(number, sign, result, &ieee_32);
  827. case 8:
  828. return to_float(number, sign, result, &ieee_64);
  829. case 10:
  830. return to_float(number, sign, result, &ieee_80);
  831. case 16:
  832. return to_float(number, sign, result, &ieee_128);
  833. default:
  834. nasm_panic(0, "strange value %d passed to float_const", bytes);
  835. return 0;
  836. }
  837. }
  838. /* Set floating-point options */
  839. int float_option(const char *option)
  840. {
  841. if (!nasm_stricmp(option, "daz")) {
  842. daz = true;
  843. return 0;
  844. } else if (!nasm_stricmp(option, "nodaz")) {
  845. daz = false;
  846. return 0;
  847. } else if (!nasm_stricmp(option, "near")) {
  848. rc = FLOAT_RC_NEAR;
  849. return 0;
  850. } else if (!nasm_stricmp(option, "down")) {
  851. rc = FLOAT_RC_DOWN;
  852. return 0;
  853. } else if (!nasm_stricmp(option, "up")) {
  854. rc = FLOAT_RC_UP;
  855. return 0;
  856. } else if (!nasm_stricmp(option, "zero")) {
  857. rc = FLOAT_RC_ZERO;
  858. return 0;
  859. } else if (!nasm_stricmp(option, "default")) {
  860. rc = FLOAT_RC_NEAR;
  861. daz = false;
  862. return 0;
  863. } else {
  864. return -1; /* Unknown option */
  865. }
  866. }