xref: /freebsd/lib/libc/gdtoa/_hdtoa.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2004-2008 David Schultz <das@FreeBSD.ORG>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 
29 #include <float.h>
30 #include <limits.h>
31 #include <math.h>
32 
33 #include "../stdio/floatio.h"
34 #include "fpmath.h"
35 #include "gdtoaimp.h"
36 
37 /* Strings values used by dtoa() */
38 #define	INFSTR	"Infinity"
39 #define	NANSTR	"NaN"
40 
41 #define	DBL_ADJ	(DBL_MAX_EXP - 2)
42 #define	SIGFIGS	((DBL_MANT_DIG + 3) / 4 + 1)
43 
44 static const float one[] = { 1.0f, -1.0f };
45 
46 /*
47  * This procedure converts a double-precision number in IEEE format
48  * into a string of hexadecimal digits and an exponent of 2.  Its
49  * behavior is bug-for-bug compatible with dtoa() in mode 2, with the
50  * following exceptions:
51  *
52  * - An ndigits < 0 causes it to use as many digits as necessary to
53  *   represent the number exactly.
54  * - The additional xdigs argument should point to either the string
55  *   "0123456789ABCDEF" or the string "0123456789abcdef", depending on
56  *   which case is desired.
57  * - This routine does not repeat dtoa's mistake of setting decpt
58  *   to 9999 in the case of an infinity or NaN.  INT_MAX is used
59  *   for this purpose instead.
60  *
61  * Note that the C99 standard does not specify what the leading digit
62  * should be for non-zero numbers.  For instance, 0x1.3p3 is the same
63  * as 0x2.6p2 is the same as 0x4.cp3.  This implementation always makes
64  * the leading digit a 1. This ensures that the exponent printed is the
65  * actual base-2 exponent, i.e., ilogb(d).
66  *
67  * Inputs:	d, xdigs, ndigits
68  * Outputs:	decpt, sign, rve
69  */
70 char *
71 __hdtoa(double d, const char *xdigs, int ndigits, int *decpt, int *sign,
72     char **rve)
73 {
74 	union IEEEd2bits u;
75 	char *s, *s0;
76 	int bufsize;
77 	uint32_t manh, manl;
78 
79 	u.d = d;
80 	*sign = u.bits.sign;
81 
82 	switch (fpclassify(d)) {
83 	case FP_NORMAL:
84 		*decpt = u.bits.exp - DBL_ADJ;
85 		break;
86 	case FP_ZERO:
87 		*decpt = 1;
88 		return (nrv_alloc("0", rve, 1));
89 	case FP_SUBNORMAL:
90 		u.d *= 0x1p514;
91 		*decpt = u.bits.exp - (514 + DBL_ADJ);
92 		break;
93 	case FP_INFINITE:
94 		*decpt = INT_MAX;
95 		return (nrv_alloc(INFSTR, rve, sizeof(INFSTR) - 1));
96 	default:	/* FP_NAN or unrecognized */
97 		*decpt = INT_MAX;
98 		return (nrv_alloc(NANSTR, rve, sizeof(NANSTR) - 1));
99 	}
100 
101 	/* FP_NORMAL or FP_SUBNORMAL */
102 
103 	if (ndigits == 0)		/* dtoa() compatibility */
104 		ndigits = 1;
105 
106 	/*
107 	 * If ndigits < 0, we are expected to auto-size, so we allocate
108 	 * enough space for all the digits.
109 	 */
110 	bufsize = (ndigits > 0) ? ndigits : SIGFIGS;
111 	s0 = rv_alloc(bufsize);
112 
113 	/* Round to the desired number of digits. */
114 	if (SIGFIGS > ndigits && ndigits > 0) {
115 		float redux = one[u.bits.sign];
116 		int offset = 4 * ndigits + DBL_MAX_EXP - 4 - DBL_MANT_DIG;
117 		u.bits.exp = offset;
118 		u.d += redux;
119 		u.d -= redux;
120 		*decpt += u.bits.exp - offset;
121 	}
122 
123 	manh = u.bits.manh;
124 	manl = u.bits.manl;
125 	*s0 = '1';
126 	for (s = s0 + 1; s < s0 + bufsize; s++) {
127 		*s = xdigs[(manh >> (DBL_MANH_SIZE - 4)) & 0xf];
128 		manh = (manh << 4) | (manl >> (DBL_MANL_SIZE - 4));
129 		manl <<= 4;
130 	}
131 
132 	/* If ndigits < 0, we are expected to auto-size the precision. */
133 	if (ndigits < 0) {
134 		for (ndigits = SIGFIGS; s0[ndigits - 1] == '0'; ndigits--)
135 			;
136 	}
137 
138 	s = s0 + ndigits;
139 	*s = '\0';
140 	if (rve != NULL)
141 		*rve = s;
142 	return (s0);
143 }
144