hdtoa.c revision 1.4 1 1.4 christos /* $NetBSD: hdtoa.c,v 1.4 2007/02/23 17:45:59 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.4 christos __RCSID("$NetBSD: hdtoa.c,v 1.4 2007/02/23 17:45:59 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.4 christos #ifndef __vax__
40 1.1 christos #include <machine/ieee.h>
41 1.4 christos #endif
42 1.1 christos #include "gdtoaimp.h"
43 1.1 christos
44 1.1 christos /* Strings values used by dtoa() */
45 1.1 christos #define INFSTR "Infinity"
46 1.1 christos #define NANSTR "NaN"
47 1.1 christos
48 1.1 christos #define DBL_ADJ (DBL_MAX_EXP - 2 + ((DBL_MANT_DIG - 1) % 4))
49 1.1 christos #define LDBL_ADJ (LDBL_MAX_EXP - 2 + ((LDBL_MANT_DIG - 1) % 4))
50 1.1 christos
51 1.1 christos /*
52 1.1 christos * Round up the given digit string. If the digit string is fff...f,
53 1.1 christos * this procedure sets it to 100...0 and returns 1 to indicate that
54 1.1 christos * the exponent needs to be bumped. Otherwise, 0 is returned.
55 1.1 christos */
56 1.1 christos static int
57 1.1 christos roundup(char *s0, int ndigits)
58 1.1 christos {
59 1.1 christos char *s;
60 1.1 christos
61 1.1 christos for (s = s0 + ndigits - 1; *s == 0xf; s--) {
62 1.1 christos if (s == s0) {
63 1.1 christos *s = 1;
64 1.1 christos return (1);
65 1.1 christos }
66 1.1 christos *s = 0;
67 1.1 christos }
68 1.1 christos ++*s;
69 1.1 christos return (0);
70 1.1 christos }
71 1.1 christos
72 1.1 christos /*
73 1.1 christos * Round the given digit string to ndigits digits according to the
74 1.1 christos * current rounding mode. Note that this could produce a string whose
75 1.1 christos * value is not representable in the corresponding floating-point
76 1.1 christos * type. The exponent pointed to by decpt is adjusted if necessary.
77 1.1 christos */
78 1.1 christos static void
79 1.1 christos dorounding(char *s0, int ndigits, int sign, int *decpt)
80 1.1 christos {
81 1.1 christos int adjust = 0; /* do we need to adjust the exponent? */
82 1.1 christos
83 1.1 christos switch (FLT_ROUNDS) {
84 1.1 christos case 0: /* toward zero */
85 1.1 christos default: /* implementation-defined */
86 1.1 christos break;
87 1.1 christos case 1: /* to nearest, halfway rounds to even */
88 1.1 christos if ((s0[ndigits] > 8) ||
89 1.1 christos (s0[ndigits] == 8 && s0[ndigits - 1] & 1))
90 1.1 christos adjust = roundup(s0, ndigits);
91 1.1 christos break;
92 1.1 christos case 2: /* toward +inf */
93 1.1 christos if (sign == 0)
94 1.1 christos adjust = roundup(s0, ndigits);
95 1.1 christos break;
96 1.1 christos case 3: /* toward -inf */
97 1.1 christos if (sign != 0)
98 1.1 christos adjust = roundup(s0, ndigits);
99 1.1 christos break;
100 1.1 christos }
101 1.1 christos
102 1.1 christos if (adjust)
103 1.1 christos *decpt += 4;
104 1.1 christos }
105 1.1 christos
106 1.1 christos /*
107 1.1 christos * This procedure converts a double-precision number in IEEE format
108 1.1 christos * into a string of hexadecimal digits and an exponent of 2. Its
109 1.1 christos * behavior is bug-for-bug compatible with dtoa() in mode 2, with the
110 1.1 christos * following exceptions:
111 1.1 christos *
112 1.1 christos * - An ndigits < 0 causes it to use as many digits as necessary to
113 1.1 christos * represent the number exactly.
114 1.1 christos * - The additional xdigs argument should point to either the string
115 1.1 christos * "0123456789ABCDEF" or the string "0123456789abcdef", depending on
116 1.1 christos * which case is desired.
117 1.1 christos * - This routine does not repeat dtoa's mistake of setting decpt
118 1.1 christos * to 9999 in the case of an infinity or NaN. INT_MAX is used
119 1.1 christos * for this purpose instead.
120 1.1 christos *
121 1.1 christos * Note that the C99 standard does not specify what the leading digit
122 1.1 christos * should be for non-zero numbers. For instance, 0x1.3p3 is the same
123 1.1 christos * as 0x2.6p2 is the same as 0x4.cp3. This implementation chooses the
124 1.1 christos * first digit so that subsequent digits are aligned on nibble
125 1.1 christos * boundaries (before rounding).
126 1.1 christos *
127 1.1 christos * Inputs: d, xdigs, ndigits
128 1.1 christos * Outputs: decpt, sign, rve
129 1.1 christos */
130 1.1 christos char *
131 1.1 christos hdtoa(double d, const char *xdigs, int ndigits, int *decpt, int *sign,
132 1.1 christos char **rve)
133 1.1 christos {
134 1.1 christos static const int sigfigs = (DBL_MANT_DIG + 3) / 4;
135 1.1 christos union ieee_double_u u;
136 1.1 christos char *s, *s0;
137 1.3 christos size_t bufsize;
138 1.1 christos
139 1.1 christos u.dblu_d = d;
140 1.1 christos *sign = u.dblu_dbl.dbl_sign;
141 1.1 christos
142 1.1 christos switch (fpclassify(d)) {
143 1.1 christos case FP_NORMAL:
144 1.1 christos *decpt = u.dblu_dbl.dbl_exp - DBL_ADJ;
145 1.1 christos break;
146 1.1 christos case FP_ZERO:
147 1.1 christos *decpt = 1;
148 1.1 christos return (nrv_alloc("0", rve, 1));
149 1.1 christos case FP_SUBNORMAL:
150 1.1 christos u.dblu_d *= 0x1p514;
151 1.1 christos *decpt = u.dblu_dbl.dbl_exp - (514 + DBL_ADJ);
152 1.1 christos break;
153 1.1 christos case FP_INFINITE:
154 1.1 christos *decpt = INT_MAX;
155 1.1 christos return (nrv_alloc(INFSTR, rve, sizeof(INFSTR) - 1));
156 1.1 christos case FP_NAN:
157 1.1 christos *decpt = INT_MAX;
158 1.1 christos return (nrv_alloc(NANSTR, rve, sizeof(NANSTR) - 1));
159 1.1 christos default:
160 1.1 christos abort();
161 1.1 christos }
162 1.1 christos
163 1.1 christos /* FP_NORMAL or FP_SUBNORMAL */
164 1.1 christos
165 1.1 christos if (ndigits == 0) /* dtoa() compatibility */
166 1.1 christos ndigits = 1;
167 1.1 christos
168 1.1 christos /*
169 1.1 christos * For simplicity, we generate all the digits even if the
170 1.1 christos * caller has requested fewer.
171 1.1 christos */
172 1.1 christos bufsize = (sigfigs > ndigits) ? sigfigs : ndigits;
173 1.1 christos s0 = rv_alloc(bufsize);
174 1.1 christos
175 1.1 christos /*
176 1.1 christos * We work from right to left, first adding any requested zero
177 1.1 christos * padding, then the least significant portion of the
178 1.1 christos * mantissa, followed by the most significant. The buffer is
179 1.1 christos * filled with the byte values 0x0 through 0xf, which are
180 1.1 christos * converted to xdigs[0x0] through xdigs[0xf] after the
181 1.1 christos * rounding phase.
182 1.1 christos */
183 1.1 christos for (s = s0 + bufsize - 1; s > s0 + sigfigs - 1; s--)
184 1.1 christos *s = 0;
185 1.1 christos for (; s > s0 + sigfigs - (DBL_FRACLBITS / 4) - 1 && s > s0; s--) {
186 1.1 christos *s = u.dblu_dbl.dbl_fracl & 0xf;
187 1.1 christos u.dblu_dbl.dbl_fracl >>= 4;
188 1.1 christos }
189 1.1 christos for (; s > s0; s--) {
190 1.1 christos *s = u.dblu_dbl.dbl_frach & 0xf;
191 1.1 christos u.dblu_dbl.dbl_frach >>= 4;
192 1.1 christos }
193 1.1 christos
194 1.1 christos /*
195 1.1 christos * At this point, we have snarfed all the bits in the
196 1.1 christos * mantissa, with the possible exception of the highest-order
197 1.1 christos * (partial) nibble, which is dealt with by the next
198 1.1 christos * statement. We also tack on the implicit normalization bit.
199 1.1 christos */
200 1.1 christos *s = u.dblu_dbl.dbl_frach | (1U << ((DBL_MANT_DIG - 1) % 4));
201 1.1 christos
202 1.1 christos /* If ndigits < 0, we are expected to auto-size the precision. */
203 1.1 christos if (ndigits < 0) {
204 1.1 christos for (ndigits = sigfigs; s0[ndigits - 1] == 0; ndigits--)
205 1.2 christos continue;
206 1.1 christos }
207 1.1 christos
208 1.1 christos if (sigfigs > ndigits && s0[ndigits] != 0)
209 1.1 christos dorounding(s0, ndigits, u.dblu_dbl.dbl_sign, decpt);
210 1.1 christos
211 1.1 christos s = s0 + ndigits;
212 1.1 christos if (rve != NULL)
213 1.1 christos *rve = s;
214 1.1 christos *s-- = '\0';
215 1.1 christos for (; s >= s0; s--)
216 1.1 christos *s = xdigs[(unsigned int)*s];
217 1.1 christos
218 1.1 christos return (s0);
219 1.1 christos }
220 1.1 christos
221 1.1 christos #if (LDBL_MANT_DIG > DBL_MANT_DIG)
222 1.1 christos
223 1.1 christos /*
224 1.1 christos * This is the long double version of hdtoa().
225 1.1 christos */
226 1.1 christos char *
227 1.1 christos hldtoa(long double e, const char *xdigs, int ndigits, int *decpt, int *sign,
228 1.1 christos char **rve)
229 1.1 christos {
230 1.1 christos static const int sigfigs = (LDBL_MANT_DIG + 3) / 4;
231 1.1 christos union ieee_ext_u u;
232 1.1 christos char *s, *s0;
233 1.3 christos size_t bufsize;
234 1.1 christos
235 1.1 christos u.extu_ld = e;
236 1.1 christos *sign = u.extu_ext.ext_sign;
237 1.1 christos
238 1.1 christos switch (fpclassify(e)) {
239 1.1 christos case FP_NORMAL:
240 1.1 christos *decpt = u.extu_ext.ext_exp - LDBL_ADJ;
241 1.1 christos break;
242 1.1 christos case FP_ZERO:
243 1.1 christos *decpt = 1;
244 1.1 christos return (nrv_alloc("0", rve, 1));
245 1.1 christos case FP_SUBNORMAL:
246 1.1 christos u.extu_ld *= 0x1p514L;
247 1.1 christos *decpt = u.extu_ext.ext_exp - (514 + LDBL_ADJ);
248 1.1 christos break;
249 1.1 christos case FP_INFINITE:
250 1.1 christos *decpt = INT_MAX;
251 1.1 christos return (nrv_alloc(INFSTR, rve, sizeof(INFSTR) - 1));
252 1.1 christos case FP_NAN:
253 1.1 christos *decpt = INT_MAX;
254 1.1 christos return (nrv_alloc(NANSTR, rve, sizeof(NANSTR) - 1));
255 1.1 christos default:
256 1.1 christos abort();
257 1.1 christos }
258 1.1 christos
259 1.1 christos /* FP_NORMAL or FP_SUBNORMAL */
260 1.1 christos
261 1.1 christos if (ndigits == 0) /* dtoa() compatibility */
262 1.1 christos ndigits = 1;
263 1.1 christos
264 1.1 christos /*
265 1.1 christos * For simplicity, we generate all the digits even if the
266 1.1 christos * caller has requested fewer.
267 1.1 christos */
268 1.1 christos bufsize = (sigfigs > ndigits) ? sigfigs : ndigits;
269 1.1 christos s0 = rv_alloc(bufsize);
270 1.1 christos
271 1.1 christos /*
272 1.1 christos * We work from right to left, first adding any requested zero
273 1.1 christos * padding, then the least significant portion of the
274 1.1 christos * mantissa, followed by the most significant. The buffer is
275 1.1 christos * filled with the byte values 0x0 through 0xf, which are
276 1.1 christos * converted to xdigs[0x0] through xdigs[0xf] after the
277 1.1 christos * rounding phase.
278 1.1 christos */
279 1.1 christos for (s = s0 + bufsize - 1; s > s0 + sigfigs - 1; s--)
280 1.1 christos *s = 0;
281 1.1 christos for (; s > s0 + sigfigs - (EXT_FRACLBITS / 4) - 1 && s > s0; s--) {
282 1.1 christos *s = u.extu_ext.ext_fracl & 0xf;
283 1.1 christos u.extu_ext.ext_fracl >>= 4;
284 1.1 christos }
285 1.2 christos #ifdef EXT_FRACHMBITS
286 1.2 christos for (; s > s0; s--) {
287 1.2 christos *s = u.extu_ext.ext_frachm & 0xf;
288 1.2 christos u.extu_ext.ext_frachm >>= 4;
289 1.2 christos }
290 1.2 christos #endif
291 1.2 christos #ifdef EXT_FRACLMBITS
292 1.2 christos for (; s > s0; s--) {
293 1.2 christos *s = u.extu_ext.ext_fraclm & 0xf;
294 1.2 christos u.extu_ext.ext_fraclm >>= 4;
295 1.2 christos }
296 1.2 christos #endif
297 1.1 christos for (; s > s0; s--) {
298 1.1 christos *s = u.extu_ext.ext_frach & 0xf;
299 1.1 christos u.extu_ext.ext_frach >>= 4;
300 1.1 christos }
301 1.1 christos
302 1.1 christos /*
303 1.1 christos * At this point, we have snarfed all the bits in the
304 1.1 christos * mantissa, with the possible exception of the highest-order
305 1.1 christos * (partial) nibble, which is dealt with by the next
306 1.1 christos * statement. We also tack on the implicit normalization bit.
307 1.1 christos */
308 1.1 christos *s = u.extu_ext.ext_frach | (1U << ((LDBL_MANT_DIG - 1) % 4));
309 1.1 christos
310 1.1 christos /* If ndigits < 0, we are expected to auto-size the precision. */
311 1.1 christos if (ndigits < 0) {
312 1.1 christos for (ndigits = sigfigs; s0[ndigits - 1] == 0; ndigits--)
313 1.1 christos continue;
314 1.1 christos }
315 1.1 christos
316 1.1 christos if (sigfigs > ndigits && s0[ndigits] != 0)
317 1.1 christos dorounding(s0, ndigits, u.extu_ext.ext_sign, decpt);
318 1.1 christos
319 1.1 christos s = s0 + ndigits;
320 1.1 christos if (rve != NULL)
321 1.1 christos *rve = s;
322 1.1 christos *s-- = '\0';
323 1.1 christos for (; s >= s0; s--)
324 1.1 christos *s = xdigs[(unsigned int)*s];
325 1.1 christos
326 1.1 christos return (s0);
327 1.1 christos }
328 1.1 christos
329 1.1 christos #else /* (LDBL_MANT_DIG == DBL_MANT_DIG) */
330 1.1 christos
331 1.1 christos char *
332 1.1 christos hldtoa(long double e, const char *xdigs, int ndigits, int *decpt, int *sign,
333 1.1 christos char **rve)
334 1.1 christos {
335 1.1 christos
336 1.1 christos return (hdtoa((double)e, xdigs, ndigits, decpt, sign, rve));
337 1.1 christos }
338 1.1 christos
339 1.1 christos #endif /* (LDBL_MANT_DIG == DBL_MANT_DIG) */
340