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 namespace _OMP {
18 namespace utils {
19 
20 /// Return the value \p Var from thread Id \p SrcLane in the warp if the thread
21 /// is identified by \p Mask.
22 int32_t shuffle(uint64_t Mask, int32_t Var, int32_t SrcLane);
23 
24 int32_t shuffleDown(uint64_t Mask, int32_t Var, uint32_t Delta, int32_t Width);
25 
26 /// Return \p LowBits and \p HighBits packed into a single 64 bit value.
27 uint64_t pack(uint32_t LowBits, uint32_t HighBits);
28 
29 /// Unpack \p Val into \p LowBits and \p HighBits.
30 void unpack(uint64_t Val, uint32_t &LowBits, uint32_t &HighBits);
31 
32 /// Round up \p V to a \p Boundary.
33 template <typename Ty> inline Ty roundUp(Ty V, Ty Boundary) {
34   return (V + Boundary - 1) / Boundary * Boundary;
35 }
36 
37 /// Advance \p Ptr by \p Bytes bytes.
38 template <typename Ty1, typename Ty2> inline Ty1 *advance(Ty1 Ptr, Ty2 Bytes) {
39   return reinterpret_cast<Ty1 *>(reinterpret_cast<char *>(Ptr) + Bytes);
40 }
41 
42 /// Return the first bit set in \p V.
43 inline uint32_t ffs(uint32_t V) {
44   static_assert(sizeof(int) == sizeof(uint32_t), "type size mismatch");
45   return __builtin_ffs(V);
46 }
47 
48 /// Return the first bit set in \p V.
49 inline uint32_t ffs(uint64_t V) {
50   static_assert(sizeof(long) == sizeof(uint64_t), "type size mismatch");
51   return __builtin_ffsl(V);
52 }
53 
54 /// Return the number of bits set in \p V.
55 inline uint32_t popc(uint32_t V) {
56   static_assert(sizeof(int) == sizeof(uint32_t), "type size mismatch");
57   return __builtin_popcount(V);
58 }
59 
60 /// Return the number of bits set in \p V.
61 inline uint32_t popc(uint64_t V) {
62   static_assert(sizeof(long) == sizeof(uint64_t), "type size mismatch");
63   return __builtin_popcountl(V);
64 }
65 
66 #define OMP_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
67 #define OMP_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
68 
69 } // namespace utils
70 } // namespace _OMP
71 
72 #endif
73