[llvm] [ADT] Add bit_width_constexpr (PR #161775)

Jakub Kuderski via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 3 05:39:59 PDT 2025


================
@@ -292,6 +292,23 @@ template <typename T> [[nodiscard]] int bit_width(T Value) {
   return std::numeric_limits<T>::digits - llvm::countl_zero(Value);
 }
 
+/// Returns the number of bits needed to represent Value if Value is nonzero.
+/// Returns 0 otherwise.
+///
+/// A constexpr version of bit_width.
+///
+/// Ex. bit_width_constexpr(5) == 3.
+template <typename T> [[nodiscard]] constexpr int bit_width_constexpr(T Value) {
----------------
kuhar wrote:

How did you decide to use `int` as the return type instead of `unsigned`?

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


More information about the llvm-commits mailing list