Skip to content

[libc][stdbit] implement stdc_trailing_zeros (C23) #80344

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 7 commits into from
Feb 6, 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 @@ -102,6 +102,11 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.stdbit.stdc_leading_ones_ui
libc.src.stdbit.stdc_leading_ones_ul
libc.src.stdbit.stdc_leading_ones_ull
libc.src.stdbit.stdc_trailing_zeros_uc
libc.src.stdbit.stdc_trailing_zeros_us
libc.src.stdbit.stdc_trailing_zeros_ui
libc.src.stdbit.stdc_trailing_zeros_ul
libc.src.stdbit.stdc_trailing_zeros_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 @@ -40,6 +40,21 @@ inline unsigned stdc_leading_ones(unsigned long x) {
inline unsigned stdc_leading_ones(unsigned long long x) {
return stdc_leading_ones_ull(x);
}
inline unsigned stdc_trailing_zeros(unsigned char x) {
return stdc_trailing_zeros_uc(x);
}
inline unsigned stdc_trailing_zeros(unsigned short x) {
return stdc_trailing_zeros_us(x);
}
inline unsigned stdc_trailing_zeros(unsigned x) {
return stdc_trailing_zeros_ui(x);
}
inline unsigned stdc_trailing_zeros(unsigned long x) {
return stdc_trailing_zeros_ul(x);
}
inline unsigned stdc_trailing_zeros(unsigned long long x) {
return stdc_trailing_zeros_ull(x);
}
#else
#define stdc_leading_zeros(x) \
_Generic((x), \
Expand All @@ -55,6 +70,13 @@ inline unsigned stdc_leading_ones(unsigned long long x) {
unsigned: stdc_leading_ones_ui, \
unsigned long: stdc_leading_ones_ul, \
unsigned long long: stdc_leading_ones_ull)(x)
#define stdc_trailing_zeros(x) \
_Generic((x), \
unsigned char: stdc_trailing_zeros_uc, \
unsigned short: stdc_trailing_zeros_us, \
unsigned: stdc_trailing_zeros_ui, \
unsigned long: stdc_trailing_zeros_ul, \
unsigned long long: stdc_trailing_zeros_ull)(x)
#endif // __cplusplus

#endif // __LLVM_LIBC_MACROS_STDBIT_MACROS_H
11 changes: 9 additions & 2 deletions libc/spec/stdc.td
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,9 @@ def StdC : StandardSpec<"stdc"> {
HeaderSpec StdBit = HeaderSpec<
"stdbit.h",
[
Macro<"stdc_leading_zeros">
Macro<"stdc_leading_zeros">,
Macro<"stdc_leading_ones">,
Macro<"stdc_trailing_zeros">
], // Macros
[], // Types
[], // Enumerations
Expand All @@ -784,7 +786,12 @@ def StdC : StandardSpec<"stdc"> {
FunctionSpec<"stdc_leading_ones_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
FunctionSpec<"stdc_leading_ones_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
FunctionSpec<"stdc_leading_ones_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
FunctionSpec<"stdc_leading_ones_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>
FunctionSpec<"stdc_leading_ones_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>,
FunctionSpec<"stdc_trailing_zeros_uc", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedCharType>]>,
FunctionSpec<"stdc_trailing_zeros_us", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedShortType>]>,
FunctionSpec<"stdc_trailing_zeros_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
FunctionSpec<"stdc_trailing_zeros_ul", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongType>]>,
FunctionSpec<"stdc_trailing_zeros_ull", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedLongLongType>]>
] // Functions
>;

Expand Down
118 changes: 19 additions & 99 deletions libc/src/stdbit/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,99 +1,19 @@
add_entrypoint_object(
stdc_leading_zeros_uc
SRCS
stdc_leading_zeros_uc.cpp
HDRS
stdc_leading_zeros_uc.h
DEPENDS
libc.src.__support.CPP.bit
)

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

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

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

add_entrypoint_object(
stdc_leading_zeros_ull
SRCS
stdc_leading_zeros_ull.cpp
HDRS
stdc_leading_zeros_ull.h
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
)
set(prefixes
leading_zeros
leading_ones
trailing_zeros
)
set(suffixes c s i l ll)
foreach(prefix IN LISTS prefixes)
foreach(suffix IN LISTS suffixes)
add_entrypoint_object(
stdc_${prefix}_u${suffix}
SRCS
stdc_${prefix}_u${suffix}.cpp
HDRS
stdc_${prefix}_u${suffix}.h
DEPENDS
libc.src.__support.CPP.bit
)
endforeach()
endforeach()
20 changes: 20 additions & 0 deletions libc/src/stdbit/stdc_trailing_zeros_uc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===-- Implementation of stdc_trailing_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_trailing_zeros_uc.h"

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

namespace LIBC_NAMESPACE {

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

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

namespace LIBC_NAMESPACE {

unsigned stdc_trailing_zeros_uc(unsigned char value);

} // namespace LIBC_NAMESPACE

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

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

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(unsigned, stdc_trailing_zeros_ui, (unsigned value)) {
return static_cast<unsigned>(cpp::countr_zero(value));
}

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

namespace LIBC_NAMESPACE {

unsigned stdc_trailing_zeros_ui(unsigned value);

} // namespace LIBC_NAMESPACE

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

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

namespace LIBC_NAMESPACE {

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

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

namespace LIBC_NAMESPACE {

unsigned stdc_trailing_zeros_ul(unsigned long value);

} // namespace LIBC_NAMESPACE

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

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

namespace LIBC_NAMESPACE {

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

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

namespace LIBC_NAMESPACE {

unsigned stdc_trailing_zeros_ull(unsigned long long value);

} // namespace LIBC_NAMESPACE

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

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

namespace LIBC_NAMESPACE {

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

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

namespace LIBC_NAMESPACE {

unsigned stdc_trailing_zeros_us(unsigned short value);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_STDBIT_STDC_TRAILING_ZEROS_US_H
Loading