1 /* $NetBSD: hdtoa.c,v 1.6 2008/03/21 23:13:48 christos Exp $ */ 2 3 /*- 4 * Copyright (c) 2004, 2005 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 <sys/cdefs.h> 30 #if 0 31 __FBSDID("$FreeBSD: src/lib/libc/gdtoa/_hdtoa.c,v 1.4 2007/01/03 04:57:58 das Exp $"); 32 #else 33 __RCSID("$NetBSD: hdtoa.c,v 1.6 2008/03/21 23:13:48 christos Exp $"); 34 #endif 35 36 #include <float.h> 37 #include <limits.h> 38 #include <math.h> 39 #ifndef __vax__ 40 #include <machine/ieee.h> 41 #else 42 #include <machine/vaxfp.h> 43 #define ieee_double_u vax_dfloating_u 44 #define dblu_d dfltu_d 45 #define dblu_dbl dfltu_dflt 46 #define dbl_sign dflt_sign 47 #define dbl_exp dflt_exp 48 #define dbl_frach dflt_frach 49 #define dbl_fracm dflt_fracm 50 #define dbl_fracl dflt_fracl 51 #define DBL_FRACHBITS DFLT_FRACHBITS 52 #define DBL_FRACMBITS DFLT_FRACMBITS 53 #define DBL_FRACLBITS DFLT_FRACLBITS 54 #define DBL_EXPBITS DFLT_EXPBITS 55 #endif 56 #include "gdtoaimp.h" 57 58 /* Strings values used by dtoa() */ 59 #define INFSTR "Infinity" 60 #define NANSTR "NaN" 61 62 #define DBL_ADJ (DBL_MAX_EXP - 2 + ((DBL_MANT_DIG - 1) % 4)) 63 #define LDBL_ADJ (LDBL_MAX_EXP - 2 + ((LDBL_MANT_DIG - 1) % 4)) 64 65 /* 66 * Round up the given digit string. If the digit string is fff...f, 67 * this procedure sets it to 100...0 and returns 1 to indicate that 68 * the exponent needs to be bumped. Otherwise, 0 is returned. 69 */ 70 static int 71 roundup(char *s0, int ndigits) 72 { 73 char *s; 74 75 for (s = s0 + ndigits - 1; *s == 0xf; s--) { 76 if (s == s0) { 77 *s = 1; 78 return (1); 79 } 80 *s = 0; 81 } 82 ++*s; 83 return (0); 84 } 85 86 /* 87 * Round the given digit string to ndigits digits according to the 88 * current rounding mode. Note that this could produce a string whose 89 * value is not representable in the corresponding floating-point 90 * type. The exponent pointed to by decpt is adjusted if necessary. 91 */ 92 static void 93 dorounding(char *s0, int ndigits, int sign, int *decpt) 94 { 95 int adjust = 0; /* do we need to adjust the exponent? */ 96 97 switch (FLT_ROUNDS) { 98 case 0: /* toward zero */ 99 default: /* implementation-defined */ 100 break; 101 case 1: /* to nearest, halfway rounds to even */ 102 if ((s0[ndigits] > 8) || 103 (s0[ndigits] == 8 && s0[ndigits - 1] & 1)) 104 adjust = roundup(s0, ndigits); 105 break; 106 case 2: /* toward +inf */ 107 if (sign == 0) 108 adjust = roundup(s0, ndigits); 109 break; 110 case 3: /* toward -inf */ 111 if (sign != 0) 112 adjust = roundup(s0, ndigits); 113 break; 114 } 115 116 if (adjust) 117 *decpt += 4; 118 } 119 120 /* 121 * This procedure converts a double-precision number in IEEE format 122 * into a string of hexadecimal digits and an exponent of 2. Its 123 * behavior is bug-for-bug compatible with dtoa() in mode 2, with the 124 * following exceptions: 125 * 126 * - An ndigits < 0 causes it to use as many digits as necessary to 127 * represent the number exactly. 128 * - The additional xdigs argument should point to either the string 129 * "0123456789ABCDEF" or the string "0123456789abcdef", depending on 130 * which case is desired. 131 * - This routine does not repeat dtoa's mistake of setting decpt 132 * to 9999 in the case of an infinity or NaN. INT_MAX is used 133 * for this purpose instead. 134 * 135 * Note that the C99 standard does not specify what the leading digit 136 * should be for non-zero numbers. For instance, 0x1.3p3 is the same 137 * as 0x2.6p2 is the same as 0x4.cp3. This implementation chooses the 138 * first digit so that subsequent digits are aligned on nibble 139 * boundaries (before rounding). 140 * 141 * Inputs: d, xdigs, ndigits 142 * Outputs: decpt, sign, rve 143 */ 144 char * 145 hdtoa(double d, const char *xdigs, int ndigits, int *decpt, int *sign, 146 char **rve) 147 { 148 static const int sigfigs = (DBL_MANT_DIG + 3) / 4; 149 union ieee_double_u u; 150 char *s, *s0; 151 size_t bufsize; 152 153 u.dblu_d = d; 154 *sign = u.dblu_dbl.dbl_sign; 155 156 switch (fpclassify(d)) { 157 case FP_NORMAL: 158 *decpt = u.dblu_dbl.dbl_exp - DBL_ADJ; 159 break; 160 case FP_ZERO: 161 *decpt = 1; 162 return (nrv_alloc("0", rve, 1)); 163 case FP_SUBNORMAL: 164 u.dblu_d *= 0x1p514; 165 *decpt = u.dblu_dbl.dbl_exp - (514 + DBL_ADJ); 166 break; 167 case FP_INFINITE: 168 *decpt = INT_MAX; 169 return (nrv_alloc(INFSTR, rve, sizeof(INFSTR) - 1)); 170 case FP_NAN: 171 *decpt = INT_MAX; 172 return (nrv_alloc(NANSTR, rve, sizeof(NANSTR) - 1)); 173 default: 174 abort(); 175 } 176 177 /* FP_NORMAL or FP_SUBNORMAL */ 178 179 if (ndigits == 0) /* dtoa() compatibility */ 180 ndigits = 1; 181 182 /* 183 * For simplicity, we generate all the digits even if the 184 * caller has requested fewer. 185 */ 186 bufsize = (sigfigs > ndigits) ? sigfigs : ndigits; 187 s0 = rv_alloc(bufsize); 188 if (s0 == NULL) 189 return NULL; 190 191 /* 192 * We work from right to left, first adding any requested zero 193 * padding, then the least significant portion of the 194 * mantissa, followed by the most significant. The buffer is 195 * filled with the byte values 0x0 through 0xf, which are 196 * converted to xdigs[0x0] through xdigs[0xf] after the 197 * rounding phase. 198 */ 199 for (s = s0 + bufsize - 1; s > s0 + sigfigs - 1; s--) 200 *s = 0; 201 for (; s > s0 + sigfigs - (DBL_FRACLBITS / 4) - 1 && s > s0; s--) { 202 *s = u.dblu_dbl.dbl_fracl & 0xf; 203 u.dblu_dbl.dbl_fracl >>= 4; 204 } 205 #ifdef DBL_FRACMBITS 206 for (; s > s0; s--) { 207 *s = u.dblu_dbl.dbl_fracm & 0xf; 208 u.dblu_dbl.dbl_fracm >>= 4; 209 } 210 #endif 211 for (; s > s0; s--) { 212 *s = u.dblu_dbl.dbl_frach & 0xf; 213 u.dblu_dbl.dbl_frach >>= 4; 214 } 215 216 /* 217 * At this point, we have snarfed all the bits in the 218 * mantissa, with the possible exception of the highest-order 219 * (partial) nibble, which is dealt with by the next 220 * statement. We also tack on the implicit normalization bit. 221 */ 222 *s = u.dblu_dbl.dbl_frach | (1U << ((DBL_MANT_DIG - 1) % 4)); 223 224 /* If ndigits < 0, we are expected to auto-size the precision. */ 225 if (ndigits < 0) { 226 for (ndigits = sigfigs; s0[ndigits - 1] == 0; ndigits--) 227 continue; 228 } 229 230 if (sigfigs > ndigits && s0[ndigits] != 0) 231 dorounding(s0, ndigits, u.dblu_dbl.dbl_sign, decpt); 232 233 s = s0 + ndigits; 234 if (rve != NULL) 235 *rve = s; 236 *s-- = '\0'; 237 for (; s >= s0; s--) 238 *s = xdigs[(unsigned int)*s]; 239 240 return (s0); 241 } 242 243 #if (LDBL_MANT_DIG > DBL_MANT_DIG) 244 245 /* 246 * This is the long double version of hdtoa(). 247 */ 248 char * 249 hldtoa(long double e, const char *xdigs, int ndigits, int *decpt, int *sign, 250 char **rve) 251 { 252 static const int sigfigs = (LDBL_MANT_DIG + 3) / 4; 253 union ieee_ext_u u; 254 char *s, *s0; 255 size_t bufsize; 256 257 u.extu_ld = e; 258 *sign = u.extu_ext.ext_sign; 259 260 switch (fpclassify(e)) { 261 case FP_NORMAL: 262 *decpt = u.extu_ext.ext_exp - LDBL_ADJ; 263 break; 264 case FP_ZERO: 265 *decpt = 1; 266 return (nrv_alloc("0", rve, 1)); 267 case FP_SUBNORMAL: 268 u.extu_ld *= 0x1p514L; 269 *decpt = u.extu_ext.ext_exp - (514 + LDBL_ADJ); 270 break; 271 case FP_INFINITE: 272 *decpt = INT_MAX; 273 return (nrv_alloc(INFSTR, rve, sizeof(INFSTR) - 1)); 274 case FP_NAN: 275 *decpt = INT_MAX; 276 return (nrv_alloc(NANSTR, rve, sizeof(NANSTR) - 1)); 277 default: 278 abort(); 279 } 280 281 /* FP_NORMAL or FP_SUBNORMAL */ 282 283 if (ndigits == 0) /* dtoa() compatibility */ 284 ndigits = 1; 285 286 /* 287 * For simplicity, we generate all the digits even if the 288 * caller has requested fewer. 289 */ 290 bufsize = (sigfigs > ndigits) ? sigfigs : ndigits; 291 s0 = rv_alloc(bufsize); 292 if (s0 == NULL) 293 return NULL; 294 295 /* 296 * We work from right to left, first adding any requested zero 297 * padding, then the least significant portion of the 298 * mantissa, followed by the most significant. The buffer is 299 * filled with the byte values 0x0 through 0xf, which are 300 * converted to xdigs[0x0] through xdigs[0xf] after the 301 * rounding phase. 302 */ 303 for (s = s0 + bufsize - 1; s > s0 + sigfigs - 1; s--) 304 *s = 0; 305 for (; s > s0 + sigfigs - (EXT_FRACLBITS / 4) - 1 && s > s0; s--) { 306 *s = u.extu_ext.ext_fracl & 0xf; 307 u.extu_ext.ext_fracl >>= 4; 308 } 309 #ifdef EXT_FRACHMBITS 310 for (; s > s0; s--) { 311 *s = u.extu_ext.ext_frachm & 0xf; 312 u.extu_ext.ext_frachm >>= 4; 313 } 314 #endif 315 #ifdef EXT_FRACLMBITS 316 for (; s > s0; s--) { 317 *s = u.extu_ext.ext_fraclm & 0xf; 318 u.extu_ext.ext_fraclm >>= 4; 319 } 320 #endif 321 for (; s > s0; s--) { 322 *s = u.extu_ext.ext_frach & 0xf; 323 u.extu_ext.ext_frach >>= 4; 324 } 325 326 /* 327 * At this point, we have snarfed all the bits in the 328 * mantissa, with the possible exception of the highest-order 329 * (partial) nibble, which is dealt with by the next 330 * statement. We also tack on the implicit normalization bit. 331 */ 332 *s = u.extu_ext.ext_frach | (1U << ((LDBL_MANT_DIG - 1) % 4)); 333 334 /* If ndigits < 0, we are expected to auto-size the precision. */ 335 if (ndigits < 0) { 336 for (ndigits = sigfigs; s0[ndigits - 1] == 0; ndigits--) 337 continue; 338 } 339 340 if (sigfigs > ndigits && s0[ndigits] != 0) 341 dorounding(s0, ndigits, u.extu_ext.ext_sign, decpt); 342 343 s = s0 + ndigits; 344 if (rve != NULL) 345 *rve = s; 346 *s-- = '\0'; 347 for (; s >= s0; s--) 348 *s = xdigs[(unsigned int)*s]; 349 350 return (s0); 351 } 352 353 #else /* (LDBL_MANT_DIG == DBL_MANT_DIG) */ 354 355 char * 356 hldtoa(long double e, const char *xdigs, int ndigits, int *decpt, int *sign, 357 char **rve) 358 { 359 360 return (hdtoa((double)e, xdigs, ndigits, decpt, sign, rve)); 361 } 362 363 #endif /* (LDBL_MANT_DIG == DBL_MANT_DIG) */ 364