1 |
|
|
/* $OpenBSD: hdtoa.c,v 1.3 2015/09/14 12:49:33 guenther 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 |
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 |
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.cp3. 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 * |
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 * |
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 |
|
|
|
229 |
|
|
*sign = p->ext_sign; |
230 |
|
|
|
231 |
|
|
switch (fpclassify(e)) { |
232 |
|
|
case FP_NORMAL: |
233 |
|
|
*decpt = p->ext_exp - LDBL_ADJ; |
234 |
|
|
break; |
235 |
|
|
case FP_ZERO: |
236 |
|
|
*decpt = 1; |
237 |
|
|
return (nrv_alloc("0", rve, 1)); |
238 |
|
|
case FP_SUBNORMAL: |
239 |
|
|
e *= 0x1p514L; |
240 |
|
|
*decpt = p->ext_exp - (514 + LDBL_ADJ); |
241 |
|
|
break; |
242 |
|
|
case FP_INFINITE: |
243 |
|
|
*decpt = INT_MAX; |
244 |
|
|
return (nrv_alloc(INFSTR, rve, sizeof(INFSTR) - 1)); |
245 |
|
|
case FP_NAN: |
246 |
|
|
*decpt = INT_MAX; |
247 |
|
|
return (nrv_alloc(NANSTR, rve, sizeof(NANSTR) - 1)); |
248 |
|
|
default: |
249 |
|
|
abort(); |
250 |
|
|
} |
251 |
|
|
|
252 |
|
|
/* FP_NORMAL or FP_SUBNORMAL */ |
253 |
|
|
|
254 |
|
|
if (ndigits == 0) /* dtoa() compatibility */ |
255 |
|
|
ndigits = 1; |
256 |
|
|
|
257 |
|
|
/* |
258 |
|
|
* For simplicity, we generate all the digits even if the |
259 |
|
|
* caller has requested fewer. |
260 |
|
|
*/ |
261 |
|
|
bufsize = (sigfigs > ndigits) ? sigfigs : ndigits; |
262 |
|
|
s0 = rv_alloc(bufsize); |
263 |
|
|
if (s0 == NULL) |
264 |
|
|
return (NULL); |
265 |
|
|
|
266 |
|
|
/* |
267 |
|
|
* We work from right to left, first adding any requested zero |
268 |
|
|
* padding, then the least significant portion of the |
269 |
|
|
* mantissa, followed by the most significant. The buffer is |
270 |
|
|
* filled with the byte values 0x0 through 0xf, which are |
271 |
|
|
* converted to xdigs[0x0] through xdigs[0xf] after the |
272 |
|
|
* rounding phase. |
273 |
|
|
*/ |
274 |
|
|
for (s = s0 + bufsize - 1; s > s0 + sigfigs - 1; s--) |
275 |
|
|
*s = 0; |
276 |
|
|
for (; s > s0 + sigfigs - (EXT_FRACLBITS / 4) - 1 && s > s0; s--) { |
277 |
|
|
*s = p->ext_fracl & 0xf; |
278 |
|
|
p->ext_fracl >>= 4; |
279 |
|
|
} |
280 |
|
|
#ifdef EXT_FRACHMBITS |
281 |
|
|
for (; s > s0; s--) { |
282 |
|
|
*s = p->ext_frachm & 0xf; |
283 |
|
|
p->ext_frachm >>= 4; |
284 |
|
|
} |
285 |
|
|
#endif |
286 |
|
|
#ifdef EXT_FRACLMBITS |
287 |
|
|
for (; s > s0; s--) { |
288 |
|
|
*s = p->ext_fraclm & 0xf; |
289 |
|
|
p->ext_fraclm >>= 4; |
290 |
|
|
} |
291 |
|
|
#endif |
292 |
|
|
for (; s > s0; s--) { |
293 |
|
|
*s = p->ext_frach & 0xf; |
294 |
|
|
p->ext_frach >>= 4; |
295 |
|
|
} |
296 |
|
|
|
297 |
|
|
/* |
298 |
|
|
* At this point, we have snarfed all the bits in the |
299 |
|
|
* mantissa, with the possible exception of the highest-order |
300 |
|
|
* (partial) nibble, which is dealt with by the next |
301 |
|
|
* statement. We also tack on the implicit normalization bit. |
302 |
|
|
*/ |
303 |
|
|
*s = p->ext_frach | (1U << ((LDBL_MANT_DIG - 1) % 4)); |
304 |
|
|
|
305 |
|
|
/* If ndigits < 0, we are expected to auto-size the precision. */ |
306 |
|
|
if (ndigits < 0) { |
307 |
|
|
for (ndigits = sigfigs; s0[ndigits - 1] == 0; ndigits--) |
308 |
|
|
; |
309 |
|
|
} |
310 |
|
|
|
311 |
|
|
if (sigfigs > ndigits && s0[ndigits] != 0) |
312 |
|
|
dorounding(s0, ndigits, p->ext_sign, decpt); |
313 |
|
|
|
314 |
|
|
s = s0 + ndigits; |
315 |
|
|
if (rve != NULL) |
316 |
|
|
*rve = s; |
317 |
|
|
*s-- = '\0'; |
318 |
|
|
for (; s >= s0; s--) |
319 |
|
|
*s = xdigs[(unsigned int)*s]; |
320 |
|
|
|
321 |
|
|
return (s0); |
322 |
|
|
} |
323 |
|
|
DEF_STRONG(__hldtoa); |
324 |
|
|
|
325 |
|
|
#else /* (LDBL_MANT_DIG == DBL_MANT_DIG) */ |
326 |
|
|
|
327 |
|
|
char * |
328 |
|
|
__hldtoa(long double e, const char *xdigs, int ndigits, int *decpt, int *sign, |
329 |
|
|
char **rve) |
330 |
|
|
{ |
331 |
|
|
return (__hdtoa((double)e, xdigs, ndigits, decpt, sign, rve)); |
332 |
|
|
} |
333 |
|
|
DEF_STRONG(__hldtoa); |
334 |
|
|
|
335 |
|
|
#endif /* (LDBL_MANT_DIG == DBL_MANT_DIG) */ |