Skip to content

Commit f0c0dda

Browse files
[libc] implement the final macros for stdbit.h support (llvm#84798)
Relevant sections of n3096: - 7.18.1p1 - 7.18.2
1 parent bae47d4 commit f0c0dda

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

libc/docs/c23.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Additions:
7575
* dfmal
7676
* fsqrt*
7777
* dsqrtl
78-
* stdbit.h (New header)
78+
* stdbit.h (New header) |check|
7979
* stdckdint.h (New header) |check|
8080
* stddef.h
8181

libc/docs/stdbit.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ Macros
110110
========================= =========
111111
Macro Name Available
112112
========================= =========
113-
__STDC_VERSION_STDBIT_H__
114-
__STDC_ENDIAN_LITTLE__
115-
__STDC_ENDIAN_BIG__
116-
__STDC_ENDIAN_NATIVE__
113+
__STDC_VERSION_STDBIT_H__ |check|
114+
__STDC_ENDIAN_LITTLE__ |check|
115+
__STDC_ENDIAN_BIG__ |check|
116+
__STDC_ENDIAN_NATIVE__ |check|
117117
stdc_leading_zeros |check|
118118
stdc_leading_ones |check|
119119
stdc_trailing_zeros |check|

libc/include/llvm-libc-macros/stdbit-macros.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
#ifndef __LLVM_LIBC_MACROS_STDBIT_MACROS_H
1010
#define __LLVM_LIBC_MACROS_STDBIT_MACROS_H
1111

12+
#define __STDC_VERSION_STDBIT_H__ 202311L
13+
#define __STDC_ENDIAN_LITTLE__ __ORDER_LITTLE_ENDIAN__
14+
#define __STDC_ENDIAN_BIG__ __ORDER_BIG_ENDIAN__
15+
#define __STDC_ENDIAN_NATIVE__ __BYTE_ORDER__
16+
1217
// TODO(https://github.com/llvm/llvm-project/issues/80509): support _BitInt().
1318
#ifdef __cplusplus
1419
inline unsigned stdc_leading_zeros(unsigned char x) {

libc/spec/stdc.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,10 @@ def StdC : StandardSpec<"stdc"> {
805805
HeaderSpec StdBit = HeaderSpec<
806806
"stdbit.h",
807807
[
808+
Macro<"__STDC_VERSION_STDBIT_H__">,
809+
Macro<"__STDC_ENDIAN_LITTLE__">,
810+
Macro<"__STDC_ENDIAN_BIG__">,
811+
Macro<"__STDC_ENDIAN_NATIVE__">,
808812
Macro<"stdc_leading_zeros">,
809813
Macro<"stdc_leading_ones">,
810814
Macro<"stdc_trailing_zeros">,

libc/test/include/stdbit_test.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,20 @@ TEST(LlvmLibcStdbitTest, TypeGenericMacroBitCeil) {
141141
EXPECT_EQ(stdc_bit_ceil(0UL), 0x6DUL);
142142
EXPECT_EQ(stdc_bit_ceil(0ULL), 0x6EULL);
143143
}
144+
145+
TEST(LlvmLibcStdbitTest, VersionMacro) {
146+
// 7.18.1p2 an integer constant expression with a value equivalent to 202311L.
147+
EXPECT_EQ(__STDC_VERSION_STDBIT_H__, 202311L);
148+
}
149+
150+
TEST(LlvmLibcStdbitTest, EndianMacros) {
151+
// 7.18.2p3 The values of the integer constant expressions for
152+
// __STDC_ENDIAN_LITTLE__ and __STDC_ENDIAN_BIG__ are not equal.
153+
EXPECT_NE(__STDC_ENDIAN_LITTLE__, __STDC_ENDIAN_BIG__);
154+
// The standard does allow for __STDC_ENDIAN_NATIVE__ to be an integer
155+
// constant expression with an implementation defined value for non-big or
156+
// little endianness environments. I assert such machines are no longer
157+
// relevant.
158+
EXPECT_TRUE(__STDC_ENDIAN_NATIVE__ == __STDC_ENDIAN_LITTLE__ ||
159+
__STDC_ENDIAN_NATIVE__ == __STDC_ENDIAN_BIG__);
160+
}

0 commit comments

Comments
 (0)