* sysdeps/m68k/bits/byteswap.h (__bswap_32): Add cast to avoid
[glibc/pb-stable.git] / misc / efgcvt_r.c
blob1408b6df2a258f7c1cb83eb628e649c33511deaf
1 /* Compatibility functions for floating point formatting, reentrant versions.
2 Copyright (C) 1995,1996,1997,1998,1999,2000 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 # if DBL_MANT_DIG == 53
34 # define NDIGIT_MAX 17
35 # elif DBL_MANT_DIG == 24
36 # define NDIGIT_MAX 9
37 # elif DBL_MANT_DIG == 56
38 # define NDIGIT_MAX 18
39 # else
40 /* See IEEE 854 5.6, table 2 for this formula. Unfortunately we need a
41 compile time constant here, so we cannot use it. */
42 # error "NDIGIT_MAX must be precomputed"
43 # define NDIGIT_MAX (lrint (ceil (M_LN2 / M_LN10 * DBL_MANT_DIG + 1.0)))
44 # endif
45 #endif
47 #define APPEND(a, b) APPEND2 (a, b)
48 #define APPEND2(a, b) a##b
50 #define FLOOR APPEND(floor, FLOAT_NAME_EXT)
51 #define FABS APPEND(fabs, FLOAT_NAME_EXT)
52 #define LOG10 APPEND(log10, FLOAT_NAME_EXT)
53 #define EXP APPEND(exp, FLOAT_NAME_EXT)
56 int
57 APPEND (FUNC_PREFIX, fcvt_r) (value, ndigit, decpt, sign, buf, len)
58 FLOAT_TYPE value;
59 int ndigit, *decpt, *sign;
60 char *buf;
61 size_t len;
63 ssize_t n;
64 ssize_t i;
65 int left;
67 if (buf == NULL)
69 __set_errno (EINVAL);
70 return -1;
73 left = 0;
74 if (isfinite (value))
76 *sign = signbit (value) != 0;
77 if (*sign)
78 value = -value;
80 if (ndigit < 0)
82 /* Rounding to the left of the decimal point. */
83 while (ndigit < 0)
85 FLOAT_TYPE new_value = value * 0.1;
87 if (new_value < 1.0)
89 ndigit = 0;
90 break;
93 value = new_value;
94 ++left;
95 ++ndigit;
99 else
100 /* Value is Inf or NaN. */
101 *sign = 0;
103 n = __snprintf (buf, len, "%.*" FLOAT_FMT_FLAG "f", MIN (ndigit, NDIGIT_MAX),
104 value);
105 /* Check for a too small buffer. */
106 if (n >= (ssize_t) len)
107 return -1;
109 i = 0;
110 while (i < n && isdigit (buf[i]))
111 ++i;
112 *decpt = i;
114 if (i == 0)
115 /* Value is Inf or NaN. */
116 return 0;
118 if (i < n)
121 ++i;
122 while (i < n && !isdigit (buf[i]));
124 if (*decpt == 1 && buf[0] == '0' && value != 0.0)
126 /* We must not have leading zeroes. Strip them all out and
127 adjust *DECPT if necessary. */
128 --*decpt;
129 while (i < n && buf[i] == '0')
131 --*decpt;
132 ++i;
136 memmove (&buf[MAX (*decpt, 0)], &buf[i], n - i);
137 buf[n - (i - MAX (*decpt, 0))] = '\0';
140 if (left)
142 *decpt += left;
143 if (--len > n)
145 while (left-- > 0 && n < len)
146 buf[n++] = '0';
147 buf[n] = '\0';
151 return 0;
155 APPEND (FUNC_PREFIX, ecvt_r) (value, ndigit, decpt, sign, buf, len)
156 FLOAT_TYPE value;
157 int ndigit, *decpt, *sign;
158 char *buf;
159 size_t len;
161 int exponent = 0;
163 if (isfinite (value) && value != 0.0)
165 /* Slow code that doesn't require -lm functions. */
166 FLOAT_TYPE d;
167 FLOAT_TYPE f = 1.0;
168 if (value < 0.0)
169 d = -value;
170 else
171 d = value;
172 if (d < 1.0)
176 f *= 10.0;
177 --exponent;
179 while (d * f < 1.0);
181 value *= f;
183 else if (d >= 10.0)
187 f *= 10;
188 ++exponent;
190 while (d >= f * 10.0);
192 value /= f;
195 else if (value == 0.0)
196 /* SUSv2 leaves it unspecified whether *DECPT is 0 or 1 for 0.0.
197 This could be changed to -1 if we want to return 0. */
198 exponent = 0;
200 if (ndigit <= 0 && len > 0)
202 buf[0] = '\0';
203 *decpt = 1;
204 *sign = isfinite (value) ? signbit (value) != 0 : 0;
206 else
207 if (APPEND (FUNC_PREFIX, fcvt_r) (value, MIN (ndigit, NDIGIT_MAX) - 1,
208 decpt, sign, buf, len))
209 return -1;
211 *decpt += exponent;
212 return 0;