| 143 | // bit_width( 0b0111 ) = 3 |
| 144 | template <class T> |
| 145 | CUTE_HOST_DEVICE constexpr |
| 146 | int |
| 147 | bit_width(T x) { |
| 148 | static_assert(is_unsigned<T>::value, "Only to be used for unsigned types."); |
| 149 | constexpr int N = (numeric_limits<T>::digits == 64 ? 6 : |
| 150 | (numeric_limits<T>::digits == 32 ? 5 : |
| 151 | (numeric_limits<T>::digits == 16 ? 4 : |
| 152 | (numeric_limits<T>::digits == 8 ? 3 : (assert(false),0))))); |
| 153 | T r = 0; |
| 154 | for (int i = N - 1; i >= 0; --i) { |
| 155 | T shift = (x > ((T(1) << (T(1) << i))-1)) << i; |
| 156 | x >>= shift; |
| 157 | r |= shift; |
| 158 | } |
| 159 | return r + (x != 0); |
| 160 | } |
| 161 | |
| 162 | // Smallest integral power of two not less than the given value |
| 163 | // bit_ceil( 0b00000000 ) = 0b00000001 |
no test coverage detected