/* * This procedure converts a double-precision number in IEEE format * into a string of hexadecimal digits and an exponent of 2. Its * behavior is bug-for-bug compatible with dtoa() in mode 2, with the * following exceptions: * * - An ndigits < 0 causes it to use as many digits as necessary to * represent the number exactly. * - The additional xdigs argument should point to either the string * "0123456789ABCDEF" or the string "0123456789abcdef", depending on * which case is desired. * - This routine does not repeat dtoa's mistake of setting decpt * to 9999 in the case of an infinity or NaN. INT_MAX is used * for this purpose instead. * * Note that the C99 standard does not specify what the leading digit * should be for non-zero numbers. For instance, 0x1.3p3 is the same * as 0x2.6p2 is the same as 0x4.cp3. This implementation chooses the * first digit so that subsequent digits are aligned on nibble * boundaries (before rounding). * * Inputs: d, xdigs, ndigits * Outputs: decpt, sign, rve */ char * __hdtoa(double d, const char *xdigs, int ndigits, int *decpt, int *sign, char **rve) { static const int sigfigs = (DBL_MANT_DIG + 3) / 4; struct vax_d_floating *p = (struct vax_d_floating *)&d; char *s, *s0; int bufsize; *sign = p->dflt_sign; switch (fpclassify(d)) { case FP_NORMAL: *decpt = p->dflt_exp - DBL_ADJ; break; case FP_ZERO: *decpt = 1; return (nrv_alloc("0", rve, 1)); default: abort(); } /* FP_NORMAL or FP_SUBNORMAL */ if (ndigits == 0) /* dtoa() compatibility */ ndigits = 1; /* * For simplicity, we generate all the digits even if the * caller has requested fewer. */ bufsize = (sigfigs > ndigits) ? sigfigs : ndigits; s0 = rv_alloc(bufsize); if (s0 == NULL) return (NULL); /* * We work from right to left, first adding any requested zero * padding, then the least significant portion of the * mantissa, followed by the most significant. The buffer is * filled with the byte values 0x0 through 0xf, which are * converted to xdigs[0x0] through xdigs[0xf] after the * rounding phase. */ for (s = s0 + bufsize - 1; s > s0 + sigfigs - 1; s--) *s = 0; for (; s > s0 + sigfigs - (DFLT_FRACLBITS / 4) - 1 && s > s0; s--) { *s = p->dflt_fracl & 0xf; p->dflt_fracl >>= 4; } for (; s > s0; s--) { *s = p->dflt_fracm & 0xf; p->dflt_fracm >>= 4; } for (; s > s0; s--) { *s = p->dflt_frach & 0xf; p->dflt_frach >>= 4; } /* * At this point, we have snarfed all the bits in the * mantissa, with the possible exception of the highest-order * (partial) nibble, which is dealt with by the next * statement. We also tack on the implicit normalization bit. */ *s = p->dflt_frach | (1U << ((DBL_MANT_DIG - 1) % 4)); /* If ndigits < 0, we are expected to auto-size the precision. */ if (ndigits < 0) { for (ndigits = sigfigs; s0[ndigits - 1] == 0; ndigits--) ; } if (sigfigs > ndigits && s0[ndigits] != 0) dorounding(s0, ndigits, p->dflt_sign, decpt); s = s0 + ndigits; if (rve != NULL) *rve = s; *s-- = '\0'; for (; s >= s0; s--) *s = xdigs[(unsigned int)*s]; return (s0); }
/* * This is the long double version of __hdtoa(). */ char * __hldtoa(long double e, const char *xdigs, int ndigits, int *decpt, int *sign, char **rve) { static const int sigfigs = (LDBL_MANT_DIG + 3) / 4; union IEEEl2bits u; char *s, *s0; int bufsize, f; u.e = e; *sign = u.bits.sign; switch (f = fpclassify(e)) { case FP_NORMAL: case FP_SUPERNORMAL: *decpt = u.bits.exp - LDBL_ADJ; break; case FP_ZERO: *decpt = 1; return (nrv_alloc("0", rve, 1)); case FP_SUBNORMAL: u.e *= 0x1p514L; *decpt = u.bits.exp - (514 + LDBL_ADJ); break; case FP_INFINITE: *decpt = INT_MAX; return (nrv_alloc(INFSTR, rve, sizeof(INFSTR) - 1)); case FP_NAN: *decpt = INT_MAX; return (nrv_alloc(NANSTR, rve, sizeof(NANSTR) - 1)); default: LIBC_ABORT("fpclassify returned %d", f); } /* FP_NORMAL or FP_SUBNORMAL */ if (ndigits == 0) /* dtoa() compatibility */ ndigits = 1; /* * For simplicity, we generate all the digits even if the * caller has requested fewer. */ bufsize = (sigfigs > ndigits) ? sigfigs : ndigits; s0 = rv_alloc(bufsize); /* * We work from right to left, first adding any requested zero * padding, then the least significant portion of the * mantissa, followed by the most significant. The buffer is * filled with the byte values 0x0 through 0xf, which are * converted to xdigs[0x0] through xdigs[0xf] after the * rounding phase. */ for (s = s0 + bufsize - 1; s > s0 + sigfigs - 1; s--) *s = 0; for (; s > s0 + sigfigs - (LDBL_MANL_SIZE / 4) - 1 && s > s0; s--) { *s = u.bits.manl & 0xf; u.bits.manl >>= 4; } for (; s > s0; s--) { *s = u.bits.manh & 0xf; u.bits.manh >>= 4; } /* * At this point, we have snarfed all the bits in the * mantissa, with the possible exception of the highest-order * (partial) nibble, which is dealt with by the next * statement. We also tack on the implicit normalization bit. */ *s = u.bits.manh | (1U << ((LDBL_MANT_DIG - 1) % 4)); /* If ndigits < 0, we are expected to auto-size the precision. */ if (ndigits < 0) { for (ndigits = sigfigs; s0[ndigits - 1] == 0; ndigits--) ; } if (sigfigs > ndigits && s0[ndigits] != 0) dorounding(s0, ndigits, u.bits.sign, decpt); s = s0 + ndigits; if (rve != NULL) *rve = s; *s-- = '\0'; for (; s >= s0; s--) *s = xdigs[(unsigned int)*s]; return (s0); }