Skip to content

Commit d6c96e3

Browse files
[libc] implement stdc_leading_zeros_u* for stdbit.h
- stdbit.stdc_leading_zeros_uc - stdbit.stdc_leading_zeros_us - stdbit.stdc_leading_zeros_ui - stdbit.stdc_leading_zeros_ul - stdbit.stdc_leading_zeros_ull Test via: $ ninja libc-stdbit-tests libc_include_tests
1 parent 26cc651 commit d6c96e3

29 files changed

+522
-8
lines changed

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ set(TARGET_LIBC_ENTRYPOINTS
9191
libc.src.inttypes.strtoimax
9292
libc.src.inttypes.strtoumax
9393

94+
# stdbit.h entrypoints
95+
libc.src.stdbit.stdc_leading_zeros_uc
96+
libc.src.stdbit.stdc_leading_zeros_us
97+
libc.src.stdbit.stdc_leading_zeros_ui
98+
libc.src.stdbit.stdc_leading_zeros_ul
99+
libc.src.stdbit.stdc_leading_zeros_ull
100+
94101
# stdlib.h entrypoints
95102
libc.src.stdlib.abs
96103
libc.src.stdlib.atoi

libc/config/linux/x86_64/headers.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ set(TARGET_PUBLIC_HEADERS
1515
libc.include.signal
1616
libc.include.spawn
1717
libc.include.setjmp
18+
libc.include.stdbit
1819
libc.include.stdio
1920
libc.include.stdlib
2021
libc.include.string

libc/include/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,15 @@ else()
223223
message(STATUS "Skipping header signal.h as the target config is missing")
224224
endif()
225225

226+
add_gen_header(
227+
stdbit
228+
DEF_FILE stdbit.h.def
229+
GEN_HDR stdbit.h
230+
DEPENDS
231+
.llvm_libc_common_h
232+
.llvm-libc-macros.stdbit_macros
233+
)
234+
226235
add_gen_header(
227236
stdio
228237
DEF_FILE stdio.h.def

libc/include/llvm-libc-macros/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ add_macro_header(
111111
signal-macros.h
112112
)
113113

114+
add_macro_header(
115+
stdbit_macros
116+
HDR
117+
stdbit-macros.h
118+
)
119+
114120
add_macro_header(
115121
stdio_macros
116122
HDR
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Definition of macros to be used with stdbit functions ----------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef __LLVM_LIBC_MACROS_STDBIT_MACROS_H
10+
#define __LLVM_LIBC_MACROS_STDBIT_MACROS_H
11+
12+
#define stdc_leading_zeros(x) _Generic((x), \
13+
unsigned char: stdc_leading_zeros_uc, \
14+
unsigned short: stdc_leading_zeros_us, \
15+
unsigned: stdc_leading_zeros_ui, \
16+
unsigned long: stdc_leading_zeros_ul, \
17+
unsigned long long: stdc_leading_zeros_ull)(x)
18+
19+
#endif // __LLVM_LIBC_MACROS_STDBIT_MACROS_H

libc/include/stdbit.h.def

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//===-- C standard library header stdbit.h --------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_STDBIT_H
10+
#define LLVM_LIBC_STDBIT_H
11+
12+
#include <__llvm-libc-common.h>
13+
#include <llvm-libc-macros/stdbit-macros.h>
14+
15+
%%public_api()
16+
17+
#endif // LLVM_LIBC_STDBIT_H

libc/spec/spec.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def DoubleType : NamedType<"double">;
5050
def LongDoubleType : NamedType<"long double">;
5151
def CharType : NamedType<"char">;
5252
def UnsignedCharType : NamedType<"unsigned char">;
53+
def UnsignedShortType : NamedType<"unsigned short">;
5354

5455
// TODO: Add compatibility layer to use C23 type _Float128 if possible.
5556
def Float128Type : NamedType<"__float128">;

libc/spec/stdc.td

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,22 @@ def StdC : StandardSpec<"stdc"> {
767767
]
768768
>;
769769

770+
HeaderSpec StdBit = HeaderSpec<
771+
"stdbit.h",
772+
[
773+
Macro<"stdc_leading_zeros">
774+
], // Macros
775+
[], // Types
776+
[], // Enumerations
777+
[
778+
FunctionSpec<"stdc_leading_zeros_uc", RetValSpec<UnsignedCharType>, [ArgSpec<UnsignedCharType>]>,
779+
FunctionSpec<"stdc_leading_zeros_us", RetValSpec<UnsignedShortType>, [ArgSpec<UnsignedShortType>]>,
780+
FunctionSpec<"stdc_leading_zeros_ui", RetValSpec<UnsignedIntType>, [ArgSpec<UnsignedIntType>]>,
781+
FunctionSpec<"stdc_leading_zeros_ul", RetValSpec<UnsignedLongType>, [ArgSpec<UnsignedLongType>]>,
782+
FunctionSpec<"stdc_leading_zeros_ull", RetValSpec<UnsignedLongLongType>, [ArgSpec<UnsignedLongLongType>]>
783+
] // Functions
784+
>;
785+
770786
HeaderSpec StdLib = HeaderSpec<
771787
"stdlib.h",
772788
[], // Macros
@@ -1141,7 +1157,7 @@ def StdC : StandardSpec<"stdc"> {
11411157
"wchar.h",
11421158
[ // Macros
11431159
Macro<"WEOF">,
1144-
],
1160+
],
11451161
[ //Types
11461162
SizeTType,
11471163
WIntType,
@@ -1167,6 +1183,7 @@ def StdC : StandardSpec<"stdc"> {
11671183
Limits,
11681184
Math,
11691185
String,
1186+
StdBit,
11701187
StdIO,
11711188
StdLib,
11721189
IntTypes,

libc/src/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ add_subdirectory(errno)
55
add_subdirectory(fenv)
66
add_subdirectory(inttypes)
77
add_subdirectory(math)
8-
add_subdirectory(string)
9-
add_subdirectory(stdlib)
8+
add_subdirectory(stdbit)
109
add_subdirectory(stdio)
10+
add_subdirectory(stdlib)
11+
add_subdirectory(string)
1112
add_subdirectory(wchar)
1213

1314
if(${LIBC_TARGET_OS} STREQUAL "linux")

libc/src/stdbit/CMakeLists.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
add_entrypoint_object(
2+
stdc_leading_zeros_uc
3+
SRCS
4+
stdc_leading_zeros_uc.cpp
5+
HDRS
6+
stdc_leading_zeros_uc.h
7+
)
8+
9+
add_entrypoint_object(
10+
stdc_leading_zeros_us
11+
SRCS
12+
stdc_leading_zeros_us.cpp
13+
HDRS
14+
stdc_leading_zeros_us.h
15+
)
16+
17+
add_entrypoint_object(
18+
stdc_leading_zeros_ui
19+
SRCS
20+
stdc_leading_zeros_ui.cpp
21+
HDRS
22+
stdc_leading_zeros_ui.h
23+
)
24+
25+
add_entrypoint_object(
26+
stdc_leading_zeros_ul
27+
SRCS
28+
stdc_leading_zeros_ul.cpp
29+
HDRS
30+
stdc_leading_zeros_ul.h
31+
)
32+
33+
add_entrypoint_object(
34+
stdc_leading_zeros_ull
35+
SRCS
36+
stdc_leading_zeros_ull.cpp
37+
HDRS
38+
stdc_leading_zeros_ull.h
39+
)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of stdc_leading_zeros_uc ---------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/stdbit/stdc_leading_zeros_uc.h"
10+
11+
#include "src/__support/CPP/bit.h"
12+
#include "src/__support/common.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
LLVM_LIBC_FUNCTION(unsigned char, stdc_leading_zeros_uc, (unsigned char value)) {
17+
return static_cast<unsigned char>(cpp::countl_zero(value));
18+
}
19+
20+
} // namespace LIBC_NAMESPACE
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for stdc_leading_zeros_uc ---------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ZEROS_UC_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ZEROS_UC_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned char stdc_leading_zeros_uc(unsigned char value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ZEROS_UC_H
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of stdc_leading_zeros_ui ---------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/stdbit/stdc_leading_zeros_ui.h"
10+
11+
#include "src/__support/CPP/bit.h"
12+
#include "src/__support/common.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
LLVM_LIBC_FUNCTION(unsigned, stdc_leading_zeros_ui, (unsigned value)) {
17+
return static_cast<unsigned>(cpp::countl_zero(value));
18+
}
19+
20+
} // namespace LIBC_NAMESPACE
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for stdc_leading_zeros_ui ---------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ZEROS_UI_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ZEROS_UI_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned stdc_leading_zeros_ui(unsigned value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ZEROS_UI_H
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of stdc_leading_zeros_ul ---------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/stdbit/stdc_leading_zeros_ul.h"
10+
11+
#include "src/__support/CPP/bit.h"
12+
#include "src/__support/common.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
LLVM_LIBC_FUNCTION(unsigned long, stdc_leading_zeros_ul, (unsigned long value)) {
17+
return static_cast<unsigned long>(cpp::countl_zero(value));
18+
}
19+
20+
} // namespace LIBC_NAMESPACE
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for stdc_leading_zeros_ul ---------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ZEROS_UL_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ZEROS_UL_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned long stdc_leading_zeros_ul(unsigned long value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ZEROS_UL_H
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of stdc_leading_zeros_ull --------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/stdbit/stdc_leading_zeros_ull.h"
10+
11+
#include "src/__support/CPP/bit.h"
12+
#include "src/__support/common.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
LLVM_LIBC_FUNCTION(unsigned long long, stdc_leading_zeros_ull, (unsigned long long value)) {
17+
return static_cast<unsigned long long>(cpp::countl_zero(value));
18+
}
19+
20+
} // namespace LIBC_NAMESPACE
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for stdc_leading_zeros_ull --------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ZEROS_ULL_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ZEROS_ULL_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned long long stdc_leading_zeros_ull(unsigned long long value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ZEROS_ULL_H
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- Implementation of stdc_leading_zeros_us ---------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/stdbit/stdc_leading_zeros_us.h"
10+
11+
#include "src/__support/CPP/bit.h"
12+
#include "src/__support/common.h"
13+
14+
namespace LIBC_NAMESPACE {
15+
16+
LLVM_LIBC_FUNCTION(unsigned short, stdc_leading_zeros_us, (unsigned short value)) {
17+
return static_cast<unsigned short>(cpp::countl_zero(value));
18+
}
19+
20+
} // namespace LIBC_NAMESPACE
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===-- Implementation header for stdc_leading_zeros_us ---------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ZEROS_US_H
10+
#define LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ZEROS_US_H
11+
12+
namespace LIBC_NAMESPACE {
13+
14+
unsigned short stdc_leading_zeros_us(unsigned short value);
15+
16+
} // namespace LIBC_NAMESPACE
17+
18+
#endif // LLVM_LIBC_SRC_STDBIT_STDC_LEADING_ZEROS_US_H

0 commit comments

Comments
 (0)