about summary refs log tree commit diff
path: root/math
diff options
context:
space:
mode:
authorSzabolcs Nagy <szabolcs.nagy@arm.com>2021-12-31 09:50:50 +0000
committerSzabolcs Nagy <szabolcs.nagy@arm.com>2022-01-10 14:27:17 +0000
commit347a5b592c91f0230d52a5117fd83e1f75028207 (patch)
treecef1eb7585b59b17118ad72f82856b39f94cfd14 /math
parente72ef23ee88187284b4b1ca9b2e314e618429d35 (diff)
downloadglibc-347a5b592c91f0230d52a5117fd83e1f75028207.tar.gz
glibc-347a5b592c91f0230d52a5117fd83e1f75028207.tar.xz
glibc-347a5b592c91f0230d52a5117fd83e1f75028207.zip
math: Fix float conversion regressions with gcc-12 [BZ #28713]
Converting double precision constants to float is now affected by the
runtime dynamic rounding mode instead of being evaluated at compile
time with default rounding mode (except static object initializers).

This can change the computed result and cause performance regression.
The known correctness issues (increased ulp errors) are already fixed,
this patch fixes remaining cases of unnecessary runtime conversions.

Add float M_* macros to math.h as new GNU extension API.  To avoid
conversions the new M_* macros are used and instead of casting double
literals to float, use float literals (only required if the conversion
is inexact).

The patch was tested on aarch64 where the following symbols had new
spurious conversion instructions that got fixed:

  __clog10f
  __gammaf_r_finite@GLIBC_2.17
  __j0f_finite@GLIBC_2.17
  __j1f_finite@GLIBC_2.17
  __jnf_finite@GLIBC_2.17
  __kernel_casinhf
  __lgamma_negf
  __log1pf
  __y0f_finite@GLIBC_2.17
  __y1f_finite@GLIBC_2.17
  cacosf
  cacoshf
  casinhf
  catanf
  catanhf
  clogf
  gammaf_positive

Fixes bug 28713.

Reviewed-by: Paul Zimmermann <Paul.Zimmermann@inria.fr>
Diffstat (limited to 'math')
-rw-r--r--math/k_casinh_template.c2
-rw-r--r--math/math.h17
-rw-r--r--math/s_cacos_template.c2
-rw-r--r--math/s_catan_template.c2
-rw-r--r--math/s_catanh_template.c2
-rw-r--r--math/s_clog10_template.c8
-rw-r--r--math/s_clog_template.c4
7 files changed, 27 insertions, 10 deletions
diff --git a/math/k_casinh_template.c b/math/k_casinh_template.c
index 98980dc393..e9fdc97f62 100644
--- a/math/k_casinh_template.c
+++ b/math/k_casinh_template.c
@@ -56,7 +56,7 @@ M_DECL_FUNC (__kernel_casinh) (CFLOAT x, int adj)
 	}
 
       res = M_SUF (__clog) (y);
-      __real__ res += (FLOAT) M_MLIT (M_LN2);
+      __real__ res += M_MLIT (M_LN2);
     }
   else if (rx >= M_LIT (0.5) && ix < M_EPSILON / 8)
     {
diff --git a/math/math.h b/math/math.h
index 7bd56c9648..27963ef6dc 100644
--- a/math/math.h
+++ b/math/math.h
@@ -1158,6 +1158,23 @@ iszero (__T __val)
 # define M_SQRT1_2	0.70710678118654752440	/* 1/sqrt(2) */
 #endif
 
+/* GNU extension to provide float constants with similar names.  */
+#ifdef __USE_GNU
+# define M_Ef		2.7182818284590452354f	/* e */
+# define M_LOG2Ef	1.4426950408889634074f	/* log_2 e */
+# define M_LOG10Ef	0.43429448190325182765f	/* log_10 e */
+# define M_LN2f		0.69314718055994530942f	/* log_e 2 */
+# define M_LN10f	2.30258509299404568402f	/* log_e 10 */
+# define M_PIf		3.14159265358979323846f	/* pi */
+# define M_PI_2f	1.57079632679489661923f	/* pi/2 */
+# define M_PI_4f	0.78539816339744830962f	/* pi/4 */
+# define M_1_PIf	0.31830988618379067154f	/* 1/pi */
+# define M_2_PIf	0.63661977236758134308f	/* 2/pi */
+# define M_2_SQRTPIf	1.12837916709551257390f	/* 2/sqrt(pi) */
+# define M_SQRT2f	1.41421356237309504880f	/* sqrt(2) */
+# define M_SQRT1_2f	0.70710678118654752440f	/* 1/sqrt(2) */
+#endif
+
 /* The above constants are not adequate for computation using `long double's.
    Therefore we provide as an extension constants with similar names as a
    GNU extension.  Provide enough digits for the 128-bit IEEE quad.  */
diff --git a/math/s_cacos_template.c b/math/s_cacos_template.c
index 644330f0f8..eaf7c80ade 100644
--- a/math/s_cacos_template.c
+++ b/math/s_cacos_template.c
@@ -32,7 +32,7 @@ M_DECL_FUNC (__cacos) (CFLOAT x)
     {
       y = M_SUF (__casin) (x);
 
-      __real__ res = (FLOAT) M_MLIT (M_PI_2) - __real__ y;
+      __real__ res = M_MLIT (M_PI_2) - __real__ y;
       if (__real__ res == 0)
 	__real__ res = 0;
       __imag__ res = -__imag__ y;
diff --git a/math/s_catan_template.c b/math/s_catan_template.c
index be3459cb4f..6a893095cf 100644
--- a/math/s_catan_template.c
+++ b/math/s_catan_template.c
@@ -106,7 +106,7 @@ M_DECL_FUNC (__catan) (CFLOAT x)
 	  if (M_FABS (__imag__ x) == 1
 	      && M_FABS (__real__ x) < M_EPSILON * M_EPSILON)
 	    __imag__ res = (M_COPYSIGN (M_LIT (0.5), __imag__ x)
-			    * ((FLOAT) M_MLIT (M_LN2)
+			    * (M_MLIT (M_LN2)
 			       - M_LOG (M_FABS (__real__ x))));
 	  else
 	    {
diff --git a/math/s_catanh_template.c b/math/s_catanh_template.c
index 59c8e36d21..7b57dda045 100644
--- a/math/s_catanh_template.c
+++ b/math/s_catanh_template.c
@@ -75,7 +75,7 @@ M_DECL_FUNC (__catanh) (CFLOAT x)
 	  if (M_FABS (__real__ x) == 1
 	      && M_FABS (__imag__ x) < M_EPSILON * M_EPSILON)
 	    __real__ res = (M_COPYSIGN (M_LIT (0.5), __real__ x)
-			    * ((FLOAT) M_MLIT (M_LN2)
+			    * (M_MLIT (M_LN2)
 			       - M_LOG (M_FABS (__imag__ x))));
 	  else
 	    {
diff --git a/math/s_clog10_template.c b/math/s_clog10_template.c
index f5a0873ad3..e50bd6ccf9 100644
--- a/math/s_clog10_template.c
+++ b/math/s_clog10_template.c
@@ -72,7 +72,7 @@ M_DECL_FUNC (__clog10) (CFLOAT x)
       if (absx == 1 && scale == 0)
 	{
 	  __real__ result = (M_LOG1P (absy * absy)
-			     * ((FLOAT) M_MLIT (M_LOG10E) / 2));
+			     * (M_MLIT (M_LOG10E) / 2));
 	  math_check_force_underflow_nonneg (__real__ result);
 	}
       else if (absx > 1 && absx < 2 && absy < 1 && scale == 0)
@@ -80,7 +80,7 @@ M_DECL_FUNC (__clog10) (CFLOAT x)
 	  FLOAT d2m1 = (absx - 1) * (absx + 1);
 	  if (absy >= M_EPSILON)
 	    d2m1 += absy * absy;
-	  __real__ result = M_LOG1P (d2m1) * ((FLOAT) M_MLIT (M_LOG10E) / 2);
+	  __real__ result = M_LOG1P (d2m1) * (M_MLIT (M_LOG10E) / 2);
 	}
       else if (absx < 1
 	       && absx >= M_LIT (0.5)
@@ -88,7 +88,7 @@ M_DECL_FUNC (__clog10) (CFLOAT x)
 	       && scale == 0)
 	{
 	  FLOAT d2m1 = (absx - 1) * (absx + 1);
-	  __real__ result = M_LOG1P (d2m1) * ((FLOAT) M_MLIT (M_LOG10E) / 2);
+	  __real__ result = M_LOG1P (d2m1) * (M_MLIT (M_LOG10E) / 2);
 	}
       else if (absx < 1
 	       && absx >= M_LIT (0.5)
@@ -96,7 +96,7 @@ M_DECL_FUNC (__clog10) (CFLOAT x)
 	       && absx * absx + absy * absy >= M_LIT (0.5))
 	{
 	  FLOAT d2m1 = M_SUF (__x2y2m1) (absx, absy);
-	  __real__ result = M_LOG1P (d2m1) * ((FLOAT) M_MLIT (M_LOG10E) / 2);
+	  __real__ result = M_LOG1P (d2m1) * (M_MLIT (M_LOG10E) / 2);
 	}
       else
 	{
diff --git a/math/s_clog_template.c b/math/s_clog_template.c
index 3b25bc9021..eff3d48796 100644
--- a/math/s_clog_template.c
+++ b/math/s_clog_template.c
@@ -32,7 +32,7 @@ M_DECL_FUNC (__clog) (CFLOAT x)
   if (__glibc_unlikely (rcls == FP_ZERO && icls == FP_ZERO))
     {
       /* Real and imaginary part are 0.0.  */
-      __imag__ result = signbit (__real__ x) ? (FLOAT) M_MLIT (M_PI) : 0;
+      __imag__ result = signbit (__real__ x) ? M_MLIT (M_PI) : 0;
       __imag__ result = M_COPYSIGN (__imag__ result, __imag__ x);
       /* Yes, the following line raises an exception.  */
       __real__ result = -1 / M_FABS (__real__ x);
@@ -94,7 +94,7 @@ M_DECL_FUNC (__clog) (CFLOAT x)
       else
 	{
 	  FLOAT d = M_HYPOT (absx, absy);
-	  __real__ result = M_LOG (d) - scale * (FLOAT) M_MLIT (M_LN2);
+	  __real__ result = M_LOG (d) - scale * M_MLIT (M_LN2);
 	}
 
       __imag__ result = M_ATAN2 (__imag__ x, __real__ x);