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

Schrodinger ZHU Yifan via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 29 10:06:55 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>);
+  if (!value)
+    return cpp::numeric_limits<T>::digits;
+  if (value & 0x1)
+    return 0;
+  // Bisection method.
+  unsigned zero_bits = 0;
+  T shift = cpp::numeric_limits<T>::digits >> 1;
+  T mask = cpp::numeric_limits<T>::max() >> shift;
+  while (shift) {
+    if ((value & mask) == 0) {
+      value >>= shift;
+      zero_bits |= shift;
+    }
+    shift >>= 1;
+    mask >>= shift;
+  }
+  return zero_bits;
+}
+#if LIBC_HAS_BUILTIN(__builtin_ctzs)
+ADD_SPECIALIZATION(countr_zero, unsigned short, __builtin_ctzs)
+#endif
+#if LIBC_HAS_BUILTIN(__builtin_ctz)
+ADD_SPECIALIZATION(countr_zero, unsigned int, __builtin_ctz)
+#endif
+#if LIBC_HAS_BUILTIN(__builtin_ctzl)
+ADD_SPECIALIZATION(countr_zero, unsigned long, __builtin_ctzl)
+#endif
+#if LIBC_HAS_BUILTIN(__builtin_ctzll)
+ADD_SPECIALIZATION(countr_zero, unsigned long long, __builtin_ctzll)
+#endif
+
+/// Count number of 0's from the most significant bit to the least
+///   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 countl_zero(T value) {
+  static_assert(cpp::is_unsigned_v<T>);
+  if (!value)
+    return cpp::numeric_limits<T>::digits;
+  // Bisection method.
+  unsigned zero_bits = 0;
+  for (T shift = cpp::numeric_limits<T>::digits >> 1; shift; shift >>= 1) {
+    T tmp = value >> shift;
+    if (tmp)
+      value = tmp;
+    else
+      zero_bits |= shift;
+  }
+  return zero_bits;
+}
+#if LIBC_HAS_BUILTIN(__builtin_clzs)
+ADD_SPECIALIZATION(countl_zero, unsigned short, __builtin_clzs)
+#endif
+#if LIBC_HAS_BUILTIN(__builtin_clz)
+ADD_SPECIALIZATION(countl_zero, unsigned int, __builtin_clz)
+#endif
+#if LIBC_HAS_BUILTIN(__builtin_clzl)
+ADD_SPECIALIZATION(countl_zero, unsigned long, __builtin_clzl)
+#endif
+#if LIBC_HAS_BUILTIN(__builtin_clzll)
+ADD_SPECIALIZATION(countl_zero, unsigned long long, __builtin_clzll)
+#endif
+
+#undef ADD_SPECIALIZATION
+
+/// Count the number of ones from the most significant bit to the first
+/// zero bit.
+///
+/// Ex. countl_one(0xFF0FFF00) == 8.
+/// Only unsigned integral types are allowed.
+///
+/// Returns cpp::numeric_limits<T>::digits on an input of all ones.
+template <typename T>
+[[nodiscard]] LIBC_INLINE constexpr int countl_one(T value) {
+  static_assert(cpp::is_unsigned_v<T>);
+  return cpp::countl_zero<T>(~value);
+}
+
+/// Count the number of ones from the least significant bit to the first
+/// zero bit.
+///
+/// Ex. countr_one(0x00FF00FF) == 8.
+/// Only unsigned integral types are allowed.
+///
+/// Returns cpp::numeric_limits<T>::digits on an input of all ones.
+template <typename T>
+[[nodiscard]] LIBC_INLINE constexpr int countr_one(T value) {
+  static_assert(cpp::is_unsigned_v<T>);
+  return cpp::countr_zero<T>(~value);
+}
+
+/// Returns the number of bits needed to represent value if value is nonzero.
+/// Returns 0 otherwise.
+///
+/// Ex. bit_width(5) == 3.
+template <typename T>
+[[nodiscard]] LIBC_INLINE constexpr int bit_width(T value) {
+  static_assert(cpp::is_unsigned_v<T>);
+  return cpp::numeric_limits<T>::digits - cpp::countl_zero(value);
+}
+
+/// Returns the largest integral power of two no greater than value if value is
+/// nonzero.  Returns 0 otherwise.
+///
+/// Ex. bit_floor(5) == 4.
+template <typename T> [[nodiscard]] LIBC_INLINE constexpr T bit_floor(T value) {
+  static_assert(cpp::is_unsigned_v<T>);
+  if (!value)
+    return 0;
+  return T(1) << (cpp::bit_width(value) - 1);
+}
+
+/// Returns the smallest integral power of two no smaller than value if value is
+/// nonzero.  Returns 1 otherwise.
+///
+/// Ex. bit_ceil(5) == 8.
+///
+/// The return value is undefined if the input is larger than the largest power
+/// of two representable in T.
+template <typename T> [[nodiscard]] LIBC_INLINE constexpr T bit_ceil(T value) {
+  static_assert(cpp::is_unsigned_v<T>);
+  if (value < 2)
+    return 1;
+  return T(1) << cpp::bit_width<T>(value - 1u);
+}
+
+/// Count the number of set bits in a value.
+/// Ex. popcount(0xF000F000) = 8
+/// Returns 0 if the word is zero.
+template <typename T, typename = cpp::enable_if_t<cpp::is_unsigned_v<T>>>
+[[nodiscard]] LIBC_INLINE constexpr int popcount(T value) {
+  if constexpr (sizeof(T) == 8) {
+#if LIBC_HAS_BUILTIN(__builtin_popcountll)
+    return (int)__builtin_popcountll(value);
+#endif
+    uint64_t v = value;
+    v = v - ((v >> 1) & 0x5555555555555555ULL);
+    v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
+    v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
+    return int((uint64_t)(v * 0x0101010101010101ULL) >> 56);
+  } else if constexpr (sizeof(T) <= 4) {
+#if LIBC_HAS_BUILTIN(__builtin_popcount)
+    return (int)__builtin_popcount(value);
+#endif
+    uint32_t v = value;
+    v = v - ((v >> 1) & 0x55555555);
+    v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
+    return int(((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24);
+  } else {
+    static_assert(cpp::always_false<T>);
+  }
+}
+
+// Forward-declare rotr so that rotl can use it.
+template <typename T, typename = cpp::enable_if_t<cpp::is_unsigned_v<T>>>
+[[nodiscard]] LIBC_INLINE constexpr T rotr(T value, int rotate);
+
+template <typename T, typename = cpp::enable_if_t<cpp::is_unsigned_v<T>>>
+[[nodiscard]] LIBC_INLINE constexpr T rotl(T value, int rotate) {
+  constexpr unsigned N = cpp::numeric_limits<T>::digits;
+  rotate = rotate % N;
+  if (!rotate)
+    return value;
+  if (rotate < 0)
+    return cpp::rotr(value, -rotate);
+  return (value << rotate) | (value >> (N - rotate));
----------------
SchrodingerZhu wrote:

@nickdesaulniers recommended a citation to "Safe, Efficient, and Portable Rotate in C/C++"
  // https://blog.regehr.org/archives/1063 in previous `__support/bit.h`. Maybe move it here?

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


More information about the llvm-commits mailing list