[llvm] [ADT] Add llvm::countr_zero_constexpr (PR #164188)

Jakub Kuderski via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 20 05:05:07 PDT 2025


================
@@ -177,6 +178,22 @@ template <typename T> [[nodiscard]] inline int popcount(T Value) noexcept {
   }
 }
 
+/// Count number of 0's from the least significant bit to the most
+///   stopping at the first 1.
+///
+/// A constexpr version of countr_zero.
+///
+/// Only unsigned integral types are allowed.
+///
+/// Returns std::numeric_limits<T>::digits on an input of 0.
+template <typename T> [[nodiscard]] constexpr int countr_zero_constexpr(T Val) {
+  static_assert(std::is_unsigned_v<T>,
+                "Only unsigned integral types are allowed.");
+  // "(Val & -Val) - 1" is a bitmask with all bits below
+  // the least significant 1 set.
----------------
kuhar wrote:

Can you reflow this comment?

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


More information about the llvm-commits mailing list