[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
Sat Mar 9 22:52:37 PST 2024


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

Currently there is no tests for generic macros of generated `stdbit.h` header in C, and it is easy to make typo mistakes as in https://github.com/llvm/llvm-project/issues/84658.  In this patch, we simplify the generic macros in `stdbit.h` to reduce typo chances, and add a simple test for them in C.

>From 185b3a7f9e38842c13f3221d40732e790248f529 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] [libc][stdbit] Update generated stdbit generic macros and add
 C tests.

---
 libc/include/llvm-libc-macros/stdbit-macros.h | 112 ++++--------------
 libc/test/include/CMakeLists.txt              |  25 ++++
 libc/test/include/stdbit_stub.h               |  65 ++++++++++
 libc/test/include/stdbit_test.c               |  59 +++++++++
 libc/test/include/stdbit_test.cpp             |  78 +-----------
 5 files changed, 175 insertions(+), 164 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 e3eb6630afddb6..d1b792e115992f 100644
--- a/libc/include/llvm-libc-macros/stdbit-macros.h
+++ b/libc/include/llvm-libc-macros/stdbit-macros.h
@@ -195,97 +195,35 @@ inline unsigned long long stdc_bit_floor(unsigned long long x) {
   return stdc_bit_floor_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)
+  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))
+
+#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..40436c95c5356f
--- /dev/null
+++ b/libc/test/include/stdbit_stub.h
@@ -0,0 +1,65 @@
+//===-- 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;
+}
+
+__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..7c1ddd23b6b44e
--- /dev/null
+++ b/libc/test/include/stdbit_test.c
@@ -0,0 +1,59 @@
+//===-- 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);
+  return 0;
+}
diff --git a/libc/test/include/stdbit_test.cpp b/libc/test/include/stdbit_test.cpp
index 20820d52fbdede..6b4bef6f13e5ea 100644
--- a/libc/test/include/stdbit_test.cpp
+++ b/libc/test/include/stdbit_test.cpp
@@ -22,83 +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;
-}
-}
+#include "stdbit_stub.h"
 
 #include "include/llvm-libc-macros/stdbit-macros.h"
 



More information about the libc-commits mailing list