From afe4ff0119db282997b7f72421a1055e652337c3 Mon Sep 17 00:00:00 2001 From: c8ef Date: Thu, 6 Mar 2025 17:56:07 +0000 Subject: [PATCH 1/2] fix bug --- libc/src/__support/big_int.h | 5 +++-- libc/src/__support/math_extras.h | 5 +++-- libc/test/src/__support/math_extras_test.cpp | 1 + 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/libc/src/__support/big_int.h b/libc/src/__support/big_int.h index f44624a7eafce..b8d0a0487eeb8 100644 --- a/libc/src/__support/big_int.h +++ b/libc/src/__support/big_int.h @@ -1383,8 +1383,9 @@ 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 || value == cpp::numeric_limits::max()) + ? 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..c40fdb9cc97a1 100644 --- a/libc/src/__support/math_extras.h +++ b/libc/src/__support/math_extras.h @@ -146,8 +146,9 @@ 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 || value == cpp::numeric_limits::max()) + ? 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 01b216081d034..d77bc5d2f80de 100644 --- a/libc/test/src/__support/math_extras_test.cpp +++ b/libc/test/src/__support/math_extras_test.cpp @@ -95,6 +95,7 @@ TYPED_TEST(LlvmLibcBitTest, FirstTrailingZero, UnsignedTypesNoBigInt) { } TYPED_TEST(LlvmLibcBitTest, FirstTrailingOne, UnsignedTypesNoBigInt) { + EXPECT_EQ(first_trailing_one(static_cast(0)), 0); EXPECT_EQ(first_trailing_one(cpp::numeric_limits::max()), 0); for (int i = 0U; i != cpp::numeric_limits::digits; ++i) { auto lhs = T(T(1) << size_t(i)); From 4467cf6867d14cf8e642607284d9992f76e5ecdc Mon Sep 17 00:00:00 2001 From: c8ef Date: Fri, 7 Mar 2025 15:26:23 +0000 Subject: [PATCH 2/2] address review comments --- libc/src/__support/big_int.h | 4 +--- libc/src/__support/math_extras.h | 4 +--- libc/test/src/__support/math_extras_test.cpp | 2 +- libc/test/src/stdbit/stdc_first_trailing_one_uc_test.cpp | 2 +- libc/test/src/stdbit/stdc_first_trailing_one_ui_test.cpp | 2 +- libc/test/src/stdbit/stdc_first_trailing_one_ul_test.cpp | 2 +- libc/test/src/stdbit/stdc_first_trailing_one_ull_test.cpp | 2 +- libc/test/src/stdbit/stdc_first_trailing_one_us_test.cpp | 2 +- 8 files changed, 8 insertions(+), 12 deletions(-) diff --git a/libc/src/__support/big_int.h b/libc/src/__support/big_int.h index b8d0a0487eeb8..b71eb73201d35 100644 --- a/libc/src/__support/big_int.h +++ b/libc/src/__support/big_int.h @@ -1383,9 +1383,7 @@ first_trailing_zero(T value) { template [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t, int> first_trailing_one(T value) { - return (value == 0 || 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 c40fdb9cc97a1..47df2a43250c8 100644 --- a/libc/src/__support/math_extras.h +++ b/libc/src/__support/math_extras.h @@ -146,9 +146,7 @@ first_trailing_zero(T value) { template [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t, int> first_trailing_one(T value) { - return (value == 0 || 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 d77bc5d2f80de..f5d4dae371827 100644 --- a/libc/test/src/__support/math_extras_test.cpp +++ b/libc/test/src/__support/math_extras_test.cpp @@ -96,7 +96,7 @@ TYPED_TEST(LlvmLibcBitTest, FirstTrailingZero, UnsignedTypesNoBigInt) { TYPED_TEST(LlvmLibcBitTest, FirstTrailingOne, UnsignedTypesNoBigInt) { EXPECT_EQ(first_trailing_one(static_cast(0)), 0); - EXPECT_EQ(first_trailing_one(cpp::numeric_limits::max()), 0); + EXPECT_EQ(first_trailing_one(cpp::numeric_limits::max()), 1); for (int i = 0U; i != cpp::numeric_limits::digits; ++i) { auto lhs = T(T(1) << size_t(i)); EXPECT_EQ(first_trailing_one(lhs), 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) {