[libc-commits] [libc] [libc][NFC] Clean up conversion warnings in math function implementations. (PR #74697)

via libc-commits libc-commits at lists.llvm.org
Wed Dec 6 21:15:07 PST 2023


https://github.com/lntue created https://github.com/llvm/llvm-project/pull/74697

None

>From c87c3949094ba63ae9b79298d847d894ceb452df Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue at google.com>
Date: Thu, 7 Dec 2023 00:13:51 -0500
Subject: [PATCH] [libc][NFC] Clean up conversion warnings in math function
 implementations.

---
 libc/src/__support/FPUtil/Hypot.h        | 2 +-
 libc/src/__support/FPUtil/dyadic_float.h | 4 ++--
 libc/src/math/generic/explogxf.h         | 3 ++-
 libc/src/math/generic/log.cpp            | 2 +-
 libc/src/math/generic/log10.cpp          | 2 +-
 libc/src/math/generic/log1p.cpp          | 5 +++--
 libc/src/math/generic/log1pf.cpp         | 4 ++--
 libc/src/math/generic/log2.cpp           | 2 +-
 8 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/libc/src/__support/FPUtil/Hypot.h b/libc/src/__support/FPUtil/Hypot.h
index 8a6eb4b920acd..db068ba77c186 100644
--- a/libc/src/__support/FPUtil/Hypot.h
+++ b/libc/src/__support/FPUtil/Hypot.h
@@ -178,7 +178,7 @@ LIBC_INLINE T hypot(T x, T y) {
   // But before that, remember to store the losing bits to sticky.
   // The shift length is for a^2 and b^2, so it's double of the exponent
   // difference between a and b.
-  uint16_t shift_length = 2 * (a_exp - b_exp);
+  uint16_t shift_length = uint16_t{2} * (a_exp - b_exp);
   sticky_bits =
       ((b_mant_sq & ((DUIntType(1) << shift_length) - DUIntType(1))) !=
        DUIntType(0));
diff --git a/libc/src/__support/FPUtil/dyadic_float.h b/libc/src/__support/FPUtil/dyadic_float.h
index b7920943804e6..06c30911c2427 100644
--- a/libc/src/__support/FPUtil/dyadic_float.h
+++ b/libc/src/__support/FPUtil/dyadic_float.h
@@ -110,12 +110,12 @@ template <size_t Bits> struct DyadicFloat {
       exp_hi = FloatProperties<T>::EXPONENT_BIAS;
     }
 
-    int exp_lo = exp_hi - PRECISION - 1;
+    int exp_lo = exp_hi - static_cast<int>(PRECISION) - 1;
 
     MantissaType m_hi(mantissa >> shift);
 
     T d_hi = FPBits<T>::create_value(sign, exp_hi,
-                                     output_bits_t(m_hi) &
+                                     static_cast<output_bits_t>(m_hi) &
                                          FloatProperties<T>::MANTISSA_MASK)
                  .get_val();
 
diff --git a/libc/src/math/generic/explogxf.h b/libc/src/math/generic/explogxf.h
index 512785be2cb85..156c24c21e231 100644
--- a/libc/src/math/generic/explogxf.h
+++ b/libc/src/math/generic/explogxf.h
@@ -311,7 +311,8 @@ LIBC_INLINE static double log_eval(double x) {
 
   // p1 is the leading 7 bits of mx, i.e.
   // p1 * 2^(-7) <= m_x < (p1 + 1) * 2^(-7).
-  int p1 = (bs.get_mantissa() >> (FPB::FloatProp::MANTISSA_WIDTH - 7));
+  int p1 = static_cast<int>(bs.get_mantissa() >>
+                            (FPB::FloatProp::MANTISSA_WIDTH - 7));
 
   // Set bs to (1 + (mx - p1*2^(-7))
   bs.bits &= FPB::FloatProp::MANTISSA_MASK >> 7;
diff --git a/libc/src/math/generic/log.cpp b/libc/src/math/generic/log.cpp
index 46b64df689086..dfa41ad64578d 100644
--- a/libc/src/math/generic/log.cpp
+++ b/libc/src/math/generic/log.cpp
@@ -769,7 +769,7 @@ LLVM_LIBC_FUNCTION(double, log, (double x)) {
   // Range reduction for log(x_m):
   // For each x_m, we would like to find r such that:
   //   -2^-8 <= r * x_m - 1 < 2^-7
-  int shifted = x_u >> 45;
+  int shifted = static_cast<int>(x_u >> 45);
   int index = shifted & 0x7F;
   double r = RD[index];
 
diff --git a/libc/src/math/generic/log10.cpp b/libc/src/math/generic/log10.cpp
index 38789acc441e5..2a801c6e98429 100644
--- a/libc/src/math/generic/log10.cpp
+++ b/libc/src/math/generic/log10.cpp
@@ -770,7 +770,7 @@ LLVM_LIBC_FUNCTION(double, log10, (double x)) {
   // Range reduction for log10(x_m):
   // For each x_m, we would like to find r such that:
   //   -2^-8 <= r * x_m - 1 < 2^-7
-  int shifted = x_u >> 45;
+  int shifted = static_cast<int>(x_u >> 45);
   int index = shifted & 0x7F;
   double r = RD[index];
 
diff --git a/libc/src/math/generic/log1p.cpp b/libc/src/math/generic/log1p.cpp
index c6ee8d8f9bbfb..02299e271770a 100644
--- a/libc/src/math/generic/log1p.cpp
+++ b/libc/src/math/generic/log1p.cpp
@@ -949,8 +949,9 @@ LLVM_LIBC_FUNCTION(double, log1p, (double x)) {
   x_u = xhi_bits.uintval();
   // Range reduction:
   // Find k such that |x_hi - k * 2^-7| <= 2^-8.
-  int idx = ((x_u & MANTISSA_MASK) + (1ULL << (MANTISSA_WIDTH - 8))) >>
-            (MANTISSA_WIDTH - 7);
+  int idx = static_cast<int>(
+      ((x_u & MANTISSA_MASK) + (1ULL << (MANTISSA_WIDTH - 8))) >>
+      (MANTISSA_WIDTH - 7));
   int x_e = xhi_bits.get_exponent() + (idx >> 7);
   double e_x = static_cast<double>(x_e);
 
diff --git a/libc/src/math/generic/log1pf.cpp b/libc/src/math/generic/log1pf.cpp
index 5b4e7edcceb47..023387d8add00 100644
--- a/libc/src/math/generic/log1pf.cpp
+++ b/libc/src/math/generic/log1pf.cpp
@@ -56,8 +56,8 @@ LIBC_INLINE float log(double x) {
 
   // Get the 8 highest bits, use 7 bits (excluding the implicit hidden bit) for
   // lookup tables.
-  int f_index =
-      xbits.get_mantissa() >> 45; // fputil::MantissaWidth<double>::VALUE - 7
+  int f_index = static_cast<int>(
+      xbits.get_mantissa() >> 45); // fputil::MantissaWidth<double>::VALUE - 7
 
   // Set bits to 1.m
   xbits.set_unbiased_exponent(0x3FF);
diff --git a/libc/src/math/generic/log2.cpp b/libc/src/math/generic/log2.cpp
index d72b0931c14b8..2ceddf87dfd56 100644
--- a/libc/src/math/generic/log2.cpp
+++ b/libc/src/math/generic/log2.cpp
@@ -890,7 +890,7 @@ LLVM_LIBC_FUNCTION(double, log2, (double x)) {
   // Range reduction for log2(x_m):
   // For each x_m, we would like to find r such that:
   //   -2^-8 <= r * x_m - 1 < 2^-7
-  int shifted = x_u >> 45;
+  int shifted = static_cast<int>(x_u >> 45);
   int index = shifted & 0x7F;
   double r = RD[index];
 



More information about the libc-commits mailing list