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
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#ifndef __METAL__
41 // MSL will not use a user-declared copy constructor to build an object in the
42 // constant address space, where GPUconstexpr() arrays of bitset live. Leaving
43 // it implicit is what makes those arrays constructible.
44 GPUdDefault() constexpr bitset(const bitset&) = default;
45#endif
46#ifdef __OPENCL__
47 GPUdDefault() constexpr bitset(const __constant bitset&) = default;
48#endif // __OPENCL__
49 GPUd() constexpr bitset(uint32_t vv) : v(vv) {};
50#ifdef __METAL__
51 GPUd() constexpr bitset(uint32_t vv) constant : v(vv) {};
52#endif
53 static GPUglobalconstexpr() uint32_t full_set = ((1ul << N) - 1ul);
54
55 GPUd() constexpr bool all() const { return (v & full_set) == full_set; }
56 GPUd() constexpr bool any() const { return v & full_set; }
57 GPUd() constexpr bool none() const { return !any(); }
58
59 GPUd() constexpr void set() { v = full_set; }
60 GPUd() constexpr void set(uint32_t i) { v |= (1u << i) & full_set; }
61 GPUd() constexpr void reset() { v = 0; }
62 GPUd() constexpr void reset(uint32_t i) { v &= ~(1u << i); }
63 GPUd() constexpr void flip() { v = (~v) & full_set; }
64
65 GPUdDefault() constexpr bitset& operator=(const bitset&) = default;
66 GPUd() constexpr bitset operator|(const bitset b) const { return v | b.v; }
67 GPUd() constexpr bitset& operator|=(const bitset b)
68 {
69 v |= b.v;
70 return *this;
71 }
72 GPUd() constexpr bitset operator&(const bitset b) const { return v & b.v; }
73 GPUd() constexpr bitset& operator&=(const bitset b)
74 {
75 v &= b.v;
76 return *this;
77 }
78 GPUd() constexpr bitset operator^(const bitset b) const { return v ^ b.v; }
79 GPUd() constexpr bitset& operator^=(const bitset b)
80 {
81 v ^= b.v;
82 return *this;
83 }
84 GPUd() constexpr bitset operator~() const { return (~v) & full_set; }
85 GPUd() constexpr bool operator==(const bitset b) const { return v == b.v; }
86 GPUd() constexpr bool operator!=(const bitset b) const { return v != b.v; }
87
88 GPUd() constexpr bool operator[](uint32_t i) const { return (v >> i) & 1u; }
89
90 GPUd() constexpr uint32_t to_ulong() const { return v; }
91
92 GPUd() constexpr uint32_t count() const
93 {
94 // count number of non-0 bits in 32bit word
95 return GPUCommonMath::Popcount(v);
96 }
97
98#ifndef GPUCA_GPUCODE_DEVICE
99 std::string to_string() const;
100#endif
101
102 private:
103 uint32_t v = 0;
104
105 ClassDefNV(bitset, 1);
106};
107
108#ifndef GPUCA_GPUCODE_DEVICE
109template <uint32_t N>
110inline std::string bitset<N>::to_string() const
111{
112 std::string retVal;
113 for (uint32_t i = N; i--;) {
114 retVal += std::to_string((int32_t)((*this)[i]));
115 }
116 return retVal;
117}
118template <class CharT, class Traits, uint32_t N>
119std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const bitset<N>& x)
120{
121 os << x.to_string();
122 return os;
123}
124
125#endif
126} // namespace o2::gpu::gpustd
127
128#endif
int32_t i
int32_t retVal
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)
static GPUglobalconstexpr() uint32_t full_set
GPUd() const expr void set()
GPUd() const expr bool none() const
GPUd() const expr bool any() const
GPUdDefault() const expr bitset()=default
GPUd() const expr bitset &operator|
GPUdDefault() const expr bitset(const bitset &)=default
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