[libc-commits] [libc] [libc][stdbit] Update generated stdbit generic macros and add C tests. (PR #84670)

via libc-commits libc-commits at lists.llvm.org
Mon Mar 11 11:23:51 PDT 2024


https://github.com/lntue updated https://github.com/llvm/llvm-project/pull/84670

>From 541d4046dd7a9ab07988335aa50806c0f53a6de6 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue at google.com>
Date: Sun, 10 Mar 2024 01:44:43 -0500
Subject: [PATCH 1/5] [libc][stdbit] Update generated stdbit generic macros and
 add C tests.

---
 libc/include/llvm-libc-macros/stdbit-macros.h | 120 ++++--------------
 libc/test/include/CMakeLists.txt              |  25 ++++
 libc/test/include/stdbit_stub.h               |  73 +++++++++++
 libc/test/include/stdbit_test.c               |  61 +++++++++
 libc/test/include/stdbit_test.cpp             |  85 +------------
 5 files changed, 186 insertions(+), 178 deletions(-)
 create mode 100644 libc/test/include/stdbit_stub.h
 create mode 100644 libc/test/include/stdbit_test.c

diff --git a/libc/include/llvm-libc-macros/stdbit-macros.h b/libc/include/llvm-libc-macros/stdbit-macros.h
index 10c0fac3c8dd86..8c1eee0b35abc3 100644
--- a/libc/include/llvm-libc-macros/stdbit-macros.h
+++ b/libc/include/llvm-libc-macros/stdbit-macros.h
@@ -208,104 +208,36 @@ inline unsigned long long stdc_bit_ceil(unsigned long long x) {
   return stdc_bit_ceil_ull(x);
 }
 #else
-#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)
-#define stdc_leading_ones(x)                                                   \
-  _Generic((x),                                                                \
-      unsigned char: stdc_leading_ones_uc,                                     \
-      unsigned short: stdc_leading_ones_us,                                    \
-      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)
-#define stdc_trailing_ones(x)                                                  \
-  _Generic((x),                                                                \
-      unsigned char: stdc_trailing_ones_uc,                                    \
-      unsigned short: stdc_trailing_ones_us,                                   \
-      unsigned: stdc_trailing_ones_ui,                                         \
-      unsigned long: stdc_trailing_ones_ul,                                    \
-      unsigned long long: stdc_trailing_ones_ull)(x)
+
+#define STDBIT_GENERIC_FUNC(FUNC_NAME, x)                                      \
+  _Generic((x), unsigned char                                                  \
+           : FUNC_NAME##_uc, unsigned short                                    \
+           : FUNC_NAME##_us, unsigned                                          \
+           : FUNC_NAME##_ui, unsigned long                                     \
+           : FUNC_NAME##_ul, unsigned long long                                \
+           : FUNC_NAME##_ull)(x)
+
+#define stdc_leading_zeros(x) STDBIT_GENERIC_FUNC(stdc_leading_zeros, (x))
+#define stdc_leading_ones(x) STDBIT_GENERIC_FUNC(stdc_leading_ones, (x))
+#define stdc_trailing_zeros(x) STDBIT_GENERIC_FUNC(stdc_trailing_zeros, (x))
+#define stdc_trailing_ones(x) STDBIT_GENERIC_FUNC(stdc_trailing_ones, (x))
 #define stdc_first_leading_zero(x)                                             \
-  _Generic((x),                                                                \
-      unsigned char: stdc_first_leading_zero_uc,                               \
-      unsigned short: stdc_first_leading_zero_us,                              \
-      unsigned: stdc_first_leading_zero_ui,                                    \
-      unsigned long: stdc_first_leading_zero_ul,                               \
-      unsigned long long: stdc_first_leading_zero_ull)(x)
+  STDBIT_GENERIC_FUNC(stdc_first_leading_zero, (x))
 #define stdc_first_leading_one(x)                                              \
-  _Generic((x),                                                                \
-      unsigned char: stdc_first_leading_one_uc,                                \
-      unsigned short: stdc_first_leading_one_us,                               \
-      unsigned: stdc_first_leading_one_ui,                                     \
-      unsigned long: stdc_first_leading_one_ul,                                \
-      unsigned long long: stdc_first_leading_one_ull)(x)
+  STDBIT_GENERIC_FUNC(stdc_first_leading_one, (x))
 #define stdc_first_trailing_zero(x)                                            \
-  _Generic((x),                                                                \
-      unsigned char: stdc_first_trailing_zero_uc,                              \
-      unsigned short: stdc_first_trailing_zero_us,                             \
-      unsigned: stdc_first_trailing_zero_ui,                                   \
-      unsigned long: stdc_first_trailing_zero_ul,                              \
-      unsigned long long: stdc_first_trailing_zero_ull)(x)
+  STDBIT_GENERIC_FUNC(stdc_first_trailing_zero, (x))
 #define stdc_first_trailing_one(x)                                             \
-  _Generic((x),                                                                \
-      unsigned char: stdc_first_trailing_one_uc,                               \
-      unsigned short: stdc_first_trailing_one_us,                              \
-      unsigned: stdc_first_trailing_one_ui,                                    \
-      unsigned long: stdc_first_trailing_one_ul,                               \
-      unsigned long long: stdc_first_trailing_one_ull)(x)
-#define stdc_count_zeros(x)                                                    \
-  _Generic((x),                                                                \
-      unsigned char: stdc_count_zeros_uc,                                      \
-      unsigned short: stdc_count_zeros_us,                                     \
-      unsigned: stdc_count_zeros_ui,                                           \
-      unsigned long: stdc_count_zeros_ul,                                      \
-      unsigned long long: stdc_count_zeros_ull)(x)
-#define stdc_count_ones(x)                                                     \
-  _Generic((x),                                                                \
-      unsigned char: stdc_count_ones_uc,                                       \
-      unsigned short: stdc_count_ones_us,                                      \
-      unsigned: stdc_count_ones_ui,                                            \
-      unsigned long: stdc_count_ones_ul,                                       \
-      unsigned long long: stdc_count_ones_ull)(x)
-#define stdc_has_single_bit(x)                                                 \
-  _Generic((x),                                                                \
-      unsigned char: stdc_has_single_bit_uc,                                   \
-      unsigned short: stdc_has_single_bit_us,                                  \
-      unsigned: stdc_has_single_bit_ui,                                        \
-      unsigned long: stdc_has_single_bit_ul,                                   \
-      unsigned long long: stdc_has_single_bit_ull)(x)
-#define stdc_bit_width(x)                                                      \
-  _Generic((x),                                                                \
-      unsigned char: stdc_bit_width_uc,                                        \
-      unsigned short: stdc_bit_width_us,                                       \
-      unsigned: stdc_bit_width_ui,                                             \
-      unsigned long: stdc_bit_width_ul,                                        \
-      unsigned long long: stdc_bit_width_ull)(x)
-#define stdc_bit_floor(x)                                                      \
-  _Generic((x),                                                                \
-      unsigned char: stdc_bit_floor_uc,                                        \
-      unsigned short: stdc_bit_floor_us,                                       \
-      unsigned: stdc_bit_floor_ui,                                             \
-      unsigned long: stdc_bit_floor_ul,                                        \
-      unsigned long long: stdc_bit_floor_ull)(x)
-#define stdc_bit_ceil(x)                                                       \
-  _Generic((x),                                                                \
-      unsigned char: stdc_bit_ceil_uc,                                         \
-      unsigned short: stdc_bit_ceil_us,                                        \
-      unsigned: stdc_bit_ceil_ui,                                              \
-      unsigned long: stdc_bit_ceil_ul,                                         \
-      unsigned long long: stdc_bit_ceil_ull)(x)
+  STDBIT_GENERIC_FUNC(stdc_first_trailing_one, (x))
+#define stdc_count_zeros(x) STDBIT_GENERIC_FUNC(stdc_count_zeros, (x))
+#define stdc_count_ones(x) STDBIT_GENERIC_FUNC(stdc_count_ones, (x))
+#define stdc_has_single_bit(x) STDBIT_GENERIC_FUNC(stdc_has_single_bit, (x))
+#define stdc_bit_width(x) STDBIT_GENERIC_FUNC(stdc_bit_width, (x))
+#define stdc_bit_floor(x) STDBIT_GENERIC_FUNC(stdc_bit_floor, (x))
+#define stdc_bit_ceil(x) STDBIT_GENERIC_FUNC(stdc_bit_ceil, (x))
+
+#undef STDBIT_GENERIC_FUNC
+
 #endif // __cplusplus
 
 #endif // __LLVM_LIBC_MACROS_STDBIT_MACROS_H
diff --git a/libc/test/include/CMakeLists.txt b/libc/test/include/CMakeLists.txt
index bf845c94170f97..d76ad442d36ce4 100644
--- a/libc/test/include/CMakeLists.txt
+++ b/libc/test/include/CMakeLists.txt
@@ -22,16 +22,41 @@ if(LLVM_LIBC_FULL_BUILD AND libc.include.stdbit IN_LIST TARGET_PUBLIC_HEADERS)
     stdbit_test
     SUITE
       libc_include_tests
+    HDRS
+      stdbit_stub.h
     SRCS
       stdbit_test.cpp
     DEPENDS
       libc.include.llvm-libc-macros.stdbit_macros
+      libc.include.llvm_libc_common_h
       libc.include.stdbit
       # Intentionally do not depend on libc.src.stdbit.*. The include test is
       # simply testing the macros provided by stdbit.h, not the implementation
       # of the underlying functions which the type generic macros may dispatch
       # to.
   )
+  add_libc_test(
+    stdbit_c_test
+    UNIT_TEST_ONLY
+    SUITE
+      libc_include_tests
+    HDRS
+      stdbit_stub.h
+    SRCS
+      stdbit_test.c
+    COMPILE_OPTIONS
+      -Wall
+      -Werror
+    DEPENDS
+      libc.include.llvm-libc-macros.stdbit_macros
+      libc.include.llvm_libc_common_h
+      libc.include.stdbit
+      libc.src.assert.__assert_fail
+      # Intentionally do not depend on libc.src.stdbit.*. The include test is
+      # simply testing the macros provided by stdbit.h, not the implementation
+      # of the underlying functions which the type generic macros may dispatch
+      # to.
+  )
 endif()
 
 add_libc_test(
diff --git a/libc/test/include/stdbit_stub.h b/libc/test/include/stdbit_stub.h
new file mode 100644
index 00000000000000..65b1ca3b2c2976
--- /dev/null
+++ b/libc/test/include/stdbit_stub.h
@@ -0,0 +1,73 @@
+//===-- Utilities for testing stdbit --------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+/*
+ * Declare these BEFORE including stdbit-macros.h so that this test may still be
+ * run even if a given target doesn't yet have these individual entrypoints
+ * enabled.
+ */
+
+#include "include/__llvm-libc-common.h"
+
+#include <stdbool.h> // bool in C
+
+#define STDBIT_STUB_FUNCTION(FUNC_NAME, LEADING_VAL)                           \
+  unsigned FUNC_NAME##_uc(unsigned char x) __NOEXCEPT {                        \
+    return LEADING_VAL##AU;                                                    \
+  }                                                                            \
+  unsigned FUNC_NAME##_us(unsigned short x) __NOEXCEPT {                       \
+    return LEADING_VAL##BU;                                                    \
+  }                                                                            \
+  unsigned FUNC_NAME##_ui(unsigned int x) __NOEXCEPT {                         \
+    return LEADING_VAL##CU;                                                    \
+  }                                                                            \
+  unsigned FUNC_NAME##_ul(unsigned long x) __NOEXCEPT {                        \
+    return LEADING_VAL##DU;                                                    \
+  }                                                                            \
+  unsigned FUNC_NAME##_ull(unsigned long long x) __NOEXCEPT {                  \
+    return LEADING_VAL##EU;                                                    \
+  }
+
+__BEGIN_C_DECLS
+
+STDBIT_STUB_FUNCTION(stdc_leading_zeros, 0xA)
+STDBIT_STUB_FUNCTION(stdc_leading_ones, 0xB)
+STDBIT_STUB_FUNCTION(stdc_trailing_zeros, 0xC)
+STDBIT_STUB_FUNCTION(stdc_trailing_ones, 0xD)
+STDBIT_STUB_FUNCTION(stdc_first_leading_zero, 0xE)
+STDBIT_STUB_FUNCTION(stdc_first_leading_one, 0xF)
+STDBIT_STUB_FUNCTION(stdc_first_trailing_zero, 0x0)
+STDBIT_STUB_FUNCTION(stdc_first_trailing_one, 0x1)
+STDBIT_STUB_FUNCTION(stdc_count_zeros, 0x2)
+STDBIT_STUB_FUNCTION(stdc_count_ones, 0x3)
+
+bool stdc_has_single_bit_uc(unsigned char x) __NOEXCEPT { return false; }
+bool stdc_has_single_bit_us(unsigned short x) __NOEXCEPT { return false; }
+bool stdc_has_single_bit_ui(unsigned x) __NOEXCEPT { return false; }
+bool stdc_has_single_bit_ul(unsigned long x) __NOEXCEPT { return false; }
+bool stdc_has_single_bit_ull(unsigned long long x) __NOEXCEPT { return false; }
+
+STDBIT_STUB_FUNCTION(stdc_bit_width, 0x4)
+
+unsigned char stdc_bit_floor_uc(unsigned char x) __NOEXCEPT { return 0x5AU; }
+unsigned short stdc_bit_floor_us(unsigned short x) __NOEXCEPT { return 0x5BU; }
+unsigned stdc_bit_floor_ui(unsigned x) __NOEXCEPT { return 0x5CU; }
+unsigned long stdc_bit_floor_ul(unsigned long x) __NOEXCEPT { return 0x5DUL; }
+unsigned long long stdc_bit_floor_ull(unsigned long long x) __NOEXCEPT {
+  return 0x5EULL;
+}
+
+unsigned char stdc_bit_ceil_uc(unsigned char x) __NOEXCEPT { return 0x6AU; }
+unsigned short stdc_bit_ceil_us(unsigned short x) __NOEXCEPT { return 0x6BU; }
+unsigned stdc_bit_ceil_ui(unsigned x) __NOEXCEPT { return 0x6CU; }
+unsigned long stdc_bit_ceil_ul(unsigned long x) __NOEXCEPT { return 0x6DUL; }
+unsigned long long stdc_bit_ceil_ull(unsigned long long x) __NOEXCEPT {
+  return 0x6EULL;
+}
+
+__END_C_DECLS
diff --git a/libc/test/include/stdbit_test.c b/libc/test/include/stdbit_test.c
new file mode 100644
index 00000000000000..99680776cf94c0
--- /dev/null
+++ b/libc/test/include/stdbit_test.c
@@ -0,0 +1,61 @@
+//===-- Unittests for stdbit ----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+/*
+ * The intent of this test is validate that:
+ * 1. We provide the definition of the various type generic macros of stdbit.h
+ * (the macros are transitively included from stdbit-macros.h by stdbit.h).
+ * 2. It dispatches to the correct underlying function.
+ * Because unit tests build without public packaging, the object files produced
+ * do not contain non-namespaced symbols.
+ */
+
+/*
+ * Declare these BEFORE including stdbit-macros.h so that this test may still be
+ * run even if a given target doesn't yet have these individual entrypoints
+ * enabled.
+ */
+#include "stdbit_stub.h"
+
+#include "include/llvm-libc-macros/stdbit-macros.h"
+
+#include <assert.h>
+
+#define CHECK_FUNCTION(FUNC_NAME, VAL)                                         \
+  do {                                                                         \
+    assert(FUNC_NAME((unsigned char)0U) == VAL##AU);                           \
+    assert(FUNC_NAME((unsigned short)0U) == VAL##BU);                          \
+    assert(FUNC_NAME(0U) == VAL##CU);                                          \
+    assert(FUNC_NAME(0UL) == VAL##DU);                                         \
+    assert(FUNC_NAME(0ULL) == VAL##EU);                                        \
+  } while (0)
+
+int main(void) {
+  CHECK_FUNCTION(stdc_leading_zeros, 0xA);
+  CHECK_FUNCTION(stdc_leading_ones, 0xB);
+  CHECK_FUNCTION(stdc_trailing_zeros, 0xC);
+  CHECK_FUNCTION(stdc_trailing_ones, 0xD);
+  CHECK_FUNCTION(stdc_first_leading_zero, 0xE);
+  CHECK_FUNCTION(stdc_first_leading_one, 0xF);
+  CHECK_FUNCTION(stdc_first_trailing_zero, 0x0);
+  CHECK_FUNCTION(stdc_first_trailing_one, 0x1);
+  CHECK_FUNCTION(stdc_count_zeros, 0x2);
+  CHECK_FUNCTION(stdc_count_ones, 0x3);
+
+  assert(!stdc_has_single_bit((unsigned char)1U));
+  assert(!stdc_has_single_bit((unsigned short)1U));
+  assert(!stdc_has_single_bit(1U));
+  assert(!stdc_has_single_bit(1UL));
+  assert(!stdc_has_single_bit(1ULL));
+
+  CHECK_FUNCTION(stdc_bit_width, 0x4);
+  CHECK_FUNCTION(stdc_bit_floor, 0x5);
+  CHECK_FUNCTION(stdc_bit_ceil, 0x6);
+  
+  return 0;
+}
diff --git a/libc/test/include/stdbit_test.cpp b/libc/test/include/stdbit_test.cpp
index 6c12665c4454d0..f3227eb86959eb 100644
--- a/libc/test/include/stdbit_test.cpp
+++ b/libc/test/include/stdbit_test.cpp
@@ -22,90 +22,7 @@
  * run even if a given target doesn't yet have these individual entrypoints
  * enabled.
  */
-extern "C" {
-unsigned stdc_leading_zeros_uc(unsigned char) noexcept { return 0xAAU; }
-unsigned stdc_leading_zeros_us(unsigned short) noexcept { return 0xABU; }
-unsigned stdc_leading_zeros_ui(unsigned) noexcept { return 0xACU; }
-unsigned stdc_leading_zeros_ul(unsigned long) noexcept { return 0xADU; }
-unsigned stdc_leading_zeros_ull(unsigned long long) noexcept { return 0xAEU; }
-unsigned stdc_leading_ones_uc(unsigned char) noexcept { return 0xBAU; }
-unsigned stdc_leading_ones_us(unsigned short) noexcept { return 0xBBU; }
-unsigned stdc_leading_ones_ui(unsigned) noexcept { return 0xBCU; }
-unsigned stdc_leading_ones_ul(unsigned long) noexcept { return 0xBDU; }
-unsigned stdc_leading_ones_ull(unsigned long long) noexcept { return 0xBEU; }
-unsigned stdc_trailing_zeros_uc(unsigned char) noexcept { return 0xCAU; }
-unsigned stdc_trailing_zeros_us(unsigned short) noexcept { return 0xCBU; }
-unsigned stdc_trailing_zeros_ui(unsigned) noexcept { return 0xCCU; }
-unsigned stdc_trailing_zeros_ul(unsigned long) noexcept { return 0xCDU; }
-unsigned stdc_trailing_zeros_ull(unsigned long long) noexcept { return 0xCEU; }
-unsigned stdc_trailing_ones_uc(unsigned char) noexcept { return 0xDAU; }
-unsigned stdc_trailing_ones_us(unsigned short) noexcept { return 0xDBU; }
-unsigned stdc_trailing_ones_ui(unsigned) noexcept { return 0xDCU; }
-unsigned stdc_trailing_ones_ul(unsigned long) noexcept { return 0xDDU; }
-unsigned stdc_trailing_ones_ull(unsigned long long) noexcept { return 0xDEU; }
-unsigned stdc_first_leading_zero_uc(unsigned char) noexcept { return 0xEAU; }
-unsigned stdc_first_leading_zero_us(unsigned short) noexcept { return 0xEBU; }
-unsigned stdc_first_leading_zero_ui(unsigned) noexcept { return 0xECU; }
-unsigned stdc_first_leading_zero_ul(unsigned long) noexcept { return 0xEDU; }
-unsigned stdc_first_leading_zero_ull(unsigned long long) noexcept {
-  return 0xEEU;
-}
-unsigned stdc_first_leading_one_uc(unsigned char) noexcept { return 0xFAU; }
-unsigned stdc_first_leading_one_us(unsigned short) noexcept { return 0xFBU; }
-unsigned stdc_first_leading_one_ui(unsigned) noexcept { return 0xFCU; }
-unsigned stdc_first_leading_one_ul(unsigned long) noexcept { return 0xFDU; }
-unsigned stdc_first_leading_one_ull(unsigned long long) noexcept {
-  return 0xFEU;
-}
-unsigned stdc_first_trailing_zero_uc(unsigned char) noexcept { return 0x0AU; }
-unsigned stdc_first_trailing_zero_us(unsigned short) noexcept { return 0x0BU; }
-unsigned stdc_first_trailing_zero_ui(unsigned) noexcept { return 0x0CU; }
-unsigned stdc_first_trailing_zero_ul(unsigned long) noexcept { return 0x0DU; }
-unsigned stdc_first_trailing_zero_ull(unsigned long long) noexcept {
-  return 0x0EU;
-}
-unsigned stdc_first_trailing_one_uc(unsigned char) noexcept { return 0x1AU; }
-unsigned stdc_first_trailing_one_us(unsigned short) noexcept { return 0x1BU; }
-unsigned stdc_first_trailing_one_ui(unsigned) noexcept { return 0x1CU; }
-unsigned stdc_first_trailing_one_ul(unsigned long) noexcept { return 0x1DU; }
-unsigned stdc_first_trailing_one_ull(unsigned long long) noexcept {
-  return 0x1EU;
-}
-unsigned stdc_count_zeros_uc(unsigned char) noexcept { return 0x2AU; }
-unsigned stdc_count_zeros_us(unsigned short) noexcept { return 0x2BU; }
-unsigned stdc_count_zeros_ui(unsigned) noexcept { return 0x2CU; }
-unsigned stdc_count_zeros_ul(unsigned long) noexcept { return 0x2DU; }
-unsigned stdc_count_zeros_ull(unsigned long long) noexcept { return 0x2EU; }
-unsigned stdc_count_ones_uc(unsigned char) noexcept { return 0x3AU; }
-unsigned stdc_count_ones_us(unsigned short) noexcept { return 0x3BU; }
-unsigned stdc_count_ones_ui(unsigned) noexcept { return 0x3CU; }
-unsigned stdc_count_ones_ul(unsigned long) noexcept { return 0x3DU; }
-unsigned stdc_count_ones_ull(unsigned long long) noexcept { return 0x3EU; }
-bool stdc_has_single_bit_uc(unsigned char) noexcept { return false; }
-bool stdc_has_single_bit_us(unsigned short) noexcept { return false; }
-bool stdc_has_single_bit_ui(unsigned) noexcept { return false; }
-bool stdc_has_single_bit_ul(unsigned long) noexcept { return false; }
-bool stdc_has_single_bit_ull(unsigned long long) noexcept { return false; }
-unsigned stdc_bit_width_uc(unsigned char) noexcept { return 0x4AU; }
-unsigned stdc_bit_width_us(unsigned short) noexcept { return 0x4BU; }
-unsigned stdc_bit_width_ui(unsigned) noexcept { return 0x4CU; }
-unsigned stdc_bit_width_ul(unsigned long) noexcept { return 0x4DU; }
-unsigned stdc_bit_width_ull(unsigned long long) noexcept { return 0x4EU; }
-unsigned char stdc_bit_floor_uc(unsigned char) noexcept { return 0x5AU; }
-unsigned short stdc_bit_floor_us(unsigned short) noexcept { return 0x5BU; }
-unsigned stdc_bit_floor_ui(unsigned) noexcept { return 0x5CU; }
-unsigned long stdc_bit_floor_ul(unsigned long) noexcept { return 0x5DU; }
-unsigned long long stdc_bit_floor_ull(unsigned long long) noexcept {
-  return 0x5EU;
-}
-unsigned char stdc_bit_ceil_uc(unsigned char) noexcept { return 0x6AU; }
-unsigned short stdc_bit_ceil_us(unsigned short) noexcept { return 0x6BU; }
-unsigned stdc_bit_ceil_ui(unsigned) noexcept { return 0x6CU; }
-unsigned long stdc_bit_ceil_ul(unsigned long) noexcept { return 0x6DU; }
-unsigned long long stdc_bit_ceil_ull(unsigned long long) noexcept {
-  return 0x6EU;
-}
-}
+#include "stdbit_stub.h"
 
 #include "include/llvm-libc-macros/stdbit-macros.h"
 

>From 5523c11383d3fb71e00514d2c32b21ae613aa636 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue at google.com>
Date: Sun, 10 Mar 2024 03:11:06 -0400
Subject: [PATCH 2/5] Fix formatting.

---
 libc/include/llvm-libc-macros/stdbit-macros.h | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/libc/include/llvm-libc-macros/stdbit-macros.h b/libc/include/llvm-libc-macros/stdbit-macros.h
index 8c1eee0b35abc3..da33e63b367519 100644
--- a/libc/include/llvm-libc-macros/stdbit-macros.h
+++ b/libc/include/llvm-libc-macros/stdbit-macros.h
@@ -210,12 +210,12 @@ inline unsigned long long stdc_bit_ceil(unsigned long long x) {
 #else
 
 #define STDBIT_GENERIC_FUNC(FUNC_NAME, x)                                      \
-  _Generic((x), unsigned char                                                  \
-           : FUNC_NAME##_uc, unsigned short                                    \
-           : FUNC_NAME##_us, unsigned                                          \
-           : FUNC_NAME##_ui, unsigned long                                     \
-           : FUNC_NAME##_ul, unsigned long long                                \
-           : FUNC_NAME##_ull)(x)
+  _Generic((x),                                                                \
+      unsigned char: FUNC_NAME##_uc,                                           \
+      unsigned short: FUNC_NAME##_us,                                          \
+      unsigned: FUNC_NAME##_ui,                                                \
+      unsigned long: FUNC_NAME##_ul,                                           \
+      unsigned long long: FUNC_NAME##_ull)(x)
 
 #define stdc_leading_zeros(x) STDBIT_GENERIC_FUNC(stdc_leading_zeros, (x))
 #define stdc_leading_ones(x) STDBIT_GENERIC_FUNC(stdc_leading_ones, (x))

>From 45586ff634a193644e9b67264f860e83d8242e53 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue at google.com>
Date: Sun, 10 Mar 2024 15:00:51 -0400
Subject: [PATCH 3/5] Remove undef.

---
 libc/include/llvm-libc-macros/stdbit-macros.h | 2 --
 1 file changed, 2 deletions(-)

diff --git a/libc/include/llvm-libc-macros/stdbit-macros.h b/libc/include/llvm-libc-macros/stdbit-macros.h
index da33e63b367519..9e320379fd614e 100644
--- a/libc/include/llvm-libc-macros/stdbit-macros.h
+++ b/libc/include/llvm-libc-macros/stdbit-macros.h
@@ -236,8 +236,6 @@ inline unsigned long long stdc_bit_ceil(unsigned long long x) {
 #define stdc_bit_floor(x) STDBIT_GENERIC_FUNC(stdc_bit_floor, (x))
 #define stdc_bit_ceil(x) STDBIT_GENERIC_FUNC(stdc_bit_ceil, (x))
 
-#undef STDBIT_GENERIC_FUNC
-
 #endif // __cplusplus
 
 #endif // __LLVM_LIBC_MACROS_STDBIT_MACROS_H

>From e676615db33654f1ba6969d8c53a1e2de1038dc2 Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue at google.com>
Date: Sun, 10 Mar 2024 15:20:34 -0400
Subject: [PATCH 4/5] Fix formatting.

---
 libc/test/include/stdbit_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/test/include/stdbit_test.c b/libc/test/include/stdbit_test.c
index 99680776cf94c0..e278e9a7374e08 100644
--- a/libc/test/include/stdbit_test.c
+++ b/libc/test/include/stdbit_test.c
@@ -56,6 +56,6 @@ int main(void) {
   CHECK_FUNCTION(stdc_bit_width, 0x4);
   CHECK_FUNCTION(stdc_bit_floor, 0x5);
   CHECK_FUNCTION(stdc_bit_ceil, 0x6);
-  
+
   return 0;
 }

>From fd3049d08f67153b97f6c2f93706db68b2e7a03f Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue at google.com>
Date: Mon, 11 Mar 2024 14:23:20 -0400
Subject: [PATCH 5/5] Revert `stdbit-macros.h` change to be added in a separate
 PR.

---
 libc/include/llvm-libc-macros/stdbit-macros.h | 116 ++++++++++++++----
 1 file changed, 93 insertions(+), 23 deletions(-)

diff --git a/libc/include/llvm-libc-macros/stdbit-macros.h b/libc/include/llvm-libc-macros/stdbit-macros.h
index 9e320379fd614e..10c0fac3c8dd86 100644
--- a/libc/include/llvm-libc-macros/stdbit-macros.h
+++ b/libc/include/llvm-libc-macros/stdbit-macros.h
@@ -208,34 +208,104 @@ inline unsigned long long stdc_bit_ceil(unsigned long long x) {
   return stdc_bit_ceil_ull(x);
 }
 #else
-
-#define STDBIT_GENERIC_FUNC(FUNC_NAME, x)                                      \
+#define stdc_leading_zeros(x)                                                  \
   _Generic((x),                                                                \
-      unsigned char: FUNC_NAME##_uc,                                           \
-      unsigned short: FUNC_NAME##_us,                                          \
-      unsigned: FUNC_NAME##_ui,                                                \
-      unsigned long: FUNC_NAME##_ul,                                           \
-      unsigned long long: FUNC_NAME##_ull)(x)
-
-#define stdc_leading_zeros(x) STDBIT_GENERIC_FUNC(stdc_leading_zeros, (x))
-#define stdc_leading_ones(x) STDBIT_GENERIC_FUNC(stdc_leading_ones, (x))
-#define stdc_trailing_zeros(x) STDBIT_GENERIC_FUNC(stdc_trailing_zeros, (x))
-#define stdc_trailing_ones(x) STDBIT_GENERIC_FUNC(stdc_trailing_ones, (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)
+#define stdc_leading_ones(x)                                                   \
+  _Generic((x),                                                                \
+      unsigned char: stdc_leading_ones_uc,                                     \
+      unsigned short: stdc_leading_ones_us,                                    \
+      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)
+#define stdc_trailing_ones(x)                                                  \
+  _Generic((x),                                                                \
+      unsigned char: stdc_trailing_ones_uc,                                    \
+      unsigned short: stdc_trailing_ones_us,                                   \
+      unsigned: stdc_trailing_ones_ui,                                         \
+      unsigned long: stdc_trailing_ones_ul,                                    \
+      unsigned long long: stdc_trailing_ones_ull)(x)
 #define stdc_first_leading_zero(x)                                             \
-  STDBIT_GENERIC_FUNC(stdc_first_leading_zero, (x))
+  _Generic((x),                                                                \
+      unsigned char: stdc_first_leading_zero_uc,                               \
+      unsigned short: stdc_first_leading_zero_us,                              \
+      unsigned: stdc_first_leading_zero_ui,                                    \
+      unsigned long: stdc_first_leading_zero_ul,                               \
+      unsigned long long: stdc_first_leading_zero_ull)(x)
 #define stdc_first_leading_one(x)                                              \
-  STDBIT_GENERIC_FUNC(stdc_first_leading_one, (x))
+  _Generic((x),                                                                \
+      unsigned char: stdc_first_leading_one_uc,                                \
+      unsigned short: stdc_first_leading_one_us,                               \
+      unsigned: stdc_first_leading_one_ui,                                     \
+      unsigned long: stdc_first_leading_one_ul,                                \
+      unsigned long long: stdc_first_leading_one_ull)(x)
 #define stdc_first_trailing_zero(x)                                            \
-  STDBIT_GENERIC_FUNC(stdc_first_trailing_zero, (x))
+  _Generic((x),                                                                \
+      unsigned char: stdc_first_trailing_zero_uc,                              \
+      unsigned short: stdc_first_trailing_zero_us,                             \
+      unsigned: stdc_first_trailing_zero_ui,                                   \
+      unsigned long: stdc_first_trailing_zero_ul,                              \
+      unsigned long long: stdc_first_trailing_zero_ull)(x)
 #define stdc_first_trailing_one(x)                                             \
-  STDBIT_GENERIC_FUNC(stdc_first_trailing_one, (x))
-#define stdc_count_zeros(x) STDBIT_GENERIC_FUNC(stdc_count_zeros, (x))
-#define stdc_count_ones(x) STDBIT_GENERIC_FUNC(stdc_count_ones, (x))
-#define stdc_has_single_bit(x) STDBIT_GENERIC_FUNC(stdc_has_single_bit, (x))
-#define stdc_bit_width(x) STDBIT_GENERIC_FUNC(stdc_bit_width, (x))
-#define stdc_bit_floor(x) STDBIT_GENERIC_FUNC(stdc_bit_floor, (x))
-#define stdc_bit_ceil(x) STDBIT_GENERIC_FUNC(stdc_bit_ceil, (x))
-
+  _Generic((x),                                                                \
+      unsigned char: stdc_first_trailing_one_uc,                               \
+      unsigned short: stdc_first_trailing_one_us,                              \
+      unsigned: stdc_first_trailing_one_ui,                                    \
+      unsigned long: stdc_first_trailing_one_ul,                               \
+      unsigned long long: stdc_first_trailing_one_ull)(x)
+#define stdc_count_zeros(x)                                                    \
+  _Generic((x),                                                                \
+      unsigned char: stdc_count_zeros_uc,                                      \
+      unsigned short: stdc_count_zeros_us,                                     \
+      unsigned: stdc_count_zeros_ui,                                           \
+      unsigned long: stdc_count_zeros_ul,                                      \
+      unsigned long long: stdc_count_zeros_ull)(x)
+#define stdc_count_ones(x)                                                     \
+  _Generic((x),                                                                \
+      unsigned char: stdc_count_ones_uc,                                       \
+      unsigned short: stdc_count_ones_us,                                      \
+      unsigned: stdc_count_ones_ui,                                            \
+      unsigned long: stdc_count_ones_ul,                                       \
+      unsigned long long: stdc_count_ones_ull)(x)
+#define stdc_has_single_bit(x)                                                 \
+  _Generic((x),                                                                \
+      unsigned char: stdc_has_single_bit_uc,                                   \
+      unsigned short: stdc_has_single_bit_us,                                  \
+      unsigned: stdc_has_single_bit_ui,                                        \
+      unsigned long: stdc_has_single_bit_ul,                                   \
+      unsigned long long: stdc_has_single_bit_ull)(x)
+#define stdc_bit_width(x)                                                      \
+  _Generic((x),                                                                \
+      unsigned char: stdc_bit_width_uc,                                        \
+      unsigned short: stdc_bit_width_us,                                       \
+      unsigned: stdc_bit_width_ui,                                             \
+      unsigned long: stdc_bit_width_ul,                                        \
+      unsigned long long: stdc_bit_width_ull)(x)
+#define stdc_bit_floor(x)                                                      \
+  _Generic((x),                                                                \
+      unsigned char: stdc_bit_floor_uc,                                        \
+      unsigned short: stdc_bit_floor_us,                                       \
+      unsigned: stdc_bit_floor_ui,                                             \
+      unsigned long: stdc_bit_floor_ul,                                        \
+      unsigned long long: stdc_bit_floor_ull)(x)
+#define stdc_bit_ceil(x)                                                       \
+  _Generic((x),                                                                \
+      unsigned char: stdc_bit_ceil_uc,                                         \
+      unsigned short: stdc_bit_ceil_us,                                        \
+      unsigned: stdc_bit_ceil_ui,                                              \
+      unsigned long: stdc_bit_ceil_ul,                                         \
+      unsigned long long: stdc_bit_ceil_ull)(x)
 #endif // __cplusplus
 
 #endif // __LLVM_LIBC_MACROS_STDBIT_MACROS_H



More information about the libc-commits mailing list