DUNE-ACFEM (unstable)

findbit.hh
1#ifndef __DUNE_ACFEM_COMMON_FINDBIT_HH__
2#define __DUNE_ACFEM_COMMON_FINDBIT_HH__
3
4#include "types.hh"
5
6namespace Dune {
7
8 namespace ACFem {
9
23 namespace {
24 template<class T, T val, bool isZero = val == 0, std::size_t N = std::numeric_limits<T>::digits/2, class SFINAE = void>
25 struct FindMostSignificantBitHelper;
26
27 // Bits only in lower half
28 template<class T, T val, bool isZero, std::size_t N>
29 struct FindMostSignificantBitHelper<T, val, isZero, N, std::enable_if_t<!isZero && (N > 0) && 0 == (val & (std::numeric_limits<T>::max() << N))> >
30 {
31 static constexpr std::size_t value = FindMostSignificantBitHelper<T, val, val == 0, N/2>::value;
32 };
33
34 // Bits in upper half
35 template<class T, T val, bool isZero, std::size_t N>
36 struct FindMostSignificantBitHelper<T, val, isZero, N, std::enable_if_t<!isZero && (N > 0) && 0 != (val & (std::numeric_limits<T>::max() << N))> >
37 {
38 static constexpr std::size_t value = N + FindMostSignificantBitHelper<T, (val >> N), (val >> N) == 0, N/2>::value;
39 };
40
41 // recursion end-point / optimization
42 template<class T, T V, std::size_t N>
43 struct FindMostSignificantBitHelper<T, V, true, N>
44 {
45 static constexpr std::size_t value = -1;
46 };
47
48 // recursion end-point / failsafe
49 template<class T, T V, bool isZero>
50 struct FindMostSignificantBitHelper<T, V, isZero, 0>
51 {
52 static constexpr std::size_t value = 0;
53 };
54 }
55
60 template<class T, T V>
61 constexpr std::size_t findMostSignificantBit(Constant<T, V>&&)
62 {
63 return FindMostSignificantBitHelper<T, V>::value;
64 }
65
69 template<std::size_t mask>
71
72 namespace {
73 template<class T, T val, bool isZero = val == 0, std::size_t N = std::numeric_limits<T>::digits/2, class = void>
74 struct FindLeastSignificantBitHelper;
75
76 // Bits in lower half
77 template<class T, T val, bool isZero, std::size_t N>
78 struct FindLeastSignificantBitHelper<T, val, isZero, N, std::enable_if_t<!isZero && (N > 0) && 0 != (val & ~(std::numeric_limits<T>::max() << N))> >
79 {
80 static constexpr std::size_t value = FindLeastSignificantBitHelper<T, val, val == 0, N/2>::value;
81 };
82
83 // Bits only in upper half
84 template<class T, T val, bool isZero, std::size_t N>
85 struct FindLeastSignificantBitHelper<T, val, isZero, N, std::enable_if_t<!isZero && (N > 0) && 0 == (val & ~(std::numeric_limits<T>::max() << N))> >
86 {
87 static constexpr std::size_t value = N + FindLeastSignificantBitHelper<T, (val >> N), (val >> N) == 0, N/2>::value;
88 };
89
90 // recursion end-point / optimization
91 template<class T, T V, std::size_t N>
92 struct FindLeastSignificantBitHelper<T, V, true, N>
93 {
94 static constexpr std::size_t value = -1;
95 };
96
97 // recursion end-point / failsafe
98 template<class T, T V, bool isZero>
99 struct FindLeastSignificantBitHelper<T, V, isZero, 0>
100 {
101 static constexpr std::size_t value = 0;
102 };
103 }
104
109 template<class T, T V>
111 {
112 return FindLeastSignificantBitHelper<T, V>::value;
113 }
114
118 template<std::size_t mask>
120
124 template<class T, T V>
125 constexpr std::size_t findFirstZeroBit(Constant<T, V>&&)
126 {
127 return FindLeastSignificantBitHelper<T, ~V>::value;
128 }
129
133 template<std::size_t mask>
135
137
139
140 } // ACFem::
141
142} // Dune::
143
144#endif // __DUNE_ACFEM_COMMON_FINDBIT_HH__
constexpr std::size_t findMostSignificantBit(Constant< T, V > &&)
Template-algorithm which returns the the index of the most significant bit in an integer-constant.
Definition: findbit.hh:61
IndexConstant< FindLeastSignificantBitHelper< std::size_t, ~mask >::value > FindFirstZeroBit
Template-algorithm which defines the index of the first 0-bit as a std::index_constant.
Definition: findbit.hh:134
IndexConstant< FindLeastSignificantBitHelper< std::size_t, mask >::value > FindLeastSignificantBit
Template-algorithm which defines the index of the least significant bit as a std::index_constant.
Definition: findbit.hh:119
constexpr std::size_t findFirstZeroBit(Constant< T, V > &&)
Template-algorithm which returns the the index of the first 0-bit in an integer-constant.
Definition: findbit.hh:125
IndexConstant< FindMostSignificantBitHelper< std::size_t, mask >::value > FindMostSignificantBit
Template-algorithm which defines the index of the most significant bit as a std::index_constant.
Definition: findbit.hh:70
constexpr std::size_t findLeastSignificantBit(Constant< T, V > &&)
Template-algorithm which returns the the index of the least significant bit in an integer-constant.
Definition: findbit.hh:110
integral_constant< T, V > Constant
Short-cut for any integral constant.
Definition: types.hh:40
Constant< std::size_t, V > IndexConstant
Short-cut for integral constant of type std::size_t.
Definition: types.hh:44
constexpr bool isZero
Shortcut identifying a zero model.
Definition: modeltraits.hh:642
STL namespace.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Jun 19, 23:17, 2026)