1 /* Compatibility functions for floating point formatting, reentrant versions.
2 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2004, 2006
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
27 #include <sys/param.h>
28 #include <math_ldbl_opt.h>
31 # define FLOAT_TYPE double
33 # define FLOAT_FMT_FLAG
34 # define FLOAT_NAME_EXT
35 # define FLOAT_MIN_10_EXP DBL_MIN_10_EXP
36 # if DBL_MANT_DIG == 53
37 # define NDIGIT_MAX 17
38 # elif DBL_MANT_DIG == 24
40 # elif DBL_MANT_DIG == 56
41 # define NDIGIT_MAX 18
43 /* See IEEE 854 5.6, table 2 for this formula. Unfortunately we need a
44 compile time constant here, so we cannot use it. */
45 # error "NDIGIT_MAX must be precomputed"
46 # define NDIGIT_MAX (lrint (ceil (M_LN2 / M_LN10 * DBL_MANT_DIG + 1.0)))
48 # if DBL_MIN_10_EXP == -37
49 # define FLOAT_MIN_10_NORM 1.0e-37
50 # elif DBL_MIN_10_EXP == -307
51 # define FLOAT_MIN_10_NORM 1.0e-307
52 # elif DBL_MIN_10_EXP == -4931
53 # define FLOAT_MIN_10_NORM 1.0e-4931
55 /* libc can't depend on libm. */
56 # error "FLOAT_MIN_10_NORM must be precomputed"
57 # define FLOAT_MIN_10_NORM exp10 (DBL_MIN_10_EXP)
60 # define LONG_DOUBLE_CVT
63 #define APPEND(a, b) APPEND2 (a, b)
64 #define APPEND2(a, b) a##b
65 #define __APPEND(a, b) __APPEND2 (a, b)
66 #define __APPEND2(a, b) __##a##b
68 #define FLOOR APPEND(floor, FLOAT_NAME_EXT)
69 #define FABS APPEND(fabs, FLOAT_NAME_EXT)
70 #define LOG10 APPEND(log10, FLOAT_NAME_EXT)
71 #define EXP APPEND(exp, FLOAT_NAME_EXT)
75 __APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign, buf, len)
77 int ndigit, *decpt, *sign;
94 *sign = signbit (value) != 0;
100 /* Rounding to the left of the decimal point. */
103 FLOAT_TYPE new_value = value * 0.1;
118 /* Value is Inf or NaN. */
121 n = __snprintf (buf, len, "%.*" FLOAT_FMT_FLAG "f", MIN (ndigit, NDIGIT_MAX),
123 /* Check for a too small buffer. */
124 if (n >= (ssize_t) len)
128 while (i < n && isdigit (buf[i]))
133 /* Value is Inf or NaN. */
140 while (i < n && !isdigit (buf[i]));
142 if (*decpt == 1 && buf[0] == '0' && value != 0.0)
144 /* We must not have leading zeroes. Strip them all out and
145 adjust *DECPT if necessary. */
147 while (i < n && buf[i] == '0')
154 memmove (&buf[MAX (*decpt, 0)], &buf[i], n - i);
155 buf[n - (i - MAX (*decpt, 0))] = '\0';
161 if ((ssize_t) --len > n)
163 while (left-- > 0 && n < (ssize_t) len)
173 __APPEND (FUNC_PREFIX, ecvt_r) (value, ndigit, decpt, sign, buf, len)
175 int ndigit, *decpt, *sign;
181 if (isfinite (value) && value != 0.0)
183 /* Slow code that doesn't require -lm functions. */
190 /* For denormalized numbers the d < 1.0 case below won't work,
191 as f can overflow to +Inf. */
192 if (d < FLOAT_MIN_10_NORM)
194 value /= FLOAT_MIN_10_NORM;
199 exponent += FLOAT_MIN_10_EXP;
219 while (d >= f * 10.0);
224 else if (value == 0.0)
225 /* SUSv2 leaves it unspecified whether *DECPT is 0 or 1 for 0.0.
226 This could be changed to -1 if we want to return 0. */
229 if (ndigit <= 0 && len > 0)
233 *sign = isfinite (value) ? signbit (value) != 0 : 0;
236 if (__APPEND (FUNC_PREFIX, fcvt_r) (value, MIN (ndigit, NDIGIT_MAX) - 1,
237 decpt, sign, buf, len))
244 #if LONG_DOUBLE_COMPAT (libc, GLIBC_2_0)
245 # ifdef LONG_DOUBLE_CVT
246 # define cvt_symbol(symbol) \
247 cvt_symbol_1 (libc, __APPEND (FUNC_PREFIX, symbol), \
248 APPEND (FUNC_PREFIX, symbol), GLIBC_2_4)
249 # define cvt_symbol_1(lib, local, symbol, version) \
250 versioned_symbol (lib, local, symbol, version)
252 # define cvt_symbol(symbol) \
253 cvt_symbol_1 (libc, __APPEND (FUNC_PREFIX, symbol), \
254 APPEND (q, symbol), GLIBC_2_0); \
255 strong_alias (__APPEND (FUNC_PREFIX, symbol), APPEND (FUNC_PREFIX, symbol))
256 # define cvt_symbol_1(lib, local, symbol, version) \
257 compat_symbol (lib, local, symbol, version)
260 # define cvt_symbol(symbol) \
261 strong_alias (__APPEND (FUNC_PREFIX, symbol), APPEND (FUNC_PREFIX, symbol))