brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.2 KiB · fa0212e Raw
70 lines · c
1//===-- Shared/Utils.h - Target independent OpenMP target RTL -- C++ ------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// Routines and classes used to provide useful functionalities for the host and10// the device.11//12//===----------------------------------------------------------------------===//13 14#ifndef OMPTARGET_SHARED_UTILS_H15#define OMPTARGET_SHARED_UTILS_H16 17#include <stdint.h>18 19namespace utils {20 21/// Return the difference (in bytes) between \p Begin and \p End.22template <typename Ty = char>23auto getPtrDiff(const void *End, const void *Begin) {24  return reinterpret_cast<const Ty *>(End) -25         reinterpret_cast<const Ty *>(Begin);26}27 28/// Return \p Ptr advanced by \p Offset bytes.29template <typename Ty1, typename Ty2> Ty1 *advancePtr(Ty1 *Ptr, Ty2 Offset) {30  return (Ty1 *)(const_cast<char *>((const char *)(Ptr)) + Offset);31}32 33/// Return \p V aligned "upwards" according to \p Align.34template <typename Ty1, typename Ty2> inline Ty1 alignPtr(Ty1 V, Ty2 Align) {35  return reinterpret_cast<Ty1>(((uintptr_t(V) + Align - 1) / Align) * Align);36}37 38/// Round up \p V to a \p Boundary.39template <typename Ty> inline Ty roundUp(Ty V, Ty Boundary) {40  return alignPtr(V, Boundary);41}42 43/// Return the first bit set in \p V.44inline uint32_t ffs(uint32_t V) {45  static_assert(sizeof(int) == sizeof(uint32_t), "type size mismatch");46  return __builtin_ffs(V);47}48 49/// Return the first bit set in \p V.50inline uint32_t ffs(uint64_t V) {51  static_assert(sizeof(long) == sizeof(uint64_t), "type size mismatch");52  return __builtin_ffsl(V);53}54 55/// Return the number of bits set in \p V.56inline uint32_t popc(uint32_t V) {57  static_assert(sizeof(int) == sizeof(uint32_t), "type size mismatch");58  return __builtin_popcount(V);59}60 61/// Return the number of bits set in \p V.62inline uint32_t popc(uint64_t V) {63  static_assert(sizeof(long) == sizeof(uint64_t), "type size mismatch");64  return __builtin_popcountl(V);65}66 67} // namespace utils68 69#endif // OMPTARGET_SHARED_UTILS_H70