efgcvt_r.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* Compatibility functions for floating point formatting, reentrant versions.
  2. Copyright (C) 1995-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library 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 GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <float.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <ctype.h>
  20. #include <math.h>
  21. #include <stdlib.h>
  22. #include <sys/param.h>
  23. #include <math_ldbl_opt.h>
  24. #ifndef FLOAT_TYPE
  25. # define FLOAT_TYPE double
  26. # define FUNC_PREFIX
  27. # define FLOAT_FMT_FLAG
  28. # define FLOAT_NAME_EXT
  29. # define FLOAT_MIN_10_EXP DBL_MIN_10_EXP
  30. # if DBL_MANT_DIG == 53
  31. # define NDIGIT_MAX 17
  32. # elif DBL_MANT_DIG == 24
  33. # define NDIGIT_MAX 9
  34. # elif DBL_MANT_DIG == 56
  35. # define NDIGIT_MAX 18
  36. # else
  37. /* See IEEE 854 5.6, table 2 for this formula. Unfortunately we need a
  38. compile time constant here, so we cannot use it. */
  39. # error "NDIGIT_MAX must be precomputed"
  40. # define NDIGIT_MAX (lrint (ceil (M_LN2 / M_LN10 * DBL_MANT_DIG + 1.0)))
  41. # endif
  42. # if DBL_MIN_10_EXP == -37
  43. # define FLOAT_MIN_10_NORM 1.0e-37
  44. # elif DBL_MIN_10_EXP == -307
  45. # define FLOAT_MIN_10_NORM 1.0e-307
  46. # elif DBL_MIN_10_EXP == -4931
  47. # define FLOAT_MIN_10_NORM 1.0e-4931
  48. # else
  49. /* libc can't depend on libm. */
  50. # error "FLOAT_MIN_10_NORM must be precomputed"
  51. # define FLOAT_MIN_10_NORM exp10 (DBL_MIN_10_EXP)
  52. # endif
  53. #else
  54. # define LONG_DOUBLE_CVT
  55. #endif
  56. #define APPEND(a, b) APPEND2 (a, b)
  57. #define APPEND2(a, b) a##b
  58. #define __APPEND(a, b) __APPEND2 (a, b)
  59. #define __APPEND2(a, b) __##a##b
  60. #define FLOOR APPEND(floor, FLOAT_NAME_EXT)
  61. #define FABS APPEND(fabs, FLOAT_NAME_EXT)
  62. #define LOG10 APPEND(log10, FLOAT_NAME_EXT)
  63. #define EXP APPEND(exp, FLOAT_NAME_EXT)
  64. int
  65. __APPEND (FUNC_PREFIX, fcvt_r) (FLOAT_TYPE value, int ndigit, int *decpt,
  66. int *sign, char *buf, size_t len)
  67. {
  68. ssize_t n;
  69. ssize_t i;
  70. int left;
  71. if (buf == NULL)
  72. {
  73. __set_errno (EINVAL);
  74. return -1;
  75. }
  76. left = 0;
  77. if (isfinite (value))
  78. {
  79. *sign = signbit (value) != 0;
  80. if (*sign)
  81. value = -value;
  82. if (ndigit < 0)
  83. {
  84. /* Rounding to the left of the decimal point. */
  85. while (ndigit < 0)
  86. {
  87. FLOAT_TYPE new_value = value * 0.1;
  88. if (new_value < 1.0)
  89. {
  90. ndigit = 0;
  91. break;
  92. }
  93. value = new_value;
  94. ++left;
  95. ++ndigit;
  96. }
  97. }
  98. }
  99. else
  100. /* Value is Inf or NaN. */
  101. *sign = 0;
  102. n = __snprintf (buf, len, "%.*" FLOAT_FMT_FLAG "f", MIN (ndigit, NDIGIT_MAX),
  103. value);
  104. /* Check for a too small buffer. */
  105. if (n >= (ssize_t) len)
  106. return -1;
  107. i = 0;
  108. while (i < n && isdigit (buf[i]))
  109. ++i;
  110. *decpt = i;
  111. if (i == 0)
  112. /* Value is Inf or NaN. */
  113. return 0;
  114. if (i < n)
  115. {
  116. do
  117. ++i;
  118. while (i < n && !isdigit (buf[i]));
  119. if (*decpt == 1 && buf[0] == '0' && value != 0.0)
  120. {
  121. /* We must not have leading zeroes. Strip them all out and
  122. adjust *DECPT if necessary. */
  123. --*decpt;
  124. while (i < n && buf[i] == '0')
  125. {
  126. --*decpt;
  127. ++i;
  128. }
  129. }
  130. memmove (&buf[MAX (*decpt, 0)], &buf[i], n - i);
  131. buf[n - (i - MAX (*decpt, 0))] = '\0';
  132. }
  133. if (left)
  134. {
  135. *decpt += left;
  136. if ((ssize_t) --len > n)
  137. {
  138. while (left-- > 0 && n < (ssize_t) len)
  139. buf[n++] = '0';
  140. buf[n] = '\0';
  141. }
  142. }
  143. return 0;
  144. }
  145. int
  146. __APPEND (FUNC_PREFIX, ecvt_r) (FLOAT_TYPE value, int ndigit, int *decpt,
  147. int *sign, char *buf, size_t len)
  148. {
  149. int exponent = 0;
  150. if (isfinite (value) && value != 0.0)
  151. {
  152. /* Slow code that doesn't require -lm functions. */
  153. FLOAT_TYPE d;
  154. FLOAT_TYPE f = 1.0;
  155. if (value < 0.0)
  156. d = -value;
  157. else
  158. d = value;
  159. /* For denormalized numbers the d < 1.0 case below won't work,
  160. as f can overflow to +Inf. */
  161. if (d < FLOAT_MIN_10_NORM)
  162. {
  163. value /= FLOAT_MIN_10_NORM;
  164. if (value < 0.0)
  165. d = -value;
  166. else
  167. d = value;
  168. exponent += FLOAT_MIN_10_EXP;
  169. }
  170. if (d < 1.0)
  171. {
  172. do
  173. {
  174. f *= 10.0;
  175. --exponent;
  176. }
  177. while (d * f < 1.0);
  178. value *= f;
  179. }
  180. else if (d >= 10.0)
  181. {
  182. do
  183. {
  184. f *= 10;
  185. ++exponent;
  186. }
  187. while (d >= f * 10.0);
  188. value /= f;
  189. }
  190. }
  191. else if (value == 0.0)
  192. /* SUSv2 leaves it unspecified whether *DECPT is 0 or 1 for 0.0.
  193. This could be changed to -1 if we want to return 0. */
  194. exponent = 0;
  195. if (ndigit <= 0 && len > 0)
  196. {
  197. buf[0] = '\0';
  198. *decpt = 1;
  199. *sign = isfinite (value) ? signbit (value) != 0 : 0;
  200. }
  201. else
  202. if (__APPEND (FUNC_PREFIX, fcvt_r) (value, MIN (ndigit, NDIGIT_MAX) - 1,
  203. decpt, sign, buf, len))
  204. return -1;
  205. *decpt += exponent;
  206. return 0;
  207. }
  208. #if LONG_DOUBLE_COMPAT (libc, GLIBC_2_0)
  209. # ifdef LONG_DOUBLE_CVT
  210. # define cvt_symbol(symbol) \
  211. cvt_symbol_1 (libc, __APPEND (FUNC_PREFIX, symbol), \
  212. APPEND (FUNC_PREFIX, symbol), GLIBC_2_4)
  213. # define cvt_symbol_1(lib, local, symbol, version) \
  214. libc_hidden_def (local) \
  215. versioned_symbol (lib, local, symbol, version)
  216. # else
  217. # define cvt_symbol(symbol) \
  218. cvt_symbol_1 (libc, __APPEND (FUNC_PREFIX, symbol), \
  219. APPEND (q, symbol), GLIBC_2_0); \
  220. weak_alias (__APPEND (FUNC_PREFIX, symbol), APPEND (FUNC_PREFIX, symbol))
  221. # define cvt_symbol_1(lib, local, symbol, version) \
  222. libc_hidden_def (local) \
  223. compat_symbol (lib, local, symbol, version)
  224. # endif
  225. #else
  226. # define cvt_symbol(symbol) \
  227. cvt_symbol_1 (__APPEND (FUNC_PREFIX, symbol), APPEND (FUNC_PREFIX, symbol))
  228. # define cvt_symbol_1(local, symbol) \
  229. libc_hidden_def (local) \
  230. weak_alias (local, symbol)
  231. #endif
  232. cvt_symbol(fcvt_r);
  233. cvt_symbol(ecvt_r);