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