Skip to content

[libc] first_trailing_one(0) should be 0. #130155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions libc/src/__support/big_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -1380,8 +1380,7 @@ first_trailing_zero(T value) {
template <typename T>
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, int>
first_trailing_one(T value) {
return value == cpp::numeric_limits<T>::max() ? 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should be just value == 0. When value is not 0, cpp::countr_zero(value) + 1 should be correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will fail with inputs like USHRT_MAX because we need it to return 0, but cpp::countr_zero(USHRT_MAX) + 1 = 1.

: cpp::countr_zero(value) + 1;
return value == 0 ? 0 : cpp::countr_zero(value) + 1;
}

} // namespace LIBC_NAMESPACE_DECL
Expand Down
3 changes: 1 addition & 2 deletions libc/src/__support/math_extras.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ first_trailing_zero(T value) {
template <typename T>
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
first_trailing_one(T value) {
return value == cpp::numeric_limits<T>::max() ? 0
: cpp::countr_zero(value) + 1;
return value == 0 ? 0 : cpp::countr_zero(value) + 1;
}

template <typename T>
Expand Down
3 changes: 2 additions & 1 deletion libc/test/src/__support/math_extras_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ TYPED_TEST(LlvmLibcBitTest, FirstTrailingZero, UnsignedTypesNoBigInt) {
}

TYPED_TEST(LlvmLibcBitTest, FirstTrailingOne, UnsignedTypesNoBigInt) {
EXPECT_EQ(first_trailing_one<T>(cpp::numeric_limits<T>::max()), 0);
EXPECT_EQ(first_trailing_one<T>(static_cast<T>(0)), 0);
EXPECT_EQ(first_trailing_one<T>(cpp::numeric_limits<T>::max()), 1);
for (int i = 0U; i != cpp::numeric_limits<T>::digits; ++i)
EXPECT_EQ(first_trailing_one<T>(T(1) << i), i + 1);
}
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/stdbit/stdc_first_trailing_one_uc_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/stdbit/stdc_first_trailing_one_ui_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/stdbit/stdc_first_trailing_one_ul_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/stdbit/stdc_first_trailing_one_ull_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion libc/test/src/stdbit/stdc_first_trailing_one_us_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down