[llvm] ec116ea - [llvm] Deprecate llvm::count{Leading,Trailing}{Zeros,Ones} and llvm::countPopulation
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Sun Feb 12 12:11:57 PST 2023
Author: Kazu Hirata
Date: 2023-02-12T12:11:51-08:00
New Revision: ec116ea684b43aadfdda03cea2c2a86423e3fc27
URL: https://github.com/llvm/llvm-project/commit/ec116ea684b43aadfdda03cea2c2a86423e3fc27
DIFF: https://github.com/llvm/llvm-project/commit/ec116ea684b43aadfdda03cea2c2a86423e3fc27.diff
LOG: [llvm] Deprecate llvm::count{Leading,Trailing}{Zeros,Ones} and llvm::countPopulation
llvm/include/llvm/ADT/bit.h now has equivalent functions
forward-ported from C++20.
Differential Revision: https://reviews.llvm.org/D143837
Added:
Modified:
llvm/include/llvm/Support/MathExtras.h
llvm/unittests/Support/MathExtrasTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Support/MathExtras.h b/llvm/include/llvm/Support/MathExtras.h
index 44af0fde9ad90..69ce1f91a6109 100644
--- a/llvm/include/llvm/Support/MathExtras.h
+++ b/llvm/include/llvm/Support/MathExtras.h
@@ -66,7 +66,9 @@ constexpr float ef = 2.71828183F, // (0x1.5bf0a8P+1) https://oeis.org/A
/// Only unsigned integral types are allowed.
///
/// Returns std::numeric_limits<T>::digits on an input of 0.
-template <typename T> unsigned countTrailingZeros(T Val) {
+template <typename T>
+LLVM_DEPRECATED("Use llvm::countr_zero instead.", "llvm::countr_zero")
+unsigned countTrailingZeros(T Val) {
static_assert(std::is_unsigned_v<T>,
"Only unsigned integral types are allowed.");
return llvm::countr_zero(Val);
@@ -78,7 +80,9 @@ template <typename T> unsigned countTrailingZeros(T Val) {
/// Only unsigned integral types are allowed.
///
/// Returns std::numeric_limits<T>::digits on an input of 0.
-template <typename T> unsigned countLeadingZeros(T Val) {
+template <typename T>
+LLVM_DEPRECATED("Use llvm::countl_zero instead.", "llvm::countl_zero")
+unsigned countLeadingZeros(T Val) {
static_assert(std::is_unsigned_v<T>,
"Only unsigned integral types are allowed.");
return llvm::countl_zero(Val);
@@ -301,7 +305,9 @@ constexpr inline bool isPowerOf2_64(uint64_t Value) {
/// Only unsigned integral types are allowed.
///
/// Returns std::numeric_limits<T>::digits on an input of all ones.
-template <typename T> unsigned countLeadingOnes(T Value) {
+template <typename T>
+LLVM_DEPRECATED("Use llvm::countl_one instead.", "llvm::countl_one")
+unsigned countLeadingOnes(T Value) {
static_assert(std::is_unsigned_v<T>,
"Only unsigned integral types are allowed.");
return llvm::countl_one<T>(Value);
@@ -314,7 +320,9 @@ template <typename T> unsigned countLeadingOnes(T Value) {
/// Only unsigned integral types are allowed.
///
/// Returns std::numeric_limits<T>::digits on an input of all ones.
-template <typename T> unsigned countTrailingOnes(T Value) {
+template <typename T>
+LLVM_DEPRECATED("Use llvm::countr_one instead.", "llvm::countr_one")
+unsigned countTrailingOnes(T Value) {
static_assert(std::is_unsigned_v<T>,
"Only unsigned integral types are allowed.");
return llvm::countr_one<T>(Value);
@@ -324,6 +332,7 @@ template <typename T> unsigned countTrailingOnes(T Value) {
/// Ex. countPopulation(0xF000F000) = 8
/// Returns 0 if the word is zero.
template <typename T>
+LLVM_DEPRECATED("Use llvm::popcount instead.", "llvm::popcount")
inline unsigned countPopulation(T Value) {
static_assert(std::is_unsigned_v<T>,
"Only unsigned integral types are allowed.");
diff --git a/llvm/unittests/Support/MathExtrasTest.cpp b/llvm/unittests/Support/MathExtrasTest.cpp
index d45197a409689..db667bfb28810 100644
--- a/llvm/unittests/Support/MathExtrasTest.cpp
+++ b/llvm/unittests/Support/MathExtrasTest.cpp
@@ -13,58 +13,6 @@ using namespace llvm;
namespace {
-TEST(MathExtras, countTrailingZeros) {
- uint8_t Z8 = 0;
- uint16_t Z16 = 0;
- uint32_t Z32 = 0;
- uint64_t Z64 = 0;
- EXPECT_EQ(8u, countTrailingZeros(Z8));
- EXPECT_EQ(16u, countTrailingZeros(Z16));
- EXPECT_EQ(32u, countTrailingZeros(Z32));
- EXPECT_EQ(64u, countTrailingZeros(Z64));
-
- uint8_t NZ8 = 42;
- uint16_t NZ16 = 42;
- uint32_t NZ32 = 42;
- uint64_t NZ64 = 42;
- EXPECT_EQ(1u, countTrailingZeros(NZ8));
- EXPECT_EQ(1u, countTrailingZeros(NZ16));
- EXPECT_EQ(1u, countTrailingZeros(NZ32));
- EXPECT_EQ(1u, countTrailingZeros(NZ64));
-}
-
-TEST(MathExtras, countLeadingZeros) {
- uint8_t Z8 = 0;
- uint16_t Z16 = 0;
- uint32_t Z32 = 0;
- uint64_t Z64 = 0;
- EXPECT_EQ(8u, countLeadingZeros(Z8));
- EXPECT_EQ(16u, countLeadingZeros(Z16));
- EXPECT_EQ(32u, countLeadingZeros(Z32));
- EXPECT_EQ(64u, countLeadingZeros(Z64));
-
- uint8_t NZ8 = 42;
- uint16_t NZ16 = 42;
- uint32_t NZ32 = 42;
- uint64_t NZ64 = 42;
- EXPECT_EQ(2u, countLeadingZeros(NZ8));
- EXPECT_EQ(10u, countLeadingZeros(NZ16));
- EXPECT_EQ(26u, countLeadingZeros(NZ32));
- EXPECT_EQ(58u, countLeadingZeros(NZ64));
-
- EXPECT_EQ(8u, countLeadingZeros(0x00F000FFu));
- EXPECT_EQ(8u, countLeadingZeros(0x00F12345u));
- for (unsigned i = 0; i <= 30; ++i) {
- EXPECT_EQ(31 - i, countLeadingZeros(1u << i));
- }
-
- EXPECT_EQ(8u, countLeadingZeros(0x00F1234500F12345ULL));
- EXPECT_EQ(1u, countLeadingZeros(1ULL << 62));
- for (unsigned i = 0; i <= 62; ++i) {
- EXPECT_EQ(63 - i, countLeadingZeros(1ULL << i));
- }
-}
-
TEST(MathExtras, onesMask) {
EXPECT_EQ(0U, maskLeadingOnes<uint8_t>(0));
EXPECT_EQ(0U, maskTrailingOnes<uint8_t>(0));
@@ -220,21 +168,6 @@ TEST(MathExtras, CTLog2) {
EXPECT_EQ(CTLog2<1ULL << 15>(), 15U);
}
-TEST(MathExtras, countLeadingOnes) {
- for (int i = 30; i >= 0; --i) {
- // Start with all ones and unset some bit.
- EXPECT_EQ(31u - i, countLeadingOnes(0xFFFFFFFF ^ (1 << i)));
- }
- for (int i = 62; i >= 0; --i) {
- // Start with all ones and unset some bit.
- EXPECT_EQ(63u - i, countLeadingOnes(0xFFFFFFFFFFFFFFFFULL ^ (1LL << i)));
- }
- for (int i = 30; i >= 0; --i) {
- // Start with all ones and unset some bit.
- EXPECT_EQ(31u - i, countLeadingOnes(0xFFFFFFFF ^ (1 << i)));
- }
-}
-
TEST(MathExtras, FloatBits) {
static const float kValue = 5632.34f;
EXPECT_FLOAT_EQ(kValue, BitsToFloat(FloatToBits(kValue)));
More information about the llvm-commits
mailing list