floatformat.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. /* IEEE floating point support routines, for GDB, the GNU Debugger.
  2. Copyright 1991, 1994, 1999, 2000, 2003, 2005, 2006, 2010, 2012
  3. Free Software Foundation, Inc.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
  16. /* This is needed to pick up the NAN macro on some systems. */
  17. #define _GNU_SOURCE
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #include <math.h>
  22. #ifdef HAVE_STRING_H
  23. #include <string.h>
  24. #endif
  25. /* On some platforms, <float.h> provides DBL_QNAN. */
  26. #ifdef STDC_HEADERS
  27. #include <float.h>
  28. #endif
  29. #include "ansidecl.h"
  30. #include "libiberty.h"
  31. #include "floatformat.h"
  32. #ifndef INFINITY
  33. #ifdef HUGE_VAL
  34. #define INFINITY HUGE_VAL
  35. #else
  36. #define INFINITY (1.0 / 0.0)
  37. #endif
  38. #endif
  39. #ifndef NAN
  40. #ifdef DBL_QNAN
  41. #define NAN DBL_QNAN
  42. #else
  43. #define NAN (0.0 / 0.0)
  44. #endif
  45. #endif
  46. static int mant_bits_set (const struct floatformat *, const unsigned char *);
  47. static unsigned long get_field (const unsigned char *,
  48. enum floatformat_byteorders,
  49. unsigned int,
  50. unsigned int,
  51. unsigned int);
  52. static int floatformat_always_valid (const struct floatformat *fmt,
  53. const void *from);
  54. static int
  55. floatformat_always_valid (const struct floatformat *fmt ATTRIBUTE_UNUSED,
  56. const void *from ATTRIBUTE_UNUSED)
  57. {
  58. return 1;
  59. }
  60. /* The odds that CHAR_BIT will be anything but 8 are low enough that I'm not
  61. going to bother with trying to muck around with whether it is defined in
  62. a system header, what we do if not, etc. */
  63. #define FLOATFORMAT_CHAR_BIT 8
  64. /* floatformats for IEEE half, single and double, big and little endian. */
  65. const struct floatformat floatformat_ieee_half_big =
  66. {
  67. floatformat_big, 16, 0, 1, 5, 15, 31, 6, 10,
  68. floatformat_intbit_no,
  69. "floatformat_ieee_half_big",
  70. floatformat_always_valid,
  71. NULL
  72. };
  73. const struct floatformat floatformat_ieee_half_little =
  74. {
  75. floatformat_little, 16, 0, 1, 5, 15, 31, 6, 10,
  76. floatformat_intbit_no,
  77. "floatformat_ieee_half_little",
  78. floatformat_always_valid,
  79. NULL
  80. };
  81. const struct floatformat floatformat_ieee_single_big =
  82. {
  83. floatformat_big, 32, 0, 1, 8, 127, 255, 9, 23,
  84. floatformat_intbit_no,
  85. "floatformat_ieee_single_big",
  86. floatformat_always_valid,
  87. NULL
  88. };
  89. const struct floatformat floatformat_ieee_single_little =
  90. {
  91. floatformat_little, 32, 0, 1, 8, 127, 255, 9, 23,
  92. floatformat_intbit_no,
  93. "floatformat_ieee_single_little",
  94. floatformat_always_valid,
  95. NULL
  96. };
  97. const struct floatformat floatformat_ieee_double_big =
  98. {
  99. floatformat_big, 64, 0, 1, 11, 1023, 2047, 12, 52,
  100. floatformat_intbit_no,
  101. "floatformat_ieee_double_big",
  102. floatformat_always_valid,
  103. NULL
  104. };
  105. const struct floatformat floatformat_ieee_double_little =
  106. {
  107. floatformat_little, 64, 0, 1, 11, 1023, 2047, 12, 52,
  108. floatformat_intbit_no,
  109. "floatformat_ieee_double_little",
  110. floatformat_always_valid,
  111. NULL
  112. };
  113. /* floatformat for IEEE double, little endian byte order, with big endian word
  114. ordering, as on the ARM. */
  115. const struct floatformat floatformat_ieee_double_littlebyte_bigword =
  116. {
  117. floatformat_littlebyte_bigword, 64, 0, 1, 11, 1023, 2047, 12, 52,
  118. floatformat_intbit_no,
  119. "floatformat_ieee_double_littlebyte_bigword",
  120. floatformat_always_valid,
  121. NULL
  122. };
  123. /* floatformat for VAX. Not quite IEEE, but close enough. */
  124. const struct floatformat floatformat_vax_f =
  125. {
  126. floatformat_vax, 32, 0, 1, 8, 129, 0, 9, 23,
  127. floatformat_intbit_no,
  128. "floatformat_vax_f",
  129. floatformat_always_valid,
  130. NULL
  131. };
  132. const struct floatformat floatformat_vax_d =
  133. {
  134. floatformat_vax, 64, 0, 1, 8, 129, 0, 9, 55,
  135. floatformat_intbit_no,
  136. "floatformat_vax_d",
  137. floatformat_always_valid,
  138. NULL
  139. };
  140. const struct floatformat floatformat_vax_g =
  141. {
  142. floatformat_vax, 64, 0, 1, 11, 1025, 0, 12, 52,
  143. floatformat_intbit_no,
  144. "floatformat_vax_g",
  145. floatformat_always_valid,
  146. NULL
  147. };
  148. static int floatformat_i387_ext_is_valid (const struct floatformat *fmt,
  149. const void *from);
  150. static int
  151. floatformat_i387_ext_is_valid (const struct floatformat *fmt, const void *from)
  152. {
  153. /* In the i387 double-extended format, if the exponent is all ones,
  154. then the integer bit must be set. If the exponent is neither 0
  155. nor ~0, the intbit must also be set. Only if the exponent is
  156. zero can it be zero, and then it must be zero. */
  157. unsigned long exponent, int_bit;
  158. const unsigned char *ufrom = (const unsigned char *) from;
  159. exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
  160. fmt->exp_start, fmt->exp_len);
  161. int_bit = get_field (ufrom, fmt->byteorder, fmt->totalsize,
  162. fmt->man_start, 1);
  163. if ((exponent == 0) != (int_bit == 0))
  164. return 0;
  165. else
  166. return 1;
  167. }
  168. const struct floatformat floatformat_i387_ext =
  169. {
  170. floatformat_little, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
  171. floatformat_intbit_yes,
  172. "floatformat_i387_ext",
  173. floatformat_i387_ext_is_valid,
  174. NULL
  175. };
  176. const struct floatformat floatformat_m68881_ext =
  177. {
  178. /* Note that the bits from 16 to 31 are unused. */
  179. floatformat_big, 96, 0, 1, 15, 0x3fff, 0x7fff, 32, 64,
  180. floatformat_intbit_yes,
  181. "floatformat_m68881_ext",
  182. floatformat_always_valid,
  183. NULL
  184. };
  185. const struct floatformat floatformat_i960_ext =
  186. {
  187. /* Note that the bits from 0 to 15 are unused. */
  188. floatformat_little, 96, 16, 17, 15, 0x3fff, 0x7fff, 32, 64,
  189. floatformat_intbit_yes,
  190. "floatformat_i960_ext",
  191. floatformat_always_valid,
  192. NULL
  193. };
  194. const struct floatformat floatformat_m88110_ext =
  195. {
  196. floatformat_big, 80, 0, 1, 15, 0x3fff, 0x7fff, 16, 64,
  197. floatformat_intbit_yes,
  198. "floatformat_m88110_ext",
  199. floatformat_always_valid,
  200. NULL
  201. };
  202. const struct floatformat floatformat_m88110_harris_ext =
  203. {
  204. /* Harris uses raw format 128 bytes long, but the number is just an ieee
  205. double, and the last 64 bits are wasted. */
  206. floatformat_big,128, 0, 1, 11, 0x3ff, 0x7ff, 12, 52,
  207. floatformat_intbit_no,
  208. "floatformat_m88110_ext_harris",
  209. floatformat_always_valid,
  210. NULL
  211. };
  212. const struct floatformat floatformat_arm_ext_big =
  213. {
  214. /* Bits 1 to 16 are unused. */
  215. floatformat_big, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
  216. floatformat_intbit_yes,
  217. "floatformat_arm_ext_big",
  218. floatformat_always_valid,
  219. NULL
  220. };
  221. const struct floatformat floatformat_arm_ext_littlebyte_bigword =
  222. {
  223. /* Bits 1 to 16 are unused. */
  224. floatformat_littlebyte_bigword, 96, 0, 17, 15, 0x3fff, 0x7fff, 32, 64,
  225. floatformat_intbit_yes,
  226. "floatformat_arm_ext_littlebyte_bigword",
  227. floatformat_always_valid,
  228. NULL
  229. };
  230. const struct floatformat floatformat_ia64_spill_big =
  231. {
  232. floatformat_big, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
  233. floatformat_intbit_yes,
  234. "floatformat_ia64_spill_big",
  235. floatformat_always_valid,
  236. NULL
  237. };
  238. const struct floatformat floatformat_ia64_spill_little =
  239. {
  240. floatformat_little, 128, 0, 1, 17, 65535, 0x1ffff, 18, 64,
  241. floatformat_intbit_yes,
  242. "floatformat_ia64_spill_little",
  243. floatformat_always_valid,
  244. NULL
  245. };
  246. const struct floatformat floatformat_ia64_quad_big =
  247. {
  248. floatformat_big, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
  249. floatformat_intbit_no,
  250. "floatformat_ia64_quad_big",
  251. floatformat_always_valid,
  252. NULL
  253. };
  254. const struct floatformat floatformat_ia64_quad_little =
  255. {
  256. floatformat_little, 128, 0, 1, 15, 16383, 0x7fff, 16, 112,
  257. floatformat_intbit_no,
  258. "floatformat_ia64_quad_little",
  259. floatformat_always_valid,
  260. NULL
  261. };
  262. static int
  263. floatformat_ibm_long_double_is_valid (const struct floatformat *fmt,
  264. const void *from)
  265. {
  266. const unsigned char *ufrom = (const unsigned char *) from;
  267. const struct floatformat *hfmt = fmt->split_half;
  268. long top_exp, bot_exp;
  269. int top_nan = 0;
  270. top_exp = get_field (ufrom, hfmt->byteorder, hfmt->totalsize,
  271. hfmt->exp_start, hfmt->exp_len);
  272. bot_exp = get_field (ufrom + 8, hfmt->byteorder, hfmt->totalsize,
  273. hfmt->exp_start, hfmt->exp_len);
  274. if ((unsigned long) top_exp == hfmt->exp_nan)
  275. top_nan = mant_bits_set (hfmt, ufrom);
  276. /* A NaN is valid with any low part. */
  277. if (top_nan)
  278. return 1;
  279. /* An infinity, zero or denormal requires low part 0 (positive or
  280. negative). */
  281. if ((unsigned long) top_exp == hfmt->exp_nan || top_exp == 0)
  282. {
  283. if (bot_exp != 0)
  284. return 0;
  285. return !mant_bits_set (hfmt, ufrom + 8);
  286. }
  287. /* The top part is now a finite normal value. The long double value
  288. is the sum of the two parts, and the top part must equal the
  289. result of rounding the long double value to nearest double. Thus
  290. the bottom part must be <= 0.5ulp of the top part in absolute
  291. value, and if it is < 0.5ulp then the long double is definitely
  292. valid. */
  293. if (bot_exp < top_exp - 53)
  294. return 1;
  295. if (bot_exp > top_exp - 53 && bot_exp != 0)
  296. return 0;
  297. if (bot_exp == 0)
  298. {
  299. /* The bottom part is 0 or denormal. Determine which, and if
  300. denormal the first two set bits. */
  301. int first_bit = -1, second_bit = -1, cur_bit;
  302. for (cur_bit = 0; (unsigned int) cur_bit < hfmt->man_len; cur_bit++)
  303. if (get_field (ufrom + 8, hfmt->byteorder, hfmt->totalsize,
  304. hfmt->man_start + cur_bit, 1))
  305. {
  306. if (first_bit == -1)
  307. first_bit = cur_bit;
  308. else
  309. {
  310. second_bit = cur_bit;
  311. break;
  312. }
  313. }
  314. /* Bottom part 0 is OK. */
  315. if (first_bit == -1)
  316. return 1;
  317. /* The real exponent of the bottom part is -first_bit. */
  318. if (-first_bit < top_exp - 53)
  319. return 1;
  320. if (-first_bit > top_exp - 53)
  321. return 0;
  322. /* The bottom part is at least 0.5ulp of the top part. For this
  323. to be OK, the bottom part must be exactly 0.5ulp (i.e. no
  324. more bits set) and the top part must have last bit 0. */
  325. if (second_bit != -1)
  326. return 0;
  327. return !get_field (ufrom, hfmt->byteorder, hfmt->totalsize,
  328. hfmt->man_start + hfmt->man_len - 1, 1);
  329. }
  330. else
  331. {
  332. /* The bottom part is at least 0.5ulp of the top part. For this
  333. to be OK, it must be exactly 0.5ulp (i.e. no explicit bits
  334. set) and the top part must have last bit 0. */
  335. if (get_field (ufrom, hfmt->byteorder, hfmt->totalsize,
  336. hfmt->man_start + hfmt->man_len - 1, 1))
  337. return 0;
  338. return !mant_bits_set (hfmt, ufrom + 8);
  339. }
  340. }
  341. const struct floatformat floatformat_ibm_long_double_big =
  342. {
  343. floatformat_big, 128, 0, 1, 11, 1023, 2047, 12, 52,
  344. floatformat_intbit_no,
  345. "floatformat_ibm_long_double_big",
  346. floatformat_ibm_long_double_is_valid,
  347. &floatformat_ieee_double_big
  348. };
  349. const struct floatformat floatformat_ibm_long_double_little =
  350. {
  351. floatformat_little, 128, 0, 1, 11, 1023, 2047, 12, 52,
  352. floatformat_intbit_no,
  353. "floatformat_ibm_long_double_little",
  354. floatformat_ibm_long_double_is_valid,
  355. &floatformat_ieee_double_little
  356. };
  357. #ifndef min
  358. #define min(a, b) ((a) < (b) ? (a) : (b))
  359. #endif
  360. /* Return 1 if any bits are explicitly set in the mantissa of UFROM,
  361. format FMT, 0 otherwise. */
  362. static int
  363. mant_bits_set (const struct floatformat *fmt, const unsigned char *ufrom)
  364. {
  365. unsigned int mant_bits, mant_off;
  366. int mant_bits_left;
  367. mant_off = fmt->man_start;
  368. mant_bits_left = fmt->man_len;
  369. while (mant_bits_left > 0)
  370. {
  371. mant_bits = min (mant_bits_left, 32);
  372. if (get_field (ufrom, fmt->byteorder, fmt->totalsize,
  373. mant_off, mant_bits) != 0)
  374. return 1;
  375. mant_off += mant_bits;
  376. mant_bits_left -= mant_bits;
  377. }
  378. return 0;
  379. }
  380. /* Extract a field which starts at START and is LEN bits long. DATA and
  381. TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
  382. static unsigned long
  383. get_field (const unsigned char *data, enum floatformat_byteorders order,
  384. unsigned int total_len, unsigned int start, unsigned int len)
  385. {
  386. unsigned long result = 0;
  387. unsigned int cur_byte;
  388. int lo_bit, hi_bit, cur_bitshift = 0;
  389. int nextbyte = (order == floatformat_little) ? 1 : -1;
  390. /* Start is in big-endian bit order! Fix that first. */
  391. start = total_len - (start + len);
  392. /* Start at the least significant part of the field. */
  393. if (order == floatformat_little)
  394. cur_byte = start / FLOATFORMAT_CHAR_BIT;
  395. else
  396. cur_byte = (total_len - start - 1) / FLOATFORMAT_CHAR_BIT;
  397. lo_bit = start % FLOATFORMAT_CHAR_BIT;
  398. hi_bit = min (lo_bit + len, FLOATFORMAT_CHAR_BIT);
  399. do
  400. {
  401. unsigned int shifted = *(data + cur_byte) >> lo_bit;
  402. unsigned int bits = hi_bit - lo_bit;
  403. unsigned int mask = (1 << bits) - 1;
  404. result |= (shifted & mask) << cur_bitshift;
  405. len -= bits;
  406. cur_bitshift += bits;
  407. cur_byte += nextbyte;
  408. lo_bit = 0;
  409. hi_bit = min (len, FLOATFORMAT_CHAR_BIT);
  410. }
  411. while (len != 0);
  412. return result;
  413. }
  414. /* Convert from FMT to a double.
  415. FROM is the address of the extended float.
  416. Store the double in *TO. */
  417. void
  418. floatformat_to_double (const struct floatformat *fmt,
  419. const void *from, double *to)
  420. {
  421. const unsigned char *ufrom = (const unsigned char *) from;
  422. double dto;
  423. long exponent;
  424. unsigned long mant;
  425. unsigned int mant_bits, mant_off;
  426. int mant_bits_left;
  427. /* Split values are not handled specially, since the top half has
  428. the correctly rounded double value (in the only supported case of
  429. split values). */
  430. exponent = get_field (ufrom, fmt->byteorder, fmt->totalsize,
  431. fmt->exp_start, fmt->exp_len);
  432. /* If the exponent indicates a NaN, we don't have information to
  433. decide what to do. So we handle it like IEEE, except that we
  434. don't try to preserve the type of NaN. FIXME. */
  435. if ((unsigned long) exponent == fmt->exp_nan)
  436. {
  437. int nan = mant_bits_set (fmt, ufrom);
  438. /* On certain systems (such as GNU/Linux), the use of the
  439. INFINITY macro below may generate a warning that can not be
  440. silenced due to a bug in GCC (PR preprocessor/11931). The
  441. preprocessor fails to recognise the __extension__ keyword in
  442. conjunction with the GNU/C99 extension for hexadecimal
  443. floating point constants and will issue a warning when
  444. compiling with -pedantic. */
  445. if (nan)
  446. dto = NAN;
  447. else
  448. dto = INFINITY;
  449. if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
  450. dto = -dto;
  451. *to = dto;
  452. return;
  453. }
  454. mant_bits_left = fmt->man_len;
  455. mant_off = fmt->man_start;
  456. dto = 0.0;
  457. /* Build the result algebraically. Might go infinite, underflow, etc;
  458. who cares. */
  459. /* For denorms use minimum exponent. */
  460. if (exponent == 0)
  461. exponent = 1 - fmt->exp_bias;
  462. else
  463. {
  464. exponent -= fmt->exp_bias;
  465. /* If this format uses a hidden bit, explicitly add it in now.
  466. Otherwise, increment the exponent by one to account for the
  467. integer bit. */
  468. if (fmt->intbit == floatformat_intbit_no)
  469. dto = ldexp (1.0, exponent);
  470. else
  471. exponent++;
  472. }
  473. while (mant_bits_left > 0)
  474. {
  475. mant_bits = min (mant_bits_left, 32);
  476. mant = get_field (ufrom, fmt->byteorder, fmt->totalsize,
  477. mant_off, mant_bits);
  478. dto += ldexp ((double) mant, exponent - mant_bits);
  479. exponent -= mant_bits;
  480. mant_off += mant_bits;
  481. mant_bits_left -= mant_bits;
  482. }
  483. /* Negate it if negative. */
  484. if (get_field (ufrom, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1))
  485. dto = -dto;
  486. *to = dto;
  487. }
  488. static void put_field (unsigned char *, enum floatformat_byteorders,
  489. unsigned int,
  490. unsigned int,
  491. unsigned int,
  492. unsigned long);
  493. /* Set a field which starts at START and is LEN bits long. DATA and
  494. TOTAL_LEN are the thing we are extracting it from, in byteorder ORDER. */
  495. static void
  496. put_field (unsigned char *data, enum floatformat_byteorders order,
  497. unsigned int total_len, unsigned int start, unsigned int len,
  498. unsigned long stuff_to_put)
  499. {
  500. unsigned int cur_byte;
  501. int lo_bit, hi_bit;
  502. int nextbyte = (order == floatformat_little) ? 1 : -1;
  503. /* Start is in big-endian bit order! Fix that first. */
  504. start = total_len - (start + len);
  505. /* Start at the least significant part of the field. */
  506. if (order == floatformat_little)
  507. cur_byte = start / FLOATFORMAT_CHAR_BIT;
  508. else
  509. cur_byte = (total_len - start - 1) / FLOATFORMAT_CHAR_BIT;
  510. lo_bit = start % FLOATFORMAT_CHAR_BIT;
  511. hi_bit = min (lo_bit + len, FLOATFORMAT_CHAR_BIT);
  512. do
  513. {
  514. unsigned char *byte_ptr = data + cur_byte;
  515. unsigned int bits = hi_bit - lo_bit;
  516. unsigned int mask = ((1 << bits) - 1) << lo_bit;
  517. *byte_ptr = (*byte_ptr & ~mask) | ((stuff_to_put << lo_bit) & mask);
  518. stuff_to_put >>= bits;
  519. len -= bits;
  520. cur_byte += nextbyte;
  521. lo_bit = 0;
  522. hi_bit = min (len, FLOATFORMAT_CHAR_BIT);
  523. }
  524. while (len != 0);
  525. }
  526. /* The converse: convert the double *FROM to an extended float
  527. and store where TO points. Neither FROM nor TO have any alignment
  528. restrictions. */
  529. void
  530. floatformat_from_double (const struct floatformat *fmt,
  531. const double *from, void *to)
  532. {
  533. double dfrom;
  534. int exponent;
  535. double mant;
  536. unsigned int mant_bits, mant_off;
  537. int mant_bits_left;
  538. unsigned char *uto = (unsigned char *) to;
  539. dfrom = *from;
  540. memset (uto, 0, fmt->totalsize / FLOATFORMAT_CHAR_BIT);
  541. /* Split values are not handled specially, since a bottom half of
  542. zero is correct for any value representable as double (in the
  543. only supported case of split values). */
  544. /* If negative, set the sign bit. */
  545. if (dfrom < 0)
  546. {
  547. put_field (uto, fmt->byteorder, fmt->totalsize, fmt->sign_start, 1, 1);
  548. dfrom = -dfrom;
  549. }
  550. if (dfrom == 0)
  551. {
  552. /* 0.0. */
  553. return;
  554. }
  555. if (dfrom != dfrom)
  556. {
  557. /* NaN. */
  558. put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
  559. fmt->exp_len, fmt->exp_nan);
  560. /* Be sure it's not infinity, but NaN value is irrelevant. */
  561. put_field (uto, fmt->byteorder, fmt->totalsize, fmt->man_start,
  562. 32, 1);
  563. return;
  564. }
  565. if (dfrom + dfrom == dfrom)
  566. {
  567. /* This can only happen for an infinite value (or zero, which we
  568. already handled above). */
  569. put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
  570. fmt->exp_len, fmt->exp_nan);
  571. return;
  572. }
  573. mant = frexp (dfrom, &exponent);
  574. if (exponent + fmt->exp_bias - 1 > 0)
  575. put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
  576. fmt->exp_len, exponent + fmt->exp_bias - 1);
  577. else
  578. {
  579. /* Handle a denormalized number. FIXME: What should we do for
  580. non-IEEE formats? */
  581. put_field (uto, fmt->byteorder, fmt->totalsize, fmt->exp_start,
  582. fmt->exp_len, 0);
  583. mant = ldexp (mant, exponent + fmt->exp_bias - 1);
  584. }
  585. mant_bits_left = fmt->man_len;
  586. mant_off = fmt->man_start;
  587. while (mant_bits_left > 0)
  588. {
  589. unsigned long mant_long;
  590. mant_bits = mant_bits_left < 32 ? mant_bits_left : 32;
  591. mant *= 4294967296.0;
  592. mant_long = (unsigned long)mant;
  593. mant -= mant_long;
  594. /* If the integer bit is implicit, and we are not creating a
  595. denormalized number, then we need to discard it. */
  596. if ((unsigned int) mant_bits_left == fmt->man_len
  597. && fmt->intbit == floatformat_intbit_no
  598. && exponent + fmt->exp_bias - 1 > 0)
  599. {
  600. mant_long &= 0x7fffffff;
  601. mant_bits -= 1;
  602. }
  603. else if (mant_bits < 32)
  604. {
  605. /* The bits we want are in the most significant MANT_BITS bits of
  606. mant_long. Move them to the least significant. */
  607. mant_long >>= 32 - mant_bits;
  608. }
  609. put_field (uto, fmt->byteorder, fmt->totalsize,
  610. mant_off, mant_bits, mant_long);
  611. mant_off += mant_bits;
  612. mant_bits_left -= mant_bits;
  613. }
  614. }
  615. /* Return non-zero iff the data at FROM is a valid number in format FMT. */
  616. int
  617. floatformat_is_valid (const struct floatformat *fmt, const void *from)
  618. {
  619. return fmt->is_valid (fmt, from);
  620. }
  621. #ifdef IEEE_DEBUG
  622. #include <stdio.h>
  623. /* This is to be run on a host which uses IEEE floating point. */
  624. void
  625. ieee_test (double n)
  626. {
  627. double result;
  628. floatformat_to_double (&floatformat_ieee_double_little, &n, &result);
  629. if ((n != result && (! isnan (n) || ! isnan (result)))
  630. || (n < 0 && result >= 0)
  631. || (n >= 0 && result < 0))
  632. printf ("Differ(to): %.20g -> %.20g\n", n, result);
  633. floatformat_from_double (&floatformat_ieee_double_little, &n, &result);
  634. if ((n != result && (! isnan (n) || ! isnan (result)))
  635. || (n < 0 && result >= 0)
  636. || (n >= 0 && result < 0))
  637. printf ("Differ(from): %.20g -> %.20g\n", n, result);
  638. #if 0
  639. {
  640. char exten[16];
  641. floatformat_from_double (&floatformat_m68881_ext, &n, exten);
  642. floatformat_to_double (&floatformat_m68881_ext, exten, &result);
  643. if (n != result)
  644. printf ("Differ(to+from): %.20g -> %.20g\n", n, result);
  645. }
  646. #endif
  647. #if IEEE_DEBUG > 1
  648. /* This is to be run on a host which uses 68881 format. */
  649. {
  650. long double ex = *(long double *)exten;
  651. if (ex != n)
  652. printf ("Differ(from vs. extended): %.20g\n", n);
  653. }
  654. #endif
  655. }
  656. int
  657. main (void)
  658. {
  659. ieee_test (0.0);
  660. ieee_test (0.5);
  661. ieee_test (1.1);
  662. ieee_test (256.0);
  663. ieee_test (0.12345);
  664. ieee_test (234235.78907234);
  665. ieee_test (-512.0);
  666. ieee_test (-0.004321);
  667. ieee_test (1.2E-70);
  668. ieee_test (1.2E-316);
  669. ieee_test (4.9406564584124654E-324);
  670. ieee_test (- 4.9406564584124654E-324);
  671. ieee_test (- 0.0);
  672. ieee_test (- INFINITY);
  673. ieee_test (- NAN);
  674. ieee_test (INFINITY);
  675. ieee_test (NAN);
  676. return 0;
  677. }
  678. #endif