This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH] Fix for logb/logbf/logbl (bz 13954/13955/13956)
Adhemerval Zanella <azanella@linux.vnet.ibm.com> writes:
> + if (__builtin_expect ((rix = ix >> 20) == 0, 0))
> + {
> + /* POSIX specifies that denormal number is treated as
> + though it were normalized. */
> + int m1 = (ix == 0) ? 0 : __builtin_clz (ix);
> + int m2 = (lx == 0) ? 0 : __builtin_clz (lx);
> + int ma = (m1 == 0) ? m2 + 32 : m1;
> + return -1022.0 + (double)(11 - ma);
That looks roundabout. We know that ix and lx cannot both be zero, and
the addition is needlessly computed in floating point. I also find it
harder to understand than the attached version (we just need to
compensate for the shifted mantissa).
(Note that the ldbl-96 version is unused: i386, x86-64 and ia64 have
their own version and and it is wrong for m68k. Perhaps it should be
removed?)
Andreas.
* sysdeps/ieee754/dbl-64/s_logb.c (__logb): Optimize
handling of denormals.
* sysdeps/ieee754/flt-32/s_logbf.c (__logbf): Likewise.
* sysdeps/ieee754/ldbl-96/s_logbl.c (__logbl): Likewise.
* sysdeps/ieee754/ldbl-128/s_logbl.c (__logbl): Likewise.
* sysdeps/ieee754/ldbl-128ibm/s_logbl.c (__logbl): Optimize
---
sysdeps/ieee754/dbl-64/s_logb.c | 10 ++++++----
sysdeps/ieee754/flt-32/s_logbf.c | 3 +--
sysdeps/ieee754/ldbl-128/s_logbl.c | 10 ++++++----
sysdeps/ieee754/ldbl-128ibm/s_logbl.c | 10 ++++++----
sysdeps/ieee754/ldbl-96/s_logbl.c | 10 ++++++----
5 files changed, 25 insertions(+), 18 deletions(-)
diff --git a/sysdeps/ieee754/dbl-64/s_logb.c b/sysdeps/ieee754/dbl-64/s_logb.c
index baa35e1..17aa94b 100644
--- a/sysdeps/ieee754/dbl-64/s_logb.c
+++ b/sysdeps/ieee754/dbl-64/s_logb.c
@@ -34,10 +34,12 @@ __logb (double x)
{
/* POSIX specifies that denormal number is treated as
though it were normalized. */
- int m1 = (ix == 0) ? 0 : __builtin_clz (ix);
- int m2 = (lx == 0) ? 0 : __builtin_clz (lx);
- int ma = (m1 == 0) ? m2 + 32 : m1;
- return -1022.0 + (double)(11 - ma);
+ int ma;
+ if (ix == 0)
+ ma = __builtin_clz (lx) + 32;
+ else
+ ma = __builtin_clz (ix);
+ rix -= ma - 12;
}
return (double) (rix - 1023);
}
diff --git a/sysdeps/ieee754/flt-32/s_logbf.c b/sysdeps/ieee754/flt-32/s_logbf.c
index 025c70d..e2b3aaa 100644
--- a/sysdeps/ieee754/flt-32/s_logbf.c
+++ b/sysdeps/ieee754/flt-32/s_logbf.c
@@ -31,8 +31,7 @@ __logbf (float x)
{
/* POSIX specifies that denormal number is treated as
though it were normalized. */
- int m = (ix == 0) ? 0 : __builtin_clz (ix);
- return -126.0 + (float)(8 - m);
+ rix -= __builtin_clz (ix) - 9;
}
return (float) (rix - 127);
}
diff --git a/sysdeps/ieee754/ldbl-128/s_logbl.c b/sysdeps/ieee754/ldbl-128/s_logbl.c
index cf6003e..3ba67b7 100644
--- a/sysdeps/ieee754/ldbl-128/s_logbl.c
+++ b/sysdeps/ieee754/ldbl-128/s_logbl.c
@@ -41,10 +41,12 @@ __logbl (long double x)
{
/* POSIX specifies that denormal number is treated as
though it were normalized. */
- int m1 = (hx == 0) ? 0 : __builtin_clzll (hx);
- int m2 = (lx == 0) ? 0 : __builtin_clzll (lx);
- int ma = (m1 == 0) ? m2 + 64 : m1;
- return -16382.0 + (long double)(15 - ma);
+ int ma;
+ if (hx == 0)
+ ma = __builtin_clzll (lx) + 64;
+ else
+ ma = __builtin_clzll (hx);
+ ex -= ma - 16;
}
return (long double) (ex - 16383);
}
diff --git a/sysdeps/ieee754/ldbl-128ibm/s_logbl.c b/sysdeps/ieee754/ldbl-128ibm/s_logbl.c
index 678b6ca..9b0bd98 100644
--- a/sysdeps/ieee754/ldbl-128ibm/s_logbl.c
+++ b/sysdeps/ieee754/ldbl-128ibm/s_logbl.c
@@ -38,10 +38,12 @@ __logbl (long double x)
{
/* POSIX specifies that denormal number is treated as
though it were normalized. */
- int m1 = (hx == 0) ? 0 : __builtin_clzll (hx);
- int m2 = (lx == 0) ? 0 : __builtin_clzll (lx);
- int ma = (m1 == 0) ? m2 + 64 : m1;
- return -1022.0 + (long double)(11 - ma);
+ int ma;
+ if (hx == 0)
+ ma = __builtin_clzll (lx) + 64;
+ else
+ ma = __builtin_clzll (hx);
+ rhx -= ma - 12;
}
return (long double) (rhx - 1023);
}
diff --git a/sysdeps/ieee754/ldbl-96/s_logbl.c b/sysdeps/ieee754/ldbl-96/s_logbl.c
index d8ad4bc..4289be1 100644
--- a/sysdeps/ieee754/ldbl-96/s_logbl.c
+++ b/sysdeps/ieee754/ldbl-96/s_logbl.c
@@ -38,10 +38,12 @@ __logbl (long double x)
{
/* POSIX specifies that denormal number is treated as
though it were normalized. */
- int m1 = (ix == 0) ? 0 : __builtin_clz (ix);
- int m2 = (lx == 0) ? 0 : __builtin_clz (lx);
- int ma = (m1 == 0) ? m2 + 32 : m1;
- return -16382.0 - (long double)(ma);
+ int ma;
+ if (ix == 0)
+ ma = __builtin_clz (lx) + 32;
+ else
+ ma = __builtin_clz (ix);
+ es -= ma - 1;
}
return (long double) (es - 16383);
}
--
1.7.10.2
--
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5
"And now for something completely different."