| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #pragma once |
|
|
| #include <thrust/detail/config.h> |
|
|
| #if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC) |
| # pragma GCC system_header |
| #elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG) |
| # pragma clang system_header |
| #elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC) |
| # pragma system_header |
| #endif |
| #include <thrust/detail/type_deduction.h> |
|
|
| #include <nv/target> |
|
|
| #include <limits> |
|
|
| THRUST_NAMESPACE_BEGIN |
| namespace detail |
| { |
|
|
| template <typename Integer> |
| __host__ __device__ __thrust_forceinline__ |
| Integer clz(Integer x) |
| { |
| Integer result; |
|
|
| NV_IF_TARGET(NV_IS_DEVICE, ( |
| result = ::__clz(x); |
| ), ( |
| int num_bits = 8 * sizeof(Integer); |
| int num_bits_minus_one = num_bits - 1; |
| result = num_bits; |
| for (int i = num_bits_minus_one; i >= 0; --i) |
| { |
| if ((Integer(1) << i) & x) |
| { |
| result = num_bits_minus_one - i; |
| break; |
| } |
| } |
| )); |
|
|
| return result; |
| } |
|
|
| template <typename Integer> |
| __host__ __device__ __thrust_forceinline__ |
| bool is_power_of_2(Integer x) |
| { |
| return 0 == (x & (x - 1)); |
| } |
|
|
| template <typename Integer> |
| __host__ __device__ __thrust_forceinline__ |
| bool is_odd(Integer x) |
| { |
| return 1 & x; |
| } |
|
|
| template <typename Integer> |
| __host__ __device__ __thrust_forceinline__ |
| Integer log2(Integer x) |
| { |
| Integer num_bits = 8 * sizeof(Integer); |
| Integer num_bits_minus_one = num_bits - 1; |
|
|
| return num_bits_minus_one - clz(x); |
| } |
|
|
|
|
| template <typename Integer> |
| __host__ __device__ __thrust_forceinline__ |
| Integer log2_ri(Integer x) |
| { |
| Integer result = log2(x); |
|
|
| |
| if (!is_power_of_2(x)) |
| ++result; |
|
|
| return result; |
| } |
|
|
| |
| |
| template <typename Integer0, typename Integer1> |
| __host__ __device__ __thrust_forceinline__ |
| #if THRUST_CPP_DIALECT >= 2011 |
| |
| auto divide_ri(Integer0 const x, Integer1 const y) |
| THRUST_DECLTYPE_RETURNS((x + (y - 1)) / y) |
| #else |
| |
| Integer0 divide_ri(Integer0 const x, Integer1 const y) |
| { |
| return (x + (y - 1)) / y; |
| } |
| #endif |
|
|
| |
| |
| template <typename Integer0, typename Integer1> |
| __host__ __device__ __thrust_forceinline__ |
| #if THRUST_CPP_DIALECT >= 2011 |
| auto divide_rz(Integer0 const x, Integer1 const y) |
| THRUST_DECLTYPE_RETURNS(x / y) |
| #else |
| |
| Integer0 divide_rz(Integer0 const x, Integer1 const y) |
| { |
| return x / y; |
| } |
| #endif |
|
|
| |
| template <typename Integer0, typename Integer1> |
| __host__ __device__ __thrust_forceinline__ |
| #if THRUST_CPP_DIALECT >= 2011 |
| auto round_i(Integer0 const x, Integer1 const y) |
| THRUST_DECLTYPE_RETURNS(y * divide_ri(x, y)) |
| #else |
| Integer0 round_i(Integer0 const x, Integer1 const y) |
| { |
| return y * divide_ri(x, y); |
| } |
| #endif |
|
|
| |
| template <typename Integer0, typename Integer1> |
| __host__ __device__ __thrust_forceinline__ |
| #if THRUST_CPP_DIALECT >= 2011 |
| auto round_z(Integer0 const x, Integer1 const y) |
| THRUST_DECLTYPE_RETURNS(y * divide_rz(x, y)) |
| #else |
| Integer0 round_z(Integer0 const x, Integer1 const y) |
| { |
| return y * divide_rz(x, y); |
| } |
| #endif |
|
|
| } |
|
|
| THRUST_NAMESPACE_END |
|
|