.
[glibc/history.git] / misc / efgcvt_r.c
blob28bf170c810f0c3d4360aee33cbf4fafb50a9b08
1 /* Compatibility functions for floating point formatting, reentrant versions.
2 Copyright (C) 1995,96,97,98,99,2000,01,02 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
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>
29 #ifndef FLOAT_TYPE
30 # define FLOAT_TYPE double
31 # define FUNC_PREFIX
32 # define FLOAT_FMT_FLAG
33 # define FLOAT_NAME_EXT
34 # define FLOAT_MIN_10_EXP DBL_MIN_10_EXP
35 # if DBL_MANT_DIG == 53
36 # define NDIGIT_MAX 17
37 # elif DBL_MANT_DIG == 24
38 # define NDIGIT_MAX 9
39 # elif DBL_MANT_DIG == 56
40 # define NDIGIT_MAX 18
41 # else
42 /* See IEEE 854 5.6, table 2 for this formula. Unfortunately we need a
43 compile time constant here, so we cannot use it. */
44 # error "NDIGIT_MAX must be precomputed"
45 # define NDIGIT_MAX (lrint (ceil (M_LN2 / M_LN10 * DBL_MANT_DIG + 1.0)))
46 # endif
47 # if DBL_MIN_10_EXP == -37
48 # define FLOAT_MIN_10_NORM 1.0e-37
49 # elif DBL_MIN_10_EXP == -307
50 # define FLOAT_MIN_10_NORM 1.0e-307
51 # elif DBL_MIN_10_EXP == -4931
52 # define FLOAT_MIN_10_NORM 1.0e-4931
53 # else
54 /* libc can't depend on libm. */
55 # error "FLOAT_MIN_10_NORM must be precomputed"
56 # define FLOAT_MIN_10_NORM exp10 (DBL_MIN_10_EXP)
57 # endif
58 #endif
60 #define APPEND(a, b) APPEND2 (a, b)
61 #define APPEND2(a, b) a##b
63 #define FLOOR APPEND(floor, FLOAT_NAME_EXT)
64 #define FABS APPEND(fabs, FLOAT_NAME_EXT)
65 #define LOG10 APPEND(log10, FLOAT_NAME_EXT)
66 #define EXP APPEND(exp, FLOAT_NAME_EXT)
69 int
70 APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign, buf, len)
71 FLOAT_TYPE value;
72 int ndigit, *decpt, *sign;
73 char *buf;
74 size_t len;
76 ssize_t n;
77 ssize_t i;
78 int left;
80 if (buf == NULL)
82 __set_errno (EINVAL);
83 return -1;
86 left = 0;
87 if (isfinite (value))
89 *sign = signbit (value) != 0;
90 if (*sign)
91 value = -value;
93 if (ndigit < 0)
95 /* Rounding to the left of the decimal point. */
96 while (ndigit < 0)
98 FLOAT_TYPE new_value = value * 0.1;
100 if (new_value < 1.0)
102 ndigit = 0;
103 break;
106 value = new_value;
107 ++left;
108 ++ndigit;
112 else
113 /* Value is Inf or NaN. */
114 *sign = 0;
116 n = __snprintf (buf, len, "%.*" FLOAT_FMT_FLAG "f", MIN (ndigit, NDIGIT_MAX),
117 value);
118 /* Check for a too small buffer. */
119 if (n >= (ssize_t) len)
120 return -1;
122 i = 0;
123 while (i < n && isdigit (buf[i]))
124 ++i;
125 *decpt = i;
127 if (i == 0)
128 /* Value is Inf or NaN. */
129 return 0;
131 if (i < n)
134 ++i;
135 while (i < n && !isdigit (buf[i]));
137 if (*decpt == 1 && buf[0] == '0' && value != 0.0)
139 /* We must not have leading zeroes. Strip them all out and
140 adjust *DECPT if necessary. */
141 --*decpt;
142 while (i < n && buf[i] == '0')
144 --*decpt;
145 ++i;
149 memmove (&buf[MAX (*decpt, 0)], &buf[i], n - i);
150 buf[n - (i - MAX (*decpt, 0))] = '\0';
153 if (left)
155 *decpt += left;
156 if ((ssize_t) --len > n)
158 while (left-- > 0 && n < (ssize_t) len)
159 buf[n++] = '0';
160 buf[n] = '\0';
164 return 0;
166 libc_hidden_def (APPEND (FUNC_PREFIX, fcvt_r))
169 APPEND (FUNC_PREFIX, ecvt_r) (value, ndigit, decpt, sign, buf, len)
170 FLOAT_TYPE value;
171 int ndigit, *decpt, *sign;
172 char *buf;
173 size_t len;
175 int exponent = 0;
177 if (isfinite (value) && value != 0.0)
179 /* Slow code that doesn't require -lm functions. */
180 FLOAT_TYPE d;
181 FLOAT_TYPE f = 1.0;
182 if (value < 0.0)
183 d = -value;
184 else
185 d = value;
186 /* For denormalized numbers the d < 1.0 case below won't work,
187 as f can overflow to +Inf. */
188 if (d < FLOAT_MIN_10_NORM)
190 value /= FLOAT_MIN_10_NORM;
191 if (value < 0.0)
192 d = -value;
193 else
194 d = value;
195 exponent += FLOAT_MIN_10_EXP;
197 if (d < 1.0)
201 f *= 10.0;
202 --exponent;
204 while (d * f < 1.0);
206 value *= f;
208 else if (d >= 10.0)
212 f *= 10;
213 ++exponent;
215 while (d >= f * 10.0);
217 value /= f;
220 else if (value == 0.0)
221 /* SUSv2 leaves it unspecified whether *DECPT is 0 or 1 for 0.0.
222 This could be changed to -1 if we want to return 0. */
223 exponent = 0;
225 if (ndigit <= 0 && len > 0)
227 buf[0] = '\0';
228 *decpt = 1;
229 *sign = isfinite (value) ? signbit (value) != 0 : 0;
231 else
232 if (APPEND (FUNC_PREFIX, fcvt_r) (value, MIN (ndigit, NDIGIT_MAX) - 1,
233 decpt, sign, buf, len))
234 return -1;
236 *decpt += exponent;
237 return 0;
239 libc_hidden_def (APPEND (FUNC_PREFIX, ecvt_r))