17#ifndef KOKKOS_BIT_MANIPULATION_HPP
18#define KOKKOS_BIT_MANIPULATION_HPP
20#include <Kokkos_Macros.hpp>
21#include <Kokkos_NumericTraits.hpp>
29KOKKOS_FUNCTION
constexpr T byteswap_fallback(T x) {
30 if constexpr (
sizeof(T) > 1) {
31 using U = std::make_unsigned_t<T>;
33 size_t shift = CHAR_BIT * (
sizeof(T) - 1);
35 U lo_mask =
static_cast<unsigned char>(~0);
36 U hi_mask = lo_mask << shift;
40 for (
size_t i = 0; i <
sizeof(T) / 2; ++i) {
41 U lo_val = val & lo_mask;
42 U hi_val = val & hi_mask;
44 val = (val & ~lo_mask) | (hi_val >> shift);
45 val = (val & ~hi_mask) | (lo_val << shift);
50 shift -= 2 * CHAR_BIT;
59KOKKOS_FUNCTION
constexpr int countl_zero_fallback(T x) {
62 using ::Kokkos::Experimental::digits_v;
64 int c = digits_v<T> / 2;
73 return n -
static_cast<int>(x);
77KOKKOS_FUNCTION
constexpr int countr_zero_fallback(T x) {
78 using ::Kokkos::Experimental::digits_v;
79 return digits_v<T> - countl_zero_fallback(
static_cast<T
>(
80 static_cast<T
>(~x) &
static_cast<T
>(x - 1)));
84KOKKOS_FUNCTION
constexpr int popcount_fallback(T x) {
86 for (; x != 0; x &= x - 1) {
93inline constexpr bool is_standard_unsigned_integer_type_v =
94 std::is_same_v<T, unsigned char> || std::is_same_v<T, unsigned short> ||
95 std::is_same_v<T, unsigned int> || std::is_same_v<T, unsigned long> ||
96 std::is_same_v<T, unsigned long long>;
103#if defined(KOKKOS_ENABLE_SYCL) && defined(__INTEL_LLVM_COMPILER) && \
104 __INTEL_LLVM_COMPILER < 20240000
105using sycl::detail::bit_cast;
107template <
class To,
class From>
108KOKKOS_FUNCTION std::enable_if_t<
sizeof(To) ==
sizeof(From) &&
109 std::is_trivially_copyable_v<To> &&
110 std::is_trivially_copyable_v<From>,
112bit_cast(From
const& from)
noexcept {
113#if defined(KOKKOS_ENABLE_SYCL) && defined(__INTEL_LLVM_COMPILER) && \
114 __INTEL_LLVM_COMPILER >= 20240000
115 return sycl::bit_cast<To>(from);
118 memcpy(
static_cast<void*
>(&to),
static_cast<const void*
>(&from),
sizeof(To));
127KOKKOS_FUNCTION
constexpr std::enable_if_t<std::is_integral_v<T>, T> byteswap(
129 return Impl::byteswap_fallback(value);
135KOKKOS_FUNCTION
constexpr std::enable_if_t<
136 Impl::is_standard_unsigned_integer_type_v<T>,
int>
137countl_zero(T x)
noexcept {
138 using ::Kokkos::Experimental::digits_v;
139 if (x == 0)
return digits_v<T>;
141 return Impl::countl_zero_fallback(x);
145KOKKOS_FUNCTION
constexpr std::enable_if_t<
146 Impl::is_standard_unsigned_integer_type_v<T>,
int>
147countl_one(T x)
noexcept {
148 using ::Kokkos::Experimental::digits_v;
149 using ::Kokkos::Experimental::finite_max_v;
150 if (x == finite_max_v<T>)
return digits_v<T>;
151 return countl_zero(
static_cast<T
>(~x));
155KOKKOS_FUNCTION
constexpr std::enable_if_t<
156 Impl::is_standard_unsigned_integer_type_v<T>,
int>
157countr_zero(T x)
noexcept {
158 using ::Kokkos::Experimental::digits_v;
159 if (x == 0)
return digits_v<T>;
161 return Impl::countr_zero_fallback(x);
165KOKKOS_FUNCTION
constexpr std::enable_if_t<
166 Impl::is_standard_unsigned_integer_type_v<T>,
int>
167countr_one(T x)
noexcept {
168 using ::Kokkos::Experimental::digits_v;
169 using ::Kokkos::Experimental::finite_max_v;
170 if (x == finite_max_v<T>)
return digits_v<T>;
171 return countr_zero(
static_cast<T
>(~x));
175KOKKOS_FUNCTION
constexpr std::enable_if_t<
176 Impl::is_standard_unsigned_integer_type_v<T>,
int>
177popcount(T x)
noexcept {
178 if (x == 0)
return 0;
180 return Impl::popcount_fallback(x);
186KOKKOS_FUNCTION
constexpr std::enable_if_t<
187 Impl::is_standard_unsigned_integer_type_v<T>,
bool>
188has_single_bit(T x)
noexcept {
189 return x != 0 && (((x & (x - 1)) == 0));
193KOKKOS_FUNCTION
constexpr std::enable_if_t<
194 Impl::is_standard_unsigned_integer_type_v<T>, T>
195bit_ceil(T x)
noexcept {
196 if (x <= 1)
return 1;
197 using ::Kokkos::Experimental::digits_v;
198 return T{1} << (digits_v<T> - countl_zero(
static_cast<T
>(x - 1)));
202KOKKOS_FUNCTION
constexpr std::enable_if_t<
203 Impl::is_standard_unsigned_integer_type_v<T>, T>
204bit_floor(T x)
noexcept {
205 if (x == 0)
return 0;
206 using ::Kokkos::Experimental::digits_v;
207 return T{1} << (digits_v<T> - 1 - countl_zero(x));
211KOKKOS_FUNCTION
constexpr std::enable_if_t<
212 Impl::is_standard_unsigned_integer_type_v<T>, T>
213bit_width(T x)
noexcept {
214 if (x == 0)
return 0;
215 using ::Kokkos::Experimental::digits_v;
216 return digits_v<T> - countl_zero(x);
222[[nodiscard]] KOKKOS_FUNCTION
constexpr std::enable_if_t<
223 Impl::is_standard_unsigned_integer_type_v<T>, T>
224rotl(T x,
int s)
noexcept {
225 using Experimental::digits_v;
226 constexpr auto dig = digits_v<T>;
227 int const rem = s % dig;
228 if (rem == 0)
return x;
229 if (rem > 0)
return (x << rem) | (x >> ((dig - rem) % dig));
230 return (x >> -rem) | (x << ((dig + rem) % dig));
234[[nodiscard]] KOKKOS_FUNCTION
constexpr std::enable_if_t<
235 Impl::is_standard_unsigned_integer_type_v<T>, T>
236rotr(T x,
int s)
noexcept {
237 using Experimental::digits_v;
238 constexpr auto dig = digits_v<T>;
239 int const rem = s % dig;
240 if (rem == 0)
return x;
241 if (rem > 0)
return (x >> rem) | (x << ((dig - rem) % dig));
242 return (x << -rem) | (x >> ((dig + rem) % dig));
250#if defined(KOKKOS_COMPILER_CLANG) || defined(KOKKOS_COMPILER_INTEL_LLVM) || \
251 defined(KOKKOS_COMPILER_GNU)
252#define KOKKOS_IMPL_USE_GCC_BUILT_IN_FUNCTIONS
256KOKKOS_IMPL_DEVICE_FUNCTION T byteswap_builtin_device(T x)
noexcept {
257 return byteswap_fallback(x);
261KOKKOS_IMPL_HOST_FUNCTION T byteswap_builtin_host(T x)
noexcept {
262#ifdef KOKKOS_IMPL_USE_GCC_BUILT_IN_FUNCTIONS
263 if constexpr (
sizeof(T) == 1) {
265 }
else if constexpr (
sizeof(T) == 2) {
266 return __builtin_bswap16(x);
267 }
else if constexpr (
sizeof(T) == 4) {
268 return __builtin_bswap32(x);
269 }
else if constexpr (
sizeof(T) == 8) {
270 return __builtin_bswap64(x);
271 }
else if constexpr (
sizeof(T) == 16) {
272#if defined(__has_builtin)
273#if __has_builtin(__builtin_bswap128)
274 return __builtin_bswap128(x);
277 return (__builtin_bswap64(x >> 64) |
278 (
static_cast<T
>(__builtin_bswap64(x)) << 64));
282 return byteswap_fallback(x);
286KOKKOS_IMPL_DEVICE_FUNCTION
287 std::enable_if_t<is_standard_unsigned_integer_type_v<T>,
int>
288 countl_zero_builtin_device(T x)
noexcept {
289#if defined(KOKKOS_ENABLE_CUDA) || defined(KOKKOS_ENABLE_HIP)
290 if constexpr (
sizeof(T) ==
sizeof(
long long int))
291 return __clzll(
reinterpret_cast<long long int&
>(x));
292 if constexpr (
sizeof(T) ==
sizeof(int))
293 return __clz(
reinterpret_cast<int&
>(x));
294 using ::Kokkos::Experimental::digits_v;
295 constexpr int shift = digits_v<unsigned int> - digits_v<T>;
296 return __clz(x) - shift;
297#elif defined(KOKKOS_ENABLE_SYCL)
300 return countl_zero_fallback(x);
305KOKKOS_IMPL_HOST_FUNCTION
306 std::enable_if_t<is_standard_unsigned_integer_type_v<T>,
int>
307 countl_zero_builtin_host(T x)
noexcept {
308 using ::Kokkos::Experimental::digits_v;
309 if (x == 0)
return digits_v<T>;
310#ifdef KOKKOS_IMPL_USE_GCC_BUILT_IN_FUNCTIONS
311 if constexpr (std::is_same_v<T, unsigned long long>) {
312 return __builtin_clzll(x);
313 }
else if constexpr (std::is_same_v<T, unsigned long>) {
314 return __builtin_clzl(x);
315 }
else if constexpr (std::is_same_v<T, unsigned int>) {
316 return __builtin_clz(x);
318 constexpr int shift = digits_v<unsigned int> - digits_v<T>;
319 return __builtin_clz(x) - shift;
322 return countl_zero_fallback(x);
327KOKKOS_IMPL_DEVICE_FUNCTION
328 std::enable_if_t<is_standard_unsigned_integer_type_v<T>,
int>
329 countr_zero_builtin_device(T x)
noexcept {
330 using ::Kokkos::Experimental::digits_v;
331 if (x == 0)
return digits_v<T>;
332#if defined(KOKKOS_ENABLE_CUDA) || defined(KOKKOS_ENABLE_HIP)
333 if constexpr (
sizeof(T) ==
sizeof(
long long int))
334 return __ffsll(
reinterpret_cast<long long int&
>(x)) - 1;
335 return __ffs(
reinterpret_cast<int&
>(x)) - 1;
336#elif defined(KOKKOS_ENABLE_SYCL)
339 return countr_zero_fallback(x);
344KOKKOS_IMPL_HOST_FUNCTION
345 std::enable_if_t<is_standard_unsigned_integer_type_v<T>,
int>
346 countr_zero_builtin_host(T x)
noexcept {
347 using ::Kokkos::Experimental::digits_v;
348 if (x == 0)
return digits_v<T>;
349#ifdef KOKKOS_IMPL_USE_GCC_BUILT_IN_FUNCTIONS
350 if constexpr (std::is_same_v<T, unsigned long long>) {
351 return __builtin_ctzll(x);
352 }
else if constexpr (std::is_same_v<T, unsigned long>) {
353 return __builtin_ctzl(x);
355 return __builtin_ctz(x);
358 return countr_zero_fallback(x);
363KOKKOS_IMPL_DEVICE_FUNCTION
364 std::enable_if_t<is_standard_unsigned_integer_type_v<T>,
int>
365 popcount_builtin_device(T x)
noexcept {
366#if defined(KOKKOS_ENABLE_CUDA) || defined(KOKKOS_ENABLE_HIP)
367 if constexpr (
sizeof(T) ==
sizeof(
long long int))
return __popcll(x);
369#elif defined(KOKKOS_ENABLE_SYCL)
370 return sycl::popcount(x);
372 return popcount_fallback(x);
377KOKKOS_IMPL_HOST_FUNCTION
378 std::enable_if_t<is_standard_unsigned_integer_type_v<T>,
int>
379 popcount_builtin_host(T x)
noexcept {
380#ifdef KOKKOS_IMPL_USE_GCC_BUILT_IN_FUNCTIONS
381 if constexpr (std::is_same_v<T, unsigned long long>) {
382 return __builtin_popcountll(x);
383 }
else if constexpr (std::is_same_v<T, unsigned long>) {
384 return __builtin_popcountl(x);
386 return __builtin_popcount(x);
389 return popcount_fallback(x);
393#undef KOKKOS_IMPL_USE_GCC_BUILT_IN_FUNCTIONS
397namespace Kokkos::Experimental {
399template <
class To,
class From>
400KOKKOS_FUNCTION std::enable_if_t<
sizeof(To) ==
sizeof(From) &&
401 std::is_trivially_copyable_v<To> &&
402 std::is_trivially_copyable_v<From>,
404bit_cast_builtin(From
const& from)
noexcept {
406 return Kokkos::bit_cast<To>(from);
410KOKKOS_FUNCTION std::enable_if_t<std::is_integral_v<T>, T> byteswap_builtin(
412 KOKKOS_IF_ON_DEVICE((return ::Kokkos::Impl::byteswap_builtin_device(x);))
413 KOKKOS_IF_ON_HOST((return ::Kokkos::Impl::byteswap_builtin_host(x);))
417KOKKOS_FUNCTION std::enable_if_t<
418 ::Kokkos::Impl::is_standard_unsigned_integer_type_v<T>,
int>
419countl_zero_builtin(T x)
noexcept {
420 KOKKOS_IF_ON_DEVICE((return ::Kokkos::Impl::countl_zero_builtin_device(x);))
421 KOKKOS_IF_ON_HOST((return ::Kokkos::Impl::countl_zero_builtin_host(x);))
425KOKKOS_FUNCTION std::enable_if_t<
426 ::Kokkos::Impl::is_standard_unsigned_integer_type_v<T>,
int>
427countl_one_builtin(T x)
noexcept {
428 if (x == finite_max_v<T>)
return digits_v<T>;
429 return countl_zero_builtin(
static_cast<T
>(~x));
433KOKKOS_FUNCTION std::enable_if_t<
434 ::Kokkos::Impl::is_standard_unsigned_integer_type_v<T>,
int>
435countr_zero_builtin(T x)
noexcept {
436 KOKKOS_IF_ON_DEVICE((return ::Kokkos::Impl::countr_zero_builtin_device(x);))
437 KOKKOS_IF_ON_HOST((return ::Kokkos::Impl::countr_zero_builtin_host(x);))
441KOKKOS_FUNCTION std::enable_if_t<
442 ::Kokkos::Impl::is_standard_unsigned_integer_type_v<T>,
int>
443countr_one_builtin(T x)
noexcept {
444 if (x == finite_max_v<T>)
return digits_v<T>;
445 return countr_zero_builtin(
static_cast<T
>(~x));
449KOKKOS_FUNCTION std::enable_if_t<
450 ::Kokkos::Impl::is_standard_unsigned_integer_type_v<T>,
int>
451popcount_builtin(T x)
noexcept {
452 KOKKOS_IF_ON_DEVICE((return ::Kokkos::Impl::popcount_builtin_device(x);))
453 KOKKOS_IF_ON_HOST((return ::Kokkos::Impl::popcount_builtin_host(x);))
457KOKKOS_FUNCTION std::enable_if_t<
458 ::Kokkos::Impl::is_standard_unsigned_integer_type_v<T>,
bool>
459has_single_bit_builtin(T x)
noexcept {
460 return has_single_bit(x);
465 std::enable_if_t<::Kokkos::Impl::is_standard_unsigned_integer_type_v<T>, T>
466 bit_ceil_builtin(T x)
noexcept {
467 if (x <= 1)
return 1;
468 return T{1} << (digits_v<T> - countl_zero_builtin(
static_cast<T
>(x - 1)));
473 std::enable_if_t<::Kokkos::Impl::is_standard_unsigned_integer_type_v<T>, T>
474 bit_floor_builtin(T x)
noexcept {
475 if (x == 0)
return 0;
476 return T{1} << (digits_v<T> - 1 - countl_zero_builtin(x));
481 std::enable_if_t<::Kokkos::Impl::is_standard_unsigned_integer_type_v<T>, T>
482 bit_width_builtin(T x)
noexcept {
483 if (x == 0)
return 0;
484 return digits_v<T> - countl_zero_builtin(x);
488[[nodiscard]] KOKKOS_FUNCTION
489 std::enable_if_t<::Kokkos::Impl::is_standard_unsigned_integer_type_v<T>, T>
490 rotl_builtin(T x,
int s)
noexcept {
495[[nodiscard]] KOKKOS_FUNCTION
496 std::enable_if_t<::Kokkos::Impl::is_standard_unsigned_integer_type_v<T>, T>
497 rotr_builtin(T x,
int s)
noexcept {
ScopeGuard Some user scope issues have been identified with some Kokkos::finalize calls; ScopeGuard a...