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

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 3 07:58:42 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) {
----------------
kazutakahirata wrote:

I took the return type from `std::bit_width`:

https://en.cppreference.com/w/cpp/numeric/bit_width.html


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


More information about the llvm-commits mailing list