hdtoa.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* $OpenBSD: hdtoa.c,v 1.2 2009/10/16 12:15:03 martynas Exp $ */
  2. /*-
  3. * Copyright (c) 2004, 2005 David Schultz <das@FreeBSD.ORG>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. */
  27. #include <sys/types.h>
  28. #include <sys/ieee.h>
  29. #include <float.h>
  30. #include <limits.h>
  31. #include <math.h>
  32. #include "gdtoaimp.h"
  33. /* Strings values used by dtoa() */
  34. #define INFSTR "Infinity"
  35. #define NANSTR "NaN"
  36. #define DBL_ADJ (DBL_MAX_EXP - 2 + ((DBL_MANT_DIG - 1) % 4))
  37. #define LDBL_ADJ (LDBL_MAX_EXP - 2 + ((LDBL_MANT_DIG - 1) % 4))
  38. /*
  39. * Round up the given digit string. If the digit string is fff...f,
  40. * this procedure sets it to 100...0 and returns 1 to indicate that
  41. * the exponent needs to be bumped. Otherwise, 0 is returned.
  42. */
  43. static int
  44. roundup(char *s0, int ndigits)
  45. {
  46. char *s;
  47. for (s = s0 + ndigits - 1; *s == 0xf; s--) {
  48. if (s == s0) {
  49. *s = 1;
  50. return (1);
  51. }
  52. *s = 0;
  53. }
  54. ++*s;
  55. return (0);
  56. }
  57. /*
  58. * Round the given digit string to ndigits digits according to the
  59. * current rounding mode. Note that this could produce a string whose
  60. * value is not representable in the corresponding floating-point
  61. * type. The exponent pointed to by decpt is adjusted if necessary.
  62. */
  63. static void
  64. dorounding(char *s0, int ndigits, int sign, int *decpt)
  65. {
  66. int adjust = 0; /* do we need to adjust the exponent? */
  67. switch (FLT_ROUNDS) {
  68. case 0: /* toward zero */
  69. default: /* implementation-defined */
  70. break;
  71. case 1: /* to nearest, halfway rounds to even */
  72. if ((s0[ndigits] > 8) ||
  73. (s0[ndigits] == 8 && s0[ndigits + 1] & 1))
  74. adjust = roundup(s0, ndigits);
  75. break;
  76. case 2: /* toward +inf */
  77. if (sign == 0)
  78. adjust = roundup(s0, ndigits);
  79. break;
  80. case 3: /* toward -inf */
  81. if (sign != 0)
  82. adjust = roundup(s0, ndigits);
  83. break;
  84. }
  85. if (adjust)
  86. *decpt += 4;
  87. }
  88. /*
  89. * This procedure converts a double-precision number in IEEE format
  90. * into a string of hexadecimal digits and an exponent of 2. Its
  91. * behavior is bug-for-bug compatible with dtoa() in mode 2, with the
  92. * following exceptions:
  93. *
  94. * - An ndigits < 0 causes it to use as many digits as necessary to
  95. * represent the number exactly.
  96. * - The additional xdigs argument should point to either the string
  97. * "0123456789ABCDEF" or the string "0123456789abcdef", depending on
  98. * which case is desired.
  99. * - This routine does not repeat dtoa's mistake of setting decpt
  100. * to 9999 in the case of an infinity or NaN. INT_MAX is used
  101. * for this purpose instead.
  102. *
  103. * Note that the C99 standard does not specify what the leading digit
  104. * should be for non-zero numbers. For instance, 0x1.3p3 is the same
  105. * as 0x2.6p2 is the same as 0x4.cp3. This implementation chooses the
  106. * first digit so that subsequent digits are aligned on nibble
  107. * boundaries (before rounding).
  108. *
  109. * Inputs: d, xdigs, ndigits
  110. * Outputs: decpt, sign, rve
  111. */
  112. char *
  113. __hdtoa(double d, const char *xdigs, int ndigits, int *decpt, int *sign,
  114. char **rve)
  115. {
  116. static const int sigfigs = (DBL_MANT_DIG + 3) / 4;
  117. struct ieee_double *p = (struct ieee_double *)&d;
  118. char *s, *s0;
  119. int bufsize;
  120. *sign = p->dbl_sign;
  121. switch (fpclassify(d)) {
  122. case FP_NORMAL:
  123. *decpt = p->dbl_exp - DBL_ADJ;
  124. break;
  125. case FP_ZERO:
  126. *decpt = 1;
  127. return (nrv_alloc("0", rve, 1));
  128. case FP_SUBNORMAL:
  129. d *= 5.363123171977039e+154; /* = 0x1p514 */
  130. *decpt = p->dbl_exp - (514 + DBL_ADJ);
  131. break;
  132. case FP_INFINITE:
  133. *decpt = INT_MAX;
  134. return (nrv_alloc(INFSTR, rve, sizeof(INFSTR) - 1));
  135. case FP_NAN:
  136. *decpt = INT_MAX;
  137. return (nrv_alloc(NANSTR, rve, sizeof(NANSTR) - 1));
  138. default:
  139. abort();
  140. }
  141. /* FP_NORMAL or FP_SUBNORMAL */
  142. if (ndigits == 0) /* dtoa() compatibility */
  143. ndigits = 1;
  144. /*
  145. * For simplicity, we generate all the digits even if the
  146. * caller has requested fewer.
  147. */
  148. bufsize = (sigfigs > ndigits) ? sigfigs : ndigits;
  149. s0 = rv_alloc(bufsize);
  150. if (s0 == NULL)
  151. return (NULL);
  152. /*
  153. * We work from right to left, first adding any requested zero
  154. * padding, then the least significant portion of the
  155. * mantissa, followed by the most significant. The buffer is
  156. * filled with the byte values 0x0 through 0xf, which are
  157. * converted to xdigs[0x0] through xdigs[0xf] after the
  158. * rounding phase.
  159. */
  160. for (s = s0 + bufsize - 1; s > s0 + sigfigs - 1; s--)
  161. *s = 0;
  162. for (; s > s0 + sigfigs - (DBL_FRACLBITS / 4) - 1 && s > s0; s--) {
  163. *s = p->dbl_fracl & 0xf;
  164. p->dbl_fracl >>= 4;
  165. }
  166. for (; s > s0; s--) {
  167. *s = p->dbl_frach & 0xf;
  168. p->dbl_frach >>= 4;
  169. }
  170. /*
  171. * At this point, we have snarfed all the bits in the
  172. * mantissa, with the possible exception of the highest-order
  173. * (partial) nibble, which is dealt with by the next
  174. * statement. We also tack on the implicit normalization bit.
  175. */
  176. *s = p->dbl_frach | (1U << ((DBL_MANT_DIG - 1) % 4));
  177. /* If ndigits < 0, we are expected to auto-size the precision. */
  178. if (ndigits < 0) {
  179. for (ndigits = sigfigs; s0[ndigits - 1] == 0; ndigits--)
  180. ;
  181. }
  182. if (sigfigs > ndigits && s0[ndigits] != 0)
  183. dorounding(s0, ndigits, p->dbl_sign, decpt);
  184. s = s0 + ndigits;
  185. if (rve != NULL)
  186. *rve = s;
  187. *s-- = '\0';
  188. for (; s >= s0; s--)
  189. *s = xdigs[(unsigned int)*s];
  190. return (s0);
  191. }
  192. #if (LDBL_MANT_DIG > DBL_MANT_DIG)
  193. /*
  194. * This is the long double version of __hdtoa().
  195. */
  196. char *
  197. __hldtoa(long double e, const char *xdigs, int ndigits, int *decpt, int *sign,
  198. char **rve)
  199. {
  200. static const int sigfigs = (LDBL_MANT_DIG + 3) / 4;
  201. struct ieee_ext *p = (struct ieee_ext *)&e;
  202. char *s, *s0;
  203. int bufsize;
  204. *sign = p->ext_sign;
  205. switch (fpclassify(e)) {
  206. case FP_NORMAL:
  207. *decpt = p->ext_exp - LDBL_ADJ;
  208. break;
  209. case FP_ZERO:
  210. *decpt = 1;
  211. return (nrv_alloc("0", rve, 1));
  212. case FP_SUBNORMAL:
  213. e *= 0x1p514L;
  214. *decpt = p->ext_exp - (514 + LDBL_ADJ);
  215. break;
  216. case FP_INFINITE:
  217. *decpt = INT_MAX;
  218. return (nrv_alloc(INFSTR, rve, sizeof(INFSTR) - 1));
  219. case FP_NAN:
  220. *decpt = INT_MAX;
  221. return (nrv_alloc(NANSTR, rve, sizeof(NANSTR) - 1));
  222. default:
  223. abort();
  224. }
  225. /* FP_NORMAL or FP_SUBNORMAL */
  226. if (ndigits == 0) /* dtoa() compatibility */
  227. ndigits = 1;
  228. /*
  229. * For simplicity, we generate all the digits even if the
  230. * caller has requested fewer.
  231. */
  232. bufsize = (sigfigs > ndigits) ? sigfigs : ndigits;
  233. s0 = rv_alloc(bufsize);
  234. if (s0 == NULL)
  235. return (NULL);
  236. /*
  237. * We work from right to left, first adding any requested zero
  238. * padding, then the least significant portion of the
  239. * mantissa, followed by the most significant. The buffer is
  240. * filled with the byte values 0x0 through 0xf, which are
  241. * converted to xdigs[0x0] through xdigs[0xf] after the
  242. * rounding phase.
  243. */
  244. for (s = s0 + bufsize - 1; s > s0 + sigfigs - 1; s--)
  245. *s = 0;
  246. for (; s > s0 + sigfigs - (EXT_FRACLBITS / 4) - 1 && s > s0; s--) {
  247. *s = p->ext_fracl & 0xf;
  248. p->ext_fracl >>= 4;
  249. }
  250. #ifdef EXT_FRACHMBITS
  251. for (; s > s0; s--) {
  252. *s = p->ext_frachm & 0xf;
  253. p->ext_frachm >>= 4;
  254. }
  255. #endif
  256. #ifdef EXT_FRACLMBITS
  257. for (; s > s0; s--) {
  258. *s = p->ext_fraclm & 0xf;
  259. p->ext_fraclm >>= 4;
  260. }
  261. #endif
  262. for (; s > s0; s--) {
  263. *s = p->ext_frach & 0xf;
  264. p->ext_frach >>= 4;
  265. }
  266. /*
  267. * At this point, we have snarfed all the bits in the
  268. * mantissa, with the possible exception of the highest-order
  269. * (partial) nibble, which is dealt with by the next
  270. * statement. We also tack on the implicit normalization bit.
  271. */
  272. *s = p->ext_frach | (1U << ((LDBL_MANT_DIG - 1) % 4));
  273. /* If ndigits < 0, we are expected to auto-size the precision. */
  274. if (ndigits < 0) {
  275. for (ndigits = sigfigs; s0[ndigits - 1] == 0; ndigits--)
  276. ;
  277. }
  278. if (sigfigs > ndigits && s0[ndigits] != 0)
  279. dorounding(s0, ndigits, p->ext_sign, decpt);
  280. s = s0 + ndigits;
  281. if (rve != NULL)
  282. *rve = s;
  283. *s-- = '\0';
  284. for (; s >= s0; s--)
  285. *s = xdigs[(unsigned int)*s];
  286. return (s0);
  287. }
  288. #else /* (LDBL_MANT_DIG == DBL_MANT_DIG) */
  289. char *
  290. __hldtoa(long double e, const char *xdigs, int ndigits, int *decpt, int *sign,
  291. char **rve)
  292. {
  293. return (__hdtoa((double)e, xdigs, ndigits, decpt, sign, rve));
  294. }
  295. #endif /* (LDBL_MANT_DIG == DBL_MANT_DIG) */