Skip to content

[libc][stdbit] implement stdc_first_leading_zero (C23) #81340

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
Feb 12, 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 @@ -112,6 +112,11 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdbit.stdc_trailing_ones_ui
libc.src.stdbit.stdc_trailing_ones_ul
libc.src.stdbit.stdc_trailing_ones_ull
libc.src.stdbit.stdc_first_leading_zero_uc
libc.src.stdbit.stdc_first_leading_zero_us
libc.src.stdbit.stdc_first_leading_zero_ui
libc.src.stdbit.stdc_first_leading_zero_ul
libc.src.stdbit.stdc_first_leading_zero_ull

# stdlib.h entrypoints
libc.src.stdlib.abs
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 @@ -71,6 +71,21 @@ inline unsigned stdc_trailing_ones(unsigned long x) {
inline unsigned stdc_trailing_ones(unsigned long long x) {
return stdc_trailing_ones_ull(x);
}
inline unsigned stdc_first_leading_zero(unsigned char x) {
return stdc_first_leading_zero_uc(x);
}
inline unsigned stdc_first_leading_zero(unsigned short x) {
return stdc_first_leading_zero_us(x);
}
inline unsigned stdc_first_leading_zero(unsigned x) {
return stdc_first_leading_zero_ui(x);
}
inline unsigned stdc_first_leading_zero(unsigned long x) {
return stdc_first_leading_zero_ul(x);
}
inline unsigned stdc_first_leading_zero(unsigned long long x) {
return stdc_first_leading_zero_ull(x);
}
#else
#define stdc_leading_zeros(x) \
_Generic((x), \
Expand Down Expand Up @@ -100,6 +115,13 @@ inline unsigned stdc_trailing_ones(unsigned long long x) {
unsigned: stdc_trailing_ones_ui, \
unsigned long: stdc_trailing_ones_ul, \
unsigned long long: stdc_trailing_ones_ull)(x)
#define stdc_first_leading_zero(x) \
_Generic((x), \
unsigned char: stdc_first_leading_zero_uc, \
unsigned short: stdc_first_leading_zero_us, \
unsigned: stdc_first_leading_zero_ui, \
unsigned long: stdc_first_leading_zero_ul, \
unsigned long long: stdc_first_leading_zero_ull)(x)
#endif // __cplusplus

#endif // __LLVM_LIBC_MACROS_STDBIT_MACROS_H
10 changes: 8 additions & 2 deletions libc/spec/stdc.td
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,8 @@ def StdC : StandardSpec<"stdc"> {
Macro<"stdc_leading_zeros">,
Macro<"stdc_leading_ones">,
Macro<"stdc_trailing_zeros">,
Macro<"stdc_trailing_ones">
Macro<"stdc_trailing_ones">,
Macro<"stdc_first_leading_zero">
], // Macros
[], // Types
[], // Enumerations
Expand All @@ -803,7 +804,12 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"stdc_trailing_ones_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
FunctionSpec<"stdc_trailing_ones_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
FunctionSpec<"stdc_trailing_ones_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
FunctionSpec<"stdc_trailing_ones_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>
FunctionSpec<"stdc_trailing_ones_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>,
FunctionSpec<"stdc_first_leading_zero_uc", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedCharType>]>,
FunctionSpec<"stdc_first_leading_zero_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
FunctionSpec<"stdc_first_leading_zero_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
FunctionSpec<"stdc_first_leading_zero_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
FunctionSpec<"stdc_first_leading_zero_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>
] // Functions
>;

Expand Down
30 changes: 30 additions & 0 deletions libc/src/__support/CPP/bit.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,36 @@ LIBC_INLINE constexpr To bit_or_static_cast(const From &from) {
}
}

#define SPECIALIZE_FLZ(NAME, TYPE, BUILTIN) \
template <> [[nodiscard]] LIBC_INLINE constexpr int NAME<TYPE>(TYPE value) { \
static_assert(cpp::is_unsigned_v<TYPE>); \
return value == cpp::numeric_limits<TYPE>::max() \
? 0 \
: BUILTIN(static_cast<TYPE>(~value)) + 1; \
}

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

#if LIBC_HAS_BUILTIN(__builtin_clzs)
SPECIALIZE_FLZ(first_leading_zero, unsigned short, __builtin_clzs)
#endif
#if LIBC_HAS_BUILTIN(__builtin_clz)
SPECIALIZE_FLZ(first_leading_zero, unsigned int, __builtin_clz)
#endif
#if LIBC_HAS_BUILTIN(__builtin_clzl)
SPECIALIZE_FLZ(first_leading_zero, unsigned long, __builtin_clzl)
#endif
#if LIBC_HAS_BUILTIN(__builtin_clzll)
SPECIALIZE_FLZ(first_leading_zero, unsigned long long, __builtin_clzll)
#endif

#undef SPECIALIZE_FLZ

} // 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 @@ -3,6 +3,7 @@ set(prefixes
leading_ones
trailing_zeros
trailing_ones
first_leading_zero
)
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_leading_zero_uc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Implementation of stdc_first_leading_zero_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_leading_zero_uc.h"

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

namespace LIBC_NAMESPACE {

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

} // namespace LIBC_NAMESPACE
18 changes: 18 additions & 0 deletions libc/src/stdbit/stdc_first_leading_zero_uc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Implementation header for stdc_first_leading_zero_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_LEADING_ZERO_UC_H
#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ZERO_UC_H

namespace LIBC_NAMESPACE {

unsigned stdc_first_leading_zero_uc(unsigned char value);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ZERO_UC_H
20 changes: 20 additions & 0 deletions libc/src/stdbit/stdc_first_leading_zero_ui.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation of stdc_first_leading_zero_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_leading_zero_ui.h"

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

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(unsigned, stdc_first_leading_zero_ui, (unsigned value)) {
return static_cast<unsigned>(cpp::first_leading_zero(value));
}

} // namespace LIBC_NAMESPACE
18 changes: 18 additions & 0 deletions libc/src/stdbit/stdc_first_leading_zero_ui.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Implementation header for stdc_first_leading_zero_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_LEADING_ZERO_UI_H
#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ZERO_UI_H

namespace LIBC_NAMESPACE {

unsigned stdc_first_leading_zero_ui(unsigned value);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ZERO_UI_H
21 changes: 21 additions & 0 deletions libc/src/stdbit/stdc_first_leading_zero_ul.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Implementation of stdc_first_leading_zero_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_leading_zero_ul.h"

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

namespace LIBC_NAMESPACE {

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

} // namespace LIBC_NAMESPACE
18 changes: 18 additions & 0 deletions libc/src/stdbit/stdc_first_leading_zero_ul.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Implementation header for stdc_first_leading_zero_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_LEADING_ZERO_UL_H
#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ZERO_UL_H

namespace LIBC_NAMESPACE {

unsigned stdc_first_leading_zero_ul(unsigned long value);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ZERO_UL_H
21 changes: 21 additions & 0 deletions libc/src/stdbit/stdc_first_leading_zero_ull.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Implementation of stdc_first_leading_zero_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_leading_zero_ull.h"

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

namespace LIBC_NAMESPACE {

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

} // namespace LIBC_NAMESPACE
18 changes: 18 additions & 0 deletions libc/src/stdbit/stdc_first_leading_zero_ull.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Implementation header for stdc_first_leading_zero_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_LEADING_ZERO_ULL_H
#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ZERO_ULL_H

namespace LIBC_NAMESPACE {

unsigned stdc_first_leading_zero_ull(unsigned long long value);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ZERO_ULL_H
21 changes: 21 additions & 0 deletions libc/src/stdbit/stdc_first_leading_zero_us.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Implementation of stdc_first_leading_zero_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_leading_zero_us.h"

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

namespace LIBC_NAMESPACE {

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

} // namespace LIBC_NAMESPACE
18 changes: 18 additions & 0 deletions libc/src/stdbit/stdc_first_leading_zero_us.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Implementation header for stdc_first_leading_zero_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_LEADING_ZERO_US_H
#define LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ZERO_US_H

namespace LIBC_NAMESPACE {

unsigned stdc_first_leading_zero_us(unsigned short value);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_STDBIT_STDC_FIRST_LEADING_ZERO_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 @@ -43,6 +43,13 @@ unsigned stdc_trailing_ones_us(unsigned short) noexcept { return 0xDBU; }
unsigned stdc_trailing_ones_ui(unsigned) noexcept { return 0xDCU; }
unsigned stdc_trailing_ones_ul(unsigned long) noexcept { return 0xDDU; }
unsigned stdc_trailing_ones_ull(unsigned long long) noexcept { return 0xDFU; }
unsigned stdc_first_leading_zero_uc(unsigned char) noexcept { return 0xEAU; }
unsigned stdc_first_leading_zero_us(unsigned short) noexcept { return 0xEBU; }
unsigned stdc_first_leading_zero_ui(unsigned) noexcept { return 0xECU; }
unsigned stdc_first_leading_zero_ul(unsigned long) noexcept { return 0xEDU; }
unsigned stdc_first_leading_zero_ull(unsigned long long) noexcept {
return 0xEFU;
}
}

#include "include/llvm-libc-macros/stdbit-macros.h"
Expand Down Expand Up @@ -78,3 +85,11 @@ TEST(LlvmLibcStdbitTest, TypeGenericMacroTrailingOnes) {
EXPECT_EQ(stdc_trailing_ones(0UL), 0xDDU);
EXPECT_EQ(stdc_trailing_ones(0ULL), 0xDFU);
}

TEST(LlvmLibcStdbitTest, TypeGenericMacroFirstLeadingZero) {
EXPECT_EQ(stdc_first_leading_zero(static_cast<unsigned char>(0U)), 0xEAU);
EXPECT_EQ(stdc_first_leading_zero(static_cast<unsigned short>(0U)), 0xEBU);
EXPECT_EQ(stdc_first_leading_zero(0U), 0xECU);
EXPECT_EQ(stdc_first_leading_zero(0UL), 0xEDU);
EXPECT_EQ(stdc_first_leading_zero(0ULL), 0xEFU);
}
7 changes: 7 additions & 0 deletions libc/test/src/__support/CPP/bit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,11 @@ TEST(LlvmLibcBitTest, Rotr) {
rotr<uint64_t>(0x12345678deadbeefULL, -19));
}

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

} // 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 @@ -5,6 +5,7 @@ set(prefixes
leading_ones
trailing_zeros
trailing_ones
first_leading_zero
)
set(suffixes c s i l ll)
foreach(prefix IN LISTS prefixes)
Expand Down
21 changes: 21 additions & 0 deletions libc/test/src/stdbit/stdc_first_leading_zero_uc_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Unittests for stdc_first_leading_zero_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_leading_zero_uc.h"
#include "test/UnitTest/Test.h"

TEST(LlvmLibcStdcFirstLeadingZeroUcTest, ALL) {
EXPECT_EQ(LIBC_NAMESPACE::stdc_first_leading_zero_uc(UCHAR_MAX), 0U);
}

TEST(LlvmLibcStdcFirstLeadingZeroUcTest, ZeroHot) {
for (unsigned i = 0U; i != UCHAR_WIDTH; ++i)
EXPECT_EQ(LIBC_NAMESPACE::stdc_first_leading_zero_uc(~(1U << i)),
UCHAR_WIDTH - i);
}
Loading