From 7d11a592c5adc286bf1845c20b20965d5e999039 Mon Sep 17 00:00:00 2001 From: Alex Brachet Date: Thu, 23 Mar 2023 17:07:19 +0000 Subject: [PATCH] [libc] Fix some math conversion warnings Differential Revision: https://reviews.llvm.org/D146738 --- libc/src/__support/FPUtil/ManipulationFunctions.h | 2 +- libc/src/__support/FPUtil/NearestIntegerOperations.h | 6 +++--- libc/src/math/generic/acosf.cpp | 8 ++++---- libc/src/math/generic/acoshf.cpp | 3 ++- libc/src/math/generic/asinf.cpp | 4 ++-- libc/src/math/generic/asinhf.cpp | 14 ++++++++------ libc/src/math/generic/atanf.cpp | 4 ++-- libc/src/math/generic/atanhf.cpp | 9 +++++---- libc/src/math/generic/cosf.cpp | 4 ++-- libc/src/math/generic/coshf.cpp | 2 +- libc/src/math/generic/exp10f.cpp | 4 ++-- libc/src/math/generic/exp2f.cpp | 2 +- libc/src/math/generic/log10.cpp | 2 +- libc/src/math/generic/log1pf.cpp | 2 +- libc/src/math/generic/sincosf.cpp | 8 ++++---- libc/src/math/generic/sinf.cpp | 6 +++--- libc/src/math/generic/sinhf.cpp | 7 ++++--- libc/src/math/generic/tanf.cpp | 7 ++++--- libc/src/math/generic/tanhf.cpp | 8 ++++---- 19 files changed, 54 insertions(+), 48 deletions(-) diff --git a/libc/src/__support/FPUtil/ManipulationFunctions.h b/libc/src/__support/FPUtil/ManipulationFunctions.h index 27d91c4..14055ab 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 normal(bits); - return normal.exponent; + return static_cast(normal.exponent); } template , int> = 0> diff --git a/libc/src/__support/FPUtil/NearestIntegerOperations.h b/libc/src/__support/FPUtil/NearestIntegerOperations.h index 8265ea1..06aa948 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(x); } } // namespace internal diff --git a/libc/src/math/generic/acosf.cpp b/libc/src/math/generic/acosf.cpp index 5835dfa..41152e0 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(x); - return fputil::multiply_add(-0x1.5555555555555p-3 * xd, xd * xd, - M_MATH_PI_2 - xd); + return static_cast(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(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(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 ac225fe..f8e5a90 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(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( + 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 c24697c..9b724d3 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(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(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 7063387..91ecf45 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(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(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( + 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 ff5d450..ed7847a 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(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(atan_eval(x)); } } // namespace __llvm_libc diff --git a/libc/src/math/generic/atanhf.cpp b/libc/src/math/generic/atanhf.cpp index b0c92fa..0a4512f 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(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(fputil::multiply_add(xdbl, pe, xdbl)); } double xdbl = x; - return 0.5 * log_eval((xdbl + 1.0) / (xdbl - 1.0)); + return static_cast(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 ef94804..2e4ca3c 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(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 1ce1bc3..1cf789a 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(x); + return static_cast(exp_pm_eval(x)); } } // namespace __llvm_libc diff --git a/libc/src/math/generic/exp10f.cpp b/libc/src/math/generic/exp10f.cpp index 06509a9..9d07f2c 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(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(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 3c319d2..15f35d0 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(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 b2dd29f..47569b4 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 bf6a91a..a7ca548 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(r); } } // namespace __llvm_libc diff --git a/libc/src/math/generic/sincosf.cpp b/libc/src/math/generic/sincosf.cpp index 8448945..277126b 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(fputil::multiply_add( + sin_y, cos_k, fputil::multiply_add(cosm1_y, sin_k, sin_k))); + *cosp = static_cast(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 1641c44..697c438 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(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(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 cc3811d..92edd4e 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( + 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(fputil::multiply_add(xdbl, pe, xdbl)); } // sinh(x) = (e^x - e^(-x)) / 2. - return exp_pm_eval(x); + return static_cast(exp_pm_eval(x)); } } // namespace __llvm_libc diff --git a/libc/src/math/generic/tanf.cpp b/libc/src/math/generic/tanf.cpp index 217664f..681f161 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(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( + 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 e67eadf..3b8506f 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( + 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(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((exp_x - 1.0) / (exp_x + 1.0)); #endif // LIBC_TARGET_CPU_HAS_FMA } -- 2.7.4