[libc] [llvm] [libc] Fix forward missing `BigInt` specialization of `mask_leading_ones` / `mask_trailing_ones` (PR #84325)

via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 7 08:30:28 PST 2024


================
@@ -1056,4 +1056,59 @@ rotr(T value, int rotate) {
 
 } // namespace LIBC_NAMESPACE::cpp
 
+namespace LIBC_NAMESPACE {
+
+// Specialization of mask_trailing_ones ('math_extras.h') for BigInt.
+template <typename T, size_t count>
+LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_big_int_v<T>, T>
+mask_trailing_ones() {
+  static_assert(!T::SIGNED);
+  if (count == 0)
+    return T();
+  constexpr unsigned T_BITS = CHAR_BIT * sizeof(T);
+  static_assert(count <= T_BITS && "Invalid bit index");
+  using word_type = typename T::word_type;
+  T out;
+  const int chunk_index_containing_bit = static_cast<int>(count / T::WORD_SIZE);
+  int index = 0;
+  for (auto &word : out.val) {
+    if (index < chunk_index_containing_bit)
+      word = -1;
+    else if (index > chunk_index_containing_bit)
+      word = 0;
+    else
+      word = mask_trailing_ones<word_type, count % T::WORD_SIZE>();
+    ++index;
+  }
+  return out;
+}
+
+// Specialization of mask_leading_ones ('math_extras.h') for BigInt.
+template <typename T, size_t count>
+LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_big_int_v<T>, T>
+mask_leading_ones() {
+  static_assert(!T::SIGNED);
+  if (count == 0)
+    return T();
+  constexpr unsigned T_BITS = CHAR_BIT * sizeof(T);
+  static_assert(count <= T_BITS && "Invalid bit index");
+  using word_type = typename T::word_type;
+  T out;
+  const int chunk_index_containing_bit =
----------------
lntue wrote:

`constexpr`

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


More information about the llvm-commits mailing list