1 /* $OpenBSD: hdtoa.c,v 1.1 2008/09/07 20:36:08 martynas 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 166 /* 167 * We work from right to left, first adding any requested zero 168 * padding, then the least significant portion of the 169 * mantissa, followed by the most significant. The buffer is 170 * filled with the byte values 0x0 through 0xf, which are 171 * converted to xdigs[0x0] through xdigs[0xf] after the 172 * rounding phase. 173 */ 174 for (s = s0 + bufsize - 1; s > s0 + sigfigs - 1; s--) 175 *s = 0; 176 for (; s > s0 + sigfigs - (DBL_FRACLBITS / 4) - 1 && s > s0; s--) { 177 *s = p->dbl_fracl & 0xf; 178 p->dbl_fracl >>= 4; 179 } 180 for (; s > s0; s--) { 181 *s = p->dbl_frach & 0xf; 182 p->dbl_frach >>= 4; 183 } 184 185 /* 186 * At this point, we have snarfed all the bits in the 187 * mantissa, with the possible exception of the highest-order 188 * (partial) nibble, which is dealt with by the next 189 * statement. We also tack on the implicit normalization bit. 190 */ 191 *s = p->dbl_frach | (1U << ((DBL_MANT_DIG - 1) % 4)); 192 193 /* If ndigits < 0, we are expected to auto-size the precision. */ 194 if (ndigits < 0) { 195 for (ndigits = sigfigs; s0[ndigits - 1] == 0; ndigits--) 196 ; 197 } 198 199 if (sigfigs > ndigits && s0[ndigits] != 0) 200 dorounding(s0, ndigits, p->dbl_sign, decpt); 201 202 s = s0 + ndigits; 203 if (rve != NULL) 204 *rve = s; 205 *s-- = '\0'; 206 for (; s >= s0; s--) 207 *s = xdigs[(unsigned int)*s]; 208 209 return (s0); 210 } 211 212 #if (LDBL_MANT_DIG > DBL_MANT_DIG) 213 214 /* 215 * This is the long double version of __hdtoa(). 216 */ 217 char * 218 __hldtoa(long double e, const char *xdigs, int ndigits, int *decpt, int *sign, 219 char **rve) 220 { 221 static const int sigfigs = (LDBL_MANT_DIG + 3) / 4; 222 struct ieee_ext *p = (struct ieee_ext *)&e; 223 char *s, *s0; 224 int bufsize; 225 226 *sign = p->ext_sign; 227 228 switch (fpclassify(e)) { 229 case FP_NORMAL: 230 *decpt = p->ext_exp - LDBL_ADJ; 231 break; 232 case FP_ZERO: 233 *decpt = 1; 234 return (nrv_alloc("0", rve, 1)); 235 case FP_SUBNORMAL: 236 e *= 0x1p514L; 237 *decpt = p->ext_exp - (514 + LDBL_ADJ); 238 break; 239 case FP_INFINITE: 240 *decpt = INT_MAX; 241 return (nrv_alloc(INFSTR, rve, sizeof(INFSTR) - 1)); 242 case FP_NAN: 243 *decpt = INT_MAX; 244 return (nrv_alloc(NANSTR, rve, sizeof(NANSTR) - 1)); 245 default: 246 abort(); 247 } 248 249 /* FP_NORMAL or FP_SUBNORMAL */ 250 251 if (ndigits == 0) /* dtoa() compatibility */ 252 ndigits = 1; 253 254 /* 255 * For simplicity, we generate all the digits even if the 256 * caller has requested fewer. 257 */ 258 bufsize = (sigfigs > ndigits) ? sigfigs : ndigits; 259 s0 = rv_alloc(bufsize); 260 261 /* 262 * We work from right to left, first adding any requested zero 263 * padding, then the least significant portion of the 264 * mantissa, followed by the most significant. The buffer is 265 * filled with the byte values 0x0 through 0xf, which are 266 * converted to xdigs[0x0] through xdigs[0xf] after the 267 * rounding phase. 268 */ 269 for (s = s0 + bufsize - 1; s > s0 + sigfigs - 1; s--) 270 *s = 0; 271 for (; s > s0 + sigfigs - (EXT_FRACLBITS / 4) - 1 && s > s0; s--) { 272 *s = p->ext_fracl & 0xf; 273 p->ext_fracl >>= 4; 274 } 275 #ifdef EXT_FRACHMBITS 276 for (; s > s0; s--) { 277 *s = p->ext_frachm & 0xf; 278 p->ext_frachm >>= 4; 279 } 280 #endif 281 #ifdef EXT_FRACLMBITS 282 for (; s > s0; s--) { 283 *s = p->ext_fraclm & 0xf; 284 p->ext_fraclm >>= 4; 285 } 286 #endif 287 for (; s > s0; s--) { 288 *s = p->ext_frach & 0xf; 289 p->ext_frach >>= 4; 290 } 291 292 /* 293 * At this point, we have snarfed all the bits in the 294 * mantissa, with the possible exception of the highest-order 295 * (partial) nibble, which is dealt with by the next 296 * statement. We also tack on the implicit normalization bit. 297 */ 298 *s = p->ext_frach | (1U << ((LDBL_MANT_DIG - 1) % 4)); 299 300 /* If ndigits < 0, we are expected to auto-size the precision. */ 301 if (ndigits < 0) { 302 for (ndigits = sigfigs; s0[ndigits - 1] == 0; ndigits--) 303 ; 304 } 305 306 if (sigfigs > ndigits && s0[ndigits] != 0) 307 dorounding(s0, ndigits, p->ext_sign, decpt); 308 309 s = s0 + ndigits; 310 if (rve != NULL) 311 *rve = s; 312 *s-- = '\0'; 313 for (; s >= s0; s--) 314 *s = xdigs[(unsigned int)*s]; 315 316 return (s0); 317 } 318 319 #else /* (LDBL_MANT_DIG == DBL_MANT_DIG) */ 320 321 char * 322 __hldtoa(long double e, const char *xdigs, int ndigits, int *decpt, int *sign, 323 char **rve) 324 { 325 return (__hdtoa((double)e, xdigs, ndigits, decpt, sign, rve)); 326 } 327 328 #endif /* (LDBL_MANT_DIG == DBL_MANT_DIG) */ 329