#ifndef OMPTARGET_DEVICERTL_UTILS_H
#define OMPTARGET_DEVICERTL_UTILS_H
#include "Types.h"
#pragma omp begin declare target device_type(nohost)
namespace ompx {
namespace utils {
int32_t shuffle(uint64_t Mask, int32_t Var, int32_t SrcLane);
int32_t shuffleDown(uint64_t Mask, int32_t Var, uint32_t Delta, int32_t Width);
int64_t shuffleDown(uint64_t Mask, int64_t Var, uint32_t Delta, int32_t Width);
uint64_t ballotSync(uint64_t Mask, int32_t Pred);
uint64_t pack(uint32_t LowBits, uint32_t HighBits);
void unpack(uint64_t Val, uint32_t &LowBits, uint32_t &HighBits);
template <typename Ty> inline Ty roundUp(Ty V, Ty Boundary) {
return (V + Boundary - 1) / Boundary * Boundary;
}
template <typename Ty1, typename Ty2> inline Ty1 *advance(Ty1 Ptr, Ty2 Bytes) {
return reinterpret_cast<Ty1 *>(reinterpret_cast<char *>(Ptr) + Bytes);
}
inline uint32_t ffs(uint32_t V) {
static_assert(sizeof(int) == sizeof(uint32_t), "type size mismatch");
return __builtin_ffs(V);
}
inline uint32_t ffs(uint64_t V) {
static_assert(sizeof(long) == sizeof(uint64_t), "type size mismatch");
return __builtin_ffsl(V);
}
inline uint32_t popc(uint32_t V) {
static_assert(sizeof(int) == sizeof(uint32_t), "type size mismatch");
return __builtin_popcount(V);
}
inline uint32_t popc(uint64_t V) {
static_assert(sizeof(long) == sizeof(uint64_t), "type size mismatch");
return __builtin_popcountl(V);
}
template <typename Ty1, typename Ty2> inline Ty1 align_up(Ty1 V, Ty2 Align) {
return ((V + Ty1(Align) - 1) / Ty1(Align)) * Ty1(Align);
}
template <typename Ty1, typename Ty2> inline Ty1 align_down(Ty1 V, Ty2 Align) {
return V - V % Align;
}
bool isSharedMemPtr(void *Ptr);
template <typename DstTy, typename SrcTy> inline DstTy convertViaPun(SrcTy V) {
return *((DstTy *)(&V));
}
[[clang::loader_uninitialized]] static void *const UndefPtr;
#define OMP_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
#define OMP_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
}
}
#pragma omp end declare target
#endif