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