(_dl_close): Fix byte count while removing the shared object from the
[glibc.git] / misc / efgcvt_r.c
blobf8acc55242e66584c58bc44ff9b017518afa33a8
1 /* Compatibility functions for floating point formatting, reentrant versions.
2 Copyright (C) 1995, 1996, 1997, 1998 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <errno.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <ctype.h>
24 #include <math.h>
25 #include <stdlib.h>
26 #include <sys/param.h>
28 #ifndef FLOAT_TYPE
29 #define FLOAT_TYPE double
30 #define FUNC_PREFIX
31 #define FLOAT_FMT_FLAG
32 #define FLOAT_NAME_EXT
33 #endif
35 #define APPEND(a, b) APPEND2 (a, b)
36 #define APPEND2(a, b) a##b
38 #define FLOOR APPEND(floor, FLOAT_NAME_EXT)
39 #define FABS APPEND(fabs, FLOAT_NAME_EXT)
40 #define LOG10 APPEND(log10, FLOAT_NAME_EXT)
41 #define EXP APPEND(exp, FLOAT_NAME_EXT)
42 #define ISINF APPEND(isinf, FLOAT_NAME_EXT)
43 #define ISNAN APPEND(isnan, FLOAT_NAME_EXT)
46 int
47 APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign, buf, len)
48 FLOAT_TYPE value;
49 int ndigit, *decpt, *sign;
50 char *buf;
51 size_t len;
53 int n, i;
54 int left;
56 if (buf == NULL)
58 __set_errno (EINVAL);
59 return -1;
62 left = 0;
63 if (!ISINF (value) && !ISNAN (value))
65 /* OK, the following is not entirely correct. -0.0 is not handled
66 correctly but glibc 2.0 does not have a portable function to
67 implement this test. */
68 *sign = value < 0.0;
69 if (*sign)
70 value = -value;
72 if (ndigit < 0)
74 /* Rounding to the left of the decimal point. */
75 while (ndigit < 0)
77 FLOAT_TYPE new_value = value * 0.1;
79 if (new_value < 1.0)
81 ndigit = 0;
82 break;
85 value = new_value;
86 ++left;
87 ++ndigit;
91 else
92 /* Value is Inf or NaN. */
93 *sign = 0;
95 n = snprintf (buf, len, "%.*" FLOAT_FMT_FLAG "f", ndigit, value);
96 if (n < 0)
97 return -1;
99 i = 0;
100 while (i < n && isdigit (buf[i]))
101 ++i;
102 *decpt = i;
104 if (i == 0)
105 /* Value is Inf or NaN. */
106 return 0;
108 if (i < n)
111 ++i;
112 while (i < n && !isdigit (buf[i]));
114 if (*decpt == 1 && buf[0] == '0' && value != 0.0)
116 /* We must not have leading zeroes. Strip them all out and
117 adjust *DECPT if necessary. */
118 --*decpt;
119 while (i < n && buf[i] == '0')
121 --*decpt;
122 ++i;
126 memmove (&buf[MAX (*decpt, 0)], &buf[i], n - i);
127 buf[n - (i - MAX (*decpt, 0))] = '\0';
130 if (left)
132 *decpt += left;
133 if (--len > n)
135 while (left-- > 0 && n < len)
136 buf[n++] = '0';
137 buf[n] = '\0';
141 return 0;
144 #define weak_extern2(name) weak_extern (name)
145 weak_extern2 (FLOOR) weak_extern2 (LOG10) weak_extern2 (FABS)
146 weak_extern2 (EXP)
149 APPEND (FUNC_PREFIX, ecvt_r) (value, ndigit, decpt, sign, buf, len)
150 FLOAT_TYPE value;
151 int ndigit, *decpt, *sign;
152 char *buf;
153 size_t len;
155 int exponent = 0;
157 if (!ISNAN (value) && !ISINF (value) && value != 0.0)
159 FLOAT_TYPE (*log10_function) (FLOAT_TYPE) = &LOG10;
161 if (log10_function)
163 /* Use the reasonable code if -lm is included. */
164 FLOAT_TYPE dexponent;
165 dexponent = FLOOR (LOG10 (FABS (value)));
166 value *= EXP (dexponent * -M_LN10);
167 exponent = (int) dexponent;
169 else
171 /* Slow code that doesn't require -lm functions. */
172 FLOAT_TYPE d;
173 if (value < 0.0)
174 d = -value;
175 else
176 d = value;
177 if (d < 1.0)
181 d *= 10.0;
182 --exponent;
184 while (d < 1.0);
186 else if (d >= 10.0)
190 d *= 0.1;
191 ++exponent;
193 while (d >= 10.0);
195 if (value < 0.0)
196 value = -d;
197 else
198 value = d;
201 else if (value == 0.0)
202 /* SUSv2 leaves it unspecified whether *DECPT is 0 or 1 for 0.0.
203 This could be changed to -1 if we want to return 0. */
204 exponent = 0;
206 if (ndigit <= 0 && len > 0)
208 buf[0] = '\0';
209 *decpt = 1;
210 if (!ISINF (value) && !ISNAN (value))
211 *sign = value < 0.0;
212 else
213 *sign = 0;
215 else
216 if (APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit - 1, decpt, sign,
217 buf, len))
218 return -1;
220 *decpt += exponent;
221 return 0;