timemory 3.3.0
Modular C++ Toolkit for Performance Analysis and Logging. Profiling API and Tools for C, C++, CUDA, Fortran, and Python. The C++ template API is essentially a framework to creating tools: it is designed to provide a unifying interface for recording various performance measurements alongside data logging and interfaces to other tools.
bit_flags.hpp
Go to the documentation of this file.
1// MIT License
2//
3// Copyright (c) 2020, The Regents of the University of California,
4// through Lawrence Berkeley National Laboratory (subject to receipt of any
5// required approvals from the U.S. Dept. of Energy). All rights reserved.
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in all
15// copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23// SOFTWARE.
24
25#pragma once
26
28
29#include <cstdint>
30#include <type_traits>
31
32namespace tim
33{
34namespace utility
35{
36template <size_t Nopts>
38{
39 static_assert(Nopts <= 64, "Error! bit_flags does not support more than 64 options");
42 (Nopts <= 8), uint8_t,
43 std::conditional_t<(Nopts <= 16), uint16_t,
44 std::conditional_t<(Nopts <= 32), uint32_t, uint64_t>>>;
45
46 template <size_t Idx>
47 TIMEMORY_INLINE bool test() const;
48 TIMEMORY_INLINE bool test(value_type idx) const;
49
50 template <size_t Idx>
51 TIMEMORY_INLINE void set(bool v);
52 TIMEMORY_INLINE void set(value_type idx, bool v);
53
54 TIMEMORY_INLINE void reset() { m_state_value = 0; }
55 TIMEMORY_INLINE void set_state_value(value_type v) { m_state_value = v; }
56 TIMEMORY_INLINE value_type get_state_value() const { return m_state_value; }
57
58private:
59 template <size_t Idx>
60 static constexpr value_type index();
61 static value_type index(value_type idx);
62
63 value_type m_state_value{ 0 };
64};
65//
66template <size_t Nopts>
67template <size_t Idx>
68bool
70{
71 return (m_state_value & index<Idx>());
72}
73//
74template <size_t Nopts>
75bool
77{
78 return (m_state_value & index(idx));
79}
80//
81template <size_t Nopts>
82template <size_t Idx>
83void
85{
86 bool _curr = test<Idx>();
87 if(_curr != v)
88 {
89 if(!_curr)
90 m_state_value |= index<Idx>();
91 else
92 m_state_value &= (~index<Idx>());
93 }
94}
95//
96template <size_t Nopts>
97void
99{
100 bool _curr = test(idx);
101 if(_curr != v)
102 {
103 if(!_curr)
104 m_state_value |= index(idx);
105 else
106 m_state_value &= (~index(idx));
107 }
108}
109//
110template <size_t Nopts>
111template <size_t Idx>
112constexpr typename bit_flags<Nopts>::value_type
114{
115 return 1 << Idx;
116}
117//
118template <size_t Nopts>
120bit_flags<Nopts>::index(value_type idx)
121{
122 return 1 << idx;
123}
124//
125} // namespace utility
126} // namespace tim
Definition: kokkosp.cpp:39
typename std::conditional< B, Lhs, Rhs >::type conditional_t
Definition: types.hpp:197
bool test(value_type idx) const
Definition: bit_flags.hpp:76
void set(value_type idx, bool v)
Definition: bit_flags.hpp:98
value_type get_state_value() const
Definition: bit_flags.hpp:56
std::conditional_t<(Nopts<=8), uint8_t, std::conditional_t<(Nopts<=16), uint16_t, std::conditional_t<(Nopts<=32), uint32_t, uint64_t > > > value_type
Definition: bit_flags.hpp:44
void set_state_value(value_type v)
Definition: bit_flags.hpp:55