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