[libc-commits] [libc] 7d11a59 - [libc] Fix some math conversion warnings

Alex Brachet via libc-commits libc-commits at lists.llvm.org
Thu Mar 23 10:08:01 PDT 2023


Author: Alex Brachet
Date: 2023-03-23T17:07:19Z
New Revision: 7d11a592c5adc286bf1845c20b20965d5e999039

URL: https://github.com/llvm/llvm-project/commit/7d11a592c5adc286bf1845c20b20965d5e999039
DIFF: https://github.com/llvm/llvm-project/commit/7d11a592c5adc286bf1845c20b20965d5e999039.diff

LOG: [libc] Fix some math conversion warnings

Differential Revision: https://reviews.llvm.org/D146738

Added: 
    

Modified: 
    libc/src/__support/FPUtil/ManipulationFunctions.h
    libc/src/__support/FPUtil/NearestIntegerOperations.h
    libc/src/math/generic/acosf.cpp
    libc/src/math/generic/acoshf.cpp
    libc/src/math/generic/asinf.cpp
    libc/src/math/generic/asinhf.cpp
    libc/src/math/generic/atanf.cpp
    libc/src/math/generic/atanhf.cpp
    libc/src/math/generic/cosf.cpp
    libc/src/math/generic/coshf.cpp
    libc/src/math/generic/exp10f.cpp
    libc/src/math/generic/exp2f.cpp
    libc/src/math/generic/log10.cpp
    libc/src/math/generic/log1pf.cpp
    libc/src/math/generic/sincosf.cpp
    libc/src/math/generic/sinf.cpp
    libc/src/math/generic/sinhf.cpp
    libc/src/math/generic/tanf.cpp
    libc/src/math/generic/tanhf.cpp

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/FPUtil/ManipulationFunctions.h b/libc/src/__support/FPUtil/ManipulationFunctions.h
index 27d91c433ac5b..14055ab74dce0 100644
--- a/libc/src/__support/FPUtil/ManipulationFunctions.h
+++ b/libc/src/__support/FPUtil/ManipulationFunctions.h
@@ -112,7 +112,7 @@ LIBC_INLINE T logb(T x) {
   }
 
   NormalFloat<T> normal(bits);
-  return normal.exponent;
+  return static_cast<T>(normal.exponent);
 }
 
 template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>

diff  --git a/libc/src/__support/FPUtil/NearestIntegerOperations.h b/libc/src/__support/FPUtil/NearestIntegerOperations.h
index 8265ea1cbb3e5..06aa9484c3f70 100644
--- a/libc/src/__support/FPUtil/NearestIntegerOperations.h
+++ b/libc/src/__support/FPUtil/NearestIntegerOperations.h
@@ -261,9 +261,9 @@ LIBC_INLINE I rounded_float_to_signed_integer(F x) {
   }
 
   // For all other cases, if `x` can fit in the integer type `I`,
-  // we just return `x`. Implicit conversion will convert the
-  // floating point value to the exact integer value.
-  return x;
+  // we just return `x`. static_cast will convert the floating
+  // point value to the exact integer value.
+  return static_cast<I>(x);
 }
 
 } // namespace internal

diff  --git a/libc/src/math/generic/acosf.cpp b/libc/src/math/generic/acosf.cpp
index 5835dfa617056..41152e06ff1f5 100644
--- a/libc/src/math/generic/acosf.cpp
+++ b/libc/src/math/generic/acosf.cpp
@@ -56,8 +56,8 @@ LLVM_LIBC_FUNCTION(float, acosf, (float x)) {
         return r.value();
 
       double xd = static_cast<double>(x);
-      return fputil::multiply_add(-0x1.5555555555555p-3 * xd, xd * xd,
-                                  M_MATH_PI_2 - xd);
+      return static_cast<float>(fputil::multiply_add(
+          -0x1.5555555555555p-3 * xd, xd * xd, M_MATH_PI_2 - xd));
     }
 
     // For |x| <= 0.5, we approximate acosf(x) by:
@@ -70,7 +70,7 @@ LLVM_LIBC_FUNCTION(float, acosf, (float x)) {
     double xsq = xd * xd;
     double x3 = xd * xsq;
     double r = asin_eval(xsq);
-    return fputil::multiply_add(-x3, r, M_MATH_PI_2 - xd);
+    return static_cast<float>(fputil::multiply_add(-x3, r, M_MATH_PI_2 - xd));
   }
 
   // |x| > 1, return NaNs.
@@ -111,7 +111,7 @@ LLVM_LIBC_FUNCTION(float, acosf, (float x)) {
 
   double r3 = asin_eval(u);
   double r = fputil::multiply_add(cv * u, r3, cv);
-  return x_sign ? M_MATH_PI - r : r;
+  return static_cast<float>(x_sign ? M_MATH_PI - r : r);
 }
 
 } // namespace __llvm_libc

diff  --git a/libc/src/math/generic/acoshf.cpp b/libc/src/math/generic/acoshf.cpp
index ac225fe5a808f..f8e5a90a4d056 100644
--- a/libc/src/math/generic/acoshf.cpp
+++ b/libc/src/math/generic/acoshf.cpp
@@ -68,7 +68,8 @@ LLVM_LIBC_FUNCTION(float, acoshf, (float x)) {
 
   double x_d = static_cast<double>(x);
   // acosh(x) = log(x + sqrt(x^2 - 1))
-  return log_eval(x_d + fputil::sqrt(fputil::multiply_add(x_d, x_d, -1.0)));
+  return static_cast<float>(
+      log_eval(x_d + fputil::sqrt(fputil::multiply_add(x_d, x_d, -1.0))));
 }
 
 } // namespace __llvm_libc

diff  --git a/libc/src/math/generic/asinf.cpp b/libc/src/math/generic/asinf.cpp
index c24697cb14727..9b724d3296c84 100644
--- a/libc/src/math/generic/asinf.cpp
+++ b/libc/src/math/generic/asinf.cpp
@@ -99,7 +99,7 @@ LLVM_LIBC_FUNCTION(float, asinf, (float x)) {
     double xsq = xd * xd;
     double x3 = xd * xsq;
     double r = asin_eval(xsq);
-    return fputil::multiply_add(x3, r, xd);
+    return static_cast<float>(fputil::multiply_add(x3, r, xd));
   }
 
   // |x| > 1, return NaNs.
@@ -149,7 +149,7 @@ LLVM_LIBC_FUNCTION(float, asinf, (float x)) {
   double c3 = c1 * u;
 
   double r = asin_eval(u);
-  return fputil::multiply_add(c3, r, c2);
+  return static_cast<float>(fputil::multiply_add(c3, r, c2));
 }
 
 } // namespace __llvm_libc

diff  --git a/libc/src/math/generic/asinhf.cpp b/libc/src/math/generic/asinhf.cpp
index 7063387313ab8..91ecf45667bfc 100644
--- a/libc/src/math/generic/asinhf.cpp
+++ b/libc/src/math/generic/asinhf.cpp
@@ -27,8 +27,9 @@ LLVM_LIBC_FUNCTION(float, asinhf, (float x)) {
   if (LIBC_UNLIKELY(x_abs <= 0x3e80'0000U)) {
     // |x| <= 2^-26
     if (LIBC_UNLIKELY(x_abs <= 0x3280'0000U)) {
-      return LIBC_UNLIKELY(x_abs == 0) ? x
-                                       : (x - 0x1.5555555555555p-3 * x * x * x);
+      return static_cast<float>(LIBC_UNLIKELY(x_abs == 0)
+                                    ? x
+                                    : (x - 0x1.5555555555555p-3 * x * x * x));
     }
 
     double x_d = x;
@@ -40,7 +41,7 @@ LLVM_LIBC_FUNCTION(float, asinhf, (float x)) {
         x_sq, 0.0, -0x1.555555555551ep-3, 0x1.3333333325495p-4,
         -0x1.6db6db5a7622bp-5, 0x1.f1c70f82928c6p-6, -0x1.6e893934266b7p-6,
         0x1.1c0b41d3fbe78p-6, -0x1.c0f47810b3c4fp-7, 0x1.2c8602690143dp-7);
-    return fputil::multiply_add(x_d, p, x_d);
+    return static_cast<float>(fputil::multiply_add(x_d, p, x_d));
   }
 
   const double SIGN[2] = {1.0, -1.0};
@@ -97,9 +98,10 @@ LLVM_LIBC_FUNCTION(float, asinhf, (float x)) {
   }
 
   // asinh(x) = log(x + sqrt(x^2 + 1))
-  return x_sign *
-         log_eval(fputil::multiply_add(
-             x_d, x_sign, fputil::sqrt(fputil::multiply_add(x_d, x_d, 1.0))));
+  return static_cast<float>(
+      x_sign *
+      log_eval(fputil::multiply_add(
+          x_d, x_sign, fputil::sqrt(fputil::multiply_add(x_d, x_d, 1.0)))));
 }
 
 } // namespace __llvm_libc

diff  --git a/libc/src/math/generic/atanf.cpp b/libc/src/math/generic/atanf.cpp
index ff5d4507afa3d..ed7847adb15a1 100644
--- a/libc/src/math/generic/atanf.cpp
+++ b/libc/src/math/generic/atanf.cpp
@@ -22,7 +22,7 @@ LLVM_LIBC_FUNCTION(float, atanf, (float x)) {
 
   if (LIBC_UNLIKELY(xbits.is_inf_or_nan())) {
     if (xbits.is_inf())
-      return opt_barrier(sign ? -M_MATH_PI_2 : M_MATH_PI_2);
+      return static_cast<float>(opt_barrier(sign ? -M_MATH_PI_2 : M_MATH_PI_2));
     else
       return x;
   }
@@ -52,7 +52,7 @@ LLVM_LIBC_FUNCTION(float, atanf, (float x)) {
     }
   }
 
-  return atan_eval(x);
+  return static_cast<float>(atan_eval(x));
 }
 
 } // namespace __llvm_libc

diff  --git a/libc/src/math/generic/atanhf.cpp b/libc/src/math/generic/atanhf.cpp
index b0c92fa8de87d..0a4512f7622da 100644
--- a/libc/src/math/generic/atanhf.cpp
+++ b/libc/src/math/generic/atanhf.cpp
@@ -40,8 +40,9 @@ LLVM_LIBC_FUNCTION(float, atanhf, (float x)) {
   if (LIBC_UNLIKELY(x_abs <= 0x3dcc'0000U)) {
     // |x| <= 2^-26
     if (LIBC_UNLIKELY(x_abs <= 0x3280'0000U)) {
-      return LIBC_UNLIKELY(x_abs == 0) ? x
-                                       : (x + 0x1.5555555555555p-2 * x * x * x);
+      return static_cast<float>(LIBC_UNLIKELY(x_abs == 0)
+                                    ? x
+                                    : (x + 0x1.5555555555555p-2 * x * x * x));
     }
 
     double xdbl = x;
@@ -50,10 +51,10 @@ LLVM_LIBC_FUNCTION(float, atanhf, (float x)) {
     double pe = fputil::polyeval(x2, 0.0, 0x1.5555555555555p-2,
                                  0x1.999999999999ap-3, 0x1.2492492492492p-3,
                                  0x1.c71c71c71c71cp-4, 0x1.745d1745d1746p-4);
-    return fputil::multiply_add(xdbl, pe, xdbl);
+    return static_cast<float>(fputil::multiply_add(xdbl, pe, xdbl));
   }
   double xdbl = x;
-  return 0.5 * log_eval((xdbl + 1.0) / (xdbl - 1.0));
+  return static_cast<float>(0.5 * log_eval((xdbl + 1.0) / (xdbl - 1.0)));
 }
 
 } // namespace __llvm_libc

diff  --git a/libc/src/math/generic/cosf.cpp b/libc/src/math/generic/cosf.cpp
index ef94804bda60d..2e4ca3c4133ff 100644
--- a/libc/src/math/generic/cosf.cpp
+++ b/libc/src/math/generic/cosf.cpp
@@ -129,8 +129,8 @@ LLVM_LIBC_FUNCTION(float, cosf, (float x)) {
 
   sincosf_eval(xd, x_abs, sin_k, cos_k, sin_y, cosm1_y);
 
-  return fputil::multiply_add(sin_y, -sin_k,
-                              fputil::multiply_add(cosm1_y, cos_k, cos_k));
+  return static_cast<float>(fputil::multiply_add(
+      sin_y, -sin_k, fputil::multiply_add(cosm1_y, cos_k, cos_k)));
 }
 
 } // namespace __llvm_libc

diff  --git a/libc/src/math/generic/coshf.cpp b/libc/src/math/generic/coshf.cpp
index 1ce1bc300d46b..1cf789a10a8ca 100644
--- a/libc/src/math/generic/coshf.cpp
+++ b/libc/src/math/generic/coshf.cpp
@@ -47,7 +47,7 @@ LLVM_LIBC_FUNCTION(float, coshf, (float x)) {
   // but not too small inputs, such as |x| < 2^-2, or |x| < 2^-3.
 
   // cosh(x) = (e^x + e^(-x)) / 2.
-  return exp_pm_eval</*is_sinh*/ false>(x);
+  return static_cast<float>(exp_pm_eval</*is_sinh*/ false>(x));
 }
 
 } // namespace __llvm_libc

diff  --git a/libc/src/math/generic/exp10f.cpp b/libc/src/math/generic/exp10f.cpp
index 06509a97fe032..9d07f2c5261ee 100644
--- a/libc/src/math/generic/exp10f.cpp
+++ b/libc/src/math/generic/exp10f.cpp
@@ -72,7 +72,7 @@ LLVM_LIBC_FUNCTION(float, exp10f, (float x)) {
       return fputil::multiply_add(x, 0x1.26bb1cp+1f, 1.0f);
     }
 
-    return Exp10Base::powb_lo(x);
+    return static_cast<float>(Exp10Base::powb_lo(x));
   }
 
   // Exceptional value.
@@ -129,7 +129,7 @@ LLVM_LIBC_FUNCTION(float, exp10f, (float x)) {
   // 10^x = 2^(mid + hi) * 10^lo
   //      ~ mh * (c0 + p * lo^2)
   //      = (mh * c0) + p * (mh * lo^2)
-  return multiply_add(p, lo2 * rr.mh, c0 * rr.mh);
+  return static_cast<float>(multiply_add(p, lo2 * rr.mh, c0 * rr.mh));
 }
 
 } // namespace __llvm_libc

diff  --git a/libc/src/math/generic/exp2f.cpp b/libc/src/math/generic/exp2f.cpp
index 3c319d288e1c9..15f35d0da82f5 100644
--- a/libc/src/math/generic/exp2f.cpp
+++ b/libc/src/math/generic/exp2f.cpp
@@ -128,7 +128,7 @@ LLVM_LIBC_FUNCTION(float, exp2f, (float x)) {
   //     = 2^(hi + mid) * 2^lo
   //     ~ mh * (1 + lo * P(lo))
   //     = mh + (mh*lo) * P(lo)
-  return fputil::multiply_add(p, dx_sq * mh, c1 * mh);
+  return static_cast<float>(fputil::multiply_add(p, dx_sq * mh, c1 * mh));
 }
 
 } // namespace __llvm_libc

diff  --git a/libc/src/math/generic/log10.cpp b/libc/src/math/generic/log10.cpp
index b2dd29f5f74d1..47569b4758a4b 100644
--- a/libc/src/math/generic/log10.cpp
+++ b/libc/src/math/generic/log10.cpp
@@ -978,7 +978,7 @@ LLVM_LIBC_FUNCTION(double, log10, (double x)) {
   //   |R * x_m - 1| < C
   uint64_t x_u = xbits.uintval();
   int shifted = x_u >> 45;
-  size_t index = shifted & 0x7F;
+  int index = shifted & 0x7F;
   double r = R[index];
 
   x_e += (x_u >> 52) & 0x7FF;

diff  --git a/libc/src/math/generic/log1pf.cpp b/libc/src/math/generic/log1pf.cpp
index bf6a91a5fc466..a7ca54887d59a 100644
--- a/libc/src/math/generic/log1pf.cpp
+++ b/libc/src/math/generic/log1pf.cpp
@@ -150,7 +150,7 @@ LLVM_LIBC_FUNCTION(float, log1pf, (float x)) {
   double c2 = fputil::multiply_add(xd, COEFFS[5], COEFFS[4]);
   double r = fputil::polyeval(xsq, xd, c0, c1, c2, COEFFS[6]);
 
-  return r;
+  return static_cast<float>(r);
 }
 
 } // namespace __llvm_libc

diff  --git a/libc/src/math/generic/sincosf.cpp b/libc/src/math/generic/sincosf.cpp
index 8448945a71d5d..277126bdc89db 100644
--- a/libc/src/math/generic/sincosf.cpp
+++ b/libc/src/math/generic/sincosf.cpp
@@ -193,10 +193,10 @@ LLVM_LIBC_FUNCTION(void, sincosf, (float x, float *sinp, float *cosp)) {
 
   sincosf_eval(xd, x_abs, sin_k, cos_k, sin_y, cosm1_y);
 
-  *sinp = fputil::multiply_add(sin_y, cos_k,
-                               fputil::multiply_add(cosm1_y, sin_k, sin_k));
-  *cosp = fputil::multiply_add(sin_y, -sin_k,
-                               fputil::multiply_add(cosm1_y, cos_k, cos_k));
+  *sinp = static_cast<float>(fputil::multiply_add(
+      sin_y, cos_k, fputil::multiply_add(cosm1_y, sin_k, sin_k)));
+  *cosp = static_cast<float>(fputil::multiply_add(
+      sin_y, -sin_k, fputil::multiply_add(cosm1_y, cos_k, cos_k)));
 }
 
 } // namespace __llvm_libc

diff  --git a/libc/src/math/generic/sinf.cpp b/libc/src/math/generic/sinf.cpp
index 1641c44e9fc00..697c438c2c67b 100644
--- a/libc/src/math/generic/sinf.cpp
+++ b/libc/src/math/generic/sinf.cpp
@@ -121,7 +121,7 @@ LLVM_LIBC_FUNCTION(float, sinf, (float x)) {
     double result =
         fputil::polyeval(xsq, 1.0, -0x1.55555555554c6p-3, 0x1.1111111085e65p-7,
                          -0x1.a019f70fb4d4fp-13, 0x1.718d179815e74p-19);
-    return xd * result;
+    return static_cast<float>(xd * result);
   }
 
   if (LIBC_UNLIKELY(x_abs == 0x4619'9998U)) { // x = 0x1.33333p13
@@ -150,8 +150,8 @@ LLVM_LIBC_FUNCTION(float, sinf, (float x)) {
 
   sincosf_eval(xd, x_abs, sin_k, cos_k, sin_y, cosm1_y);
 
-  return fputil::multiply_add(sin_y, cos_k,
-                              fputil::multiply_add(cosm1_y, sin_k, sin_k));
+  return static_cast<float>(fputil::multiply_add(
+      sin_y, cos_k, fputil::multiply_add(cosm1_y, sin_k, sin_k)));
 }
 
 } // namespace __llvm_libc

diff  --git a/libc/src/math/generic/sinhf.cpp b/libc/src/math/generic/sinhf.cpp
index cc3811deeb9de..92edd4ea6a98f 100644
--- a/libc/src/math/generic/sinhf.cpp
+++ b/libc/src/math/generic/sinhf.cpp
@@ -21,7 +21,8 @@ LLVM_LIBC_FUNCTION(float, sinhf, (float x)) {
 
   // |x| <= 2^-26
   if (LIBC_UNLIKELY(x_abs <= 0x3280'0000U)) {
-    return LIBC_UNLIKELY(x_abs == 0) ? x : (x + 0.25 * x * x * x);
+    return static_cast<float>(
+        LIBC_UNLIKELY(x_abs == 0) ? x : (x + 0.25 * x * x * x));
   }
 
   // When |x| >= 90, or x is inf or nan
@@ -65,11 +66,11 @@ LLVM_LIBC_FUNCTION(float, sinhf, (float x)) {
     // Therefore, output of Sollya = x * pe;
     double pe = fputil::polyeval(x2, 0.0, 0x1.5555555556583p-3,
                                  0x1.111110d239f1fp-7, 0x1.a02b5a284013cp-13);
-    return fputil::multiply_add(xdbl, pe, xdbl);
+    return static_cast<float>(fputil::multiply_add(xdbl, pe, xdbl));
   }
 
   // sinh(x) = (e^x - e^(-x)) / 2.
-  return exp_pm_eval</*is_sinh*/ true>(x);
+  return static_cast<float>(exp_pm_eval</*is_sinh*/ true>(x));
 }
 
 } // namespace __llvm_libc

diff  --git a/libc/src/math/generic/tanf.cpp b/libc/src/math/generic/tanf.cpp
index 217664f8b2acb..681f16177fde5 100644
--- a/libc/src/math/generic/tanf.cpp
+++ b/libc/src/math/generic/tanf.cpp
@@ -90,7 +90,7 @@ LLVM_LIBC_FUNCTION(float, tanf, (float x)) {
     double result =
         fputil::polyeval(xsq, 1.0, 0x1.555555553d022p-2, 0x1.111111ce442c1p-3,
                          0x1.ba180a6bbdecdp-5, 0x1.69c0a88a0b71fp-6);
-    return xd * result;
+    return static_cast<float>(xd * result);
   }
 
   // Check for exceptional values
@@ -134,8 +134,9 @@ LLVM_LIBC_FUNCTION(float, tanf, (float x)) {
   // tan(x) = sin(x) / cos(x)
   //        = (sin_y * cos_k + cos_y * sin_k) / (cos_y * cos_k - sin_y * sin_k)
   using fputil::multiply_add;
-  return multiply_add(sin_y, cos_k, multiply_add(cosm1_y, sin_k, sin_k)) /
-         multiply_add(sin_y, -sin_k, multiply_add(cosm1_y, cos_k, cos_k));
+  return static_cast<float>(
+      multiply_add(sin_y, cos_k, multiply_add(cosm1_y, sin_k, sin_k)) /
+      multiply_add(sin_y, -sin_k, multiply_add(cosm1_y, cos_k, cos_k)));
 }
 
 } // namespace __llvm_libc

diff  --git a/libc/src/math/generic/tanhf.cpp b/libc/src/math/generic/tanhf.cpp
index e67eadf3ce4b7..3b8506f809c3b 100644
--- a/libc/src/math/generic/tanhf.cpp
+++ b/libc/src/math/generic/tanhf.cpp
@@ -22,8 +22,8 @@ LLVM_LIBC_FUNCTION(float, tanhf, (float x)) {
 
   // |x| <= 2^-26
   if (LIBC_UNLIKELY(x_abs <= 0x3280'0000U)) {
-    return LIBC_UNLIKELY(x_abs == 0) ? x
-                                     : (x - 0x1.5555555555555p-2 * x * x * x);
+    return static_cast<float>(
+        LIBC_UNLIKELY(x_abs == 0) ? x : (x - 0x1.5555555555555p-2 * x * x * x));
   }
 
   // When |x| >= 15, or x is inf or nan
@@ -48,7 +48,7 @@ LLVM_LIBC_FUNCTION(float, tanhf, (float x)) {
     double pe = fputil::polyeval(x2, 0.0, -0x1.5555555555555p-2,
                                  0x1.1111111111111p-3, -0x1.ba1ba1ba1ba1cp-5,
                                  0x1.664f4882c10fap-6, -0x1.226e355e6c23dp-7);
-    return fputil::multiply_add(xdbl, pe, xdbl);
+    return static_cast<float>(fputil::multiply_add(xdbl, pe, xdbl));
   }
 
   if (LIBC_UNLIKELY(xbits.bits == 0x4058'e0a3U)) {
@@ -65,7 +65,7 @@ LLVM_LIBC_FUNCTION(float, tanhf, (float x)) {
          fputil::multiply_add(ep.mh, r, 1.0);
 #else
   double exp_x = ep.mh * r;
-  return (exp_x - 1.0) / (exp_x + 1.0);
+  return static_cast<float>((exp_x - 1.0) / (exp_x + 1.0));
 #endif // LIBC_TARGET_CPU_HAS_FMA
 }
 


        


More information about the libc-commits mailing list