floatformat.c 22 KB

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