-
Notifications
You must be signed in to change notification settings - Fork 496
Expand file tree
/
Copy pathGPUCommonBitSet.h
More file actions
120 lines (102 loc) · 3.64 KB
/
GPUCommonBitSet.h
File metadata and controls
120 lines (102 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// \file GPUCommonBitSet.h
/// \author David Rohr
#ifndef GPUCOMMONBITSET_H
#define GPUCOMMONBITSET_H
// Limited reimplementation of std::bitset for the GPU.
// Fixed to 32 bits for now.
// In contrast to the GPUCommonArray, we cannot just use std::bitset on the host.
// The layout may be implementation defined, so it is not guarantueed that we
// get correct data after copying it into a gpustd::bitset on the GPU.
#include "GPUCommonDef.h"
#include "GPUCommonRtypes.h"
#include "GPUCommonMath.h"
#ifndef GPUCA_GPUCODE_DEVICE
#include <string>
#endif
namespace o2::gpu::gpustd
{
template <uint32_t N>
class bitset
{
static_assert(N <= 32, "> 32 bits not supported");
public:
GPUdDefault() constexpr bitset() = default;
GPUdDefault() constexpr bitset(const bitset&) = default;
#ifdef __OPENCL__
GPUdDefault() constexpr bitset(const __constant bitset&) = default;
#endif // __OPENCL__
GPUd() constexpr bitset(uint32_t vv) : v(vv) {};
static constexpr uint32_t full_set = ((1ul << N) - 1ul);
GPUd() constexpr bool all() const { return (v & full_set) == full_set; }
GPUd() constexpr bool any() const { return v & full_set; }
GPUd() constexpr bool none() const { return !any(); }
GPUd() constexpr void set() { v = full_set; }
GPUd() constexpr void set(uint32_t i) { v |= (1u << i) & full_set; }
GPUd() constexpr void reset() { v = 0; }
GPUd() constexpr void reset(uint32_t i) { v &= ~(1u << i); }
GPUd() constexpr void flip() { v = (~v) & full_set; }
GPUdDefault() constexpr bitset& operator=(const bitset&) = default;
GPUd() constexpr bitset operator|(const bitset b) const { return v | b.v; }
GPUd() constexpr bitset& operator|=(const bitset b)
{
v |= b.v;
return *this;
}
GPUd() constexpr bitset operator&(const bitset b) const { return v & b.v; }
GPUd() constexpr bitset& operator&=(const bitset b)
{
v &= b.v;
return *this;
}
GPUd() constexpr bitset operator^(const bitset b) const { return v ^ b.v; }
GPUd() constexpr bitset& operator^=(const bitset b)
{
v ^= b.v;
return *this;
}
GPUd() constexpr bitset operator~() const { return (~v) & full_set; }
GPUd() constexpr bool operator==(const bitset b) const { return v == b.v; }
GPUd() constexpr bool operator!=(const bitset b) const { return v != b.v; }
GPUd() constexpr bool operator[](uint32_t i) const { return (v >> i) & 1u; }
GPUd() constexpr uint32_t to_ulong() const { return v; }
GPUd() constexpr uint32_t count() const
{
// count number of non-0 bits in 32bit word
return GPUCommonMath::Popcount(v);
}
#ifndef GPUCA_GPUCODE_DEVICE
std::string to_string() const;
#endif
private:
uint32_t v = 0;
ClassDefNV(bitset, 1);
};
#ifndef GPUCA_GPUCODE_DEVICE
template <uint32_t N>
inline std::string bitset<N>::to_string() const
{
std::string retVal;
for (uint32_t i = N; i--;) {
retVal += std::to_string((int32_t)((*this)[i]));
}
return retVal;
}
template <class CharT, class Traits, uint32_t N>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const bitset<N>& x)
{
os << x.to_string();
return os;
}
#endif
} // namespace o2::gpu::gpustd
#endif