Merge branch 'master' of ssh://sources.redhat.com/git/glibc
[glibc.git] / misc / efgcvt_r.c
blobba9566d22c5e68bd32fbc8294d86df283165cb22
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
19 02111-1307 USA. */
21 #include <errno.h>
22 #include <float.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <ctype.h>
26 #include <math.h>
27 #include <stdlib.h>
28 #include <sys/param.h>
29 #include <math_ldbl_opt.h>
31 #ifndef FLOAT_TYPE
32 # define FLOAT_TYPE double
33 # define FUNC_PREFIX
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
40 # define NDIGIT_MAX 9
41 # elif DBL_MANT_DIG == 56
42 # define NDIGIT_MAX 18
43 # else
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)))
48 # endif
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
55 # else
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)
59 # endif
60 #else
61 # define LONG_DOUBLE_CVT
62 #endif
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)
75 int
76 __APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign, buf, len)
77 FLOAT_TYPE value;
78 int ndigit, *decpt, *sign;
79 char *buf;
80 size_t len;
82 ssize_t n;
83 ssize_t i;
84 int left;
86 if (buf == NULL)
88 __set_errno (EINVAL);
89 return -1;
92 left = 0;
93 if (isfinite (value))
95 *sign = signbit (value) != 0;
96 if (*sign)
97 value = -value;
99 if (ndigit < 0)
101 /* Rounding to the left of the decimal point. */
102 while (ndigit < 0)
104 FLOAT_TYPE new_value = value * 0.1;
106 if (new_value < 1.0)
108 ndigit = 0;
109 break;
112 value = new_value;
113 ++left;
114 ++ndigit;
118 else
119 /* Value is Inf or NaN. */
120 *sign = 0;
122 n = __snprintf (buf, len, "%.*" FLOAT_FMT_FLAG "f", MIN (ndigit, NDIGIT_MAX),
123 value);
124 /* Check for a too small buffer. */
125 if (n >= (ssize_t) len)
126 return -1;
128 i = 0;
129 while (i < n && isdigit (buf[i]))
130 ++i;
131 *decpt = i;
133 if (i == 0)
134 /* Value is Inf or NaN. */
135 return 0;
137 if (i < n)
140 ++i;
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. */
147 --*decpt;
148 while (i < n && buf[i] == '0')
150 --*decpt;
151 ++i;
155 memmove (&buf[MAX (*decpt, 0)], &buf[i], n - i);
156 buf[n - (i - MAX (*decpt, 0))] = '\0';
159 if (left)
161 *decpt += left;
162 if ((ssize_t) --len > n)
164 while (left-- > 0 && n < (ssize_t) len)
165 buf[n++] = '0';
166 buf[n] = '\0';
170 return 0;
174 __APPEND (FUNC_PREFIX, ecvt_r) (value, ndigit, decpt, sign, buf, len)
175 FLOAT_TYPE value;
176 int ndigit, *decpt, *sign;
177 char *buf;
178 size_t len;
180 int exponent = 0;
182 if (isfinite (value) && value != 0.0)
184 /* Slow code that doesn't require -lm functions. */
185 FLOAT_TYPE d;
186 FLOAT_TYPE f = 1.0;
187 if (value < 0.0)
188 d = -value;
189 else
190 d = value;
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;
196 if (value < 0.0)
197 d = -value;
198 else
199 d = value;
200 exponent += FLOAT_MIN_10_EXP;
202 if (d < 1.0)
206 f *= 10.0;
207 --exponent;
209 while (d * f < 1.0);
211 value *= f;
213 else if (d >= 10.0)
217 f *= 10;
218 ++exponent;
220 while (d >= f * 10.0);
222 value /= f;
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. */
228 exponent = 0;
230 if (ndigit <= 0 && len > 0)
232 buf[0] = '\0';
233 *decpt = 1;
234 *sign = isfinite (value) ? signbit (value) != 0 : 0;
236 else
237 if (__APPEND (FUNC_PREFIX, fcvt_r) (value, MIN (ndigit, NDIGIT_MAX) - 1,
238 decpt, sign, buf, len))
239 return -1;
241 *decpt += exponent;
242 return 0;
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)
252 # else
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)
259 # endif
260 #else
261 # define cvt_symbol(symbol) \
262 strong_alias (__APPEND (FUNC_PREFIX, symbol), APPEND (FUNC_PREFIX, symbol))
263 #endif
264 cvt_symbol(fcvt_r);
265 cvt_symbol(ecvt_r);