[libc-commits] [libc] [libc] fix type generic stdc_leading_zeros for GCC (PR #79917)

via libc-commits libc-commits at lists.llvm.org
Mon Jan 29 16:28:00 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Nick Desaulniers (nickdesaulniers)

<details>
<summary>Changes</summary>

GCC does not support _Generic in C++ mode. Use inline function definitions
with function overloading in __cplusplus mode.


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


2 Files Affected:

- (modified) libc/include/llvm-libc-macros/stdbit-macros.h (+18) 
- (modified) libc/include/stdbit.h.def (+2-1) 


``````````diff
diff --git a/libc/include/llvm-libc-macros/stdbit-macros.h b/libc/include/llvm-libc-macros/stdbit-macros.h
index febe95fe0a1e3c..da0fb1a578f139 100644
--- a/libc/include/llvm-libc-macros/stdbit-macros.h
+++ b/libc/include/llvm-libc-macros/stdbit-macros.h
@@ -9,6 +9,23 @@
 #ifndef __LLVM_LIBC_MACROS_STDBIT_MACROS_H
 #define __LLVM_LIBC_MACROS_STDBIT_MACROS_H
 
+#ifdef __cplusplus
+inline unsigned char stdc_leading_zeros(unsigned char x) {
+  return stdc_leading_zeros_uc(x);
+}
+inline unsigned short stdc_leading_zeros(unsigned short x) {
+  return stdc_leading_zeros_us(x);
+}
+inline unsigned stdc_leading_zeros(unsigned x) {
+  return stdc_leading_zeros_ui(x);
+}
+inline unsigned long stdc_leading_zeros(unsigned long x) {
+  return stdc_leading_zeros_ul(x);
+}
+inline unsigned long long stdc_leading_zeros(unsigned long long x) {
+  return stdc_leading_zeros_ull(x);
+}
+#else
 #define stdc_leading_zeros(x)                                                  \
   _Generic((x),                                                                \
       unsigned char: stdc_leading_zeros_uc,                                    \
@@ -16,5 +33,6 @@
       unsigned: stdc_leading_zeros_ui,                                         \
       unsigned long: stdc_leading_zeros_ul,                                    \
       unsigned long long: stdc_leading_zeros_ull)(x)
+#endif // __cplusplus
 
 #endif // __LLVM_LIBC_MACROS_STDBIT_MACROS_H
diff --git a/libc/include/stdbit.h.def b/libc/include/stdbit.h.def
index cb79ac1caf049f..c5a77329fbfe74 100644
--- a/libc/include/stdbit.h.def
+++ b/libc/include/stdbit.h.def
@@ -10,8 +10,9 @@
 #define LLVM_LIBC_STDBIT_H
 
 #include <__llvm-libc-common.h>
-#include <llvm-libc-macros/stdbit-macros.h>
 
 %%public_api()
 
+#include <llvm-libc-macros/stdbit-macros.h>
+
 #endif // LLVM_LIBC_STDBIT_H

``````````

</details>


https://github.com/llvm/llvm-project/pull/79917


More information about the libc-commits mailing list