Power of Two Utilities
template <typename T>
[[nodiscard]] __host__ __device__ inline constexpr
bool is_power_of_two(T value) noexcept;
template <typename T>
[[nodiscard]] __host__ __device__ inline constexpr
T next_power_of_two(T value) noexcept;
template <typename T>
[[nodiscard]] __host__ __device__ inline constexpr
T prev_power_of_two(T value) noexcept;
The functions provide utilities to determine if an integer value is a power of two, and to compute the next and previous power of two.
Parameters
value
: The input value.
Return value
is_power_of_two
: Returntrue
ifvalue
is a power of two,false
otherwise.next_power_of_two
: Return the smallest power of two greater than or equal tovalue
.prev_power_of_two
: Return the largest power of two less than or equal tovalue
.
Constraints
T
is an integer types. Contrary tocuda::std::has_single_bit
,cuda::bit_floor
, andcuda::bit_ceil
,T
can be both signed and unsigned.
Preconditions
value > 0
Performance considerations
See <cuda/std/bit> performance considerations
Example
#include <cuda/cmath>
#include <cuda/std/cassert>
__global__ void pow2_kernel() {
assert(!cuda::is_power_of_two(20));
assert(cuda::next_power_of_two(20) == 32);
assert(cuda::prev_power_of_two(20) == 16);
}
int main() {
pow2_kernel<<<1, 1>>>();
cudaDeviceSynchronize();
return 0;
}