diff --git a/libc/src/__support/big_int.h b/libc/src/__support/big_int.h index e726a094b5dac..f680a76d82a7d 100644 --- a/libc/src/__support/big_int.h +++ b/libc/src/__support/big_int.h @@ -1380,8 +1380,7 @@ first_trailing_zero(T value) { template [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t, int> first_trailing_one(T value) { - return value == cpp::numeric_limits::max() ? 0 - : cpp::countr_zero(value) + 1; + return value == 0 ? 0 : cpp::countr_zero(value) + 1; } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/__support/math_extras.h b/libc/src/__support/math_extras.h index 6f4a006aad270..47df2a43250c8 100644 --- a/libc/src/__support/math_extras.h +++ b/libc/src/__support/math_extras.h @@ -146,8 +146,7 @@ first_trailing_zero(T value) { template [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t, int> first_trailing_one(T value) { - return value == cpp::numeric_limits::max() ? 0 - : cpp::countr_zero(value) + 1; + return value == 0 ? 0 : cpp::countr_zero(value) + 1; } template diff --git a/libc/test/src/__support/math_extras_test.cpp b/libc/test/src/__support/math_extras_test.cpp index 08c090017c1a1..9b1a3dd86731c 100644 --- a/libc/test/src/__support/math_extras_test.cpp +++ b/libc/test/src/__support/math_extras_test.cpp @@ -91,7 +91,8 @@ TYPED_TEST(LlvmLibcBitTest, FirstTrailingZero, UnsignedTypesNoBigInt) { } TYPED_TEST(LlvmLibcBitTest, FirstTrailingOne, UnsignedTypesNoBigInt) { - EXPECT_EQ(first_trailing_one(cpp::numeric_limits::max()), 0); + EXPECT_EQ(first_trailing_one(static_cast(0)), 0); + EXPECT_EQ(first_trailing_one(cpp::numeric_limits::max()), 1); for (int i = 0U; i != cpp::numeric_limits::digits; ++i) EXPECT_EQ(first_trailing_one(T(1) << i), i + 1); } diff --git a/libc/test/src/stdbit/stdc_first_trailing_one_uc_test.cpp b/libc/test/src/stdbit/stdc_first_trailing_one_uc_test.cpp index 5ca4cfca1b479..f507e9ac12c2a 100644 --- a/libc/test/src/stdbit/stdc_first_trailing_one_uc_test.cpp +++ b/libc/test/src/stdbit/stdc_first_trailing_one_uc_test.cpp @@ -11,7 +11,7 @@ #include "test/UnitTest/Test.h" TEST(LlvmLibcStdcFirstTrailingOneUcTest, ALL) { - EXPECT_EQ(LIBC_NAMESPACE::stdc_first_trailing_one_uc(UCHAR_MAX), 0U); + EXPECT_EQ(LIBC_NAMESPACE::stdc_first_trailing_one_uc(UCHAR_MAX), 1U); } TEST(LlvmLibcStdcFirstTrailingOneUcTest, OneHot) { diff --git a/libc/test/src/stdbit/stdc_first_trailing_one_ui_test.cpp b/libc/test/src/stdbit/stdc_first_trailing_one_ui_test.cpp index 137c8a42e407d..3109d6888d534 100644 --- a/libc/test/src/stdbit/stdc_first_trailing_one_ui_test.cpp +++ b/libc/test/src/stdbit/stdc_first_trailing_one_ui_test.cpp @@ -11,7 +11,7 @@ #include "test/UnitTest/Test.h" TEST(LlvmLibcStdcFirstTrailingOneUiTest, ALL) { - EXPECT_EQ(LIBC_NAMESPACE::stdc_first_trailing_one_ui(UINT_MAX), 0U); + EXPECT_EQ(LIBC_NAMESPACE::stdc_first_trailing_one_ui(UINT_MAX), 1U); } TEST(LlvmLibcStdcFirstTrailingOneUiTest, OneHot) { diff --git a/libc/test/src/stdbit/stdc_first_trailing_one_ul_test.cpp b/libc/test/src/stdbit/stdc_first_trailing_one_ul_test.cpp index 3fc1f3f16c60d..8d1f39acff8d3 100644 --- a/libc/test/src/stdbit/stdc_first_trailing_one_ul_test.cpp +++ b/libc/test/src/stdbit/stdc_first_trailing_one_ul_test.cpp @@ -11,7 +11,7 @@ #include "test/UnitTest/Test.h" TEST(LlvmLibcStdcFirstTrailingOneUlTest, ALL) { - EXPECT_EQ(LIBC_NAMESPACE::stdc_first_trailing_one_ul(ULONG_MAX), 0U); + EXPECT_EQ(LIBC_NAMESPACE::stdc_first_trailing_one_ul(ULONG_MAX), 1U); } TEST(LlvmLibcStdcFirstTrailingOneUlTest, OneHot) { diff --git a/libc/test/src/stdbit/stdc_first_trailing_one_ull_test.cpp b/libc/test/src/stdbit/stdc_first_trailing_one_ull_test.cpp index 5719e09a5120a..12ef0537e3281 100644 --- a/libc/test/src/stdbit/stdc_first_trailing_one_ull_test.cpp +++ b/libc/test/src/stdbit/stdc_first_trailing_one_ull_test.cpp @@ -11,7 +11,7 @@ #include "test/UnitTest/Test.h" TEST(LlvmLibcStdcFirstTrailingOneUllTest, ALL) { - EXPECT_EQ(LIBC_NAMESPACE::stdc_first_trailing_one_ull(ULLONG_MAX), 0U); + EXPECT_EQ(LIBC_NAMESPACE::stdc_first_trailing_one_ull(ULLONG_MAX), 1U); } TEST(LlvmLibcStdcFirstTrailingOneUllTest, OneHot) { diff --git a/libc/test/src/stdbit/stdc_first_trailing_one_us_test.cpp b/libc/test/src/stdbit/stdc_first_trailing_one_us_test.cpp index 46c69acfb7f07..6a506e6cc9632 100644 --- a/libc/test/src/stdbit/stdc_first_trailing_one_us_test.cpp +++ b/libc/test/src/stdbit/stdc_first_trailing_one_us_test.cpp @@ -11,7 +11,7 @@ #include "test/UnitTest/Test.h" TEST(LlvmLibcStdcFirstTrailingOneUsTest, ALL) { - EXPECT_EQ(LIBC_NAMESPACE::stdc_first_trailing_one_us(USHRT_MAX), 0U); + EXPECT_EQ(LIBC_NAMESPACE::stdc_first_trailing_one_us(USHRT_MAX), 1U); } TEST(LlvmLibcStdcFirstTrailingOneUsTest, OneHot) {