2 * Copyright (c) 2004-2008 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$");
39 #include "../stdio/floatio.h"
43 #if (LDBL_MANT_DIG > DBL_MANT_DIG)
45 /* Strings values used by dtoa() */
46 #define INFSTR "Infinity"
49 #ifdef LDBL_IMPLICIT_NBIT
50 #define MANH_SIZE LDBL_MANH_SIZE
52 #define MANH_SIZE (LDBL_MANH_SIZE - 1)
56 typedef uint64_t manh_t;
58 typedef uint32_t manh_t;
61 #if LDBL_MANL_SIZE > 32
62 typedef uint64_t manl_t;
64 typedef uint32_t manl_t;
67 #define LDBL_ADJ (LDBL_MAX_EXP - 2)
68 #define SIGFIGS ((LDBL_MANT_DIG + 3) / 4 + 1)
70 static const float one[] = { 1.0f, -1.0f };
73 * This is the long double version of __hdtoa().
76 __hldtoa(long double e, const char *xdigs, int ndigits, int *decpt, int *sign,
91 switch (fpclassify(e)) {
93 *decpt = u.bits.exp - LDBL_ADJ;
97 return (nrv_alloc("0", rve, 1));
100 oldprec = fpsetprec(FP_PE);
103 *decpt = u.bits.exp - (514 + LDBL_ADJ);
110 return (nrv_alloc(INFSTR, rve, sizeof(INFSTR) - 1));
111 default: /* FP_NAN or unrecognized */
113 return (nrv_alloc(NANSTR, rve, sizeof(NANSTR) - 1));
116 /* FP_NORMAL or FP_SUBNORMAL */
118 if (ndigits == 0) /* dtoa() compatibility */
122 * If ndigits < 0, we are expected to auto-size, so we allocate
123 * enough space for all the digits.
125 bufsize = (ndigits > 0) ? ndigits : SIGFIGS;
126 s0 = rv_alloc(bufsize);
128 /* Round to the desired number of digits. */
129 if (SIGFIGS > ndigits && ndigits > 0) {
130 float redux = one[u.bits.sign];
131 int offset = 4 * ndigits + LDBL_MAX_EXP - 4 - LDBL_MANT_DIG;
133 oldprec = fpsetprec(FP_PE);
138 *decpt += u.bits.exp - offset;
148 for (s = s0 + 1; s < s0 + bufsize; s++) {
149 *s = xdigs[(manh >> (MANH_SIZE - 4)) & 0xf];
150 manh = (manh << 4) | (manl >> (LDBL_MANL_SIZE - 4));
154 /* If ndigits < 0, we are expected to auto-size the precision. */
156 for (ndigits = SIGFIGS; s0[ndigits - 1] == '0'; ndigits--)
167 #else /* (LDBL_MANT_DIG == DBL_MANT_DIG) */
170 __hldtoa(long double e, const char *xdigs, int ndigits, int *decpt, int *sign,
174 return (__hdtoa((double)e, xdigs, ndigits, decpt, sign, rve));
177 #endif /* (LDBL_MANT_DIG == DBL_MANT_DIG) */