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 static GPUglobalconstexpr() uint32_t full_set = ((1ul << N) - 1ul);
51
52 GPUd() constexpr bool all() const { return (v & full_set) == full_set; }
53 GPUd() constexpr bool any() const { return v & full_set; }
54 GPUd() constexpr bool none() const { return !any(); }
55
56 GPUd() constexpr void set() { v = full_set; }
57 GPUd() constexpr void set(uint32_t i) { v |= (1u << i) & full_set; }
58 GPUd() constexpr void reset() { v = 0; }
59 GPUd() constexpr void reset(uint32_t i) { v &= ~(1u << i); }
60 GPUd() constexpr void flip() { v = (~v) & full_set; }
61
62 GPUdDefault() constexpr bitset& operator=(const bitset&) = default;
63 GPUd() constexpr bitset operator|(const bitset b) const { return v | b.v; }
64 GPUd() constexpr bitset& operator|=(const bitset b)
65 {
66 v |= b.v;
67 return *this;
68 }
69 GPUd() constexpr bitset operator&(const bitset b) const { return v & b.v; }
70 GPUd() constexpr bitset& operator&=(const bitset b)
71 {
72 v &= b.v;
73 return *this;
74 }
75 GPUd() constexpr bitset operator^(const bitset b) const { return v ^ b.v; }
76 GPUd() constexpr bitset& operator^=(const bitset b)
77 {
78 v ^= b.v;
79 return *this;
80 }
81 GPUd() constexpr bitset operator~() const { return (~v) & full_set; }
82 GPUd() constexpr bool operator==(const bitset b) const { return v == b.v; }
83 GPUd() constexpr bool operator!=(const bitset b) const { return v != b.v; }
84
85 GPUd() constexpr bool operator[](uint32_t i) const { return (v >> i) & 1u; }
86
87 GPUd() constexpr uint32_t to_ulong() const { return v; }
88
89 GPUd() constexpr uint32_t count() const
90 {
91 // count number of non-0 bits in 32bit word
92 return GPUCommonMath::Popcount(v);
93 }
94
95#ifndef GPUCA_GPUCODE_DEVICE
96 std::string to_string() const;
97#endif
98
99 private:
100 uint32_t v = 0;
101
102 ClassDefNV(bitset, 1);
103};
104
105#ifndef GPUCA_GPUCODE_DEVICE
106template <uint32_t N>
107inline std::string bitset<N>::to_string() const
108{
109 std::string retVal;
110 for (uint32_t i = N; i--;) {
111 retVal += std::to_string((int32_t)((*this)[i]));
112 }
113 return retVal;
114}
115template <class CharT, class Traits, uint32_t N>
116std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const bitset<N>& x)
117{
118 os << x.to_string();
119 return os;
120}
121
122#endif
123} // namespace o2::gpu::gpustd
124
125#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