Skip to content

[libc] implement stdc_leading_zeros_u* for stdbit.h #79669

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 10 commits into from
Jan 29, 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
7 changes: 7 additions & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.inttypes.strtoimax
libc.src.inttypes.strtoumax

# stdbit.h entrypoints
libc.src.stdbit.stdc_leading_zeros_uc
libc.src.stdbit.stdc_leading_zeros_us
libc.src.stdbit.stdc_leading_zeros_ui
libc.src.stdbit.stdc_leading_zeros_ul
libc.src.stdbit.stdc_leading_zeros_ull

# stdlib.h entrypoints
libc.src.stdlib.abs
libc.src.stdlib.atoi
Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/x86_64/headers.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ set(TARGET_PUBLIC_HEADERS
libc.include.signal
libc.include.spawn
libc.include.setjmp
libc.include.stdbit
libc.include.stdio
libc.include.stdlib
libc.include.string
Expand Down
9 changes: 9 additions & 0 deletions libc/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,15 @@ else()
message(STATUS "Skipping header signal.h as the target config is missing")
endif()

add_gen_header(
stdbit
DEF_FILE stdbit.h.def
GEN_HDR stdbit.h
DEPENDS
.llvm_libc_common_h
.llvm-libc-macros.stdbit_macros
)

add_gen_header(
stdio
DEF_FILE stdio.h.def
Expand Down
6 changes: 6 additions & 0 deletions libc/include/llvm-libc-macros/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ add_macro_header(
signal-macros.h
)

add_macro_header(
stdbit_macros
HDR
stdbit-macros.h
)

add_macro_header(
stdio_macros
HDR
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
@@ -0,0 +1,20 @@
//===-- Definition of macros to be used with stdbit functions ----------===//
//
// 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_MACROS_STDBIT_MACROS_H
#define __LLVM_LIBC_MACROS_STDBIT_MACROS_H

#define stdc_leading_zeros(x) \
_Generic((x), \
unsigned char: stdc_leading_zeros_uc, \
unsigned short: stdc_leading_zeros_us, \
unsigned: stdc_leading_zeros_ui, \
unsigned long: stdc_leading_zeros_ul, \
unsigned long long: stdc_leading_zeros_ull)(x)

#endif // __LLVM_LIBC_MACROS_STDBIT_MACROS_H
17 changes: 17 additions & 0 deletions libc/include/stdbit.h.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//===-- C standard library header stdbit.h --------------------------------===//
//
// 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_STDBIT_H
#define LLVM_LIBC_STDBIT_H

#include <__llvm-libc-common.h>
#include <llvm-libc-macros/stdbit-macros.h>

%%public_api()

#endif // LLVM_LIBC_STDBIT_H
1 change: 1 addition & 0 deletions libc/spec/spec.td
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def DoubleType : NamedType<"double">;
def LongDoubleType : NamedType<"long double">;
def CharType : NamedType<"char">;
def UnsignedCharType : NamedType<"unsigned char">;
def UnsignedShortType : NamedType<"unsigned short">;

// TODO: Add compatibility layer to use C23 type _Float128 if possible.
def Float128Type : NamedType<"__float128">;
Expand Down
19 changes: 18 additions & 1 deletion libc/spec/stdc.td
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,22 @@ def StdC : StandardSpec<"stdc"> {
]
>;

HeaderSpec StdBit = HeaderSpec<
"stdbit.h",
[
Macro<"stdc_leading_zeros">
], // Macros
[], // Types
[], // Enumerations
[
FunctionSpec<"stdc_leading_zeros_uc", RetValSpec<UnsignedCharType>, [ArgSpec<UnsignedCharType>]>,
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>]>
] // Functions
>;

HeaderSpec StdLib = HeaderSpec<
"stdlib.h",
[], // Macros
Expand Down Expand Up @@ -1141,7 +1157,7 @@ def StdC : StandardSpec<"stdc"> {
"wchar.h",
[ // Macros
Macro<"WEOF">,
],
],
[ //Types
SizeTType,
WIntType,
Expand All @@ -1167,6 +1183,7 @@ def StdC : StandardSpec<"stdc"> {
Limits,
Math,
String,
StdBit,
StdIO,
StdLib,
IntTypes,
Expand Down
5 changes: 3 additions & 2 deletions libc/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ add_subdirectory(errno)
add_subdirectory(fenv)
add_subdirectory(inttypes)
add_subdirectory(math)
add_subdirectory(string)
add_subdirectory(stdlib)
add_subdirectory(stdbit)
add_subdirectory(stdio)
add_subdirectory(stdlib)
add_subdirectory(string)
add_subdirectory(wchar)

if(${LIBC_TARGET_OS} STREQUAL "linux")
Expand Down
49 changes: 49 additions & 0 deletions libc/src/stdbit/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add DEPENDS libc.src.__support.CPP.bit to these entrypoints?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in 7a898f3, good catch (I'll get the hang of this)


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
)
21 changes: 21 additions & 0 deletions libc/src/stdbit/stdc_leading_zeros_uc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Implementation of stdc_leading_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_leading_zeros_uc.h"

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

namespace LIBC_NAMESPACE {

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

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

namespace LIBC_NAMESPACE {

unsigned char stdc_leading_zeros_uc(unsigned char value);

} // namespace LIBC_NAMESPACE

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

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

namespace LIBC_NAMESPACE {

LLVM_LIBC_FUNCTION(unsigned, stdc_leading_zeros_ui, (unsigned value)) {
return static_cast<unsigned>(cpp::countl_zero(value));
}

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

namespace LIBC_NAMESPACE {

unsigned stdc_leading_zeros_ui(unsigned value);

} // namespace LIBC_NAMESPACE

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

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

namespace LIBC_NAMESPACE {

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

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

namespace LIBC_NAMESPACE {

unsigned long stdc_leading_zeros_ul(unsigned long value);

} // namespace LIBC_NAMESPACE

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

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

namespace LIBC_NAMESPACE {

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

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

namespace LIBC_NAMESPACE {

unsigned long long stdc_leading_zeros_ull(unsigned long long value);

} // namespace LIBC_NAMESPACE

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

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

namespace LIBC_NAMESPACE {

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

} // namespace LIBC_NAMESPACE
Loading