Skip to content

Commit

Permalink
[Support] Remove findFirstSet and findLastSet
Browse files Browse the repository at this point in the history
This patch removes findFirstSet and findLastSet as there are no uses
left in LLVM.

I am not aware of any uses of findFirstSet and findLastSet in the
open-source world outside LLVM, so I am skipping the deprecation step.

Differential Revision: https://reviews.llvm.org/D142603
  • Loading branch information
kazutakahirata committed Jan 26, 2023
1 parent 1b9fbc8 commit 32ac9db
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 76 deletions.
36 changes: 0 additions & 36 deletions llvm/include/llvm/Support/MathExtras.h
Expand Up @@ -24,14 +24,6 @@

namespace llvm {

/// The behavior an operation has on an input of 0.
enum ZeroBehavior {
/// The returned value is undefined.
ZB_Undefined,
/// The returned value is numeric_limits<T>::max()
ZB_Max
};

/// Mathematical constants.
namespace numbers {
// TODO: Track C++20 std::numbers.
Expand Down Expand Up @@ -92,19 +84,6 @@ template <typename T> unsigned countLeadingZeros(T Val) {
return llvm::countl_zero(Val);
}

/// Get the index of the first set bit starting from the least
/// significant bit.
///
/// Only unsigned integral types are allowed.
///
/// \param ZB the behavior on an input of 0.
template <typename T> T findFirstSet(T Val, ZeroBehavior ZB = ZB_Max) {
if (ZB == ZB_Max && Val == 0)
return std::numeric_limits<T>::max();

return llvm::countr_zero(Val);
}

/// Create a bitmask with the N right-most bits set to 1, and all other
/// bits set to 0. Only unsigned types are allowed.
template <typename T> T maskTrailingOnes(unsigned N) {
Expand Down Expand Up @@ -132,21 +111,6 @@ template <typename T> T maskLeadingZeros(unsigned N) {
return maskTrailingOnes<T>(CHAR_BIT * sizeof(T) - N);
}

/// Get the index of the last set bit starting from the least
/// significant bit.
///
/// Only unsigned integral types are allowed.
///
/// \param ZB the behavior on an input of 0.
template <typename T> T findLastSet(T Val, ZeroBehavior ZB = ZB_Max) {
if (ZB == ZB_Max && Val == 0)
return std::numeric_limits<T>::max();

// Use ^ instead of - because both gcc and llvm can remove the associated ^
// in the __builtin_clz intrinsic on x86.
return llvm::countl_zero(Val) ^ (std::numeric_limits<T>::digits - 1);
}

/// Macro compressed bit reversal table for 256 bits.
///
/// http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable
Expand Down
40 changes: 0 additions & 40 deletions llvm/unittests/Support/MathExtrasTest.cpp
Expand Up @@ -90,46 +90,6 @@ TEST(MathExtras, onesMask) {
EXPECT_EQ(0xFFFFFFFFFFFF0000ULL, maskLeadingOnes<uint64_t>(48U));
}

TEST(MathExtras, findFirstSet) {
uint8_t Z8 = 0;
uint16_t Z16 = 0;
uint32_t Z32 = 0;
uint64_t Z64 = 0;
EXPECT_EQ(0xFFULL, findFirstSet(Z8));
EXPECT_EQ(0xFFFFULL, findFirstSet(Z16));
EXPECT_EQ(0xFFFFFFFFULL, findFirstSet(Z32));
EXPECT_EQ(0xFFFFFFFFFFFFFFFFULL, findFirstSet(Z64));

uint8_t NZ8 = 42;
uint16_t NZ16 = 42;
uint32_t NZ32 = 42;
uint64_t NZ64 = 42;
EXPECT_EQ(1u, findFirstSet(NZ8));
EXPECT_EQ(1u, findFirstSet(NZ16));
EXPECT_EQ(1u, findFirstSet(NZ32));
EXPECT_EQ(1u, findFirstSet(NZ64));
}

TEST(MathExtras, findLastSet) {
uint8_t Z8 = 0;
uint16_t Z16 = 0;
uint32_t Z32 = 0;
uint64_t Z64 = 0;
EXPECT_EQ(0xFFULL, findLastSet(Z8));
EXPECT_EQ(0xFFFFULL, findLastSet(Z16));
EXPECT_EQ(0xFFFFFFFFULL, findLastSet(Z32));
EXPECT_EQ(0xFFFFFFFFFFFFFFFFULL, findLastSet(Z64));

uint8_t NZ8 = 42;
uint16_t NZ16 = 42;
uint32_t NZ32 = 42;
uint64_t NZ64 = 42;
EXPECT_EQ(5u, findLastSet(NZ8));
EXPECT_EQ(5u, findLastSet(NZ16));
EXPECT_EQ(5u, findLastSet(NZ32));
EXPECT_EQ(5u, findLastSet(NZ64));
}

TEST(MathExtras, isIntN) {
EXPECT_TRUE(isIntN(16, 32767));
EXPECT_FALSE(isIntN(16, 32768));
Expand Down

0 comments on commit 32ac9db

Please sign in to comment.