1 //===--------- Utils.h - OpenMP device runtime utility functions -- 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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //
10 //===----------------------------------------------------------------------===//
11
12 #ifndef OMPTARGET_DEVICERTL_UTILS_H
13 #define OMPTARGET_DEVICERTL_UTILS_H
14
15 #include "Types.h"
16
17 #pragma omp begin declare target device_type(nohost)
18
19 namespace _OMP {
20 namespace utils {
21
22 /// Return the value \p Var from thread Id \p SrcLane in the warp if the thread
23 /// is identified by \p Mask.
24 int32_t shuffle(uint64_t Mask, int32_t Var, int32_t SrcLane);
25
26 int32_t shuffleDown(uint64_t Mask, int32_t Var, uint32_t Delta, int32_t Width);
27
28 /// Return \p LowBits and \p HighBits packed into a single 64 bit value.
29 uint64_t pack(uint32_t LowBits, uint32_t HighBits);
30
31 /// Unpack \p Val into \p LowBits and \p HighBits.
32 void unpack(uint64_t Val, uint32_t &LowBits, uint32_t &HighBits);
33
34 /// Round up \p V to a \p Boundary.
roundUp(Ty V,Ty Boundary)35 template <typename Ty> inline Ty roundUp(Ty V, Ty Boundary) {
36 return (V + Boundary - 1) / Boundary * Boundary;
37 }
38
39 /// Advance \p Ptr by \p Bytes bytes.
advance(Ty1 Ptr,Ty2 Bytes)40 template <typename Ty1, typename Ty2> inline Ty1 *advance(Ty1 Ptr, Ty2 Bytes) {
41 return reinterpret_cast<Ty1 *>(reinterpret_cast<char *>(Ptr) + Bytes);
42 }
43
44 /// Return the first bit set in \p V.
ffs(uint32_t V)45 inline uint32_t ffs(uint32_t V) {
46 static_assert(sizeof(int) == sizeof(uint32_t), "type size mismatch");
47 return __builtin_ffs(V);
48 }
49
50 /// Return the first bit set in \p V.
ffs(uint64_t V)51 inline uint32_t ffs(uint64_t V) {
52 static_assert(sizeof(long) == sizeof(uint64_t), "type size mismatch");
53 return __builtin_ffsl(V);
54 }
55
56 /// Return the number of bits set in \p V.
popc(uint32_t V)57 inline uint32_t popc(uint32_t V) {
58 static_assert(sizeof(int) == sizeof(uint32_t), "type size mismatch");
59 return __builtin_popcount(V);
60 }
61
62 /// Return the number of bits set in \p V.
popc(uint64_t V)63 inline uint32_t popc(uint64_t V) {
64 static_assert(sizeof(long) == sizeof(uint64_t), "type size mismatch");
65 return __builtin_popcountl(V);
66 }
67
68 /// Return \p V aligned "upwards" according to \p Align.
align_up(Ty1 V,Ty2 Align)69 template <typename Ty1, typename Ty2> inline Ty1 align_up(Ty1 V, Ty2 Align) {
70 return ((V + Ty1(Align) - 1) / Ty1(Align)) * Ty1(Align);
71 }
72 /// Return \p V aligned "downwards" according to \p Align.
align_down(Ty1 V,Ty2 Align)73 template <typename Ty1, typename Ty2> inline Ty1 align_down(Ty1 V, Ty2 Align) {
74 return V - V % Align;
75 }
76
77 /// A pointer variable that has by design an `undef` value. Use with care.
78 __attribute__((loader_uninitialized)) static void *const UndefPtr;
79
80 #define OMP_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
81 #define OMP_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
82
83 } // namespace utils
84 } // namespace _OMP
85
86 #pragma omp end declare target
87
88 #endif
89