[libc-commits] [libc] [llvm] [libc] Add more functions in CPP/bit.h (PR #73814)

Clement Courbet via libc-commits libc-commits at lists.llvm.org
Wed Nov 29 09:18:29 PST 2023


================
@@ -1,57 +1,261 @@
-//===-- Freestanding version of bit_cast  -----------------------*- C++ -*-===//
+//===-- Implementation of the C++20 bit header  -----------------*- C++ -*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
 //===----------------------------------------------------------------------===//
+// This is inspired from LLVM ADT/bit.h header.
 
 #ifndef LLVM_LIBC_SRC___SUPPORT_CPP_BIT_H
 #define LLVM_LIBC_SRC___SUPPORT_CPP_BIT_H
 
+#include "src/__support/CPP/limits.h" // numeric_limits
 #include "src/__support/CPP/type_traits.h"
 #include "src/__support/macros/attributes.h"
 #include "src/__support/macros/config.h" // LIBC_HAS_BUILTIN
 #include "src/__support/macros/sanitizer.h"
 
-namespace LIBC_NAMESPACE::cpp {
+#include <stdint.h>
 
-#if LIBC_HAS_BUILTIN(__builtin_bit_cast)
-#define LLVM_LIBC_HAS_BUILTIN_BIT_CAST
-#endif
+namespace LIBC_NAMESPACE::cpp {
 
 #if LIBC_HAS_BUILTIN(__builtin_memcpy_inline)
 #define LLVM_LIBC_HAS_BUILTIN_MEMCPY_INLINE
 #endif
 
-// This function guarantees the bitcast to be optimized away by the compiler for
-// GCC >= 8 and Clang >= 6.
-template <class To, class From>
+// This implementation of bit_cast requires trivially-constructible To, to avoid
+// UB in the implementation.
+template <
+    typename To, typename From,
+    typename = cpp::enable_if_t<sizeof(To) == sizeof(From)>,
+    typename = cpp::enable_if_t<cpp::is_trivially_constructible<To>::value>,
+    typename = cpp::enable_if_t<cpp::is_trivially_copyable<To>::value>,
+    typename = cpp::enable_if_t<cpp::is_trivially_copyable<From>::value>>
 LIBC_INLINE constexpr To bit_cast(const From &from) {
-  static_assert(sizeof(To) == sizeof(From), "To and From must be of same size");
-  static_assert(cpp::is_trivially_copyable<To>::value &&
-                    cpp::is_trivially_copyable<From>::value,
-                "Cannot bit-cast instances of non-trivially copyable classes.");
   MSAN_UNPOISON(&from, sizeof(From));
-#if defined(LLVM_LIBC_HAS_BUILTIN_BIT_CAST)
+#if LIBC_HAS_BUILTIN(__builtin_bit_cast)
   return __builtin_bit_cast(To, from);
 #else
-  static_assert(cpp::is_trivially_constructible<To>::value,
-                "This implementation additionally requires destination type to "
-                "be trivially constructible");
   To to;
   char *dst = reinterpret_cast<char *>(&to);
   const char *src = reinterpret_cast<const char *>(&from);
-#if defined(LLVM_LIBC_HAS_BUILTIN_MEMCPY_INLINE)
+#if LIBC_HAS_BUILTIN(__builtin_memcpy_inline)
   __builtin_memcpy_inline(dst, src, sizeof(To));
 #else
   for (unsigned i = 0; i < sizeof(To); ++i)
     dst[i] = src[i];
-#endif // defined(LLVM_LIBC_HAS_BUILTIN_MEMCPY_INLINE)
+#endif // LIBC_HAS_BUILTIN(__builtin_memcpy_inline)
   return to;
-#endif // defined(LLVM_LIBC_HAS_BUILTIN_BIT_CAST)
+#endif // LIBC_HAS_BUILTIN(__builtin_bit_cast)
+}
+
+template <typename T, typename = cpp::enable_if_t<cpp::is_unsigned_v<T>>>
+[[nodiscard]] LIBC_INLINE constexpr bool has_single_bit(T value) {
+  return (value != 0) && ((value & (value - 1)) == 0);
+}
+
+// A temporary macro to add template function specialization when compiler
+// builtin is available.
+#define ADD_SPECIALIZATION(NAME, TYPE, BUILTIN)                                \
+  template <> [[nodiscard]] LIBC_INLINE constexpr int NAME<TYPE>(TYPE value) { \
+    static_assert(cpp::is_unsigned_v<TYPE>);                                   \
+    return value == 0 ? cpp::numeric_limits<TYPE>::digits : BUILTIN(value);    \
+  }
+
+/// Count number of 0's from the least significant bit to the most
+///   stopping at the first 1.
+///
+/// Only unsigned integral types are allowed.
+///
+/// Returns cpp::numeric_limits<T>::digits on an input of 0.
+template <typename T>
+[[nodiscard]] LIBC_INLINE constexpr int countr_zero(T value) {
+  static_assert(cpp::is_unsigned_v<T>);
----------------
legrosbuffle wrote:

Why is this not SFINAEd in ? "This overload participates in overload resolution only if T is an unsigned integer type"



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


More information about the libc-commits mailing list