1 /* $NetBSD: hdtoa.c,v 1.6 2008/03/21 23:13:48 christos Exp $ */
4 * Copyright (c) 2004, 2005 David Schultz <das@FreeBSD.ORG>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD: src/lib/libc/gdtoa/_hdtoa.c,v 1.4 2007/01/03 04:57:58 das Exp $");
33 __RCSID("$NetBSD: hdtoa.c,v 1.6 2008/03/21 23:13:48 christos Exp $");
40 #include <machine/ieee.h>
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
58 /* Strings values used by dtoa() */
59 #define INFSTR "Infinity"
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))
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.
71 roundup(char *s0
, int ndigits
)
75 for (s
= s0
+ ndigits
- 1; *s
== 0xf; s
--) {
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.
93 dorounding(char *s0
, int ndigits
, int sign
, int *decpt
)
95 int adjust
= 0; /* do we need to adjust the exponent? */
98 case 0: /* toward zero */
99 default: /* implementation-defined */
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
);
106 case 2: /* toward +inf */
108 adjust
= roundup(s0
, ndigits
);
110 case 3: /* toward -inf */
112 adjust
= roundup(s0
, ndigits
);
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:
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.
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).
141 * Inputs: d, xdigs, ndigits
142 * Outputs: decpt, sign, rve
145 hdtoa(double d
, const char *xdigs
, int ndigits
, int *decpt
, int *sign
,
148 static const int sigfigs
= (DBL_MANT_DIG
+ 3) / 4;
149 union ieee_double_u u
;
154 *sign
= u
.dblu_dbl
.dbl_sign
;
156 switch (fpclassify(d
)) {
158 *decpt
= u
.dblu_dbl
.dbl_exp
- DBL_ADJ
;
162 return (nrv_alloc("0", rve
, 1));
165 *decpt
= u
.dblu_dbl
.dbl_exp
- (514 + DBL_ADJ
);
169 return (nrv_alloc(INFSTR
, rve
, sizeof(INFSTR
) - 1));
172 return (nrv_alloc(NANSTR
, rve
, sizeof(NANSTR
) - 1));
177 /* FP_NORMAL or FP_SUBNORMAL */
179 if (ndigits
== 0) /* dtoa() compatibility */
183 * For simplicity, we generate all the digits even if the
184 * caller has requested fewer.
186 bufsize
= (sigfigs
> ndigits
) ? sigfigs
: ndigits
;
187 s0
= rv_alloc(bufsize
);
192 * We work from right to left, first adding any requested zero
193 * padding, then the least significant portion of the
194 * mantissa, followed by the most significant. The buffer is
195 * filled with the byte values 0x0 through 0xf, which are
196 * converted to xdigs[0x0] through xdigs[0xf] after the
199 for (s
= s0
+ bufsize
- 1; s
> s0
+ sigfigs
- 1; s
--)
201 for (; s
> s0
+ sigfigs
- (DBL_FRACLBITS
/ 4) - 1 && s
> s0
; s
--) {
202 *s
= u
.dblu_dbl
.dbl_fracl
& 0xf;
203 u
.dblu_dbl
.dbl_fracl
>>= 4;
206 for (; s
> s0
; s
--) {
207 *s
= u
.dblu_dbl
.dbl_fracm
& 0xf;
208 u
.dblu_dbl
.dbl_fracm
>>= 4;
211 for (; s
> s0
; s
--) {
212 *s
= u
.dblu_dbl
.dbl_frach
& 0xf;
213 u
.dblu_dbl
.dbl_frach
>>= 4;
217 * At this point, we have snarfed all the bits in the
218 * mantissa, with the possible exception of the highest-order
219 * (partial) nibble, which is dealt with by the next
220 * statement. We also tack on the implicit normalization bit.
222 *s
= u
.dblu_dbl
.dbl_frach
| (1U << ((DBL_MANT_DIG
- 1) % 4));
224 /* If ndigits < 0, we are expected to auto-size the precision. */
226 for (ndigits
= sigfigs
; s0
[ndigits
- 1] == 0; ndigits
--)
230 if (sigfigs
> ndigits
&& s0
[ndigits
] != 0)
231 dorounding(s0
, ndigits
, u
.dblu_dbl
.dbl_sign
, decpt
);
238 *s
= xdigs
[(unsigned int)*s
];
243 #if (LDBL_MANT_DIG > DBL_MANT_DIG)
246 * This is the long double version of hdtoa().
249 hldtoa(long double e
, const char *xdigs
, int ndigits
, int *decpt
, int *sign
,
252 static const int sigfigs
= (LDBL_MANT_DIG
+ 3) / 4;
258 *sign
= u
.extu_ext
.ext_sign
;
260 switch (fpclassify(e
)) {
262 *decpt
= u
.extu_ext
.ext_exp
- LDBL_ADJ
;
266 return (nrv_alloc("0", rve
, 1));
268 u
.extu_ld
*= 0x1p
514L;
269 *decpt
= u
.extu_ext
.ext_exp
- (514 + LDBL_ADJ
);
273 return (nrv_alloc(INFSTR
, rve
, sizeof(INFSTR
) - 1));
276 return (nrv_alloc(NANSTR
, rve
, sizeof(NANSTR
) - 1));
281 /* FP_NORMAL or FP_SUBNORMAL */
283 if (ndigits
== 0) /* dtoa() compatibility */
287 * For simplicity, we generate all the digits even if the
288 * caller has requested fewer.
290 bufsize
= (sigfigs
> ndigits
) ? sigfigs
: ndigits
;
291 s0
= rv_alloc(bufsize
);
296 * We work from right to left, first adding any requested zero
297 * padding, then the least significant portion of the
298 * mantissa, followed by the most significant. The buffer is
299 * filled with the byte values 0x0 through 0xf, which are
300 * converted to xdigs[0x0] through xdigs[0xf] after the
303 for (s
= s0
+ bufsize
- 1; s
> s0
+ sigfigs
- 1; s
--)
305 for (; s
> s0
+ sigfigs
- (EXT_FRACLBITS
/ 4) - 1 && s
> s0
; s
--) {
306 *s
= u
.extu_ext
.ext_fracl
& 0xf;
307 u
.extu_ext
.ext_fracl
>>= 4;
309 #ifdef EXT_FRACHMBITS
310 for (; s
> s0
; s
--) {
311 *s
= u
.extu_ext
.ext_frachm
& 0xf;
312 u
.extu_ext
.ext_frachm
>>= 4;
315 #ifdef EXT_FRACLMBITS
316 for (; s
> s0
; s
--) {
317 *s
= u
.extu_ext
.ext_fraclm
& 0xf;
318 u
.extu_ext
.ext_fraclm
>>= 4;
321 for (; s
> s0
; s
--) {
322 *s
= u
.extu_ext
.ext_frach
& 0xf;
323 u
.extu_ext
.ext_frach
>>= 4;
327 * At this point, we have snarfed all the bits in the
328 * mantissa, with the possible exception of the highest-order
329 * (partial) nibble, which is dealt with by the next
330 * statement. We also tack on the implicit normalization bit.
332 *s
= u
.extu_ext
.ext_frach
| (1U << ((LDBL_MANT_DIG
- 1) % 4));
334 /* If ndigits < 0, we are expected to auto-size the precision. */
336 for (ndigits
= sigfigs
; s0
[ndigits
- 1] == 0; ndigits
--)
340 if (sigfigs
> ndigits
&& s0
[ndigits
] != 0)
341 dorounding(s0
, ndigits
, u
.extu_ext
.ext_sign
, decpt
);
348 *s
= xdigs
[(unsigned int)*s
];
353 #else /* (LDBL_MANT_DIG == DBL_MANT_DIG) */
356 hldtoa(long double e
, const char *xdigs
, int ndigits
, int *decpt
, int *sign
,
360 return (hdtoa((double)e
, xdigs
, ndigits
, decpt
, sign
, rve
));
363 #endif /* (LDBL_MANT_DIG == DBL_MANT_DIG) */