]>
git.saurik.com Git - apple/libc.git/blob - gdtoa/FreeBSD/_hdtoa.c
2 * Copyright (c) 2004 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.2 2004/01/21 04:51:50 grehan Exp $");
38 /* Strings values used by dtoa() */
39 #define INFSTR "Infinity"
42 #define DBL_BIAS (DBL_MAX_EXP - 1)
43 #define LDBL_BIAS (LDBL_MAX_EXP - 1)
45 #ifdef LDBL_IMPLICIT_NBIT
46 #define LDBL_NBIT_ADJ 0
48 #define LDBL_NBIT_ADJ 1
52 * Efficiently compute the log2 of an integer. Uses a combination of
53 * arcane tricks found in fortune and arcane tricks not (yet) in
54 * fortune. This routine behaves similarly to fls(9).
66 n
= (n
& 0x55555555) + ((n
& 0xaaaaaaaa) >> 1);
67 n
= (n
& 0x33333333) + ((n
& 0xcccccccc) >> 2);
68 n
= (n
& 0x0f0f0f0f) + ((n
& 0xf0f0f0f0) >> 4);
69 n
= (n
& 0x00ff00ff) + ((n
& 0xff00ff00) >> 8);
70 n
= (n
& 0x0000ffff) + ((n
& 0xffff0000) >> 16);
74 #if (LDBL_MANH_SIZE > 32 || LDBL_MANL_SIZE > 32)
81 return (log2_32((uint32_t)(n
>> 32)) + 32);
83 return (log2_32((uint32_t)n
));
86 #endif /* (LDBL_MANH_SIZE > 32 || LDBL_MANL_SIZE > 32) */
89 * Round up the given digit string. If the digit string is fff...f,
90 * this procedure sets it to 100...0 and returns 1 to indicate that
91 * the exponent needs to be bumped. Otherwise, 0 is returned.
94 roundup(char *s0
, int ndigits
)
98 for (s
= s0
+ ndigits
- 1; *s
== 0xf; s
--) {
110 * Round the given digit string to ndigits digits according to the
111 * current rounding mode. Note that this could produce a string whose
112 * value is not representable in the corresponding floating-point
113 * type. The exponent pointed to by decpt is adjusted if necessary.
116 dorounding(char *s0
, int ndigits
, int sign
, int *decpt
)
118 int adjust
= 0; /* do we need to adjust the exponent? */
120 switch (FLT_ROUNDS
) {
121 case 0: /* toward zero */
122 default: /* implementation-defined */
124 case 1: /* to nearest, halfway rounds to even */
125 if ((s0
[ndigits
] > 8) ||
126 (s0
[ndigits
] == 8 && s0
[ndigits
- 1] & 1))
127 adjust
= roundup(s0
, ndigits
);
129 case 2: /* toward +inf */
131 adjust
= roundup(s0
, ndigits
);
133 case 3: /* toward -inf */
135 adjust
= roundup(s0
, ndigits
);
144 * This procedure converts a double-precision number in IEEE format
145 * into a string of hexadecimal digits and an exponent of 2. Its
146 * behavior is bug-for-bug compatible with dtoa() in mode 2, with the
147 * following exceptions:
149 * - An ndigits < 0 causes it to use as many digits as necessary to
150 * represent the number exactly.
151 * - The additional xdigs argument should point to either the string
152 * "0123456789ABCDEF" or the string "0123456789abcdef", depending on
153 * which case is desired.
154 * - This routine does not repeat dtoa's mistake of setting decpt
155 * to 9999 in the case of an infinity or NaN. INT_MAX is used
156 * for this purpose instead.
158 * Note that the C99 standard does not specify what the leading digit
159 * should be for non-zero numbers. For instance, 0x1.3p3 is the same
160 * as 0x2.6p2 is the same as 0x4.cp3. This implementation chooses the
161 * first digit so that subsequent digits are aligned on nibble
162 * boundaries (before rounding).
164 * Inputs: d, xdigs, ndigits
165 * Outputs: decpt, sign, rve
168 __hdtoa(double d
, const char *xdigs
, int ndigits
, int *decpt
, int *sign
,
174 int impnbit
; /* implicit normalization bit */
176 int shift
; /* for subnormals, # of shifts required to normalize */
177 int sigfigs
; /* number of significant hex figures in result */
182 switch (fpclassify(d
)) {
184 sigfigs
= (DBL_MANT_DIG
+ 3) / 4;
185 impnbit
= 1 << ((DBL_MANT_DIG
- 1) % 4);
186 *decpt
= u
.bits
.exp
- DBL_BIAS
+ 1 -
187 ((DBL_MANT_DIG
- 1) % 4);
191 return (nrv_alloc("0", rve
, 1));
194 * The position of the highest-order bit tells us by
195 * how much to adjust the exponent (decpt). The
196 * adjustment is raised to the next nibble boundary
197 * since we will later choose the leftmost hexadecimal
198 * digit so that all subsequent digits align on nibble
201 if (u
.bits
.manh
!= 0) {
202 pos
= log2_32(u
.bits
.manh
);
203 shift
= DBL_MANH_SIZE
- pos
;
205 pos
= log2_32(u
.bits
.manl
);
206 shift
= DBL_MANH_SIZE
+ DBL_MANL_SIZE
- pos
;
208 sigfigs
= (3 + DBL_MANT_DIG
- shift
) / 4;
210 *decpt
= DBL_MIN_EXP
- ((shift
+ 3) & ~(4 - 1));
214 return (nrv_alloc(INFSTR
, rve
, sizeof(INFSTR
) - 1));
217 return (nrv_alloc(NANSTR
, rve
, sizeof(NANSTR
) - 1));
222 /* FP_NORMAL or FP_SUBNORMAL */
224 if (ndigits
== 0) /* dtoa() compatibility */
228 * For simplicity, we generate all the digits even if the
229 * caller has requested fewer.
231 bufsize
= (sigfigs
> ndigits
) ? sigfigs
: ndigits
;
232 s0
= rv_alloc(bufsize
);
235 * We work from right to left, first adding any requested zero
236 * padding, then the least significant portion of the
237 * mantissa, followed by the most significant. The buffer is
238 * filled with the byte values 0x0 through 0xf, which are
239 * converted to xdigs[0x0] through xdigs[0xf] after the
242 for (s
= s0
+ bufsize
- 1; s
> s0
+ sigfigs
- 1; s
--)
244 for (; s
> s0
+ sigfigs
- (DBL_MANL_SIZE
/ 4) - 1 && s
> s0
; s
--) {
245 *s
= u
.bits
.manl
& 0xf;
248 for (; s
> s0
; s
--) {
249 *s
= u
.bits
.manh
& 0xf;
254 * At this point, we have snarfed all the bits in the
255 * mantissa, with the possible exception of the highest-order
256 * (partial) nibble, which is dealt with by the next
257 * statement. That nibble is usually in manh, but it could be
258 * in manl instead for small subnormals. We also tack on the
259 * implicit normalization bit if appropriate.
261 *s
= u
.bits
.manh
| u
.bits
.manl
| impnbit
;
263 /* If ndigits < 0, we are expected to auto-size the precision. */
265 for (ndigits
= sigfigs
; s0
[ndigits
- 1] == 0; ndigits
--)
269 if (sigfigs
> ndigits
&& s0
[ndigits
] != 0)
270 dorounding(s0
, ndigits
, u
.bits
.sign
, decpt
);
277 *s
= xdigs
[(unsigned int)*s
];
282 #if (LDBL_MANT_DIG > DBL_MANT_DIG)
285 * This is the long double version of __hdtoa().
287 * On architectures that have an explicit integer bit, unnormals and
288 * pseudo-denormals cause problems in the conversion routine, so they
289 * are ``fixed'' by effectively toggling the integer bit. Although
290 * this is not correct behavior, the hardware will not produce these
291 * formats externally.
294 __hldtoa(long double e
, const char *xdigs
, int ndigits
, int *decpt
, int *sign
,
300 int impnbit
; /* implicit normalization bit */
302 int shift
; /* for subnormals, # of shifts required to normalize */
303 int sigfigs
; /* number of significant hex figures in result */
308 switch (fpclassify(e
)) {
310 sigfigs
= (LDBL_MANT_DIG
+ 3) / 4;
311 impnbit
= 1 << ((LDBL_MANT_DIG
- 1) % 4);
312 *decpt
= u
.bits
.exp
- LDBL_BIAS
+ 1 -
313 ((LDBL_MANT_DIG
- 1) % 4);
317 return (nrv_alloc("0", rve
, 1));
320 * The position of the highest-order bit tells us by
321 * how much to adjust the exponent (decpt). The
322 * adjustment is raised to the next nibble boundary
323 * since we will later choose the leftmost hexadecimal
324 * digit so that all subsequent digits align on nibble
327 #ifdef LDBL_IMPLICIT_NBIT
328 /* Don't trust the normalization bit to be off. */
329 u
.bits
.manh
&= ~(~0ULL << (LDBL_MANH_SIZE
- 1));
331 if (u
.bits
.manh
!= 0) {
332 #if LDBL_MANH_SIZE > 32
333 pos
= log2_64(u
.bits
.manh
);
335 pos
= log2_32(u
.bits
.manh
);
337 shift
= LDBL_MANH_SIZE
- LDBL_NBIT_ADJ
- pos
;
339 #if LDBL_MANL_SIZE > 32
340 pos
= log2_64(u
.bits
.manl
);
342 pos
= log2_32(u
.bits
.manl
);
344 shift
= LDBL_MANH_SIZE
+ LDBL_MANL_SIZE
-
347 sigfigs
= (3 + LDBL_MANT_DIG
- LDBL_NBIT_ADJ
- shift
) / 4;
348 *decpt
= LDBL_MIN_EXP
+ LDBL_NBIT_ADJ
-
349 ((shift
+ 3) & ~(4 - 1));
354 return (nrv_alloc(INFSTR
, rve
, sizeof(INFSTR
) - 1));
357 return (nrv_alloc(NANSTR
, rve
, sizeof(NANSTR
) - 1));
362 /* FP_NORMAL or FP_SUBNORMAL */
364 if (ndigits
== 0) /* dtoa() compatibility */
368 * For simplicity, we generate all the digits even if the
369 * caller has requested fewer.
371 bufsize
= (sigfigs
> ndigits
) ? sigfigs
: ndigits
;
372 s0
= rv_alloc(bufsize
);
375 * We work from right to left, first adding any requested zero
376 * padding, then the least significant portion of the
377 * mantissa, followed by the most significant. The buffer is
378 * filled with the byte values 0x0 through 0xf, which are
379 * converted to xdigs[0x0] through xdigs[0xf] after the
382 for (s
= s0
+ bufsize
- 1; s
> s0
+ sigfigs
- 1; s
--)
384 for (; s
> s0
+ sigfigs
- (LDBL_MANL_SIZE
/ 4) - 1 && s
> s0
; s
--) {
385 *s
= u
.bits
.manl
& 0xf;
388 for (; s
> s0
; s
--) {
389 *s
= u
.bits
.manh
& 0xf;
394 * At this point, we have snarfed all the bits in the
395 * mantissa, with the possible exception of the highest-order
396 * (partial) nibble, which is dealt with by the next
397 * statement. That nibble is usually in manh, but it could be
398 * in manl instead for small subnormals. We also tack on the
399 * implicit normalization bit if appropriate.
401 *s
= u
.bits
.manh
| u
.bits
.manl
| impnbit
;
403 /* If ndigits < 0, we are expected to auto-size the precision. */
405 for (ndigits
= sigfigs
; s0
[ndigits
- 1] == 0; ndigits
--)
409 if (sigfigs
> ndigits
&& s0
[ndigits
] != 0)
410 dorounding(s0
, ndigits
, u
.bits
.sign
, decpt
);
417 *s
= xdigs
[(unsigned int)*s
];
422 #else /* (LDBL_MANT_DIG == DBL_MANT_DIG) */
425 __hldtoa(long double e
, const char *xdigs
, int ndigits
, int *decpt
, int *sign
,
429 return (__hdtoa((double)e
, xdigs
, ndigits
, decpt
, sign
, rve
));
432 #endif /* (LDBL_MANT_DIG == DBL_MANT_DIG) */