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, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 #include <sys/param.h>
29 #include <math_ldbl_opt.h>
32 # define FLOAT_TYPE double
34 # define FLOAT_FMT_FLAG
35 # define FLOAT_NAME_EXT
36 # define FLOAT_MIN_10_EXP DBL_MIN_10_EXP
37 # if DBL_MANT_DIG == 53
38 # define NDIGIT_MAX 17
39 # elif DBL_MANT_DIG == 24
41 # elif DBL_MANT_DIG == 56
42 # define NDIGIT_MAX 18
44 /* See IEEE 854 5.6, table 2 for this formula. Unfortunately we need a
45 compile time constant here, so we cannot use it. */
46 # error "NDIGIT_MAX must be precomputed"
47 # define NDIGIT_MAX (lrint (ceil (M_LN2 / M_LN10 * DBL_MANT_DIG + 1.0)))
49 # if DBL_MIN_10_EXP == -37
50 # define FLOAT_MIN_10_NORM 1.0e-37
51 # elif DBL_MIN_10_EXP == -307
52 # define FLOAT_MIN_10_NORM 1.0e-307
53 # elif DBL_MIN_10_EXP == -4931
54 # define FLOAT_MIN_10_NORM 1.0e-4931
56 /* libc can't depend on libm. */
57 # error "FLOAT_MIN_10_NORM must be precomputed"
58 # define FLOAT_MIN_10_NORM exp10 (DBL_MIN_10_EXP)
61 # define LONG_DOUBLE_CVT
64 #define APPEND(a, b) APPEND2 (a, b)
65 #define APPEND2(a, b) a##b
66 #define __APPEND(a, b) __APPEND2 (a, b)
67 #define __APPEND2(a, b) __##a##b
69 #define FLOOR APPEND(floor, FLOAT_NAME_EXT)
70 #define FABS APPEND(fabs, FLOAT_NAME_EXT)
71 #define LOG10 APPEND(log10, FLOAT_NAME_EXT)
72 #define EXP APPEND(exp, FLOAT_NAME_EXT)
76 __APPEND (FUNC_PREFIX
, fcvt_r
) (value
, ndigit
, decpt
, sign
, buf
, len
)
78 int ndigit
, *decpt
, *sign
;
95 *sign
= signbit (value
) != 0;
101 /* Rounding to the left of the decimal point. */
104 FLOAT_TYPE new_value
= value
* 0.1;
119 /* Value is Inf or NaN. */
122 n
= __snprintf (buf
, len
, "%.*" FLOAT_FMT_FLAG
"f", MIN (ndigit
, NDIGIT_MAX
),
124 /* Check for a too small buffer. */
125 if (n
>= (ssize_t
) len
)
129 while (i
< n
&& isdigit (buf
[i
]))
134 /* Value is Inf or NaN. */
141 while (i
< n
&& !isdigit (buf
[i
]));
143 if (*decpt
== 1 && buf
[0] == '0' && value
!= 0.0)
145 /* We must not have leading zeroes. Strip them all out and
146 adjust *DECPT if necessary. */
148 while (i
< n
&& buf
[i
] == '0')
155 memmove (&buf
[MAX (*decpt
, 0)], &buf
[i
], n
- i
);
156 buf
[n
- (i
- MAX (*decpt
, 0))] = '\0';
162 if ((ssize_t
) --len
> n
)
164 while (left
-- > 0 && n
< (ssize_t
) len
)
174 __APPEND (FUNC_PREFIX
, ecvt_r
) (value
, ndigit
, decpt
, sign
, buf
, len
)
176 int ndigit
, *decpt
, *sign
;
182 if (isfinite (value
) && value
!= 0.0)
184 /* Slow code that doesn't require -lm functions. */
191 /* For denormalized numbers the d < 1.0 case below won't work,
192 as f can overflow to +Inf. */
193 if (d
< FLOAT_MIN_10_NORM
)
195 value
/= FLOAT_MIN_10_NORM
;
200 exponent
+= FLOAT_MIN_10_EXP
;
220 while (d
>= f
* 10.0);
225 else if (value
== 0.0)
226 /* SUSv2 leaves it unspecified whether *DECPT is 0 or 1 for 0.0.
227 This could be changed to -1 if we want to return 0. */
230 if (ndigit
<= 0 && len
> 0)
234 *sign
= isfinite (value
) ? signbit (value
) != 0 : 0;
237 if (__APPEND (FUNC_PREFIX
, fcvt_r
) (value
, MIN (ndigit
, NDIGIT_MAX
) - 1,
238 decpt
, sign
, buf
, len
))
245 #if LONG_DOUBLE_COMPAT (libc, GLIBC_2_0)
246 # ifdef LONG_DOUBLE_CVT
247 # define cvt_symbol(symbol) \
248 cvt_symbol_1 (libc, __APPEND (FUNC_PREFIX, symbol), \
249 APPEND (FUNC_PREFIX, symbol), GLIBC_2_4)
250 # define cvt_symbol_1(lib, local, symbol, version) \
251 versioned_symbol (lib, local, symbol, version)
253 # define cvt_symbol(symbol) \
254 cvt_symbol_1 (libc, __APPEND (FUNC_PREFIX, symbol), \
255 APPEND (q, symbol), GLIBC_2_0); \
256 strong_alias (__APPEND (FUNC_PREFIX, symbol), APPEND (FUNC_PREFIX, symbol))
257 # define cvt_symbol_1(lib, local, symbol, version) \
258 compat_symbol (lib, local, symbol, version)
261 # define cvt_symbol(symbol) \
262 strong_alias (__APPEND (FUNC_PREFIX, symbol), APPEND (FUNC_PREFIX, symbol))