forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbitfield.h
More file actions
104 lines (97 loc) · 2.98 KB
/
bitfield.h
File metadata and controls
104 lines (97 loc) · 2.98 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
// 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 bitfield.h
/// \author David Rohr
#ifndef Q_BITFIELD_H
#define Q_BITFIELD_H
#if !defined(GPUCA_GPUCODE_DEVICE) && !defined(GPUCA_GPUCODE_COMPILEKERNELS)
#include <type_traits>
#endif
template <class T, class S>
class bitfield
{
public:
bitfield(T v) : bits((S)v) {}
bitfield(S v = 0) : bits(v) {}
bitfield(const bitfield&) = default;
bitfield& operator=(const bitfield&) = default;
bitfield operator|(const bitfield v) const { return bits | v.bits; }
bitfield operator|(const T v) const { return bits | static_cast<S>(v); }
bitfield& operator|=(const bitfield v)
{
bits |= v.bits;
return *this;
}
bitfield operator&(const bitfield v) const { return bits & v.bits; }
bitfield operator&(const T v) const { return bits & static_cast<S>(v); }
bitfield& operator&=(const bitfield v)
{
bits &= v.bits;
return *this;
}
bitfield operator~() const { return ~bits; }
bool operator==(const bitfield v) { return bits == v.bits; }
bool operator==(const T v) { return bits == static_cast<S>(v); }
bool operator!=(const bitfield v) { return bits != v.bits; }
bool operator!=(const T v) { return bits != static_cast<S>(v); }
bitfield& setBits(const bitfield v, bool w)
{
if (w) {
bits |= v.bits;
} else {
bits &= ~v.bits;
}
return *this;
}
void clear() { bits = 0; }
void set(S v) { bits = v; }
void set(T v) { bits = static_cast<S>(v); }
template <typename... Args>
void set(T v, Args... args)
{
this->set(args...);
bits |= static_cast<S>(v);
}
S get() const { return bits; }
operator bool() const { return bits; }
operator S() const { return bits; }
bool isSet(const bitfield& v) const { return *this & v; }
bool isSet(const S v) const { return bits & v; }
template <typename... Args>
bool isSetAll(Args... args)
{
return (bits & lor(args...).bits) == lor(args...).bits;
}
template <typename... Args>
bool isSetAny(Args... args)
{
return (bits & lor(args...).bits) != 0;
}
template <typename... Args>
bool isOnlySet(Args... args)
{
return (bits & ~lor(args...).bits) == 0;
}
template <typename... Args>
static bitfield lor(const Args&... args)
{
bitfield retVal;
retVal.set(args...);
return retVal;
}
#if !defined(GPUCA_GPUCODE_DEVICE)
static_assert(std::is_integral_v<S>, "Storage type non integral");
static_assert(sizeof(S) >= sizeof(T), "Storage type has insufficient capacity");
#endif
private:
S bits;
};
#endif