]>
git.saurik.com Git - apple/libc.git/blob - gdtoa/FreeBSD/_hdtoa.c
2 * Copyright (c) 2004, 2005 David Schultz <das@FreeBSD.ORG>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD: src/lib/libc/gdtoa/_hdtoa.c,v 1.5 2007/05/08 02:59:37 das Exp $");
36 /* Strings values used by dtoa() */
37 #define INFSTR "Infinity"
40 #define DBL_ADJ (DBL_MAX_EXP - 2 + ((DBL_MANT_DIG - 1) % 4))
41 #define LDBL_ADJ (LDBL_MAX_EXP - 2 + ((LDBL_MANT_DIG - 1) % 4))
44 * Round up the given digit string. If the digit string is fff...f,
45 * this procedure sets it to 100...0 and returns 1 to indicate that
46 * the exponent needs to be bumped. Otherwise, 0 is returned.
49 roundup(char *s0
, int ndigits
)
53 for (s
= s0
+ ndigits
- 1; *s
== 0xf; s
--) {
65 * Round the given digit string to ndigits digits according to the
66 * current rounding mode. Note that this could produce a string whose
67 * value is not representable in the corresponding floating-point
68 * type. The exponent pointed to by decpt is adjusted if necessary.
71 dorounding(char *s0
, int ndigits
, int sign
, int *decpt
)
73 int adjust
= 0; /* do we need to adjust the exponent? */
76 case 0: /* toward zero */
77 default: /* implementation-defined */
79 case 1: /* to nearest, halfway rounds to even */
80 if ((s0
[ndigits
] > 8) ||
81 (s0
[ndigits
] == 8 && s0
[ndigits
+ 1] & 1))
82 adjust
= roundup(s0
, ndigits
);
84 case 2: /* toward +inf */
86 adjust
= roundup(s0
, ndigits
);
88 case 3: /* toward -inf */
90 adjust
= roundup(s0
, ndigits
);
99 * This procedure converts a double-precision number in IEEE format
100 * into a string of hexadecimal digits and an exponent of 2. Its
101 * behavior is bug-for-bug compatible with dtoa() in mode 2, with the
102 * following exceptions:
104 * - An ndigits < 0 causes it to use as many digits as necessary to
105 * represent the number exactly.
106 * - The additional xdigs argument should point to either the string
107 * "0123456789ABCDEF" or the string "0123456789abcdef", depending on
108 * which case is desired.
109 * - This routine does not repeat dtoa's mistake of setting decpt
110 * to 9999 in the case of an infinity or NaN. INT_MAX is used
111 * for this purpose instead.
113 * Note that the C99 standard does not specify what the leading digit
114 * should be for non-zero numbers. For instance, 0x1.3p3 is the same
115 * as 0x2.6p2 is the same as 0x4.cp3. This implementation chooses the
116 * first digit so that subsequent digits are aligned on nibble
117 * boundaries (before rounding).
119 * Inputs: d, xdigs, ndigits
120 * Outputs: decpt, sign, rve
123 __hdtoa(double d
, const char *xdigs
, int ndigits
, int *decpt
, int *sign
,
126 static const int sigfigs
= (DBL_MANT_DIG
+ 3) / 4;
134 switch (f
= fpclassify(d
)) {
136 *decpt
= u
.bits
.exp
- DBL_ADJ
;
141 return (nrv_alloc("0", rve
, 1));
144 * For processors that treat subnormals as zero, comparison
145 * with zero will be equal, so we jump to the FP_ZERO case.
147 if(u
.d
== 0.0) goto return_zero
;
149 *decpt
= u
.bits
.exp
- (514 + DBL_ADJ
);
153 return (nrv_alloc(INFSTR
, rve
, sizeof(INFSTR
) - 1));
156 return (nrv_alloc(NANSTR
, rve
, sizeof(NANSTR
) - 1));
158 LIBC_ABORT("fpclassify returned %d", f
);
161 /* FP_NORMAL or FP_SUBNORMAL */
163 if (ndigits
== 0) /* dtoa() compatibility */
167 * For simplicity, we generate all the digits even if the
168 * caller has requested fewer.
170 bufsize
= (sigfigs
> ndigits
) ? sigfigs
: ndigits
;
171 s0
= rv_alloc(bufsize
);
174 * We work from right to left, first adding any requested zero
175 * padding, then the least significant portion of the
176 * mantissa, followed by the most significant. The buffer is
177 * filled with the byte values 0x0 through 0xf, which are
178 * converted to xdigs[0x0] through xdigs[0xf] after the
181 for (s
= s0
+ bufsize
- 1; s
> s0
+ sigfigs
- 1; s
--)
183 for (; s
> s0
+ sigfigs
- (DBL_MANL_SIZE
/ 4) - 1 && s
> s0
; s
--) {
184 *s
= u
.bits
.manl
& 0xf;
187 for (; s
> s0
; s
--) {
188 *s
= u
.bits
.manh
& 0xf;
193 * At this point, we have snarfed all the bits in the
194 * mantissa, with the possible exception of the highest-order
195 * (partial) nibble, which is dealt with by the next
196 * statement. We also tack on the implicit normalization bit.
198 *s
= u
.bits
.manh
| (1U << ((DBL_MANT_DIG
- 1) % 4));
200 /* If ndigits < 0, we are expected to auto-size the precision. */
202 for (ndigits
= sigfigs
; s0
[ndigits
- 1] == 0; ndigits
--)
206 if (sigfigs
> ndigits
&& s0
[ndigits
] != 0)
207 dorounding(s0
, ndigits
, u
.bits
.sign
, decpt
);
214 *s
= xdigs
[(unsigned int)*s
];
219 #if (LDBL_MANT_DIG > DBL_MANT_DIG)
222 * This is the long double version of __hdtoa().
225 __hldtoa(long double e
, const char *xdigs
, int ndigits
, int *decpt
, int *sign
,
228 static const int sigfigs
= (LDBL_MANT_DIG
+ 3) / 4;
236 switch (f
= fpclassify(e
)) {
239 *decpt
= u
.bits
.exp
- LDBL_ADJ
;
243 return (nrv_alloc("0", rve
, 1));
246 *decpt
= u
.bits
.exp
- (514 + LDBL_ADJ
);
250 return (nrv_alloc(INFSTR
, rve
, sizeof(INFSTR
) - 1));
253 return (nrv_alloc(NANSTR
, rve
, sizeof(NANSTR
) - 1));
255 LIBC_ABORT("fpclassify returned %d", f
);
258 /* FP_NORMAL or FP_SUBNORMAL */
260 if (ndigits
== 0) /* dtoa() compatibility */
264 * For simplicity, we generate all the digits even if the
265 * caller has requested fewer.
267 bufsize
= (sigfigs
> ndigits
) ? sigfigs
: ndigits
;
268 s0
= rv_alloc(bufsize
);
271 * We work from right to left, first adding any requested zero
272 * padding, then the least significant portion of the
273 * mantissa, followed by the most significant. The buffer is
274 * filled with the byte values 0x0 through 0xf, which are
275 * converted to xdigs[0x0] through xdigs[0xf] after the
278 for (s
= s0
+ bufsize
- 1; s
> s0
+ sigfigs
- 1; s
--)
280 for (; s
> s0
+ sigfigs
- (LDBL_MANL_SIZE
/ 4) - 1 && s
> s0
; s
--) {
281 *s
= u
.bits
.manl
& 0xf;
284 for (; s
> s0
; s
--) {
285 *s
= u
.bits
.manh
& 0xf;
290 * At this point, we have snarfed all the bits in the
291 * mantissa, with the possible exception of the highest-order
292 * (partial) nibble, which is dealt with by the next
293 * statement. We also tack on the implicit normalization bit.
295 *s
= u
.bits
.manh
| (1U << ((LDBL_MANT_DIG
- 1) % 4));
297 /* If ndigits < 0, we are expected to auto-size the precision. */
299 for (ndigits
= sigfigs
; s0
[ndigits
- 1] == 0; ndigits
--)
303 if (sigfigs
> ndigits
&& s0
[ndigits
] != 0)
304 dorounding(s0
, ndigits
, u
.bits
.sign
, decpt
);
311 *s
= xdigs
[(unsigned int)*s
];
316 #else /* (LDBL_MANT_DIG == DBL_MANT_DIG) */
319 __hldtoa(long double e
, const char *xdigs
, int ndigits
, int *decpt
, int *sign
,
323 return (__hdtoa((double)e
, xdigs
, ndigits
, decpt
, sign
, rve
));
326 #endif /* (LDBL_MANT_DIG == DBL_MANT_DIG) */