hdtoa.c revision 1.5 1 /* $NetBSD: hdtoa.c,v 1.5 2007/02/26 01:29:25 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2004, 2005 David Schultz <das (at) FreeBSD.ORG>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 #if 0
31 __FBSDID("$FreeBSD: src/lib/libc/gdtoa/_hdtoa.c,v 1.4 2007/01/03 04:57:58 das Exp $");
32 #else
33 __RCSID("$NetBSD: hdtoa.c,v 1.5 2007/02/26 01:29:25 christos Exp $");
34 #endif
35
36 #include <float.h>
37 #include <limits.h>
38 #include <math.h>
39 #ifndef __vax__
40 #include <machine/ieee.h>
41 #else
42 #include <machine/vaxfp.h>
43 #define ieee_double_u vax_dfloating_u
44 #define dblu_d dfltu_d
45 #define dblu_dbl dfltu_dflt
46 #define dbl_sign dflt_sign
47 #define dbl_exp dflt_exp
48 #define dbl_frach dflt_frach
49 #define dbl_fracm dflt_fracm
50 #define dbl_fracl dflt_fracl
51 #define DBL_FRACHBITS DFLT_FRACHBITS
52 #define DBL_FRACMBITS DFLT_FRACMBITS
53 #define DBL_FRACLBITS DFLT_FRACLBITS
54 #define DBL_EXPBITS DFLT_EXPBITS
55 #endif
56 #include "gdtoaimp.h"
57
58 /* Strings values used by dtoa() */
59 #define INFSTR "Infinity"
60 #define NANSTR "NaN"
61
62 #define DBL_ADJ (DBL_MAX_EXP - 2 + ((DBL_MANT_DIG - 1) % 4))
63 #define LDBL_ADJ (LDBL_MAX_EXP - 2 + ((LDBL_MANT_DIG - 1) % 4))
64
65 /*
66 * Round up the given digit string. If the digit string is fff...f,
67 * this procedure sets it to 100...0 and returns 1 to indicate that
68 * the exponent needs to be bumped. Otherwise, 0 is returned.
69 */
70 static int
71 roundup(char *s0, int ndigits)
72 {
73 char *s;
74
75 for (s = s0 + ndigits - 1; *s == 0xf; s--) {
76 if (s == s0) {
77 *s = 1;
78 return (1);
79 }
80 *s = 0;
81 }
82 ++*s;
83 return (0);
84 }
85
86 /*
87 * Round the given digit string to ndigits digits according to the
88 * current rounding mode. Note that this could produce a string whose
89 * value is not representable in the corresponding floating-point
90 * type. The exponent pointed to by decpt is adjusted if necessary.
91 */
92 static void
93 dorounding(char *s0, int ndigits, int sign, int *decpt)
94 {
95 int adjust = 0; /* do we need to adjust the exponent? */
96
97 switch (FLT_ROUNDS) {
98 case 0: /* toward zero */
99 default: /* implementation-defined */
100 break;
101 case 1: /* to nearest, halfway rounds to even */
102 if ((s0[ndigits] > 8) ||
103 (s0[ndigits] == 8 && s0[ndigits - 1] & 1))
104 adjust = roundup(s0, ndigits);
105 break;
106 case 2: /* toward +inf */
107 if (sign == 0)
108 adjust = roundup(s0, ndigits);
109 break;
110 case 3: /* toward -inf */
111 if (sign != 0)
112 adjust = roundup(s0, ndigits);
113 break;
114 }
115
116 if (adjust)
117 *decpt += 4;
118 }
119
120 /*
121 * This procedure converts a double-precision number in IEEE format
122 * into a string of hexadecimal digits and an exponent of 2. Its
123 * behavior is bug-for-bug compatible with dtoa() in mode 2, with the
124 * following exceptions:
125 *
126 * - An ndigits < 0 causes it to use as many digits as necessary to
127 * represent the number exactly.
128 * - The additional xdigs argument should point to either the string
129 * "0123456789ABCDEF" or the string "0123456789abcdef", depending on
130 * which case is desired.
131 * - This routine does not repeat dtoa's mistake of setting decpt
132 * to 9999 in the case of an infinity or NaN. INT_MAX is used
133 * for this purpose instead.
134 *
135 * Note that the C99 standard does not specify what the leading digit
136 * should be for non-zero numbers. For instance, 0x1.3p3 is the same
137 * as 0x2.6p2 is the same as 0x4.cp3. This implementation chooses the
138 * first digit so that subsequent digits are aligned on nibble
139 * boundaries (before rounding).
140 *
141 * Inputs: d, xdigs, ndigits
142 * Outputs: decpt, sign, rve
143 */
144 char *
145 hdtoa(double d, const char *xdigs, int ndigits, int *decpt, int *sign,
146 char **rve)
147 {
148 static const int sigfigs = (DBL_MANT_DIG + 3) / 4;
149 union ieee_double_u u;
150 char *s, *s0;
151 size_t bufsize;
152
153 u.dblu_d = d;
154 *sign = u.dblu_dbl.dbl_sign;
155
156 switch (fpclassify(d)) {
157 case FP_NORMAL:
158 *decpt = u.dblu_dbl.dbl_exp - DBL_ADJ;
159 break;
160 case FP_ZERO:
161 *decpt = 1;
162 return (nrv_alloc("0", rve, 1));
163 case FP_SUBNORMAL:
164 u.dblu_d *= 0x1p514;
165 *decpt = u.dblu_dbl.dbl_exp - (514 + DBL_ADJ);
166 break;
167 case FP_INFINITE:
168 *decpt = INT_MAX;
169 return (nrv_alloc(INFSTR, rve, sizeof(INFSTR) - 1));
170 case FP_NAN:
171 *decpt = INT_MAX;
172 return (nrv_alloc(NANSTR, rve, sizeof(NANSTR) - 1));
173 default:
174 abort();
175 }
176
177 /* FP_NORMAL or FP_SUBNORMAL */
178
179 if (ndigits == 0) /* dtoa() compatibility */
180 ndigits = 1;
181
182 /*
183 * For simplicity, we generate all the digits even if the
184 * caller has requested fewer.
185 */
186 bufsize = (sigfigs > ndigits) ? sigfigs : ndigits;
187 s0 = rv_alloc(bufsize);
188
189 /*
190 * We work from right to left, first adding any requested zero
191 * padding, then the least significant portion of the
192 * mantissa, followed by the most significant. The buffer is
193 * filled with the byte values 0x0 through 0xf, which are
194 * converted to xdigs[0x0] through xdigs[0xf] after the
195 * rounding phase.
196 */
197 for (s = s0 + bufsize - 1; s > s0 + sigfigs - 1; s--)
198 *s = 0;
199 for (; s > s0 + sigfigs - (DBL_FRACLBITS / 4) - 1 && s > s0; s--) {
200 *s = u.dblu_dbl.dbl_fracl & 0xf;
201 u.dblu_dbl.dbl_fracl >>= 4;
202 }
203 #ifdef DBL_FRACMBITS
204 for (; s > s0; s--) {
205 *s = u.dblu_dbl.dbl_fracm & 0xf;
206 u.dblu_dbl.dbl_fracm >>= 4;
207 }
208 #endif
209 for (; s > s0; s--) {
210 *s = u.dblu_dbl.dbl_frach & 0xf;
211 u.dblu_dbl.dbl_frach >>= 4;
212 }
213
214 /*
215 * At this point, we have snarfed all the bits in the
216 * mantissa, with the possible exception of the highest-order
217 * (partial) nibble, which is dealt with by the next
218 * statement. We also tack on the implicit normalization bit.
219 */
220 *s = u.dblu_dbl.dbl_frach | (1U << ((DBL_MANT_DIG - 1) % 4));
221
222 /* If ndigits < 0, we are expected to auto-size the precision. */
223 if (ndigits < 0) {
224 for (ndigits = sigfigs; s0[ndigits - 1] == 0; ndigits--)
225 continue;
226 }
227
228 if (sigfigs > ndigits && s0[ndigits] != 0)
229 dorounding(s0, ndigits, u.dblu_dbl.dbl_sign, decpt);
230
231 s = s0 + ndigits;
232 if (rve != NULL)
233 *rve = s;
234 *s-- = '\0';
235 for (; s >= s0; s--)
236 *s = xdigs[(unsigned int)*s];
237
238 return (s0);
239 }
240
241 #if (LDBL_MANT_DIG > DBL_MANT_DIG)
242
243 /*
244 * This is the long double version of hdtoa().
245 */
246 char *
247 hldtoa(long double e, const char *xdigs, int ndigits, int *decpt, int *sign,
248 char **rve)
249 {
250 static const int sigfigs = (LDBL_MANT_DIG + 3) / 4;
251 union ieee_ext_u u;
252 char *s, *s0;
253 size_t bufsize;
254
255 u.extu_ld = e;
256 *sign = u.extu_ext.ext_sign;
257
258 switch (fpclassify(e)) {
259 case FP_NORMAL:
260 *decpt = u.extu_ext.ext_exp - LDBL_ADJ;
261 break;
262 case FP_ZERO:
263 *decpt = 1;
264 return (nrv_alloc("0", rve, 1));
265 case FP_SUBNORMAL:
266 u.extu_ld *= 0x1p514L;
267 *decpt = u.extu_ext.ext_exp - (514 + LDBL_ADJ);
268 break;
269 case FP_INFINITE:
270 *decpt = INT_MAX;
271 return (nrv_alloc(INFSTR, rve, sizeof(INFSTR) - 1));
272 case FP_NAN:
273 *decpt = INT_MAX;
274 return (nrv_alloc(NANSTR, rve, sizeof(NANSTR) - 1));
275 default:
276 abort();
277 }
278
279 /* FP_NORMAL or FP_SUBNORMAL */
280
281 if (ndigits == 0) /* dtoa() compatibility */
282 ndigits = 1;
283
284 /*
285 * For simplicity, we generate all the digits even if the
286 * caller has requested fewer.
287 */
288 bufsize = (sigfigs > ndigits) ? sigfigs : ndigits;
289 s0 = rv_alloc(bufsize);
290
291 /*
292 * We work from right to left, first adding any requested zero
293 * padding, then the least significant portion of the
294 * mantissa, followed by the most significant. The buffer is
295 * filled with the byte values 0x0 through 0xf, which are
296 * converted to xdigs[0x0] through xdigs[0xf] after the
297 * rounding phase.
298 */
299 for (s = s0 + bufsize - 1; s > s0 + sigfigs - 1; s--)
300 *s = 0;
301 for (; s > s0 + sigfigs - (EXT_FRACLBITS / 4) - 1 && s > s0; s--) {
302 *s = u.extu_ext.ext_fracl & 0xf;
303 u.extu_ext.ext_fracl >>= 4;
304 }
305 #ifdef EXT_FRACHMBITS
306 for (; s > s0; s--) {
307 *s = u.extu_ext.ext_frachm & 0xf;
308 u.extu_ext.ext_frachm >>= 4;
309 }
310 #endif
311 #ifdef EXT_FRACLMBITS
312 for (; s > s0; s--) {
313 *s = u.extu_ext.ext_fraclm & 0xf;
314 u.extu_ext.ext_fraclm >>= 4;
315 }
316 #endif
317 for (; s > s0; s--) {
318 *s = u.extu_ext.ext_frach & 0xf;
319 u.extu_ext.ext_frach >>= 4;
320 }
321
322 /*
323 * At this point, we have snarfed all the bits in the
324 * mantissa, with the possible exception of the highest-order
325 * (partial) nibble, which is dealt with by the next
326 * statement. We also tack on the implicit normalization bit.
327 */
328 *s = u.extu_ext.ext_frach | (1U << ((LDBL_MANT_DIG - 1) % 4));
329
330 /* If ndigits < 0, we are expected to auto-size the precision. */
331 if (ndigits < 0) {
332 for (ndigits = sigfigs; s0[ndigits - 1] == 0; ndigits--)
333 continue;
334 }
335
336 if (sigfigs > ndigits && s0[ndigits] != 0)
337 dorounding(s0, ndigits, u.extu_ext.ext_sign, decpt);
338
339 s = s0 + ndigits;
340 if (rve != NULL)
341 *rve = s;
342 *s-- = '\0';
343 for (; s >= s0; s--)
344 *s = xdigs[(unsigned int)*s];
345
346 return (s0);
347 }
348
349 #else /* (LDBL_MANT_DIG == DBL_MANT_DIG) */
350
351 char *
352 hldtoa(long double e, const char *xdigs, int ndigits, int *decpt, int *sign,
353 char **rve)
354 {
355
356 return (hdtoa((double)e, xdigs, ndigits, decpt, sign, rve));
357 }
358
359 #endif /* (LDBL_MANT_DIG == DBL_MANT_DIG) */
360