This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH] Update sparc ULPs.
From: "Joseph S. Myers" <joseph@codesourcery.com>
Date: Mon, 24 Sep 2012 12:57:54 +0000
> On Sun, 23 Sep 2012, David Miller wrote:
>
>> >> The expression "0x1p-16378L + 1.0L" should underflow, right?
>> >
>> > No. The result is in the normal range (equal to 1.0L in round-to-nearest
>> > mode), so no underflow.
>>
>> Hmmm, even considering the implicit bit, we can't fit both bits at the
>> same time.
>>
>> The result here is "tiny" and there is a loss of precision.
>
> The result, 1.0L, is not tiny. (Nor is either operand, 0x1p-16378L or
> 1.0L.)
>
> Are you sure you've identified the correct expression as being the one
> that is underflowing? On looking at the code, I'd think that "z = x * x;"
> (where at this point x stores the argument, xm1) is a much more likely
> underflow location.
Sorry, it's the one that generates an inexact, my bad.
Just for reference my test case is below which I compiled with "-O2
-fno-builtin". It produces output:
FUN
1 fsr[0x0]
hx: 0x00050000
w1: 0x00000000
w2: 0x00000000
w3: 0x00000000
2 fsr[0x0]
3 fsr[0x21]
4 fsr[0x20]
5 fsr[0x20]
12 fsr[0x20]
0.000000 (0x0 --> 0xa0)
That 0x21 FSR value means current and accumulated inexact.
I'll do more work today to spot the location of the underflow.
Sorry for the noise.
#include <math.h>
#include <fpu_control.h>
#include <sys/types.h>
#undef _FPU_GETCW
#undef _FPU_SETCW
#if __WORDSIZE == 64
# define _FPU_GETCW(cw) __asm__ __volatile__ ("stx %%fsr,%0" : "=m" (*&cw))
# define _FPU_SETCW(cw) __asm__ __volatile__ ("ldx %0,%%fsr" : : "m" (*&cw))
#else
# define _FPU_GETCW(cw) __asm__ __volatile__ ("st %%fsr,%0" : "=m" (*&cw))
# define _FPU_SETCW(cw) __asm__ __volatile__ ("ld %0,%%fsr" : : "m" (*&cw))
#endif
typedef union
{
long double value;
struct
{
u_int64_t msw;
u_int64_t lsw;
} parts64;
struct
{
u_int32_t w0, w1, w2, w3;
} parts32;
} ieee854_long_double_shape_type;
/* Get two 64 bit ints from a long double. */
#define GET_LDOUBLE_WORDS64(ix0,ix1,d) \
do { \
ieee854_long_double_shape_type qw_u; \
qw_u.value = (d); \
(ix0) = qw_u.parts64.msw; \
(ix1) = qw_u.parts64.lsw; \
} while (0)
/* Set a long double from two 64 bit ints. */
#define SET_LDOUBLE_WORDS64(d,ix0,ix1) \
do { \
ieee854_long_double_shape_type qw_u; \
qw_u.parts64.msw = (ix0); \
qw_u.parts64.lsw = (ix1); \
(d) = qw_u.value; \
} while (0)
/* Get the more significant 64 bits of a long double mantissa. */
#define GET_LDOUBLE_MSW64(v,d) \
do { \
ieee854_long_double_shape_type sh_u; \
sh_u.value = (d); \
(v) = sh_u.parts64.msw; \
} while (0)
/* Set the more significant 64 bits of a long double mantissa from an int. */
#define SET_LDOUBLE_MSW64(d,v) \
do { \
ieee854_long_double_shape_type sh_u; \
sh_u.value = (d); \
sh_u.parts64.msw = (v); \
(d) = sh_u.value; \
} while (0)
/* Get the least significant 64 bits of a long double mantissa. */
#define GET_LDOUBLE_LSW64(v,d) \
do { \
ieee854_long_double_shape_type sh_u; \
sh_u.value = (d); \
(v) = sh_u.parts64.lsw; \
} while (0)
/* Coefficients for log(1+x) = x - x^2 / 2 + x^3 P(x)/Q(x)
* 1/sqrt(2) <= 1+x < sqrt(2)
* Theoretical peak relative error = 5.3e-37,
* relative peak error spread = 2.3e-14
*/
static const long double
P12 = 1.538612243596254322971797716843006400388E-6L,
P11 = 4.998469661968096229986658302195402690910E-1L,
P10 = 2.321125933898420063925789532045674660756E1L,
P9 = 4.114517881637811823002128927449878962058E2L,
P8 = 3.824952356185897735160588078446136783779E3L,
P7 = 2.128857716871515081352991964243375186031E4L,
P6 = 7.594356839258970405033155585486712125861E4L,
P5 = 1.797628303815655343403735250238293741397E5L,
P4 = 2.854829159639697837788887080758954924001E5L,
P3 = 3.007007295140399532324943111654767187848E5L,
P2 = 2.014652742082537582487669938141683759923E5L,
P1 = 7.771154681358524243729929227226708890930E4L,
P0 = 1.313572404063446165910279910527789794488E4L,
/* Q12 = 1.000000000000000000000000000000000000000E0L, */
Q11 = 4.839208193348159620282142911143429644326E1L,
Q10 = 9.104928120962988414618126155557301584078E2L,
Q9 = 9.147150349299596453976674231612674085381E3L,
Q8 = 5.605842085972455027590989944010492125825E4L,
Q7 = 2.248234257620569139969141618556349415120E5L,
Q6 = 6.132189329546557743179177159925690841200E5L,
Q5 = 1.158019977462989115839826904108208787040E6L,
Q4 = 1.514882452993549494932585972882995548426E6L,
Q3 = 1.347518538384329112529391120390701166528E6L,
Q2 = 7.777690340007566932935753241556479363645E5L,
Q1 = 2.626900195321832660448791748036714883242E5L,
Q0 = 3.940717212190338497730839731583397586124E4L;
/* Coefficients for log(x) = z + z^3 P(z^2)/Q(z^2),
* where z = 2(x-1)/(x+1)
* 1/sqrt(2) <= x < sqrt(2)
* Theoretical peak relative error = 1.1e-35,
* relative peak error spread 1.1e-9
*/
static const long double
R5 = -8.828896441624934385266096344596648080902E-1L,
R4 = 8.057002716646055371965756206836056074715E1L,
R3 = -2.024301798136027039250415126250455056397E3L,
R2 = 2.048819892795278657810231591630928516206E4L,
R1 = -8.977257995689735303686582344659576526998E4L,
R0 = 1.418134209872192732479751274970992665513E5L,
/* S6 = 1.000000000000000000000000000000000000000E0L, */
S5 = -1.186359407982897997337150403816839480438E2L,
S4 = 3.998526750980007367835804959888064681098E3L,
S3 = -5.748542087379434595104154610899551484314E4L,
S2 = 4.001557694070773974936904547424676279307E5L,
S1 = -1.332535117259762928288745111081235577029E6L,
S0 = 1.701761051846631278975701529965589676574E6L;
/* C1 + C2 = ln 2 */
static const long double C1 = 6.93145751953125E-1L;
static const long double C2 = 1.428606820309417232121458176568075500134E-6L;
static const long double sqrth = 0.7071067811865475244008443621048490392848L;
/* ln (2^16384 * (1 - 2^-113)) */
static const long double maxlog = 1.1356523406294143949491931077970764891253E4L;
static const long double zero = 0.0L;
long double
__log1pl (long double xm1)
{
long double x, y, z, r, s;
ieee854_long_double_shape_type u;
fpu_control_t fsr;
int32_t hx;
int e;
/* Test for NaN or infinity input. */
printf("FUN\n");
_FPU_GETCW(fsr); printf("1 fsr[0x%lx]\n", fsr);
u.value = xm1;
hx = u.parts32.w0;
printf("hx: 0x%08x\n", hx);
printf("w1: 0x%08x\n", u.parts32.w1);
printf("w2: 0x%08x\n", u.parts32.w2);
printf("w3: 0x%08x\n", u.parts32.w3);
if (hx >= 0x7fff0000)
return xm1;
/* log1p(+- 0) = +- 0. */
_FPU_GETCW(fsr); printf("2 fsr[0x%lx]\n", fsr);
if (((hx & 0x7fffffff) == 0)
&& (u.parts32.w1 | u.parts32.w2 | u.parts32.w3) == 0)
return xm1;
#if 0
if ((hx & 0x7fffffff) >= 0x7ffeffff)
x = xm1 + 1.0L;
else
x = xm1;
#else
x = xm1 + 1.0L;
#endif
_FPU_GETCW(fsr); printf("3 fsr[0x%lx]\n", fsr);
/* log1p(-1) = -inf */
if (x <= 0.0L)
{
if (x == 0.0L)
return (-1.0L / (x - x));
else
return (zero / (x - x));
}
/* Separate mantissa from exponent. */
/* Use frexp used so that denormal numbers will be handled properly. */
_FPU_GETCW(fsr); printf("4 fsr[0x%lx]\n", fsr);
x = frexpl (x, &e);
_FPU_GETCW(fsr); printf("5 fsr[0x%lx]\n", fsr);
/* Logarithm using log(x) = z + z^3 P(z^2)/Q(z^2),
where z = 2(x-1)/x+1). */
if ((e > 2) || (e < -2))
{
if (x < sqrth)
{ /* 2( 2x-1 )/( 2x+1 ) */
e -= 1;
z = x - 0.5L;
y = 0.5L * z + 0.5L;
_FPU_GETCW(fsr); printf("6 fsr[0x%lx]\n", fsr);
}
else
{ /* 2 (x-1)/(x+1) */
z = x - 0.5L;
z -= 0.5L;
y = 0.5L * x + 0.5L;
_FPU_GETCW(fsr); printf("7 fsr[0x%lx]\n", fsr);
}
x = z / y;
z = x * x;
_FPU_GETCW(fsr); printf("8 fsr[0x%lx]\n", fsr);
r = ((((R5 * z
+ R4) * z
+ R3) * z
+ R2) * z
+ R1) * z
+ R0;
_FPU_GETCW(fsr); printf("9 fsr[0x%lx]\n", fsr);
s = (((((z
+ S5) * z
+ S4) * z
+ S3) * z
+ S2) * z
+ S1) * z
+ S0;
_FPU_GETCW(fsr); printf("10 fsr[0x%lx]\n", fsr);
z = x * (z * r / s);
z = z + e * C2;
z = z + x;
z = z + e * C1;
_FPU_GETCW(fsr); printf("11 fsr[0x%lx]\n", fsr);
return (z);
}
/* Logarithm using log(1+x) = x - .5x^2 + x^3 P(x)/Q(x). */
_FPU_GETCW(fsr); printf("12 fsr[0x%lx]\n", fsr);
if (x < sqrth)
{
e -= 1;
if (e != 0)
x = 2.0L * x - 1.0L; /* 2x - 1 */
else
x = xm1;
}
else
{
if (e != 0)
x = x - 1.0L;
else
x = xm1;
}
z = x * x;
r = (((((((((((P12 * x
+ P11) * x
+ P10) * x
+ P9) * x
+ P8) * x
+ P7) * x
+ P6) * x
+ P5) * x
+ P4) * x
+ P3) * x
+ P2) * x
+ P1) * x
+ P0;
s = (((((((((((x
+ Q11) * x
+ Q10) * x
+ Q9) * x
+ Q8) * x
+ Q7) * x
+ Q6) * x
+ Q5) * x
+ Q4) * x
+ Q3) * x
+ Q2) * x
+ Q1) * x
+ Q0;
y = x * (z * r / s);
y = y + e * C2;
z = y - 0.5L * z;
z = z + x;
z = z + e * C1;
return (z);
}
int main(void)
{
long double ret;
fpu_control_t fb, fa;
_FPU_GETCW(fb);
ret = __log1pl(0x1p-16378L);
_FPU_GETCW(fa);
printf("%Lf (0x%lx --> 0x%lx)\n", ret, fb, fa);
return 0;
}