[clang] [clang]Implement the c23 stdc bit builtins (PR #185978)

Nikolas Klauser via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 14 03:11:32 PDT 2026


================
@@ -0,0 +1,19 @@
+#ifndef LLVM_CLANG_TEST_STDBIT_H
+#define LLVM_CLANG_TEST_STDBIT_H
+
+#define stdc_leading_zeros(x) (__builtin_stdc_leading_zeros((x)))
----------------
philnik777 wrote:

> We'd need to hear from @ldionne or @philnik777 (or others) as to what the STL plans to do here at least for libc++, but it's not uncommon for the C++ standard library to vend a C standard header for use in C++ mode: https://github.com/llvm/llvm-project/blob/main/libcxx/include/stdio.h

It's not uncommon, but we try to avoid it whenever possible, since it's always a horrible mess. IMO the best solution would be if Clang and GCC just vend their own, C++-friendly, headers. These functions should really never not be inlined, so there isn't much the implementation has to do except call the appropriate builtins. Is there a reason we can't provide an inline function like we do all the time in C++ headers? (I'm not super familiar with the C requirements on headers and function addresses etc.) That might require some way to generate `linkonce_odr` functions in C (I don't think that's currently possible), but that should be doable. Another option would be something like
```c++
#ifdef __cplusplus
#  define _CLANG_INLINE inline
#else
#  define _CLANG_INLINE static inline
#endif

#define stdc_has_single_bit(val) (__builtin_popcountg(val) == 1)

_CLANG_INLINE bool stdc_has_single_bit_ui(unsigned __v) {
  return stdc_has_single_bit(__v);
}

// and so on
```
At least I don't think there is anything in C that could distinguish functions with internal linkage from ones with external linkage? I guess different addresses, but I don't know whether C has any requirements on that. Either way, this is a solved problem in C++ and we should be able to solve it in C through some extension if there is no standard way to write such a thing.


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


More information about the cfe-commits mailing list