2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2004-2008 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>
30 __FBSDID("$FreeBSD$");
41 #include "../stdio/floatio.h"
45 #if (LDBL_MANT_DIG > DBL_MANT_DIG)
47 /* Strings values used by dtoa() */
48 #define INFSTR "Infinity"
51 #ifdef LDBL_IMPLICIT_NBIT
52 #define MANH_SIZE LDBL_MANH_SIZE
54 #define MANH_SIZE (LDBL_MANH_SIZE - 1)
58 typedef uint64_t manh_t;
60 typedef uint32_t manh_t;
63 #if LDBL_MANL_SIZE > 32
64 typedef uint64_t manl_t;
66 typedef uint32_t manl_t;
69 #define LDBL_ADJ (LDBL_MAX_EXP - 2)
70 #define SIGFIGS ((LDBL_MANT_DIG + 3) / 4 + 1)
72 static const float one[] = { 1.0f, -1.0f };
75 * This is the long double version of __hdtoa().
78 __hldtoa(long double e, const char *xdigs, int ndigits, int *decpt, int *sign,
93 switch (fpclassify(e)) {
95 *decpt = u.bits.exp - LDBL_ADJ;
99 return (nrv_alloc("0", rve, 1));
102 oldprec = fpsetprec(FP_PE);
105 *decpt = u.bits.exp - (514 + LDBL_ADJ);
112 return (nrv_alloc(INFSTR, rve, sizeof(INFSTR) - 1));
113 default: /* FP_NAN or unrecognized */
115 return (nrv_alloc(NANSTR, rve, sizeof(NANSTR) - 1));
118 /* FP_NORMAL or FP_SUBNORMAL */
120 if (ndigits == 0) /* dtoa() compatibility */
124 * If ndigits < 0, we are expected to auto-size, so we allocate
125 * enough space for all the digits.
127 bufsize = (ndigits > 0) ? ndigits : SIGFIGS;
128 s0 = rv_alloc(bufsize);
130 /* Round to the desired number of digits. */
131 if (SIGFIGS > ndigits && ndigits > 0) {
132 float redux = one[u.bits.sign];
133 int offset = 4 * ndigits + LDBL_MAX_EXP - 4 - LDBL_MANT_DIG;
135 oldprec = fpsetprec(FP_PE);
140 *decpt += u.bits.exp - offset;
150 for (s = s0 + 1; s < s0 + bufsize; s++) {
151 *s = xdigs[(manh >> (MANH_SIZE - 4)) & 0xf];
152 manh = (manh << 4) | (manl >> (LDBL_MANL_SIZE - 4));
156 /* If ndigits < 0, we are expected to auto-size the precision. */
158 for (ndigits = SIGFIGS; s0[ndigits - 1] == '0'; ndigits--)
169 #else /* (LDBL_MANT_DIG == DBL_MANT_DIG) */
172 __hldtoa(long double e, const char *xdigs, int ndigits, int *decpt, int *sign,
176 return (__hdtoa((double)e, xdigs, ndigits, decpt, sign, rve));
179 #endif /* (LDBL_MANT_DIG == DBL_MANT_DIG) */