Skip to content

[libc] implement the final macros for stdbit.h support #84798

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 1 commit into from
Mar 12, 2024

Conversation

nickdesaulniers
Copy link
Member

Relevant sections of n3096:

  • 7.18.1p1
  • 7.18.2

Relevant sections of n3096:
- 7.18.1p1
- 7.18.2
@llvmbot llvmbot added the libc label Mar 11, 2024
@llvmbot
Copy link
Member

llvmbot commented Mar 11, 2024

@llvm/pr-subscribers-libc

Author: Nick Desaulniers (nickdesaulniers)

Changes

Relevant sections of n3096:

  • 7.18.1p1
  • 7.18.2

Full diff: https://github.com/llvm/llvm-project/pull/84798.diff

5 Files Affected:

  • (modified) libc/docs/c23.rst (+1-1)
  • (modified) libc/docs/stdbit.rst (+4-4)
  • (modified) libc/include/llvm-libc-macros/stdbit-macros.h (+5)
  • (modified) libc/spec/stdc.td (+4)
  • (modified) libc/test/include/stdbit_test.cpp (+17)
diff --git a/libc/docs/c23.rst b/libc/docs/c23.rst
index 24cef8539393df..3f64722bc8e644 100644
--- a/libc/docs/c23.rst
+++ b/libc/docs/c23.rst
@@ -75,7 +75,7 @@ Additions:
   * dfmal
   * fsqrt*
   * dsqrtl
-* stdbit.h (New header)
+* stdbit.h (New header)    |check|
 * stdckdint.h (New header) |check|
 * stddef.h
 
diff --git a/libc/docs/stdbit.rst b/libc/docs/stdbit.rst
index 9b4974cf1479b1..d42f7938246293 100644
--- a/libc/docs/stdbit.rst
+++ b/libc/docs/stdbit.rst
@@ -110,10 +110,10 @@ Macros
 =========================  =========
 Macro Name                 Available
 =========================  =========
-__STDC_VERSION_STDBIT_H__
-__STDC_ENDIAN_LITTLE__
-__STDC_ENDIAN_BIG__
-__STDC_ENDIAN_NATIVE__
+__STDC_VERSION_STDBIT_H__  |check|
+__STDC_ENDIAN_LITTLE__     |check|
+__STDC_ENDIAN_BIG__        |check|
+__STDC_ENDIAN_NATIVE__     |check|
 stdc_leading_zeros         |check|
 stdc_leading_ones          |check|
 stdc_trailing_zeros        |check|
diff --git a/libc/include/llvm-libc-macros/stdbit-macros.h b/libc/include/llvm-libc-macros/stdbit-macros.h
index 10c0fac3c8dd86..c5b2f097783440 100644
--- a/libc/include/llvm-libc-macros/stdbit-macros.h
+++ b/libc/include/llvm-libc-macros/stdbit-macros.h
@@ -9,6 +9,11 @@
 #ifndef __LLVM_LIBC_MACROS_STDBIT_MACROS_H
 #define __LLVM_LIBC_MACROS_STDBIT_MACROS_H
 
+#define __STDC_VERSION_STDBIT_H__ 202311L
+#define __STDC_ENDIAN_LITTLE__ __ORDER_LITTLE_ENDIAN__
+#define __STDC_ENDIAN_BIG__ __ORDER_BIG_ENDIAN__
+#define __STDC_ENDIAN_NATIVE__ __BYTE_ORDER__
+
 // TODO(https://github.com/llvm/llvm-project/issues/80509): support _BitInt().
 #ifdef __cplusplus
 inline unsigned stdc_leading_zeros(unsigned char x) {
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index d91f5c1f723345..8d03f2b30368b2 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -804,6 +804,10 @@ def StdC : StandardSpec<"stdc"> {
   HeaderSpec StdBit = HeaderSpec<
       "stdbit.h",
       [
+        Macro<"__STDC_VERSION_STDBIT_H__">,
+        Macro<"__STDC_ENDIAN_LITTLE__">,
+        Macro<"__STDC_ENDIAN_BIG__">,
+        Macro<"__STDC_ENDIAN_NATIVE__">,
         Macro<"stdc_leading_zeros">,
         Macro<"stdc_leading_ones">,
         Macro<"stdc_trailing_zeros">,
diff --git a/libc/test/include/stdbit_test.cpp b/libc/test/include/stdbit_test.cpp
index 6c12665c4454d0..dedab3d82bb2db 100644
--- a/libc/test/include/stdbit_test.cpp
+++ b/libc/test/include/stdbit_test.cpp
@@ -224,3 +224,20 @@ TEST(LlvmLibcStdbitTest, TypeGenericMacroBitCeil) {
   EXPECT_EQ(stdc_bit_ceil(0UL), 0x6DUL);
   EXPECT_EQ(stdc_bit_ceil(0ULL), 0x6EULL);
 }
+
+TEST(LlvmLibcStdbitTest, VersionMacro) {
+  // 7.18.1p2 an integer constant expression with a value equivalent to 202311L.
+  EXPECT_EQ(__STDC_VERSION_STDBIT_H__, 202311L);
+}
+
+TEST(LlvmLibcStdbitTest, EndianMacros) {
+  // 7.18.2p3 The values of the integer constant expressions for
+  // __STDC_ENDIAN_LITTLE__ and __STDC_ENDIAN_BIG__ are not equal.
+  EXPECT_NE(__STDC_ENDIAN_LITTLE__, __STDC_ENDIAN_BIG__);
+  // The standard does allow for __STDC_ENDIAN_NATIVE__ to be an integer
+  // constant expression with an implementation defined value for non-big or
+  // little endianness environments.  I assert such machines are no longer
+  // relevant.
+  EXPECT_TRUE(__STDC_ENDIAN_NATIVE__ == __STDC_ENDIAN_LITTLE__ ||
+              __STDC_ENDIAN_NATIVE__ == __STDC_ENDIAN_BIG__);
+}

@nickdesaulniers nickdesaulniers merged commit f0c0dda into llvm:main Mar 12, 2024
@nickdesaulniers nickdesaulniers deleted the final_stdbit_macros branch March 12, 2024 15:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants