Make several changes to the way printf handles hex floating point (%a):
1. Previously, printing the number 1.0 could produce 0x1p+0, 0x2p-1,
0x4p-2, or 0x8p-3, depending on what happened to be convenient. This
meant that printing a value as a double and printing the same value
as a long double could produce different (but equivalent) results.
The change is to always make the leading digit a 1, unless the
number is 0. This solves the aforementioned problem and has
several other advantages.
2. Use the FPU to do rounding. This is far simpler and more portable
than manipulating the bits, and it fixes an obsure round-to-even
bug. It also raises the exceptions now required by IEEE 754R.
The drawbacks are that it is usually slightly slower, and it makes
printf less effective as a debugging tool when the FPU is hosed
(e.g., due to a buggy softfloat implementation).
3. On i386, twiddle the rounding precision so that (2) works properly
for long doubles.
4. Make several simplifications that are now possible due to (2).
5. Split __hldtoa() into a separate file.
Thanks to remko for access to a sparc64 box for testing.
2008-04-12 03:11:36 +00:00
|
|
|
/*-
|
2017-11-25 17:12:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
Make several changes to the way printf handles hex floating point (%a):
1. Previously, printing the number 1.0 could produce 0x1p+0, 0x2p-1,
0x4p-2, or 0x8p-3, depending on what happened to be convenient. This
meant that printing a value as a double and printing the same value
as a long double could produce different (but equivalent) results.
The change is to always make the leading digit a 1, unless the
number is 0. This solves the aforementioned problem and has
several other advantages.
2. Use the FPU to do rounding. This is far simpler and more portable
than manipulating the bits, and it fixes an obsure round-to-even
bug. It also raises the exceptions now required by IEEE 754R.
The drawbacks are that it is usually slightly slower, and it makes
printf less effective as a debugging tool when the FPU is hosed
(e.g., due to a buggy softfloat implementation).
3. On i386, twiddle the rounding precision so that (2) works properly
for long doubles.
4. Make several simplifications that are now possible due to (2).
5. Split __hldtoa() into a separate file.
Thanks to remko for access to a sparc64 box for testing.
2008-04-12 03:11:36 +00:00
|
|
|
* Copyright (c) 2004-2008 David Schultz <das@FreeBSD.ORG>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <float.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#ifdef __i386__
|
|
|
|
#include <ieeefp.h>
|
|
|
|
#endif
|
|
|
|
|
2008-04-12 14:53:52 +00:00
|
|
|
#include "../stdio/floatio.h"
|
Make several changes to the way printf handles hex floating point (%a):
1. Previously, printing the number 1.0 could produce 0x1p+0, 0x2p-1,
0x4p-2, or 0x8p-3, depending on what happened to be convenient. This
meant that printing a value as a double and printing the same value
as a long double could produce different (but equivalent) results.
The change is to always make the leading digit a 1, unless the
number is 0. This solves the aforementioned problem and has
several other advantages.
2. Use the FPU to do rounding. This is far simpler and more portable
than manipulating the bits, and it fixes an obsure round-to-even
bug. It also raises the exceptions now required by IEEE 754R.
The drawbacks are that it is usually slightly slower, and it makes
printf less effective as a debugging tool when the FPU is hosed
(e.g., due to a buggy softfloat implementation).
3. On i386, twiddle the rounding precision so that (2) works properly
for long doubles.
4. Make several simplifications that are now possible due to (2).
5. Split __hldtoa() into a separate file.
Thanks to remko for access to a sparc64 box for testing.
2008-04-12 03:11:36 +00:00
|
|
|
#include "fpmath.h"
|
|
|
|
#include "gdtoaimp.h"
|
|
|
|
|
|
|
|
#if (LDBL_MANT_DIG > DBL_MANT_DIG)
|
|
|
|
|
|
|
|
/* Strings values used by dtoa() */
|
|
|
|
#define INFSTR "Infinity"
|
|
|
|
#define NANSTR "NaN"
|
|
|
|
|
|
|
|
#ifdef LDBL_IMPLICIT_NBIT
|
|
|
|
#define MANH_SIZE LDBL_MANH_SIZE
|
|
|
|
#else
|
|
|
|
#define MANH_SIZE (LDBL_MANH_SIZE - 1)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if MANH_SIZE > 32
|
|
|
|
typedef uint64_t manh_t;
|
|
|
|
#else
|
|
|
|
typedef uint32_t manh_t;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if LDBL_MANL_SIZE > 32
|
|
|
|
typedef uint64_t manl_t;
|
|
|
|
#else
|
|
|
|
typedef uint32_t manl_t;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define LDBL_ADJ (LDBL_MAX_EXP - 2)
|
|
|
|
#define SIGFIGS ((LDBL_MANT_DIG + 3) / 4 + 1)
|
|
|
|
|
|
|
|
static const float one[] = { 1.0f, -1.0f };
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is the long double version of __hdtoa().
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
__hldtoa(long double e, const char *xdigs, int ndigits, int *decpt, int *sign,
|
|
|
|
char **rve)
|
|
|
|
{
|
|
|
|
union IEEEl2bits u;
|
|
|
|
char *s, *s0;
|
|
|
|
manh_t manh;
|
|
|
|
manl_t manl;
|
|
|
|
int bufsize;
|
|
|
|
#ifdef __i386__
|
|
|
|
fp_prec_t oldprec;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
u.e = e;
|
|
|
|
*sign = u.bits.sign;
|
|
|
|
|
|
|
|
switch (fpclassify(e)) {
|
|
|
|
case FP_NORMAL:
|
|
|
|
*decpt = u.bits.exp - LDBL_ADJ;
|
|
|
|
break;
|
|
|
|
case FP_ZERO:
|
|
|
|
*decpt = 1;
|
|
|
|
return (nrv_alloc("0", rve, 1));
|
|
|
|
case FP_SUBNORMAL:
|
|
|
|
#ifdef __i386__
|
|
|
|
oldprec = fpsetprec(FP_PE);
|
|
|
|
#endif
|
|
|
|
u.e *= 0x1p514L;
|
|
|
|
*decpt = u.bits.exp - (514 + LDBL_ADJ);
|
|
|
|
#ifdef __i386__
|
|
|
|
fpsetprec(oldprec);
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case FP_INFINITE:
|
|
|
|
*decpt = INT_MAX;
|
|
|
|
return (nrv_alloc(INFSTR, rve, sizeof(INFSTR) - 1));
|
|
|
|
default: /* FP_NAN or unrecognized */
|
|
|
|
*decpt = INT_MAX;
|
|
|
|
return (nrv_alloc(NANSTR, rve, sizeof(NANSTR) - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FP_NORMAL or FP_SUBNORMAL */
|
|
|
|
|
|
|
|
if (ndigits == 0) /* dtoa() compatibility */
|
|
|
|
ndigits = 1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If ndigits < 0, we are expected to auto-size, so we allocate
|
|
|
|
* enough space for all the digits.
|
|
|
|
*/
|
|
|
|
bufsize = (ndigits > 0) ? ndigits : SIGFIGS;
|
|
|
|
s0 = rv_alloc(bufsize);
|
|
|
|
|
|
|
|
/* Round to the desired number of digits. */
|
|
|
|
if (SIGFIGS > ndigits && ndigits > 0) {
|
|
|
|
float redux = one[u.bits.sign];
|
|
|
|
int offset = 4 * ndigits + LDBL_MAX_EXP - 4 - LDBL_MANT_DIG;
|
|
|
|
#ifdef __i386__
|
|
|
|
oldprec = fpsetprec(FP_PE);
|
|
|
|
#endif
|
|
|
|
u.bits.exp = offset;
|
|
|
|
u.e += redux;
|
|
|
|
u.e -= redux;
|
|
|
|
*decpt += u.bits.exp - offset;
|
|
|
|
#ifdef __i386__
|
|
|
|
fpsetprec(oldprec);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
mask_nbit_l(u);
|
|
|
|
manh = u.bits.manh;
|
|
|
|
manl = u.bits.manl;
|
|
|
|
*s0 = '1';
|
|
|
|
for (s = s0 + 1; s < s0 + bufsize; s++) {
|
|
|
|
*s = xdigs[(manh >> (MANH_SIZE - 4)) & 0xf];
|
|
|
|
manh = (manh << 4) | (manl >> (LDBL_MANL_SIZE - 4));
|
|
|
|
manl <<= 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If ndigits < 0, we are expected to auto-size the precision. */
|
|
|
|
if (ndigits < 0) {
|
|
|
|
for (ndigits = SIGFIGS; s0[ndigits - 1] == '0'; ndigits--)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
s = s0 + ndigits;
|
|
|
|
*s = '\0';
|
|
|
|
if (rve != NULL)
|
|
|
|
*rve = s;
|
|
|
|
return (s0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* (LDBL_MANT_DIG == DBL_MANT_DIG) */
|
|
|
|
|
|
|
|
char *
|
|
|
|
__hldtoa(long double e, const char *xdigs, int ndigits, int *decpt, int *sign,
|
|
|
|
char **rve)
|
|
|
|
{
|
|
|
|
|
|
|
|
return (__hdtoa((double)e, xdigs, ndigits, decpt, sign, rve));
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* (LDBL_MANT_DIG == DBL_MANT_DIG) */
|