Skip to content

Commit

Permalink
[libc][stdbit] implement stdc_count_zeros (C23) (llvm#82437)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickdesaulniers authored Feb 26, 2024
1 parent 4bc3b35 commit f70d5c0
Show file tree
Hide file tree
Showing 22 changed files with 349 additions and 8 deletions.
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 @@ -132,6 +132,11 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdbit.stdc_first_trailing_one_ui
libc.src.stdbit.stdc_first_trailing_one_ul
libc.src.stdbit.stdc_first_trailing_one_ull
libc.src.stdbit.stdc_count_zeros_uc
libc.src.stdbit.stdc_count_zeros_us
libc.src.stdbit.stdc_count_zeros_ui
libc.src.stdbit.stdc_count_zeros_ul
libc.src.stdbit.stdc_count_zeros_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 @@ -71,11 +71,11 @@ 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
stdc_count_zeros_ul
stdc_count_zeros_ull
stdc_count_zeros_uc |check|
stdc_count_zeros_us |check|
stdc_count_zeros_ui |check|
stdc_count_zeros_ul |check|
stdc_count_zeros_ull |check|
stdc_count_ones_uc
stdc_count_ones_us
stdc_count_ones_ui
Expand Down Expand Up @@ -122,7 +122,7 @@ stdc_first_leading_zero |check|
stdc_first_leading_one |check|
stdc_first_trailing_zero |check|
stdc_first_trailing_one |check|
stdc_count_zeros
stdc_count_zeros |check|
stdc_count_ones
stdc_has_single_bit
stdc_bit_width
Expand Down
20 changes: 20 additions & 0 deletions libc/include/llvm-libc-macros/stdbit-macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,19 @@ inline unsigned stdc_first_trailing_one(unsigned long x) {
inline unsigned stdc_first_trailing_one(unsigned long long x) {
return stdc_first_trailing_one_ull(x);
}
inline unsigned stdc_count_zeros(unsigned char x) {
return stdc_count_zeros_uc(x);
}
inline unsigned stdc_count_zeros(unsigned short x) {
return stdc_count_zeros_us(x);
}
inline unsigned stdc_count_zeros(unsigned x) { return stdc_count_zeros_ui(x); }
inline unsigned stdc_count_zeros(unsigned long x) {
return stdc_count_zeros_ul(x);
}
inline unsigned stdc_count_zeros(unsigned long long x) {
return stdc_count_zeros_ull(x);
}
#else
#define stdc_leading_zeros(x) \
_Generic((x), \
Expand Down Expand Up @@ -188,6 +201,13 @@ inline unsigned stdc_first_trailing_one(unsigned long long x) {
unsigned: stdc_first_trailing_one_ui, \
unsigned long: stdc_first_trailing_one_ul, \
unsigned long long: stdc_first_trailing_one_ull)(x)
#define stdc_count_zeros(x) \
_Generic((x), \
unsigned char: stdc_count_zeros_uc, \
unsigned short: stdc_count_zeros_us, \
unsigned: stdc_count_zeros_ui, \
unsigned long: stdc_count_zeros_ul, \
unsigned long long: stdc_count_zeros_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 @@ -790,7 +790,8 @@ def StdC : StandardSpec<"stdc"> {
Macro<"stdc_first_leading_zero">,
Macro<"stdc_first_leading_one">,
Macro<"stdc_first_trailing_zero">,
Macro<"stdc_first_trailing_one">
Macro<"stdc_first_trailing_one">,
Macro<"stdc_count_zeros">
], // Macros
[], // Types
[], // Enumerations
Expand Down Expand Up @@ -829,7 +830,12 @@ def StdC : StandardSpec<"stdc"> {
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>]>
FunctionSpec<"stdc_first_trailing_one_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>,
FunctionSpec<"stdc_count_zeros_uc", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedCharType>]>,
FunctionSpec<"stdc_count_zeros_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
FunctionSpec<"stdc_count_zeros_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
FunctionSpec<"stdc_count_zeros_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
FunctionSpec<"stdc_count_zeros_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>
] // Functions
>;

Expand Down
1 change: 1 addition & 0 deletions libc/src/stdbit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ set(prefixes
first_leading_one
first_trailing_zero
first_trailing_one
count_zeros
)
set(suffixes c s i l ll)
foreach(prefix IN LISTS prefixes)
Expand Down
20 changes: 20 additions & 0 deletions libc/src/stdbit/stdc_count_zeros_uc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation of stdc_count_zeros_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_count_zeros_uc.h"

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

namespace LIBC_NAMESPACE {

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

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

namespace LIBC_NAMESPACE {

unsigned stdc_count_zeros_uc(unsigned char value);

} // namespace LIBC_NAMESPACE

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

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

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(unsigned, stdc_count_zeros_ui, (unsigned value)) {
return static_cast<unsigned>(cpp::count_zeros(value));
}

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

namespace LIBC_NAMESPACE {

unsigned stdc_count_zeros_ui(unsigned value);

} // namespace LIBC_NAMESPACE

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

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

namespace LIBC_NAMESPACE {

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

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

namespace LIBC_NAMESPACE {

unsigned stdc_count_zeros_ul(unsigned long value);

} // namespace LIBC_NAMESPACE

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

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

namespace LIBC_NAMESPACE {

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

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

namespace LIBC_NAMESPACE {

unsigned stdc_count_zeros_ull(unsigned long long value);

} // namespace LIBC_NAMESPACE

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

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

namespace LIBC_NAMESPACE {

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

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

namespace LIBC_NAMESPACE {

unsigned stdc_count_zeros_us(unsigned short value);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_STDBIT_STDC_COUNT_ZEROS_US_H
13 changes: 13 additions & 0 deletions libc/test/include/stdbit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ unsigned stdc_first_trailing_one_ul(unsigned long) noexcept { return 0x1DU; }
unsigned stdc_first_trailing_one_ull(unsigned long long) noexcept {
return 0x1FU;
}
unsigned stdc_count_zeros_uc(unsigned char) noexcept { return 0x2AU; }
unsigned stdc_count_zeros_us(unsigned short) noexcept { return 0x2BU; }
unsigned stdc_count_zeros_ui(unsigned) noexcept { return 0x2CU; }
unsigned stdc_count_zeros_ul(unsigned long) noexcept { return 0x2DU; }
unsigned stdc_count_zeros_ull(unsigned long long) noexcept { return 0x2FU; }
}

#include "include/llvm-libc-macros/stdbit-macros.h"
Expand Down Expand Up @@ -138,3 +143,11 @@ TEST(LlvmLibcStdbitTest, TypeGenericMacroFirstTrailingOne) {
EXPECT_EQ(stdc_first_trailing_one(0UL), 0x1DU);
EXPECT_EQ(stdc_first_trailing_one(0ULL), 0x1FU);
}

TEST(LlvmLibcStdbitTest, TypeGenericMacroCountZeros) {
EXPECT_EQ(stdc_count_zeros(static_cast<unsigned char>(0U)), 0x2AU);
EXPECT_EQ(stdc_count_zeros(static_cast<unsigned short>(0U)), 0x2BU);
EXPECT_EQ(stdc_count_zeros(0U), 0x2CU);
EXPECT_EQ(stdc_count_zeros(0UL), 0x2DU);
EXPECT_EQ(stdc_count_zeros(0ULL), 0x2FU);
}
1 change: 1 addition & 0 deletions libc/test/src/stdbit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ set(prefixes
first_leading_one
first_trailing_zero
first_trailing_one
count_zeros
)
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_count_zeros_uc_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Unittests for stdc_count_zeros_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_count_zeros_uc.h"
#include "test/UnitTest/Test.h"

TEST(LlvmLibcStdcCountZerosUcTest, Zero) {
EXPECT_EQ(LIBC_NAMESPACE::stdc_count_zeros_uc(0U),
static_cast<unsigned>(UCHAR_WIDTH));
}

TEST(LlvmLibcStdcCountZerosUcTest, Ones) {
for (unsigned i = 0U; i != UCHAR_WIDTH; ++i)
EXPECT_EQ(LIBC_NAMESPACE::stdc_count_zeros_uc(UCHAR_MAX >> i), i);
}
21 changes: 21 additions & 0 deletions libc/test/src/stdbit/stdc_count_zeros_ui_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Unittests for stdc_count_zeros_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/__support/CPP/limits.h"
#include "src/stdbit/stdc_count_zeros_ui.h"
#include "test/UnitTest/Test.h"

TEST(LlvmLibcStdcCountZerosUiTest, Zero) {
EXPECT_EQ(LIBC_NAMESPACE::stdc_count_zeros_ui(0U),
static_cast<unsigned>(UINT_WIDTH));
}

TEST(LlvmLibcStdcCountZerosUiTest, Ones) {
for (unsigned i = 0U; i != UINT_WIDTH; ++i)
EXPECT_EQ(LIBC_NAMESPACE::stdc_count_zeros_ui(UINT_MAX >> i), i);
}
Loading

0 comments on commit f70d5c0

Please sign in to comment.