diff mbox series

math: Fix acosf when building with gcc <= 11

Message ID 20250102143444.3574078-1-adhemerval.zanella@linaro.org
State New
Headers show
Series math: Fix acosf when building with gcc <= 11 | expand

Commit Message

Adhemerval Zanella Netto Jan. 2, 2025, 2:34 p.m. UTC
GCC <= 11 wrongly assumes the rounding is to nearest and performs a
constant folding where it should evaluate since the result is not
exact [1].

[1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57245
---
 sysdeps/aarch64/libm-test-ulps                 |  2 --
 sysdeps/i386/fpu/libm-test-ulps                |  2 --
 sysdeps/i386/i686/fpu/multiarch/libm-test-ulps |  2 --
 sysdeps/ieee754/flt-32/e_acosf.c               | 10 +++++++---
 sysdeps/powerpc/fpu/libm-test-ulps             |  2 --
 sysdeps/x86_64/fpu/libm-test-ulps              |  2 --
 6 files changed, 7 insertions(+), 13 deletions(-)

Comments

Alexander Monakov Jan. 2, 2025, 2:43 p.m. UTC | #1
On Thu, 2 Jan 2025, Adhemerval Zanella wrote:

> GCC <= 11 wrongly assumes the rounding is to nearest and performs a
> constant folding where it should evaluate since the result is not
> exact [1].
> 
> [1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57245

Note that the problematic folding still happens a few lines below the
fixed instance where '+ 0x1p-25' without the 'f' suffix causes the
sum to be evaluated in double precision, and then converted to float.
With 0x1p-25f, gcc doesn't fold the summation.

Alexander
Adhemerval Zanella Netto Jan. 2, 2025, 3:09 p.m. UTC | #2
On 02/01/25 11:43, Alexander Monakov wrote:
> 
> On Thu, 2 Jan 2025, Adhemerval Zanella wrote:
> 
>> GCC <= 11 wrongly assumes the rounding is to nearest and performs a
>> constant folding where it should evaluate since the result is not
>> exact [1].
>>
>> [1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57245
> 
> Note that the problematic folding still happens a few lines below the
> fixed instance where '+ 0x1p-25' without the 'f' suffix causes the
> sum to be evaluated in double precision, and then converted to float.
> With 0x1p-25f, gcc doesn't fold the summation.
> 
> Alexander

Yes, I will let Paul fix it and sync with core-math changes.  At least
this is not triggering any regressions so far, although our current
test does not check for an extensive range as core-math tests do.
diff mbox series

Patch

diff --git a/sysdeps/aarch64/libm-test-ulps b/sysdeps/aarch64/libm-test-ulps
index f1ab172f27..190685f893 100644
--- a/sysdeps/aarch64/libm-test-ulps
+++ b/sysdeps/aarch64/libm-test-ulps
@@ -11,7 +11,6 @@  float: 1
 
 Function: "acos_downward":
 double: 1
-float: 1
 ldouble: 1
 
 Function: "acos_sve":
@@ -20,7 +19,6 @@  float: 1
 
 Function: "acos_towardzero":
 double: 1
-float: 1
 ldouble: 1
 
 Function: "acos_upward":
diff --git a/sysdeps/i386/fpu/libm-test-ulps b/sysdeps/i386/fpu/libm-test-ulps
index 27f0f71b0a..423061e12e 100644
--- a/sysdeps/i386/fpu/libm-test-ulps
+++ b/sysdeps/i386/fpu/libm-test-ulps
@@ -7,12 +7,10 @@  float128: 1
 ldouble: 2
 
 Function: "acos_downward":
-float: 1
 float128: 1
 ldouble: 2
 
 Function: "acos_towardzero":
-float: 1
 float128: 1
 ldouble: 2
 
diff --git a/sysdeps/i386/i686/fpu/multiarch/libm-test-ulps b/sysdeps/i386/i686/fpu/multiarch/libm-test-ulps
index 66733c2d1d..466e68dae0 100644
--- a/sysdeps/i386/i686/fpu/multiarch/libm-test-ulps
+++ b/sysdeps/i386/i686/fpu/multiarch/libm-test-ulps
@@ -7,12 +7,10 @@  float128: 1
 ldouble: 2
 
 Function: "acos_downward":
-float: 1
 float128: 1
 ldouble: 2
 
 Function: "acos_towardzero":
-float: 1
 float128: 1
 ldouble: 2
 
diff --git a/sysdeps/ieee754/flt-32/e_acosf.c b/sysdeps/ieee754/flt-32/e_acosf.c
index cba01221dc..a5a4de4fc2 100644
--- a/sysdeps/ieee754/flt-32/e_acosf.c
+++ b/sysdeps/ieee754/flt-32/e_acosf.c
@@ -3,7 +3,7 @@ 
 Copyright (c) 2023-2024 Alexei Sibidanov.
 
 The original version of this file was copied from the CORE-MATH
-project (file src/binary32/acos/acosf.c, revision 61d7bef).
+project (file src/binary32/acos/acosf.c, revision 56dd347).
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
@@ -28,6 +28,7 @@  SOFTWARE.
 #include <math.h>
 #include <math_private.h>
 #include <libm-alias-finite.h>
+#include <math-barriers.h>
 #include "math_config.h"
 
 static __attribute__ ((noinline)) float
@@ -66,7 +67,7 @@  poly12 (double z, const double *c)
 float
 __ieee754_acosf (float x)
 {
-  const double pi2 = 0x1.921fb54442d18p+0;
+  double pi2 = 0x1.921fb54442d18p+0;
   static const double o[] = { 0, 0x1.921fb54442d18p+1 };
   double xs = x;
   double r;
@@ -87,7 +88,10 @@  __ieee754_acosf (float x)
 	};
       /* Avoid spurious underflow exception.  */
       if (__glibc_unlikely (ax <= 0x40000000u)) /* |x| < 2^-63 */
-	return (float) pi2;
+	/* GCC <= 11 wrongly assumes the rounding is to nearest and
+	   performs a constant folding here:
+	   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=57245 */
+	return math_opt_barrier (pi2);
       double z = xs;
       double z2 = z * z;
       double z4 = z2 * z2;
diff --git a/sysdeps/powerpc/fpu/libm-test-ulps b/sysdeps/powerpc/fpu/libm-test-ulps
index 89dcef3603..07b91f8534 100644
--- a/sysdeps/powerpc/fpu/libm-test-ulps
+++ b/sysdeps/powerpc/fpu/libm-test-ulps
@@ -8,13 +8,11 @@  ldouble: 1
 
 Function: "acos_downward":
 double: 1
-float: 1
 float128: 1
 ldouble: 3
 
 Function: "acos_towardzero":
 double: 1
-float: 1
 float128: 1
 ldouble: 3
 
diff --git a/sysdeps/x86_64/fpu/libm-test-ulps b/sysdeps/x86_64/fpu/libm-test-ulps
index 624186f3f6..ad936ad32f 100644
--- a/sysdeps/x86_64/fpu/libm-test-ulps
+++ b/sysdeps/x86_64/fpu/libm-test-ulps
@@ -8,13 +8,11 @@  ldouble: 2
 
 Function: "acos_downward":
 double: 1
-float: 1
 float128: 1
 ldouble: 2
 
 Function: "acos_towardzero":
 double: 1
-float: 1
 float128: 1
 ldouble: 2