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