[PATCH v2 06/11] math: Compute the j0, j1, y0, and y1 trigonometric terms in round-to-nearest

Adhemerval Zanella adhemerval.zanella@linaro.org
Wed Nov 18 18:15:18 GMT 2026


For |x| >= 2, the double j0, j1, y0, and y1 use sin(x) and cos(x) in sums
and differences that cancel near the zeros of the functions, and cos(2x)
to compute the cancelling one, which amplifies the error of the
trigonometric values.

The IBM sin, cos, and sincos implementations always compute in
round-to-nearest, while the correctly rounded CORE-MATH ones round in the
current rounding mode.  With CORE-MATH implementations the routines now
show larger errors than the testsuite threshold of 9 ulp (for instance
12 ulp for j1_downward (0x6.874828p+4) and 10 ulp for y1_upward (0x2p+0)).

Compute the trigonometric terms in round-to-nearest, as jn and yn do for
the whole evaluation (a8e2112ae3e), which keeps the previous accuracy.

Checked on x86_64-linux-gnu and i686-linux-gnu.
---
 sysdeps/ieee754/dbl-64/e_j0.c | 17 +++++++++++++----
 sysdeps/ieee754/dbl-64/e_j1.c | 15 +++++++++++----
 2 files changed, 24 insertions(+), 8 deletions(-)

diff --git a/sysdeps/ieee754/dbl-64/e_j0.c b/sysdeps/ieee754/dbl-64/e_j0.c
index d0e683637a6..bc2ad414ea2 100644
--- a/sysdeps/ieee754/dbl-64/e_j0.c
+++ b/sysdeps/ieee754/dbl-64/e_j0.c
@@ -61,8 +61,17 @@
 #include <math.h>
 #include <math-barriers.h>
 #include <math_private.h>
+#include <fenv_private.h>
 #include <libm-alias-finite.h>
 
+static inline void
+sincos_rn (double x, double *s, double *c, double *c2)
+{
+  SET_RESTORE_ROUND (FE_TONEAREST);
+  __sincos (x, s, c);
+  *c2 = fabs (x) < 0x1p1023 ? __cos (x + x) : 0.0;
+}
+
 static double pzero (double), qzero (double);
 
 static const double
@@ -95,12 +104,12 @@ __ieee754_j0 (double x)
   x = fabs (x);
   if (ix >= 0x40000000)         /* |x| >= 2.0 */
     {
-      __sincos (x, &s, &c);
+      sincos_rn (x, &s, &c, &z);
       ss = s - c;
       cc = s + c;
       if (ix < 0x7fe00000)           /* make sure x+x not overflow */
 	{
-	  z = -__cos (x + x);
+	  z = -z;
 	  if ((s * c) < zero)
 	    cc = z / ss;
 	  else
@@ -186,7 +195,7 @@ __ieee754_y0 (double x)
 		 *              sin(x) +- cos(x) = -cos(2x)/(sin(x) -+ cos(x))
 		 * to compute the worse one.
 		 */
-      __sincos (x, &s, &c);
+      sincos_rn (x, &s, &c, &z);
       ss = s - c;
       cc = s + c;
       /*
@@ -195,7 +204,7 @@ __ieee754_y0 (double x)
        */
       if (ix < 0x7fe00000)           /* make sure x+x not overflow */
 	{
-	  z = -__cos (x + x);
+	  z = -z;
 	  if ((s * c) < zero)
 	    cc = z / ss;
 	  else
diff --git a/sysdeps/ieee754/dbl-64/e_j1.c b/sysdeps/ieee754/dbl-64/e_j1.c
index 8757bd57509..ae298968a8b 100644
--- a/sysdeps/ieee754/dbl-64/e_j1.c
+++ b/sysdeps/ieee754/dbl-64/e_j1.c
@@ -63,9 +63,18 @@
 #include <math.h>
 #include <math-narrow-eval.h>
 #include <math_private.h>
+#include <fenv_private.h>
 #include <math-underflow.h>
 #include <libm-alias-finite.h>
 
+static inline void
+sincos_rn (double x, double *s, double *c, double *c2)
+{
+  SET_RESTORE_ROUND (FE_TONEAREST);
+  __sincos (x, s, c);
+  *c2 = fabs (x) < 0x1p1023 ? __cos (x + x) : 0.0;
+}
+
 static double pone (double), qone (double);
 
 static const double
@@ -99,12 +108,11 @@ __ieee754_j1 (double x)
   y = fabs (x);
   if (ix >= 0x40000000)         /* |x| >= 2.0 */
     {
-      __sincos (y, &s, &c);
+      sincos_rn (y, &s, &c, &z);
       ss = -s - c;
       cc = s - c;
       if (ix < 0x7fe00000)           /* make sure y+y not overflow */
 	{
-	  z = __cos (y + y);
 	  if ((s * c) > zero)
 	    cc = z / ss;
 	  else
@@ -183,12 +191,11 @@ __ieee754_y1 (double x)
     return zero / (zero * x);
   if (ix >= 0x40000000)         /* |x| >= 2.0 */
     {
-      __sincos (x, &s, &c);
+      sincos_rn (x, &s, &c, &z);
       ss = -s - c;
       cc = s - c;
       if (ix < 0x7fe00000)           /* make sure x+x not overflow */
 	{
-	  z = __cos (x + x);
 	  if ((s * c) > zero)
 	    cc = z / ss;
 	  else
-- 
2.53.0



More information about the Libc-alpha mailing list