Add missing magic to GLIBC_PROVIDES.
[glibc.git] / misc / efgcvt_r.c
blobe9f32d673132279d5c9459e728fbc76ad2c2e2da
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/>. */
20 #include <errno.h>
21 #include <float.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <ctype.h>
25 #include <math.h>
26 #include <stdlib.h>
27 #include <sys/param.h>
28 #include <math_ldbl_opt.h>
30 #ifndef FLOAT_TYPE
31 # define FLOAT_TYPE double
32 # define FUNC_PREFIX
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
39 # define NDIGIT_MAX 9
40 # elif DBL_MANT_DIG == 56
41 # define NDIGIT_MAX 18
42 # else
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)))
47 # endif
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
54 # else
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)
58 # endif
59 #else
60 # define LONG_DOUBLE_CVT
61 #endif
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)
74 int
75 __APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign, buf, len)
76 FLOAT_TYPE value;
77 int ndigit, *decpt, *sign;
78 char *buf;
79 size_t len;
81 ssize_t n;
82 ssize_t i;
83 int left;
85 if (buf == NULL)
87 __set_errno (EINVAL);
88 return -1;
91 left = 0;
92 if (isfinite (value))
94 *sign = signbit (value) != 0;
95 if (*sign)
96 value = -value;
98 if (ndigit < 0)
100 /* Rounding to the left of the decimal point. */
101 while (ndigit < 0)
103 FLOAT_TYPE new_value = value * 0.1;
105 if (new_value < 1.0)
107 ndigit = 0;
108 break;
111 value = new_value;
112 ++left;
113 ++ndigit;
117 else
118 /* Value is Inf or NaN. */
119 *sign = 0;
121 n = __snprintf (buf, len, "%.*" FLOAT_FMT_FLAG "f", MIN (ndigit, NDIGIT_MAX),
122 value);
123 /* Check for a too small buffer. */
124 if (n >= (ssize_t) len)
125 return -1;
127 i = 0;
128 while (i < n && isdigit (buf[i]))
129 ++i;
130 *decpt = i;
132 if (i == 0)
133 /* Value is Inf or NaN. */
134 return 0;
136 if (i < n)
139 ++i;
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. */
146 --*decpt;
147 while (i < n && buf[i] == '0')
149 --*decpt;
150 ++i;
154 memmove (&buf[MAX (*decpt, 0)], &buf[i], n - i);
155 buf[n - (i - MAX (*decpt, 0))] = '\0';
158 if (left)
160 *decpt += left;
161 if ((ssize_t) --len > n)
163 while (left-- > 0 && n < (ssize_t) len)
164 buf[n++] = '0';
165 buf[n] = '\0';
169 return 0;
173 __APPEND (FUNC_PREFIX, ecvt_r) (value, ndigit, decpt, sign, buf, len)
174 FLOAT_TYPE value;
175 int ndigit, *decpt, *sign;
176 char *buf;
177 size_t len;
179 int exponent = 0;
181 if (isfinite (value) && value != 0.0)
183 /* Slow code that doesn't require -lm functions. */
184 FLOAT_TYPE d;
185 FLOAT_TYPE f = 1.0;
186 if (value < 0.0)
187 d = -value;
188 else
189 d = value;
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;
195 if (value < 0.0)
196 d = -value;
197 else
198 d = value;
199 exponent += FLOAT_MIN_10_EXP;
201 if (d < 1.0)
205 f *= 10.0;
206 --exponent;
208 while (d * f < 1.0);
210 value *= f;
212 else if (d >= 10.0)
216 f *= 10;
217 ++exponent;
219 while (d >= f * 10.0);
221 value /= f;
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. */
227 exponent = 0;
229 if (ndigit <= 0 && len > 0)
231 buf[0] = '\0';
232 *decpt = 1;
233 *sign = isfinite (value) ? signbit (value) != 0 : 0;
235 else
236 if (__APPEND (FUNC_PREFIX, fcvt_r) (value, MIN (ndigit, NDIGIT_MAX) - 1,
237 decpt, sign, buf, len))
238 return -1;
240 *decpt += exponent;
241 return 0;
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)
251 # else
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)
258 # endif
259 #else
260 # define cvt_symbol(symbol) \
261 strong_alias (__APPEND (FUNC_PREFIX, symbol), APPEND (FUNC_PREFIX, symbol))
262 #endif
263 cvt_symbol(fcvt_r);
264 cvt_symbol(ecvt_r);