floatnum.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. /*
  2. * Floating point number functions.
  3. *
  4. * Copyright (C) 2001-2007 Peter Johnson
  5. *
  6. * Based on public-domain x86 assembly code by Randall Hyde (8/28/91).
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
  21. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include "util.h"
  30. #include <ctype.h>
  31. #include "coretype.h"
  32. #include "bitvect.h"
  33. #include "file.h"
  34. #include "errwarn.h"
  35. #include "floatnum.h"
  36. /* 97-bit internal floating point format:
  37. * 0000000s eeeeeeee eeeeeeee m.....................................m
  38. * Sign exponent mantissa (80 bits)
  39. * 79 0
  40. *
  41. * Only L.O. bit of Sign byte is significant. The rest is zero.
  42. * Exponent is bias 32767.
  43. * Mantissa does NOT have an implied one bit (it's explicit).
  44. */
  45. struct yasm_floatnum {
  46. /*@only@*/ wordptr mantissa; /* Allocated to MANT_BITS bits */
  47. unsigned short exponent;
  48. unsigned char sign;
  49. unsigned char flags;
  50. };
  51. /* constants describing parameters of internal floating point format */
  52. #define MANT_BITS 80
  53. #define MANT_BYTES 10
  54. #define MANT_SIGDIGITS 24
  55. #define EXP_BIAS 0x7FFF
  56. #define EXP_INF 0xFFFF
  57. #define EXP_MAX 0xFFFE
  58. #define EXP_MIN 1
  59. #define EXP_ZERO 0
  60. /* Flag settings for flags field */
  61. #define FLAG_ISZERO 1<<0
  62. /* Note this structure integrates the floatnum structure */
  63. typedef struct POT_Entry_s {
  64. yasm_floatnum f;
  65. int dec_exponent;
  66. } POT_Entry;
  67. /* "Source" for POT_Entry. */
  68. typedef struct POT_Entry_Source_s {
  69. unsigned char mantissa[MANT_BYTES]; /* little endian mantissa */
  70. unsigned short exponent; /* Bias 32767 exponent */
  71. } POT_Entry_Source;
  72. /* Power of ten tables used by the floating point I/O routines.
  73. * The POT_Table? arrays are built from the POT_Table?_Source arrays at
  74. * runtime by POT_Table_Init().
  75. */
  76. /* This table contains the powers of ten raised to negative powers of two:
  77. *
  78. * entry[12-n] = 10 ** (-2 ** n) for 0 <= n <= 12.
  79. * entry[13] = 1.0
  80. */
  81. static /*@only@*/ POT_Entry *POT_TableN;
  82. static POT_Entry_Source POT_TableN_Source[] = {
  83. {{0xe3,0x2d,0xde,0x9f,0xce,0xd2,0xc8,0x04,0xdd,0xa6},0x4ad8}, /* 1e-4096 */
  84. {{0x25,0x49,0xe4,0x2d,0x36,0x34,0x4f,0x53,0xae,0xce},0x656b}, /* 1e-2048 */
  85. {{0xa6,0x87,0xbd,0xc0,0x57,0xda,0xa5,0x82,0xa6,0xa2},0x72b5}, /* 1e-1024 */
  86. {{0x33,0x71,0x1c,0xd2,0x23,0xdb,0x32,0xee,0x49,0x90},0x795a}, /* 1e-512 */
  87. {{0x91,0xfa,0x39,0x19,0x7a,0x63,0x25,0x43,0x31,0xc0},0x7cac}, /* 1e-256 */
  88. {{0x7d,0xac,0xa0,0xe4,0xbc,0x64,0x7c,0x46,0xd0,0xdd},0x7e55}, /* 1e-128 */
  89. {{0x24,0x3f,0xa5,0xe9,0x39,0xa5,0x27,0xea,0x7f,0xa8},0x7f2a}, /* 1e-64 */
  90. {{0xde,0x67,0xba,0x94,0x39,0x45,0xad,0x1e,0xb1,0xcf},0x7f94}, /* 1e-32 */
  91. {{0x2f,0x4c,0x5b,0xe1,0x4d,0xc4,0xbe,0x94,0x95,0xe6},0x7fc9}, /* 1e-16 */
  92. {{0xc2,0xfd,0xfc,0xce,0x61,0x84,0x11,0x77,0xcc,0xab},0x7fe4}, /* 1e-8 */
  93. {{0xc3,0xd3,0x2b,0x65,0x19,0xe2,0x58,0x17,0xb7,0xd1},0x7ff1}, /* 1e-4 */
  94. {{0x71,0x3d,0x0a,0xd7,0xa3,0x70,0x3d,0x0a,0xd7,0xa3},0x7ff8}, /* 1e-2 */
  95. {{0xcd,0xcc,0xcc,0xcc,0xcc,0xcc,0xcc,0xcc,0xcc,0xcc},0x7ffb}, /* 1e-1 */
  96. {{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80},0x7fff}, /* 1e-0 */
  97. };
  98. /* This table contains the powers of ten raised to positive powers of two:
  99. *
  100. * entry[12-n] = 10 ** (2 ** n) for 0 <= n <= 12.
  101. * entry[13] = 1.0
  102. * entry[-1] = entry[0];
  103. *
  104. * There is a -1 entry since it is possible for the algorithm to back up
  105. * before the table. This -1 entry is created at runtime by duplicating the
  106. * 0 entry.
  107. */
  108. static /*@only@*/ POT_Entry *POT_TableP;
  109. static POT_Entry_Source POT_TableP_Source[] = {
  110. {{0x4c,0xc9,0x9a,0x97,0x20,0x8a,0x02,0x52,0x60,0xc4},0xb525}, /* 1e+4096 */
  111. {{0x4d,0xa7,0xe4,0x5d,0x3d,0xc5,0x5d,0x3b,0x8b,0x9e},0x9a92}, /* 1e+2048 */
  112. {{0x0d,0x65,0x17,0x0c,0x75,0x81,0x86,0x75,0x76,0xc9},0x8d48}, /* 1e+1024 */
  113. {{0x65,0xcc,0xc6,0x91,0x0e,0xa6,0xae,0xa0,0x19,0xe3},0x86a3}, /* 1e+512 */
  114. {{0xbc,0xdd,0x8d,0xde,0xf9,0x9d,0xfb,0xeb,0x7e,0xaa},0x8351}, /* 1e+256 */
  115. {{0x6f,0xc6,0xdf,0x8c,0xe9,0x80,0xc9,0x47,0xba,0x93},0x81a8}, /* 1e+128 */
  116. {{0xbf,0x3c,0xd5,0xa6,0xcf,0xff,0x49,0x1f,0x78,0xc2},0x80d3}, /* 1e+64 */
  117. {{0x20,0xf0,0x9d,0xb5,0x70,0x2b,0xa8,0xad,0xc5,0x9d},0x8069}, /* 1e+32 */
  118. {{0x00,0x00,0x00,0x00,0x00,0x04,0xbf,0xc9,0x1b,0x8e},0x8034}, /* 1e+16 */
  119. {{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0xbc,0xbe},0x8019}, /* 1e+8 */
  120. {{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x9c},0x800c}, /* 1e+4 */
  121. {{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xc8},0x8005}, /* 1e+2 */
  122. {{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xa0},0x8002}, /* 1e+1 */
  123. {{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80},0x7fff}, /* 1e+0 */
  124. };
  125. static void
  126. POT_Table_Init_Entry(/*@out@*/ POT_Entry *e, POT_Entry_Source *s, int dec_exp)
  127. {
  128. /* Save decimal exponent */
  129. e->dec_exponent = dec_exp;
  130. /* Initialize mantissa */
  131. e->f.mantissa = BitVector_Create(MANT_BITS, FALSE);
  132. BitVector_Block_Store(e->f.mantissa, s->mantissa, MANT_BYTES);
  133. /* Initialize exponent */
  134. e->f.exponent = s->exponent;
  135. /* Set sign to 0 (positive) */
  136. e->f.sign = 0;
  137. /* Clear flags */
  138. e->f.flags = 0;
  139. }
  140. /*@-compdef@*/
  141. void
  142. yasm_floatnum_initialize(void)
  143. /*@globals undef POT_TableN, undef POT_TableP, POT_TableP_Source,
  144. POT_TableN_Source @*/
  145. {
  146. int dec_exp = 1;
  147. int i;
  148. /* Allocate space for two POT tables */
  149. POT_TableN = yasm_xmalloc(14*sizeof(POT_Entry));
  150. POT_TableP = yasm_xmalloc(15*sizeof(POT_Entry)); /* note 1 extra for -1 */
  151. /* Initialize entry[0..12] */
  152. for (i=12; i>=0; i--) {
  153. POT_Table_Init_Entry(&POT_TableN[i], &POT_TableN_Source[i], 0-dec_exp);
  154. POT_Table_Init_Entry(&POT_TableP[i+1], &POT_TableP_Source[i], dec_exp);
  155. dec_exp *= 2; /* Update decimal exponent */
  156. }
  157. /* Initialize entry[13] */
  158. POT_Table_Init_Entry(&POT_TableN[13], &POT_TableN_Source[13], 0);
  159. POT_Table_Init_Entry(&POT_TableP[14], &POT_TableP_Source[13], 0);
  160. /* Initialize entry[-1] for POT_TableP */
  161. POT_Table_Init_Entry(&POT_TableP[0], &POT_TableP_Source[0], 4096);
  162. /* Offset POT_TableP so that [0] becomes [-1] */
  163. POT_TableP++;
  164. }
  165. /*@=compdef@*/
  166. /*@-globstate@*/
  167. void
  168. yasm_floatnum_cleanup(void)
  169. {
  170. int i;
  171. /* Un-offset POT_TableP */
  172. POT_TableP--;
  173. for (i=0; i<14; i++) {
  174. BitVector_Destroy(POT_TableN[i].f.mantissa);
  175. BitVector_Destroy(POT_TableP[i].f.mantissa);
  176. }
  177. BitVector_Destroy(POT_TableP[14].f.mantissa);
  178. yasm_xfree(POT_TableN);
  179. yasm_xfree(POT_TableP);
  180. }
  181. /*@=globstate@*/
  182. static void
  183. floatnum_normalize(yasm_floatnum *flt)
  184. {
  185. long norm_amt;
  186. if (BitVector_is_empty(flt->mantissa)) {
  187. flt->exponent = 0;
  188. return;
  189. }
  190. /* Look for the highest set bit, shift to make it the MSB, and adjust
  191. * exponent. Don't let exponent go negative. */
  192. norm_amt = (MANT_BITS-1)-Set_Max(flt->mantissa);
  193. if (norm_amt > (long)flt->exponent)
  194. norm_amt = (long)flt->exponent;
  195. BitVector_Move_Left(flt->mantissa, (N_int)norm_amt);
  196. flt->exponent -= (unsigned short)norm_amt;
  197. }
  198. /* acc *= op */
  199. static void
  200. floatnum_mul(yasm_floatnum *acc, const yasm_floatnum *op)
  201. {
  202. long expon;
  203. wordptr product, op1, op2;
  204. long norm_amt;
  205. /* Compute the new sign */
  206. acc->sign ^= op->sign;
  207. /* Check for multiply by 0 */
  208. if (BitVector_is_empty(acc->mantissa) || BitVector_is_empty(op->mantissa)) {
  209. BitVector_Empty(acc->mantissa);
  210. acc->exponent = EXP_ZERO;
  211. return;
  212. }
  213. /* Add exponents, checking for overflow/underflow. */
  214. expon = (((int)acc->exponent)-EXP_BIAS) + (((int)op->exponent)-EXP_BIAS);
  215. expon += EXP_BIAS;
  216. if (expon > EXP_MAX) {
  217. /* Overflow; return infinity. */
  218. BitVector_Empty(acc->mantissa);
  219. acc->exponent = EXP_INF;
  220. return;
  221. } else if (expon < EXP_MIN) {
  222. /* Underflow; return zero. */
  223. BitVector_Empty(acc->mantissa);
  224. acc->exponent = EXP_ZERO;
  225. return;
  226. }
  227. /* Add one to the final exponent, as the multiply shifts one extra time. */
  228. acc->exponent = (unsigned short)(expon+1);
  229. /* Allocate space for the multiply result */
  230. product = BitVector_Create((N_int)((MANT_BITS+1)*2), FALSE);
  231. /* Allocate 1-bit-longer fields to force the operands to be unsigned */
  232. op1 = BitVector_Create((N_int)(MANT_BITS+1), FALSE);
  233. op2 = BitVector_Create((N_int)(MANT_BITS+1), FALSE);
  234. /* Make the operands unsigned after copying from original operands */
  235. BitVector_Copy(op1, acc->mantissa);
  236. BitVector_MSB(op1, 0);
  237. BitVector_Copy(op2, op->mantissa);
  238. BitVector_MSB(op2, 0);
  239. /* Compute the product of the mantissas */
  240. BitVector_Multiply(product, op1, op2);
  241. /* Normalize the product. Note: we know the product is non-zero because
  242. * both of the original operands were non-zero.
  243. *
  244. * Look for the highest set bit, shift to make it the MSB, and adjust
  245. * exponent. Don't let exponent go negative.
  246. */
  247. norm_amt = (MANT_BITS*2-1)-Set_Max(product);
  248. if (norm_amt > (long)acc->exponent)
  249. norm_amt = (long)acc->exponent;
  250. BitVector_Move_Left(product, (N_int)norm_amt);
  251. acc->exponent -= (unsigned short)norm_amt;
  252. /* Store the highest bits of the result */
  253. BitVector_Interval_Copy(acc->mantissa, product, 0, MANT_BITS, MANT_BITS);
  254. /* Free allocated variables */
  255. BitVector_Destroy(product);
  256. BitVector_Destroy(op1);
  257. BitVector_Destroy(op2);
  258. }
  259. yasm_floatnum *
  260. yasm_floatnum_create(const char *str)
  261. {
  262. yasm_floatnum *flt;
  263. int dec_exponent, dec_exp_add; /* decimal (powers of 10) exponent */
  264. int POT_index;
  265. wordptr operand[2];
  266. int sig_digits;
  267. int decimal_pt;
  268. boolean carry;
  269. flt = yasm_xmalloc(sizeof(yasm_floatnum));
  270. flt->mantissa = BitVector_Create(MANT_BITS, TRUE);
  271. /* allocate and initialize calculation variables */
  272. operand[0] = BitVector_Create(MANT_BITS, TRUE);
  273. operand[1] = BitVector_Create(MANT_BITS, TRUE);
  274. dec_exponent = 0;
  275. sig_digits = 0;
  276. decimal_pt = 1;
  277. /* set initial flags to 0 */
  278. flt->flags = 0;
  279. /* check for + or - character and skip */
  280. if (*str == '-') {
  281. flt->sign = 1;
  282. str++;
  283. } else if (*str == '+') {
  284. flt->sign = 0;
  285. str++;
  286. } else
  287. flt->sign = 0;
  288. /* eliminate any leading zeros (which do not count as significant digits) */
  289. while (*str == '0')
  290. str++;
  291. /* When we reach the end of the leading zeros, first check for a decimal
  292. * point. If the number is of the form "0---0.0000" we need to get rid
  293. * of the zeros after the decimal point and not count them as significant
  294. * digits.
  295. */
  296. if (*str == '.') {
  297. str++;
  298. while (*str == '0') {
  299. str++;
  300. dec_exponent--;
  301. }
  302. } else {
  303. /* The number is of the form "yyy.xxxx" (where y <> 0). */
  304. while (isdigit(*str)) {
  305. /* See if we've processed more than the max significant digits: */
  306. if (sig_digits < MANT_SIGDIGITS) {
  307. /* Multiply mantissa by 10 [x = (x<<1)+(x<<3)] */
  308. BitVector_shift_left(flt->mantissa, 0);
  309. BitVector_Copy(operand[0], flt->mantissa);
  310. BitVector_Move_Left(flt->mantissa, 2);
  311. carry = 0;
  312. BitVector_add(operand[1], operand[0], flt->mantissa, &carry);
  313. /* Add in current digit */
  314. BitVector_Empty(operand[0]);
  315. BitVector_Chunk_Store(operand[0], 4, 0, (N_long)(*str-'0'));
  316. carry = 0;
  317. BitVector_add(flt->mantissa, operand[1], operand[0], &carry);
  318. } else {
  319. /* Can't integrate more digits with mantissa, so instead just
  320. * raise by a power of ten.
  321. */
  322. dec_exponent++;
  323. }
  324. sig_digits++;
  325. str++;
  326. }
  327. if (*str == '.')
  328. str++;
  329. else
  330. decimal_pt = 0;
  331. }
  332. if (decimal_pt) {
  333. /* Process the digits to the right of the decimal point. */
  334. while (isdigit(*str)) {
  335. /* See if we've processed more than 19 significant digits: */
  336. if (sig_digits < 19) {
  337. /* Raise by a power of ten */
  338. dec_exponent--;
  339. /* Multiply mantissa by 10 [x = (x<<1)+(x<<3)] */
  340. BitVector_shift_left(flt->mantissa, 0);
  341. BitVector_Copy(operand[0], flt->mantissa);
  342. BitVector_Move_Left(flt->mantissa, 2);
  343. carry = 0;
  344. BitVector_add(operand[1], operand[0], flt->mantissa, &carry);
  345. /* Add in current digit */
  346. BitVector_Empty(operand[0]);
  347. BitVector_Chunk_Store(operand[0], 4, 0, (N_long)(*str-'0'));
  348. carry = 0;
  349. BitVector_add(flt->mantissa, operand[1], operand[0], &carry);
  350. }
  351. sig_digits++;
  352. str++;
  353. }
  354. }
  355. if (*str == 'e' || *str == 'E') {
  356. str++;
  357. /* We just saw the "E" character, now read in the exponent value and
  358. * add it into dec_exponent.
  359. */
  360. dec_exp_add = 0;
  361. sscanf(str, "%d", &dec_exp_add);
  362. dec_exponent += dec_exp_add;
  363. }
  364. /* Free calculation variables. */
  365. BitVector_Destroy(operand[1]);
  366. BitVector_Destroy(operand[0]);
  367. /* Normalize the number, checking for 0 first. */
  368. if (BitVector_is_empty(flt->mantissa)) {
  369. /* Mantissa is 0, zero exponent too. */
  370. flt->exponent = 0;
  371. /* Set zero flag so output functions don't see 0 value as underflow. */
  372. flt->flags |= FLAG_ISZERO;
  373. /* Return 0 value. */
  374. return flt;
  375. }
  376. /* Exponent if already norm. */
  377. flt->exponent = (unsigned short)(0x7FFF+(MANT_BITS-1));
  378. floatnum_normalize(flt);
  379. /* The number is normalized. Now multiply by 10 the number of times
  380. * specified in DecExponent. This uses the power of ten tables to speed
  381. * up this operation (and make it more accurate).
  382. */
  383. if (dec_exponent > 0) {
  384. POT_index = 0;
  385. /* Until we hit 1.0 or finish exponent or overflow */
  386. while ((POT_index < 14) && (dec_exponent != 0) &&
  387. (flt->exponent != EXP_INF)) {
  388. /* Find the first power of ten in the table which is just less than
  389. * the exponent.
  390. */
  391. while (dec_exponent < POT_TableP[POT_index].dec_exponent)
  392. POT_index++;
  393. if (POT_index < 14) {
  394. /* Subtract out what we're multiplying in from exponent */
  395. dec_exponent -= POT_TableP[POT_index].dec_exponent;
  396. /* Multiply by current power of 10 */
  397. floatnum_mul(flt, &POT_TableP[POT_index].f);
  398. }
  399. }
  400. } else if (dec_exponent < 0) {
  401. POT_index = 0;
  402. /* Until we hit 1.0 or finish exponent or underflow */
  403. while ((POT_index < 14) && (dec_exponent != 0) &&
  404. (flt->exponent != EXP_ZERO)) {
  405. /* Find the first power of ten in the table which is just less than
  406. * the exponent.
  407. */
  408. while (dec_exponent > POT_TableN[POT_index].dec_exponent)
  409. POT_index++;
  410. if (POT_index < 14) {
  411. /* Subtract out what we're multiplying in from exponent */
  412. dec_exponent -= POT_TableN[POT_index].dec_exponent;
  413. /* Multiply by current power of 10 */
  414. floatnum_mul(flt, &POT_TableN[POT_index].f);
  415. }
  416. }
  417. }
  418. /* Round the result. (Don't round underflow or overflow). Also don't
  419. * increment if this would cause the mantissa to wrap.
  420. */
  421. if ((flt->exponent != EXP_INF) && (flt->exponent != EXP_ZERO) &&
  422. !BitVector_is_full(flt->mantissa))
  423. BitVector_increment(flt->mantissa);
  424. return flt;
  425. }
  426. yasm_floatnum *
  427. yasm_floatnum_copy(const yasm_floatnum *flt)
  428. {
  429. yasm_floatnum *f = yasm_xmalloc(sizeof(yasm_floatnum));
  430. f->mantissa = BitVector_Clone(flt->mantissa);
  431. f->exponent = flt->exponent;
  432. f->sign = flt->sign;
  433. f->flags = flt->flags;
  434. return f;
  435. }
  436. void
  437. yasm_floatnum_destroy(yasm_floatnum *flt)
  438. {
  439. BitVector_Destroy(flt->mantissa);
  440. yasm_xfree(flt);
  441. }
  442. int
  443. yasm_floatnum_calc(yasm_floatnum *acc, yasm_expr_op op,
  444. /*@unused@*/ yasm_floatnum *operand)
  445. {
  446. if (op != YASM_EXPR_NEG) {
  447. yasm_error_set(YASM_ERROR_FLOATING_POINT,
  448. N_("Unsupported floating-point arithmetic operation"));
  449. return 1;
  450. }
  451. acc->sign ^= 1;
  452. return 0;
  453. }
  454. int
  455. yasm_floatnum_get_int(const yasm_floatnum *flt, unsigned long *ret_val)
  456. {
  457. unsigned char t[4];
  458. if (yasm_floatnum_get_sized(flt, t, 4, 32, 0, 0, 0)) {
  459. *ret_val = 0xDEADBEEFUL; /* Obviously incorrect return value */
  460. return 1;
  461. }
  462. YASM_LOAD_32_L(*ret_val, &t[0]);
  463. return 0;
  464. }
  465. /* Function used by conversion routines to actually perform the conversion.
  466. *
  467. * ptr -> the array to return the little-endian floating point value into.
  468. * flt -> the floating point value to convert.
  469. * byte_size -> the size in bytes of the output format.
  470. * mant_bits -> the size in bits of the output mantissa.
  471. * implicit1 -> does the output format have an implicit 1? 1=yes, 0=no.
  472. * exp_bits -> the size in bits of the output exponent.
  473. *
  474. * Returns 0 on success, 1 if overflow, -1 if underflow.
  475. */
  476. static int
  477. floatnum_get_common(const yasm_floatnum *flt, /*@out@*/ unsigned char *ptr,
  478. N_int byte_size, N_int mant_bits, int implicit1,
  479. N_int exp_bits)
  480. {
  481. long exponent = (long)flt->exponent;
  482. wordptr output;
  483. charptr buf;
  484. unsigned int len;
  485. unsigned int overflow = 0, underflow = 0;
  486. int retval = 0;
  487. long exp_bias = (1<<(exp_bits-1))-1;
  488. long exp_inf = (1<<exp_bits)-1;
  489. output = BitVector_Create(byte_size*8, TRUE);
  490. /* copy mantissa */
  491. BitVector_Interval_Copy(output, flt->mantissa, 0,
  492. (N_int)((MANT_BITS-implicit1)-mant_bits),
  493. mant_bits);
  494. /* round mantissa */
  495. if (BitVector_bit_test(flt->mantissa, (MANT_BITS-implicit1)-(mant_bits+1)))
  496. BitVector_increment(output);
  497. if (BitVector_bit_test(output, mant_bits)) {
  498. /* overflowed, so zero mantissa (and set explicit bit if necessary) */
  499. BitVector_Empty(output);
  500. BitVector_Bit_Copy(output, mant_bits-1, !implicit1);
  501. /* and up the exponent (checking for overflow) */
  502. if (exponent+1 >= EXP_INF)
  503. overflow = 1;
  504. else
  505. exponent++;
  506. }
  507. /* adjust the exponent to the output bias, checking for overflow */
  508. exponent -= EXP_BIAS-exp_bias;
  509. if (exponent >= exp_inf)
  510. overflow = 1;
  511. else if (exponent <= 0)
  512. underflow = 1;
  513. /* underflow and overflow both set!? */
  514. if (underflow && overflow)
  515. yasm_internal_error(N_("Both underflow and overflow set"));
  516. /* check for underflow or overflow and set up appropriate output */
  517. if (underflow) {
  518. BitVector_Empty(output);
  519. exponent = 0;
  520. if (!(flt->flags & FLAG_ISZERO))
  521. retval = -1;
  522. } else if (overflow) {
  523. BitVector_Empty(output);
  524. exponent = exp_inf;
  525. retval = 1;
  526. }
  527. /* move exponent into place */
  528. BitVector_Chunk_Store(output, exp_bits, mant_bits, (N_long)exponent);
  529. /* merge in sign bit */
  530. BitVector_Bit_Copy(output, byte_size*8-1, flt->sign);
  531. /* get little-endian bytes */
  532. buf = BitVector_Block_Read(output, &len);
  533. if (len < byte_size)
  534. yasm_internal_error(
  535. N_("Byte length of BitVector does not match bit length"));
  536. /* copy to output */
  537. memcpy(ptr, buf, byte_size*sizeof(unsigned char));
  538. /* free allocated resources */
  539. yasm_xfree(buf);
  540. BitVector_Destroy(output);
  541. return retval;
  542. }
  543. /* IEEE-754r "half precision" format:
  544. * 16 bits:
  545. * 15 9 Bit 0
  546. * | | |
  547. * seee eemm mmmm mmmm
  548. *
  549. * e = bias 15 exponent
  550. * s = sign bit
  551. * m = mantissa bits, bit 10 is an implied one bit.
  552. *
  553. * IEEE-754 (Intel) "single precision" format:
  554. * 32 bits:
  555. * Bit 31 Bit 22 Bit 0
  556. * | | |
  557. * seeeeeee emmmmmmm mmmmmmmm mmmmmmmm
  558. *
  559. * e = bias 127 exponent
  560. * s = sign bit
  561. * m = mantissa bits, bit 23 is an implied one bit.
  562. *
  563. * IEEE-754 (Intel) "double precision" format:
  564. * 64 bits:
  565. * bit 63 bit 51 bit 0
  566. * | | |
  567. * seeeeeee eeeemmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm
  568. *
  569. * e = bias 1023 exponent.
  570. * s = sign bit.
  571. * m = mantissa bits. Bit 52 is an implied one bit.
  572. *
  573. * IEEE-754 (Intel) "extended precision" format:
  574. * 80 bits:
  575. * bit 79 bit 63 bit 0
  576. * | | |
  577. * seeeeeee eeeeeeee mmmmmmmm m...m m...m m...m m...m m...m
  578. *
  579. * e = bias 16383 exponent
  580. * m = 64 bit mantissa with NO implied bit!
  581. * s = sign (for mantissa)
  582. */
  583. int
  584. yasm_floatnum_get_sized(const yasm_floatnum *flt, unsigned char *ptr,
  585. size_t destsize, size_t valsize, size_t shift,
  586. int bigendian, int warn)
  587. {
  588. int retval;
  589. if (destsize*8 != valsize || shift>0 || bigendian) {
  590. /* TODO */
  591. yasm_internal_error(N_("unsupported floatnum functionality"));
  592. }
  593. switch (destsize) {
  594. case 2:
  595. retval = floatnum_get_common(flt, ptr, 2, 10, 1, 5);
  596. break;
  597. case 4:
  598. retval = floatnum_get_common(flt, ptr, 4, 23, 1, 8);
  599. break;
  600. case 8:
  601. retval = floatnum_get_common(flt, ptr, 8, 52, 1, 11);
  602. break;
  603. case 10:
  604. retval = floatnum_get_common(flt, ptr, 10, 64, 0, 15);
  605. break;
  606. default:
  607. yasm_internal_error(N_("Invalid float conversion size"));
  608. /*@notreached@*/
  609. return 1;
  610. }
  611. if (warn) {
  612. if (retval < 0)
  613. yasm_warn_set(YASM_WARN_GENERAL,
  614. N_("underflow in floating point expression"));
  615. else if (retval > 0)
  616. yasm_warn_set(YASM_WARN_GENERAL,
  617. N_("overflow in floating point expression"));
  618. }
  619. return retval;
  620. }
  621. /* 1 if the size is valid, 0 if it isn't */
  622. int
  623. yasm_floatnum_check_size(/*@unused@*/ const yasm_floatnum *flt, size_t size)
  624. {
  625. switch (size) {
  626. case 16:
  627. case 32:
  628. case 64:
  629. case 80:
  630. return 1;
  631. default:
  632. return 0;
  633. }
  634. }
  635. void
  636. yasm_floatnum_print(const yasm_floatnum *flt, FILE *f)
  637. {
  638. unsigned char out[10];
  639. unsigned char *str;
  640. int i;
  641. /* Internal format */
  642. str = BitVector_to_Hex(flt->mantissa);
  643. fprintf(f, "%c %s *2^%04x\n", flt->sign?'-':'+', (char *)str,
  644. flt->exponent);
  645. yasm_xfree(str);
  646. /* 32-bit (single precision) format */
  647. fprintf(f, "32-bit: %d: ",
  648. yasm_floatnum_get_sized(flt, out, 4, 32, 0, 0, 0));
  649. for (i=0; i<4; i++)
  650. fprintf(f, "%02x ", out[i]);
  651. fprintf(f, "\n");
  652. /* 64-bit (double precision) format */
  653. fprintf(f, "64-bit: %d: ",
  654. yasm_floatnum_get_sized(flt, out, 8, 64, 0, 0, 0));
  655. for (i=0; i<8; i++)
  656. fprintf(f, "%02x ", out[i]);
  657. fprintf(f, "\n");
  658. /* 80-bit (extended precision) format */
  659. fprintf(f, "80-bit: %d: ",
  660. yasm_floatnum_get_sized(flt, out, 10, 80, 0, 0, 0));
  661. for (i=0; i<10; i++)
  662. fprintf(f, "%02x ", out[i]);
  663. fprintf(f, "\n");
  664. }