]> git.ipfire.org Git - thirdparty/glibc.git/blame - misc/efgcvt_r.c
2006-01-14 Jakub Jelinek <jakub@redhat.com>
[thirdparty/glibc.git] / misc / efgcvt_r.c
CommitLineData
19361cb7 1/* Compatibility functions for floating point formatting, reentrant versions.
a334319f 2 Copyright (C) 1995,96,97,98,99,2000,01,02,04 Free Software Foundation, Inc.
19361cb7
UD
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
19361cb7
UD
9
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
41bdb6e2 13 Lesser General Public License for more details.
19361cb7 14
41bdb6e2
AJ
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. */
60478656
RM
19
20#include <errno.h>
1420df2c 21#include <float.h>
60478656
RM
22#include <stdio.h>
23#include <string.h>
24#include <ctype.h>
25#include <math.h>
1d8dc429 26#include <stdlib.h>
da2d1bc5 27#include <sys/param.h>
60478656 28
2064087b 29#ifndef FLOAT_TYPE
67994d6f
UD
30# define FLOAT_TYPE double
31# define FUNC_PREFIX
32# define FLOAT_FMT_FLAG
33# define FLOAT_NAME_EXT
ec8c29dc 34# define FLOAT_MIN_10_EXP DBL_MIN_10_EXP
b113c12c
UD
35# if DBL_MANT_DIG == 53
36# define NDIGIT_MAX 17
f9a2a636
UD
37# elif DBL_MANT_DIG == 24
38# define NDIGIT_MAX 9
39# elif DBL_MANT_DIG == 56
40# define NDIGIT_MAX 18
b113c12c 41# else
f9a2a636
UD
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"
b113c12c
UD
45# define NDIGIT_MAX (lrint (ceil (M_LN2 / M_LN10 * DBL_MANT_DIG + 1.0)))
46# endif
ec8c29dc
UD
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
2064087b
RM
58#endif
59
60#define APPEND(a, b) APPEND2 (a, b)
61#define APPEND2(a, b) a##b
62
63#define FLOOR APPEND(floor, FLOAT_NAME_EXT)
64#define FABS APPEND(fabs, FLOAT_NAME_EXT)
65#define LOG10 APPEND(log10, FLOAT_NAME_EXT)
1f205a47 66#define EXP APPEND(exp, FLOAT_NAME_EXT)
2064087b
RM
67
68
60478656 69int
a334319f 70APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign, buf, len)
2064087b 71 FLOAT_TYPE value;
60478656
RM
72 int ndigit, *decpt, *sign;
73 char *buf;
c2216480 74 size_t len;
60478656 75{
2a068d20
UD
76 ssize_t n;
77 ssize_t i;
1eb687d0 78 int left;
60478656
RM
79
80 if (buf == NULL)
81 {
c4029823 82 __set_errno (EINVAL);
60478656
RM
83 return -1;
84 }
85
1eb687d0 86 left = 0;
1f205a47
UD
87 if (isfinite (value))
88 {
89 *sign = signbit (value) != 0;
90 if (*sign)
91 value = -value;
da2d1bc5
UD
92
93 if (ndigit < 0)
94 {
95 /* Rounding to the left of the decimal point. */
1eb687d0
UD
96 while (ndigit < 0)
97 {
98 FLOAT_TYPE new_value = value * 0.1;
99
100 if (new_value < 1.0)
101 {
102 ndigit = 0;
103 break;
104 }
da2d1bc5 105
1eb687d0
UD
106 value = new_value;
107 ++left;
108 ++ndigit;
109 }
da2d1bc5 110 }
1f205a47 111 }
07b51ba5
UD
112 else
113 /* Value is Inf or NaN. */
114 *sign = 0;
115
67994d6f
UD
116 n = __snprintf (buf, len, "%.*" FLOAT_FMT_FLAG "f", MIN (ndigit, NDIGIT_MAX),
117 value);
b9d3d9f7 118 /* Check for a too small buffer. */
2a068d20 119 if (n >= (ssize_t) len)
60478656
RM
120 return -1;
121
122 i = 0;
123 while (i < n && isdigit (buf[i]))
124 ++i;
125 *decpt = i;
1f205a47
UD
126
127 if (i == 0)
07b51ba5
UD
128 /* Value is Inf or NaN. */
129 return 0;
1f205a47
UD
130
131 if (i < n)
132 {
133 do
134 ++i;
135 while (i < n && !isdigit (buf[i]));
da2d1bc5 136
b8ce6e3e 137 if (*decpt == 1 && buf[0] == '0' && value != 0.0)
da2d1bc5
UD
138 {
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')
143 {
144 --*decpt;
145 ++i;
146 }
147 }
148
149 memmove (&buf[MAX (*decpt, 0)], &buf[i], n - i);
150 buf[n - (i - MAX (*decpt, 0))] = '\0';
1f205a47 151 }
60478656 152
1eb687d0
UD
153 if (left)
154 {
155 *decpt += left;
6dd67bd5 156 if ((ssize_t) --len > n)
1eb687d0 157 {
6dd67bd5 158 while (left-- > 0 && n < (ssize_t) len)
1eb687d0
UD
159 buf[n++] = '0';
160 buf[n] = '\0';
161 }
162 }
163
60478656
RM
164 return 0;
165}
a334319f 166libc_hidden_def (APPEND (FUNC_PREFIX, fcvt_r))
60478656
RM
167
168int
a334319f 169APPEND (FUNC_PREFIX, ecvt_r) (value, ndigit, decpt, sign, buf, len)
2064087b 170 FLOAT_TYPE value;
60478656
RM
171 int ndigit, *decpt, *sign;
172 char *buf;
c2216480 173 size_t len;
60478656 174{
1f205a47 175 int exponent = 0;
0676b5fd 176
1f205a47 177 if (isfinite (value) && value != 0.0)
4d585333 178 {
ff8ac383
UD
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;
1f205a47 184 else
ff8ac383 185 d = value;
ec8c29dc
UD
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)
189 {
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;
196 }
ff8ac383 197 if (d < 1.0)
1f205a47 198 {
ff8ac383 199 do
1f205a47 200 {
ff8ac383
UD
201 f *= 10.0;
202 --exponent;
1f205a47 203 }
ff8ac383
UD
204 while (d * f < 1.0);
205
206 value *= f;
207 }
208 else if (d >= 10.0)
209 {
210 do
1f205a47 211 {
ff8ac383
UD
212 f *= 10;
213 ++exponent;
1f205a47 214 }
ff8ac383
UD
215 while (d >= f * 10.0);
216
217 value /= f;
1f205a47 218 }
4d585333 219 }
da2d1bc5
UD
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;
224
225 if (ndigit <= 0 && len > 0)
226 {
227 buf[0] = '\0';
228 *decpt = 1;
229 *sign = isfinite (value) ? signbit (value) != 0 : 0;
230 }
231 else
a334319f
UD
232 if (APPEND (FUNC_PREFIX, fcvt_r) (value, MIN (ndigit, NDIGIT_MAX) - 1,
233 decpt, sign, buf, len))
da2d1bc5 234 return -1;
4d585333 235
1f205a47
UD
236 *decpt += exponent;
237 return 0;
60478656 238}
a334319f 239libc_hidden_def (APPEND (FUNC_PREFIX, ecvt_r))