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