1 /* $OpenBSD: hdtoa.c,v 1.5 2020/05/31 12:27:19 mortimer Exp $ */
2 /*-
3 * Copyright (c) 2004, 2005 David Schultz <das@FreeBSD.ORG>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/types.h>
29 #include <machine/ieee.h>
30 #include <float.h>
31 #include <limits.h>
32 #include <math.h>
33
34 #include "gdtoaimp.h"
35
36 /* Strings values used by dtoa() */
37 #define INFSTR "Infinity"
38 #define NANSTR "NaN"
39
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))
42
43 /*
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.
47 */
48 static int
roundup(char * s0,int ndigits)49 roundup(char *s0, int ndigits)
50 {
51 char *s;
52
53 for (s = s0 + ndigits - 1; *s == 0xf; s--) {
54 if (s == s0) {
55 *s = 1;
56 return (1);
57 }
58 *s = 0;
59 }
60 ++*s;
61 return (0);
62 }
63
64 /*
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.
69 */
70 static void
dorounding(char * s0,int ndigits,int sign,int * decpt)71 dorounding(char *s0, int ndigits, int sign, int *decpt)
72 {
73 int adjust = 0; /* do we need to adjust the exponent? */
74
75 switch (FLT_ROUNDS) {
76 case 0: /* toward zero */
77 default: /* implementation-defined */
78 break;
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);
83 break;
84 case 2: /* toward +inf */
85 if (sign == 0)
86 adjust = roundup(s0, ndigits);
87 break;
88 case 3: /* toward -inf */
89 if (sign != 0)
90 adjust = roundup(s0, ndigits);
91 break;
92 }
93
94 if (adjust)
95 *decpt += 4;
96 }
97
98 /*
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:
103 *
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.
112 *
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.cp1. This implementation chooses the
116 * first digit so that subsequent digits are aligned on nibble
117 * boundaries (before rounding).
118 *
119 * Inputs: d, xdigs, ndigits
120 * Outputs: decpt, sign, rve
121 */
122 char *
__hdtoa(double d,const char * xdigs,int ndigits,int * decpt,int * sign,char ** rve)123 __hdtoa(double d, const char *xdigs, int ndigits, int *decpt, int *sign,
124 char **rve)
125 {
126 static const int sigfigs = (DBL_MANT_DIG + 3) / 4;
127 struct ieee_double *p = (struct ieee_double *)&d;
128 char *s, *s0;
129 int bufsize;
130
131 *sign = p->dbl_sign;
132
133 switch (fpclassify(d)) {
134 case FP_NORMAL:
135 *decpt = p->dbl_exp - DBL_ADJ;
136 break;
137 case FP_ZERO:
138 *decpt = 1;
139 return (nrv_alloc("0", rve, 1));
140 case FP_SUBNORMAL:
141 d *= 0x1p514;
142 *decpt = p->dbl_exp - (514 + DBL_ADJ);
143 break;
144 case FP_INFINITE:
145 *decpt = INT_MAX;
146 return (nrv_alloc(INFSTR, rve, sizeof(INFSTR) - 1));
147 case FP_NAN:
148 *decpt = INT_MAX;
149 return (nrv_alloc(NANSTR, rve, sizeof(NANSTR) - 1));
150 default:
151 abort();
152 }
153
154 /* FP_NORMAL or FP_SUBNORMAL */
155
156 if (ndigits == 0) /* dtoa() compatibility */
157 ndigits = 1;
158
159 /*
160 * For simplicity, we generate all the digits even if the
161 * caller has requested fewer.
162 */
163 bufsize = (sigfigs > ndigits) ? sigfigs : ndigits;
164 s0 = rv_alloc(bufsize);
165 if (s0 == NULL)
166 return (NULL);
167
168 /*
169 * We work from right to left, first adding any requested zero
170 * padding, then the least significant portion of the
171 * mantissa, followed by the most significant. The buffer is
172 * filled with the byte values 0x0 through 0xf, which are
173 * converted to xdigs[0x0] through xdigs[0xf] after the
174 * rounding phase.
175 */
176 for (s = s0 + bufsize - 1; s > s0 + sigfigs - 1; s--)
177 *s = 0;
178 for (; s > s0 + sigfigs - (DBL_FRACLBITS / 4) - 1 && s > s0; s--) {
179 *s = p->dbl_fracl & 0xf;
180 p->dbl_fracl >>= 4;
181 }
182 for (; s > s0; s--) {
183 *s = p->dbl_frach & 0xf;
184 p->dbl_frach >>= 4;
185 }
186
187 /*
188 * At this point, we have snarfed all the bits in the
189 * mantissa, with the possible exception of the highest-order
190 * (partial) nibble, which is dealt with by the next
191 * statement. We also tack on the implicit normalization bit.
192 */
193 *s = p->dbl_frach | (1U << ((DBL_MANT_DIG - 1) % 4));
194
195 /* If ndigits < 0, we are expected to auto-size the precision. */
196 if (ndigits < 0) {
197 for (ndigits = sigfigs; s0[ndigits - 1] == 0; ndigits--)
198 ;
199 }
200
201 if (sigfigs > ndigits && s0[ndigits] != 0)
202 dorounding(s0, ndigits, p->dbl_sign, decpt);
203
204 s = s0 + ndigits;
205 if (rve != NULL)
206 *rve = s;
207 *s-- = '\0';
208 for (; s >= s0; s--)
209 *s = xdigs[(unsigned int)*s];
210
211 return (s0);
212 }
213 DEF_STRONG(__hdtoa);
214
215 #if (LDBL_MANT_DIG > DBL_MANT_DIG)
216
217 /*
218 * This is the long double version of __hdtoa().
219 */
220 char *
__hldtoa(long double e,const char * xdigs,int ndigits,int * decpt,int * sign,char ** rve)221 __hldtoa(long double e, const char *xdigs, int ndigits, int *decpt, int *sign,
222 char **rve)
223 {
224 static const int sigfigs = (LDBL_MANT_DIG + 3) / 4;
225 struct ieee_ext *p = (struct ieee_ext *)&e;
226 char *s, *s0;
227 int bufsize;
228 int fbits = 0;
229
230 *sign = p->ext_sign;
231
232 switch (fpclassify(e)) {
233 case FP_NORMAL:
234 *decpt = p->ext_exp - LDBL_ADJ;
235 break;
236 case FP_ZERO:
237 *decpt = 1;
238 return (nrv_alloc("0", rve, 1));
239 case FP_SUBNORMAL:
240 e *= 0x1p514L;
241 *decpt = p->ext_exp - (514 + LDBL_ADJ);
242 break;
243 case FP_INFINITE:
244 *decpt = INT_MAX;
245 return (nrv_alloc(INFSTR, rve, sizeof(INFSTR) - 1));
246 case FP_NAN:
247 *decpt = INT_MAX;
248 return (nrv_alloc(NANSTR, rve, sizeof(NANSTR) - 1));
249 default:
250 abort();
251 }
252
253 /* FP_NORMAL or FP_SUBNORMAL */
254
255 if (ndigits == 0) /* dtoa() compatibility */
256 ndigits = 1;
257
258 /*
259 * For simplicity, we generate all the digits even if the
260 * caller has requested fewer.
261 */
262 bufsize = (sigfigs > ndigits) ? sigfigs : ndigits;
263 s0 = rv_alloc(bufsize);
264 if (s0 == NULL)
265 return (NULL);
266
267 /*
268 * We work from right to left, first adding any requested zero
269 * padding, then the least significant portion of the
270 * mantissa, followed by the most significant. The buffer is
271 * filled with the byte values 0x0 through 0xf, which are
272 * converted to xdigs[0x0] through xdigs[0xf] after the
273 * rounding phase.
274 */
275 for (s = s0 + bufsize - 1; s > s0 + sigfigs - 1; s--)
276 *s = 0;
277
278 for (fbits = EXT_FRACLBITS / 4; fbits > 0 && s > s0; s--, fbits--) {
279 *s = p->ext_fracl & 0xf;
280 p->ext_fracl >>= 4;
281 }
282 #ifdef EXT_FRACLMBITS
283 for (fbits = EXT_FRACLMBITS / 4; fbits > 0 && s > s0; s--, fbits--) {
284 *s = p->ext_fraclm & 0xf;
285 p->ext_fraclm >>= 4;
286 }
287 #endif
288 #ifdef EXT_FRACHMBITS
289 for (fbits = EXT_FRACHMBITS / 4; fbits > 0 && s > s0; s--, fbits--) {
290 *s = p->ext_frachm & 0xf;
291 p->ext_frachm >>= 4;
292 }
293 #endif
294 for (fbits = EXT_FRACHBITS / 4; fbits > 0 && s > s0; s--, fbits--) {
295 *s = p->ext_frach & 0xf;
296 p->ext_frach >>= 4;
297 }
298
299 /*
300 * At this point, we have snarfed all the bits in the
301 * mantissa, with the possible exception of the highest-order
302 * (partial) nibble, which is dealt with by the next
303 * statement. We also tack on the implicit normalization bit.
304 */
305 *s = (p->ext_frach | (1U << ((LDBL_MANT_DIG - 1) % 4))) & 0xf;
306
307 /* If ndigits < 0, we are expected to auto-size the precision. */
308 if (ndigits < 0) {
309 for (ndigits = sigfigs; s0[ndigits - 1] == 0; ndigits--)
310 ;
311 }
312
313 if (sigfigs > ndigits && s0[ndigits] != 0)
314 dorounding(s0, ndigits, p->ext_sign, decpt);
315
316 s = s0 + ndigits;
317 if (rve != NULL)
318 *rve = s;
319 *s-- = '\0';
320 for (; s >= s0; s--)
321 *s = xdigs[(unsigned int)*s];
322
323 return (s0);
324 }
325 DEF_STRONG(__hldtoa);
326
327 #else /* (LDBL_MANT_DIG == DBL_MANT_DIG) */
328
329 char *
__hldtoa(long double e,const char * xdigs,int ndigits,int * decpt,int * sign,char ** rve)330 __hldtoa(long double e, const char *xdigs, int ndigits, int *decpt, int *sign,
331 char **rve)
332 {
333 return (__hdtoa((double)e, xdigs, ndigits, decpt, sign, rve));
334 }
335 DEF_STRONG(__hldtoa);
336
337 #endif /* (LDBL_MANT_DIG == DBL_MANT_DIG) */
338