#ifndef OMPTARGET_SHARED_UTILS_H
#define OMPTARGET_SHARED_UTILS_H
#include <stdint.h>
namespace utils {
template <typename Ty = char>
auto getPtrDiff(const void *End, const void *Begin) {
return reinterpret_cast<const Ty *>(End) -
reinterpret_cast<const Ty *>(Begin);
}
template <typename Ty1, typename Ty2> Ty1 *advancePtr(Ty1 *Ptr, Ty2 Offset) {
return (Ty1 *)(const_cast<char *>((const char *)(Ptr)) + Offset);
}
template <typename Ty1, typename Ty2> inline Ty1 alignPtr(Ty1 V, Ty2 Align) {
return reinterpret_cast<Ty1>(((uintptr_t(V) + Align - 1) / Align) * Align);
}
template <typename Ty> inline Ty roundUp(Ty V, Ty Boundary) {
return alignPtr(V, Boundary);
}
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);
}
}
#endif