Skip to content

Commit b157193

Browse files
[ADT] Handle uint8_t and uint16_t in countr_zero (#158518)
Without this patch, the uint8_t and uint16_t cases are sent to the fallback route. This patch fixes that by relaxing the "if" condition. While it's hard to test that the correct control path is taken within countr_zero, this patch adds a few tests just to verify the correctness on uint8_t and uint16_t inputs.
1 parent e2455bf commit b157193

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

llvm/include/llvm/ADT/bit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ template <typename T> [[nodiscard]] int countr_zero(T Val) {
161161
return std::numeric_limits<T>::digits;
162162

163163
// Use the intrinsic if available.
164-
if constexpr (sizeof(T) == 4) {
164+
if constexpr (sizeof(T) <= 4) {
165165
#if __has_builtin(__builtin_ctz) || defined(__GNUC__)
166166
return __builtin_ctz(Val);
167167
#elif defined(_MSC_VER)

llvm/unittests/ADT/BitTest.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,14 @@ TEST(BitTest, CountrZero) {
297297
EXPECT_EQ(1, llvm::countr_zero(NZ16));
298298
EXPECT_EQ(1, llvm::countr_zero(NZ32));
299299
EXPECT_EQ(1, llvm::countr_zero(NZ64));
300+
301+
EXPECT_EQ(0, llvm::countr_zero(uint8_t(1)));
302+
EXPECT_EQ(3, llvm::countr_zero(uint8_t(8)));
303+
EXPECT_EQ(7, llvm::countr_zero(uint8_t(128)));
304+
305+
EXPECT_EQ(0, llvm::countr_zero(uint16_t(1)));
306+
EXPECT_EQ(8, llvm::countr_zero(uint16_t(256)));
307+
EXPECT_EQ(15, llvm::countr_zero(uint16_t(32768)));
300308
}
301309

302310
TEST(BitTest, CountlOne) {

0 commit comments

Comments
 (0)