Skip to content

[libc][stdbit] implement stdc_first_trailing_one (C23) #81768

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 1 commit into from
Feb 14, 2024
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
5 changes: 5 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdbit.stdc_first_trailing_zero_ui
libc.src.stdbit.stdc_first_trailing_zero_ul
libc.src.stdbit.stdc_first_trailing_zero_ull
libc.src.stdbit.stdc_first_trailing_one_uc
libc.src.stdbit.stdc_first_trailing_one_us
libc.src.stdbit.stdc_first_trailing_one_ui
libc.src.stdbit.stdc_first_trailing_one_ul
libc.src.stdbit.stdc_first_trailing_one_ull

# stdlib.h entrypoints
libc.src.stdlib.abs
Expand Down
12 changes: 6 additions & 6 deletions libc/docs/stdbit.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ stdc_first_trailing_zero_us |check|
stdc_first_trailing_zero_ui |check|
stdc_first_trailing_zero_ul |check|
stdc_first_trailing_zero_ull |check|
stdc_first_trailing_one_uc
stdc_first_trailing_one_us
stdc_first_trailing_one_ui
stdc_first_trailing_one_ul
stdc_first_trailing_one_ull
stdc_first_trailing_one_uc |check|
stdc_first_trailing_one_us |check|
stdc_first_trailing_one_ui |check|
stdc_first_trailing_one_ul |check|
stdc_first_trailing_one_ull |check|
stdc_count_zeros_uc
stdc_count_zeros_us
stdc_count_zeros_ui
Expand Down Expand Up @@ -121,7 +121,7 @@ stdc_trailing_ones |check|
stdc_first_leading_zero |check|
stdc_first_leading_one |check|
stdc_first_trailing_zero |check|
stdc_first_trailing_one
stdc_first_trailing_one |check|
stdc_count_zeros
stdc_count_ones
stdc_has_single_bit
Expand Down
22 changes: 22 additions & 0 deletions libc/include/llvm-libc-macros/stdbit-macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,21 @@ inline unsigned stdc_first_trailing_zero(unsigned long x) {
inline unsigned stdc_first_trailing_zero(unsigned long long x) {
return stdc_first_trailing_zero_ull(x);
}
inline unsigned stdc_first_trailing_one(unsigned char x) {
return stdc_first_trailing_one_uc(x);
}
inline unsigned stdc_first_trailing_one(unsigned short x) {
return stdc_first_trailing_one_us(x);
}
inline unsigned stdc_first_trailing_one(unsigned x) {
return stdc_first_trailing_one_ui(x);
}
inline unsigned stdc_first_trailing_one(unsigned long x) {
return stdc_first_trailing_one_ul(x);
}
inline unsigned stdc_first_trailing_one(unsigned long long x) {
return stdc_first_trailing_one_ull(x);
}
#else
#define stdc_leading_zeros(x) \
_Generic((x), \
Expand Down Expand Up @@ -166,6 +181,13 @@ inline unsigned stdc_first_trailing_zero(unsigned long long x) {
unsigned: stdc_first_trailing_zero_ui, \
unsigned long: stdc_first_trailing_zero_ul, \
unsigned long long: stdc_first_trailing_zero_ull)(x)
#define stdc_first_trailing_one(x) \
_Generic((x), \
unsigned char: stdc_first_trailing_one_uc, \
unsigned short: stdc_first_trailing_one_us, \
unsigned: stdc_first_trailing_one_ui, \
unsigned long: stdc_first_trailing_one_ul, \
unsigned long long: stdc_first_trailing_one_ull)(x)
#endif // __cplusplus

#endif // __LLVM_LIBC_MACROS_STDBIT_MACROS_H
13 changes: 7 additions & 6 deletions libc/spec/stdc.td
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,8 @@ def StdC : StandardSpec<"stdc"> {
Macro<"stdc_trailing_ones">,
Macro<"stdc_first_leading_zero">,
Macro<"stdc_first_leading_one">,
Macro<"stdc_first_trailing_zero">
Macro<"stdc_first_trailing_zero">,
Macro<"stdc_first_trailing_one">
], // Macros
[], // Types
[], // Enumerations
Expand Down Expand Up @@ -818,11 +819,11 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"stdc_first_leading_one_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
FunctionSpec<"stdc_first_leading_one_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
FunctionSpec<"stdc_first_leading_one_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>,
FunctionSpec<"stdc_first_trailing_zero_uc", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedCharType>]>,
FunctionSpec<"stdc_first_trailing_zero_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
FunctionSpec<"stdc_first_trailing_zero_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
FunctionSpec<"stdc_first_trailing_zero_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
FunctionSpec<"stdc_first_trailing_zero_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>,
FunctionSpec<"stdc_first_trailing_one_uc", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedCharType>]>,
FunctionSpec<"stdc_first_trailing_one_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
FunctionSpec<"stdc_first_trailing_one_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
FunctionSpec<"stdc_first_trailing_one_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
FunctionSpec<"stdc_first_trailing_one_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>
] // Functions
>;

Expand Down
5 changes: 5 additions & 0 deletions libc/src/__support/CPP/bit.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@ template <typename T, typename = cpp::enable_if_t<cpp::is_unsigned_v<T>>>
: countr_zero(static_cast<T>(~value)) + 1;
}

template <typename T, typename = cpp::enable_if_t<cpp::is_unsigned_v<T>>>
[[nodiscard]] LIBC_INLINE constexpr int first_trailing_one(T value) {
return value == cpp::numeric_limits<T>::max() ? 0 : countr_zero(value) + 1;
}

} // namespace LIBC_NAMESPACE::cpp

#endif // LLVM_LIBC_SRC___SUPPORT_CPP_BIT_H
1 change: 1 addition & 0 deletions libc/src/stdbit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set(prefixes
first_leading_zero
first_leading_one
first_trailing_zero
first_trailing_one
)
set(suffixes c s i l ll)
foreach(prefix IN LISTS prefixes)
Expand Down
21 changes: 21 additions & 0 deletions libc/src/stdbit/stdc_first_trailing_one_uc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Implementation of stdc_first_trailing_one_uc ----------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/stdbit/stdc_first_trailing_one_uc.h"

#include "src/__support/CPP/bit.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(unsigned, stdc_first_trailing_one_uc,
(unsigned char value)) {
return static_cast<unsigned>(cpp::first_trailing_one(value));
}

} // namespace LIBC_NAMESPACE
18 changes: 18 additions & 0 deletions libc/src/stdbit/stdc_first_trailing_one_uc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Implementation header for stdc_first_trailing_one_uc ---*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ONE_UC_H
#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ONE_UC_H

namespace LIBC_NAMESPACE {

unsigned stdc_first_trailing_one_uc(unsigned char value);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ONE_UC_H
20 changes: 20 additions & 0 deletions libc/src/stdbit/stdc_first_trailing_one_ui.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation of stdc_first_trailing_one_ui ----------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/stdbit/stdc_first_trailing_one_ui.h"

#include "src/__support/CPP/bit.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(unsigned, stdc_first_trailing_one_ui, (unsigned value)) {
return static_cast<unsigned>(cpp::first_trailing_one(value));
}

} // namespace LIBC_NAMESPACE
18 changes: 18 additions & 0 deletions libc/src/stdbit/stdc_first_trailing_one_ui.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Implementation header for stdc_first_trailing_one_ui ---*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ONE_UI_H
#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ONE_UI_H

namespace LIBC_NAMESPACE {

unsigned stdc_first_trailing_one_ui(unsigned value);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ONE_UI_H
21 changes: 21 additions & 0 deletions libc/src/stdbit/stdc_first_trailing_one_ul.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Implementation of stdc_first_trailing_one_ul ----------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/stdbit/stdc_first_trailing_one_ul.h"

#include "src/__support/CPP/bit.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(unsigned, stdc_first_trailing_one_ul,
(unsigned long value)) {
return static_cast<unsigned>(cpp::first_trailing_one(value));
}

} // namespace LIBC_NAMESPACE
18 changes: 18 additions & 0 deletions libc/src/stdbit/stdc_first_trailing_one_ul.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Implementation header for stdc_first_trailing_one_ul ---*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ONE_UL_H
#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ONE_UL_H

namespace LIBC_NAMESPACE {

unsigned stdc_first_trailing_one_ul(unsigned long value);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ONE_UL_H
21 changes: 21 additions & 0 deletions libc/src/stdbit/stdc_first_trailing_one_ull.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Implementation of stdc_first_trailing_one_ull ---------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/stdbit/stdc_first_trailing_one_ull.h"

#include "src/__support/CPP/bit.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(unsigned, stdc_first_trailing_one_ull,
(unsigned long long value)) {
return static_cast<unsigned>(cpp::first_trailing_one(value));
}

} // namespace LIBC_NAMESPACE
18 changes: 18 additions & 0 deletions libc/src/stdbit/stdc_first_trailing_one_ull.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Implementation header for stdc_first_trailing_one_ull --*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ONE_ULL_H
#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ONE_ULL_H

namespace LIBC_NAMESPACE {

unsigned stdc_first_trailing_one_ull(unsigned long long value);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ONE_ULL_H
21 changes: 21 additions & 0 deletions libc/src/stdbit/stdc_first_trailing_one_us.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Implementation of stdc_first_trailing_one_us ----------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/stdbit/stdc_first_trailing_one_us.h"

#include "src/__support/CPP/bit.h"
#include "src/__support/common.h"

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(unsigned, stdc_first_trailing_one_us,
(unsigned short value)) {
return static_cast<unsigned>(cpp::first_trailing_one(value));
}

} // namespace LIBC_NAMESPACE
18 changes: 18 additions & 0 deletions libc/src/stdbit/stdc_first_trailing_one_us.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Implementation header for stdc_first_trailing_one_us ---*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ONE_US_H
#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ONE_US_H

namespace LIBC_NAMESPACE {

unsigned stdc_first_trailing_one_us(unsigned short value);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_TRAILING_ONE_US_H
15 changes: 15 additions & 0 deletions libc/test/include/stdbit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ unsigned stdc_first_trailing_zero_ul(unsigned long) noexcept { return 0x0DU; }
unsigned stdc_first_trailing_zero_ull(unsigned long long) noexcept {
return 0x0FU;
}
unsigned stdc_first_trailing_one_uc(unsigned char) noexcept { return 0x1AU; }
unsigned stdc_first_trailing_one_us(unsigned short) noexcept { return 0x1BU; }
unsigned stdc_first_trailing_one_ui(unsigned) noexcept { return 0x1CU; }
unsigned stdc_first_trailing_one_ul(unsigned long) noexcept { return 0x1DU; }
unsigned stdc_first_trailing_one_ull(unsigned long long) noexcept {
return 0x1FU;
}
}

#include "include/llvm-libc-macros/stdbit-macros.h"
Expand Down Expand Up @@ -123,3 +130,11 @@ TEST(LlvmLibcStdbitTest, TypeGenericMacroFirstTrailingZero) {
EXPECT_EQ(stdc_first_trailing_zero(0UL), 0x0DU);
EXPECT_EQ(stdc_first_trailing_zero(0ULL), 0x0FU);
}

TEST(LlvmLibcStdbitTest, TypeGenericMacroFirstTrailingOne) {
EXPECT_EQ(stdc_first_trailing_one(static_cast<unsigned char>(0U)), 0x1AU);
EXPECT_EQ(stdc_first_trailing_one(static_cast<unsigned short>(0U)), 0x1BU);
EXPECT_EQ(stdc_first_trailing_one(0U), 0x1CU);
EXPECT_EQ(stdc_first_trailing_one(0UL), 0x1DU);
EXPECT_EQ(stdc_first_trailing_one(0ULL), 0x1FU);
}
6 changes: 6 additions & 0 deletions libc/test/src/__support/CPP/bit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,10 @@ TYPED_TEST(LlvmLibcBitTest, FirstTrailingZero, UnsignedTypes) {
EXPECT_EQ(first_trailing_zero<T>(~(T(1) << i)), i + 1);
}

TYPED_TEST(LlvmLibcBitTest, FirstTrailingOne, UnsignedTypes) {
EXPECT_EQ(first_trailing_one<T>(cpp::numeric_limits<T>::max()), 0);
for (int i = 0U; i != cpp::numeric_limits<T>::digits; ++i)
EXPECT_EQ(first_trailing_one<T>(T(1) << i), i + 1);
}

} // namespace LIBC_NAMESPACE::cpp
1 change: 1 addition & 0 deletions libc/test/src/stdbit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ set(prefixes
first_leading_zero
first_leading_one
first_trailing_zero
first_trailing_one
)
set(suffixes c s i l ll)
foreach(prefix IN LISTS prefixes)
Expand Down
20 changes: 20 additions & 0 deletions libc/test/src/stdbit/stdc_first_trailing_one_uc_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Unittests for stdc_first_trailing_one_uc -------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "src/__support/CPP/limits.h"
#include "src/stdbit/stdc_first_trailing_one_uc.h"
#include "test/UnitTest/Test.h"

TEST(LlvmLibcStdcFirstTrailingOneUcTest, ALL) {
EXPECT_EQ(LIBC_NAMESPACE::stdc_first_trailing_one_uc(UCHAR_MAX), 0U);
}

TEST(LlvmLibcStdcFirstTrailingOneUcTest, OneHot) {
for (unsigned i = 0U; i != UCHAR_WIDTH; ++i)
EXPECT_EQ(LIBC_NAMESPACE::stdc_first_trailing_one_uc(1U << i), i + 1);
}
Loading