Skip to content
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

[libc] implement stdc_leading_ones (C23) #80082

Merged
merged 7 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -97,6 +97,11 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdbit.stdc_leading_zeros_ui
libc.src.stdbit.stdc_leading_zeros_ul
libc.src.stdbit.stdc_leading_zeros_ull
libc.src.stdbit.stdc_leading_ones_uc
libc.src.stdbit.stdc_leading_ones_us
libc.src.stdbit.stdc_leading_ones_ui
libc.src.stdbit.stdc_leading_ones_ul
libc.src.stdbit.stdc_leading_ones_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 @@ -25,6 +25,21 @@ inline unsigned long stdc_leading_zeros(unsigned long x) {
inline unsigned long long stdc_leading_zeros(unsigned long long x) {
return stdc_leading_zeros_ull(x);
}
inline unsigned char stdc_leading_ones(unsigned char x) {
return stdc_leading_ones_uc(x);
}
inline unsigned short stdc_leading_ones(unsigned short x) {
return stdc_leading_ones_us(x);
}
inline unsigned stdc_leading_ones(unsigned x) {
return stdc_leading_ones_ui(x);
}
inline unsigned long stdc_leading_ones(unsigned long x) {
return stdc_leading_ones_ul(x);
}
inline unsigned long long stdc_leading_ones(unsigned long long x) {
return stdc_leading_ones_ull(x);
}
#else
#define stdc_leading_zeros(x) \
_Generic((x), \
Expand All @@ -33,6 +48,13 @@ inline unsigned long long stdc_leading_zeros(unsigned long long x) {
unsigned: stdc_leading_zeros_ui, \
unsigned long: stdc_leading_zeros_ul, \
unsigned long long: stdc_leading_zeros_ull)(x)
#define stdc_leading_ones(x) \
_Generic((x), \
unsigned char: stdc_leading_ones_uc, \
unsigned short: stdc_leading_ones_us, \
unsigned: stdc_leading_ones_ui, \
unsigned long: stdc_leading_ones_ul, \
unsigned long long: stdc_leading_ones_ull)(x)
#endif // __cplusplus

#endif // __LLVM_LIBC_MACROS_STDBIT_MACROS_H
7 changes: 6 additions & 1 deletion libc/spec/stdc.td
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,12 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"stdc_leading_zeros_us", RetValSpec<UnsignedShortType>, [ArgSpec<UnsignedShortType>]>,
FunctionSpec<"stdc_leading_zeros_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
FunctionSpec<"stdc_leading_zeros_ul", RetValSpec<UnsignedLongType>, [ArgSpec<UnsignedLongType>]>,
FunctionSpec<"stdc_leading_zeros_ull", RetValSpec<UnsignedLongLongType>, [ArgSpec<UnsignedLongLongType>]>
FunctionSpec<"stdc_leading_zeros_ull", RetValSpec<UnsignedLongLongType>, [ArgSpec<UnsignedLongLongType>]>,
FunctionSpec<"stdc_leading_ones_uc", RetValSpec<UnsignedCharType>, [ArgSpec<UnsignedCharType>]>,
FunctionSpec<"stdc_leading_ones_us", RetValSpec<UnsignedShortType>, [ArgSpec<UnsignedShortType>]>,
FunctionSpec<"stdc_leading_ones_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
FunctionSpec<"stdc_leading_ones_ul", RetValSpec<UnsignedLongType>, [ArgSpec<UnsignedLongType>]>,
FunctionSpec<"stdc_leading_ones_ull", RetValSpec<UnsignedLongLongType>, [ArgSpec<UnsignedLongLongType>]>
] // Functions
>;

Expand Down
50 changes: 50 additions & 0 deletions libc/src/stdbit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,53 @@ add_entrypoint_object(
DEPENDS
libc.src.__support.CPP.bit
)

add_entrypoint_object(
stdc_leading_ones_uc
SRCS
stdc_leading_ones_uc.cpp
HDRS
stdc_leading_ones_uc.h
DEPENDS
libc.src.__support.CPP.bit
)

add_entrypoint_object(
stdc_leading_ones_us
SRCS
stdc_leading_ones_us.cpp
HDRS
stdc_leading_ones_us.h
DEPENDS
libc.src.__support.CPP.bit
)

add_entrypoint_object(
stdc_leading_ones_ui
SRCS
stdc_leading_ones_ui.cpp
HDRS
stdc_leading_ones_ui.h
DEPENDS
libc.src.__support.CPP.bit
)

add_entrypoint_object(
stdc_leading_ones_ul
SRCS
stdc_leading_ones_ul.cpp
HDRS
stdc_leading_ones_ul.h
DEPENDS
libc.src.__support.CPP.bit
)

add_entrypoint_object(
stdc_leading_ones_ull
SRCS
stdc_leading_ones_ull.cpp
HDRS
stdc_leading_ones_ull.h
DEPENDS
libc.src.__support.CPP.bit
)
20 changes: 20 additions & 0 deletions libc/src/stdbit/stdc_leading_ones_uc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation of stdc_leading_ones_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_leading_ones_uc.h"

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

namespace LIBC_NAMESPACE {

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

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

namespace LIBC_NAMESPACE {

unsigned char stdc_leading_ones_uc(unsigned char value);

} // namespace LIBC_NAMESPACE

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

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

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(unsigned, stdc_leading_ones_ui, (unsigned value)) {
return cpp::countl_one(value);
nickdesaulniers marked this conversation as resolved.
Show resolved Hide resolved
}

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

namespace LIBC_NAMESPACE {

unsigned stdc_leading_ones_ui(unsigned value);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ONES_Ui_H
nickdesaulniers marked this conversation as resolved.
Show resolved Hide resolved
20 changes: 20 additions & 0 deletions libc/src/stdbit/stdc_leading_ones_ul.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation of stdc_leading_ones_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_leading_ones_ul.h"

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

namespace LIBC_NAMESPACE {

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

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

namespace LIBC_NAMESPACE {

unsigned long stdc_leading_ones_ul(unsigned long value);

} // namespace LIBC_NAMESPACE

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

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

namespace LIBC_NAMESPACE {

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

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

namespace LIBC_NAMESPACE {

unsigned long long stdc_leading_ones_ull(unsigned long long value);

} // namespace LIBC_NAMESPACE

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

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

namespace LIBC_NAMESPACE {

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

} // namespace LIBC_NAMESPACE
18 changes: 18 additions & 0 deletions libc/src/stdbit/stdc_leading_ones_us.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Implementation header for stdc_leading_ones_uc ----------*- C++ -*-===//
nickdesaulniers marked this conversation as resolved.
Show resolved Hide resolved
//
// 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_LEADING_ONES_US_H
#define LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ONES_US_H

namespace LIBC_NAMESPACE {

unsigned short stdc_leading_ones_us(unsigned short value);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ONES_US_H
17 changes: 16 additions & 1 deletion libc/test/include/stdbit_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@ unsigned short stdc_leading_zeros_us(unsigned short) { return 0xAB; }
unsigned stdc_leading_zeros_ui(unsigned) { return 0xAC; }
unsigned long stdc_leading_zeros_ul(unsigned long) { return 0xAD; }
unsigned long long stdc_leading_zeros_ull(unsigned long long) { return 0xAF; }
unsigned char stdc_leading_ones_uc(unsigned char) { return 0xBA; }
unsigned short stdc_leading_ones_us(unsigned short) { return 0xBB; }
unsigned stdc_leading_ones_ui(unsigned) { return 0xBC; }
unsigned long stdc_leading_ones_ul(unsigned long) { return 0xBD; }
unsigned long long stdc_leading_ones_ull(unsigned long long) { return 0xBF; }

TEST(LlvmLibcStdbitTest, TypeGenericMacro) {
TEST(LlvmLibcStdbitTest, TypeGenericMacroLeadingZeros) {
EXPECT_EQ(stdc_leading_zeros(static_cast<unsigned char>(0U)),
static_cast<unsigned char>(0xAA));
EXPECT_EQ(stdc_leading_zeros(static_cast<unsigned short>(0U)),
Expand All @@ -33,3 +38,13 @@ TEST(LlvmLibcStdbitTest, TypeGenericMacro) {
EXPECT_EQ(stdc_leading_zeros(0UL), static_cast<unsigned long>(0xAD));
EXPECT_EQ(stdc_leading_zeros(0ULL), static_cast<unsigned long long>(0xAF));
}

TEST(LlvmLibcStdbitTest, TypeGenericMacroLeadingOnes) {
EXPECT_EQ(stdc_leading_ones(static_cast<unsigned char>(0U)),
static_cast<unsigned char>(0xBA));
EXPECT_EQ(stdc_leading_ones(static_cast<unsigned short>(0U)),
static_cast<unsigned short>(0xBB));
EXPECT_EQ(stdc_leading_ones(0U), static_cast<unsigned>(0xBC));
EXPECT_EQ(stdc_leading_ones(0UL), static_cast<unsigned long>(0xBD));
EXPECT_EQ(stdc_leading_ones(0ULL), static_cast<unsigned long long>(0xBF));
}
Loading
Loading