]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
math: Fix float conversion regressions with gcc-12 [BZ #28713]
authorSzabolcs Nagy <szabolcs.nagy@arm.com>
Fri, 31 Dec 2021 09:50:50 +0000 (09:50 +0000)
committerSzabolcs Nagy <szabolcs.nagy@arm.com>
Mon, 10 Jan 2022 14:27:17 +0000 (14:27 +0000)
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>
16 files changed:
NEWS
manual/math.texi
math/k_casinh_template.c
math/math.h
math/s_cacos_template.c
math/s_catan_template.c
math/s_catanh_template.c
math/s_clog10_template.c
math/s_clog_template.c
sysdeps/generic/math-type-macros-float.h
sysdeps/ieee754/flt-32/e_gammaf_r.c
sysdeps/ieee754/flt-32/e_j0f.c
sysdeps/ieee754/flt-32/e_j1f.c
sysdeps/ieee754/flt-32/e_jnf.c
sysdeps/ieee754/flt-32/lgamma_negf.c
sysdeps/ieee754/flt-32/s_log1pf.c

diff --git a/NEWS b/NEWS
index eb68bb1651631c5bc688b15858ef3063528c093d..a957b19fdce54b18a167bea68d10a76510eed96f 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -43,6 +43,11 @@ Major new features:
   fminimum_mag, fminimum_mag_num and corresponding functions for float,
   long double, _FloatN and _FloatNx.
 
+* <math.h> macros for single-precision float constants are added as a
+  GNU extension: M_Ef, M_LOG2Ef, M_LOG10Ef, M_LN2f, M_LN10f, M_PIf,
+  M_PI_2f, M_PI_4f, M_1_PIf, M_2_PIf, M_2_SQRTPIf, M_SQRT2f and
+  M_SQRT1_2f.
+
 * The __STDC_IEC_60559_BFP__ and __STDC_IEC_60559_COMPLEX__ macros are
   predefined as specified in TS 18661-1:2014.
 
index 376306cfc55efda9e85a30c4b5cefa50ea7cdc92..e00871c303940815a1f0be165e43380e17070dfe 100644 (file)
@@ -131,9 +131,10 @@ defined.  The default set of features includes these constants.
 @xref{Feature Test Macros}.
 
 All values are of type @code{double}.  As an extension, @theglibc{}
-also defines these constants with type @code{long double}.  The
-@code{long double} macros have a lowercase @samp{l} appended to their
-names: @code{M_El}, @code{M_PIl}, and so forth.  These are only
+also defines these constants with type @code{long double} and
+@code{float}.  The @code{long double} macros have a lowercase @samp{l}
+while the @code{float} macros have a lowercase @samp{f} appended to
+their names: @code{M_El}, @code{M_PIl}, and so forth.  These are only
 available if @code{_GNU_SOURCE} is defined.
 
 Likewise, @theglibc{} also defines these constants with the types
index 98980dc39390ed64fb05c64725e75d946f682ea4..e9fdc97f624eb9df7cd95719fb73a441aee2230b 100644 (file)
@@ -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)
     {
index 7bd56c96486db045d35b7bc1e7efcec056b91446..27963ef6dcd4dcd718eccaca9844829dff03eab6 100644 (file)
@@ -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.  */
index 644330f0f8222cd090f96b666f8c7ee632640e50..eaf7c80aded692519a29fbf9dd08ec90001198ae 100644 (file)
@@ -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;
index be3459cb4f84ea9698f817c91d66400e85b1938c..6a893095cf7330cf773dc3d894cfef1a222acdff 100644 (file)
@@ -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
            {
index 59c8e36d211a47021ada2a5549316fadfd88faa2..7b57dda0459dda89e574885def22d7387d8e4aef 100644 (file)
@@ -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
            {
index f5a0873ad38831a4c81ad8166f60f8ee3f79a87f..e50bd6ccf9e98f9f8f0d7621735cde9f93e9a4a1 100644 (file)
@@ -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
        {
index 3b25bc9021dfa13474268a6160fcbfef67bcc8fb..eff3d48796b8eaac436ae32b5d88abe7836cd8e6 100644 (file)
@@ -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);
index 5ee0ec3d026bf415b6be5dab2d2a465d14bf471c..3299abccf522166788c08dace9a1b35823dd68b6 100644 (file)
@@ -27,9 +27,8 @@
 #define M_STRTO_NAN __strtof_nan
 #define M_USE_BUILTIN(c) USE_ ##c ##F_BUILTIN
 
-/* Standard/GNU macro literals do not exist for the float type.  Use
-   the double macro constants.  */
-#define M_MLIT(c) c
+/* GNU extension float constant macros.  */
+#define M_MLIT(c) c ## f
 
 #include <libm-alias-float.h>
 #include <math-nan-payload-float.h>
index 782beaa144b0440faa41c2f7b75138cafcf6d729..3db84634077456d315be48924b361ff1932da276 100644 (file)
@@ -85,7 +85,7 @@ gammaf_positive (float x, int *exp2_adj)
       float x_adj_frac = x_adj - x_adj_int;
       int x_adj_log2;
       float x_adj_mant = __frexpf (x_adj, &x_adj_log2);
-      if (x_adj_mant < (float) M_SQRT1_2)
+      if (x_adj_mant < M_SQRT1_2f)
        {
          x_adj_log2--;
          x_adj_mant *= 2.0f;
@@ -94,7 +94,7 @@ gammaf_positive (float x, int *exp2_adj)
       float ret = (__ieee754_powf (x_adj_mant, x_adj)
                   * __ieee754_exp2f (x_adj_log2 * x_adj_frac)
                   * __ieee754_expf (-x_adj)
-                  * sqrtf (2 * (float) M_PI / x_adj)
+                  * sqrtf (2 * M_PIf / x_adj)
                   / prod);
       exp_adj += x_eps * __ieee754_logf (x_adj);
       float bsum = gamma_coeff[NCOEFF - 1];
@@ -176,11 +176,11 @@ __ieee754_gammaf_r (float x, int *signgamp)
              if (frac > 0.5f)
                frac = 1.0f - frac;
              float sinpix = (frac <= 0.25f
-                             ? __sinf ((float) M_PI * frac)
-                             : __cosf ((float) M_PI * (0.5f - frac)));
+                             ? __sinf (M_PIf * frac)
+                             : __cosf (M_PIf * (0.5f - frac)));
              int exp2_adj;
-             float tret = (float) M_PI / (-x * sinpix
-                                          * gammaf_positive (-x, &exp2_adj));
+             float tret = M_PIf / (-x * sinpix
+                                   * gammaf_positive (-x, &exp2_adj));
              ret = __scalbnf (tret, -exp2_adj);
              math_check_force_underflow_nonneg (ret);
            }
index 0453a301097f42588259c758209a5572e0c022e8..9ae91a9dccc07f33951c518f97611d03c5a6d387 100644 (file)
@@ -233,7 +233,7 @@ j0f_near_root (float x, float z)
   float index_f;
   int index;
 
-  index_f = roundf ((x - FIRST_ZERO_J0) / (float) M_PI);
+  index_f = roundf ((x - FIRST_ZERO_J0) / M_PIf);
   /* j0f_asympt fails to give an error <= 9 ulps for x=0x1.324e92p+7
      (index 48) thus we can't reduce SMALL_SIZE below 49.  */
   if (index_f >= SMALL_SIZE)
@@ -514,7 +514,7 @@ y0f_near_root (float x, float z)
   float index_f;
   int index;
 
-  index_f = roundf ((x - FIRST_ZERO_Y0) / (float) M_PI);
+  index_f = roundf ((x - FIRST_ZERO_Y0) / M_PIf);
   if (index_f >= SMALL_SIZE)
     return y0f_asympt (x);
   index = (int) index_f;
index 052c661db9966adbddc0dbacb00d48ae78825d0f..ade10402fdb64aaf100d781bfd0c3a7e9d19d501 100644 (file)
@@ -243,7 +243,7 @@ j1f_near_root (float x, float z)
       x = -x;
       sign = -1.0f;
     }
-  index_f = roundf ((x - FIRST_ZERO_J1) / (float) M_PI);
+  index_f = roundf ((x - FIRST_ZERO_J1) / M_PIf);
   if (index_f >= SMALL_SIZE)
     return sign * j1f_asympt (x);
   index = (int) index_f;
@@ -525,7 +525,7 @@ y1f_near_root (float x, float z)
   float index_f;
   int index;
 
-  index_f = roundf ((x - FIRST_ZERO_Y1) / (float) M_PI);
+  index_f = roundf ((x - FIRST_ZERO_Y1) / M_PIf);
   if (index_f >= SMALL_SIZE)
     return y1f_asympt (x);
   index = (int) index_f;
index 80b684dd97e1e0ba3f97da3b5c7f07b3d4506d52..ff0a9d7c6349ca20b94debaacbc11996a470969e 100644 (file)
@@ -134,7 +134,7 @@ __ieee754_jnf(int n, float x)
                tmp = n;
                v = two/x;
                tmp = tmp*__ieee754_logf(fabsf(v*tmp));
-               if(tmp<(float)8.8721679688e+01) {
+               if(tmp<8.8721679688e+01f) {
                    for(i=n-1,di=(float)(i+i);i>0;i--){
                        temp = b;
                        b *= di;
index 756742edd8b7f3f2689ce51718b5ebb75cb0230a..541b5a3ab123731f2026fb33af48c8d559acfe14 100644 (file)
@@ -165,9 +165,9 @@ static float
 lg_sinpi (float x)
 {
   if (x <= 0.25f)
-    return __sinf ((float) M_PI * x);
+    return __sinf (M_PIf * x);
   else
-    return __cosf ((float) M_PI * (0.5f - x));
+    return __cosf (M_PIf * (0.5f - x));
 }
 
 /* Compute cos (pi * X) for -0.25 <= X <= 0.5.  */
@@ -176,9 +176,9 @@ static float
 lg_cospi (float x)
 {
   if (x <= 0.25f)
-    return __cosf ((float) M_PI * x);
+    return __cosf (M_PIf * x);
   else
-    return __sinf ((float) M_PI * (0.5f - x));
+    return __sinf (M_PIf * (0.5f - x));
 }
 
 /* Compute cot (pi * X) for -0.25 <= X <= 0.5.  */
index 6630f5a78e6e8272fa5f1e62ed609ef335ed8524..555f0f82c8ff9d5da129ada01bbc1624bbbc4dda 100644 (file)
@@ -92,7 +92,7 @@ __log1pf(float x)
                if(k==0) return zero;
                else {c += k*ln2_lo; return k*ln2_hi+c;}
            }
-           R = hfsq*((float)1.0-(float)0.66666666666666666*f);
+           R = hfsq*(1.0f-0.66666666666666666f*f);
            if(k==0) return f-R; else
                     return k*ln2_hi-((R-(k*ln2_lo+c))-f);
        }