@@ -17,12 +17,22 @@ namespace sw { namespace universal {
17
17
18
18
// static const unsigned int bval[] = { 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4 };
19
19
20
+ template <typename BitPatternType>
21
+ constexpr unsigned find_msb (const BitPatternType& x) {
22
+ constexpr unsigned nbits = x.nbits ;
23
+ for (unsigned i = 0 ; i < nbits; ++i) {
24
+ unsigned bitIndex = nbits - 1 - i;
25
+ if (x.test (bitIndex)) return bitIndex + 1 ;
26
+ }
27
+ return 0 ;
28
+ }
29
+
20
30
// / <summary>
21
31
// / find most significant bit that is set
22
32
// / </summary>
23
33
// / <param name="x">value to scan</param>
24
34
// / <returns> position of MSB that is set. LSB is defined to be at position 1, so no bits set returns 0</returns>
25
- inline constexpr unsigned int find_msb (unsigned int x) {
35
+ inline constexpr unsigned find_msb (unsigned int x) {
26
36
// find the first non-zero bit
27
37
unsigned int base = 0 ;
28
38
if (x & 0xFFFF0000 ) { base += 16 ; x >>= 16 ; }
@@ -41,7 +51,7 @@ inline constexpr unsigned int find_msb(unsigned int x) {
41
51
// / </summary>
42
52
// / <param name="x">value to scan</param>
43
53
// / <returns> position of MSB that is set. LSB is defined to be at position 1, so no bits set returns 0</returns>
44
- inline constexpr unsigned int find_msb (unsigned long x) {
54
+ inline constexpr unsigned find_msb (unsigned long x) {
45
55
// find the first non-zero bit
46
56
unsigned int base = 0 ;
47
57
if (x & 0xFFFF0000 ) { base += 16 ; x >>= 16 ; }
@@ -60,7 +70,7 @@ inline constexpr unsigned int find_msb(unsigned long x) {
60
70
// / </summary>
61
71
// / <param name="x">value to scan</param>
62
72
// / <returns> position of MSB that is set. LSB is defined to be at position 1, so no bits set returns 0</returns>
63
- inline constexpr unsigned int find_msb (unsigned long long x) {
73
+ inline constexpr unsigned find_msb (unsigned long long x) {
64
74
// find the first non-zero bit
65
75
unsigned int base = 0 ;
66
76
if (x & 0xFFFFFFFF00000000 ) { base += 32 ; x >>= 32 ; }
@@ -82,7 +92,7 @@ inline constexpr unsigned int find_msb(unsigned long long x) {
82
92
// / </summary>
83
93
// / <param name="x">value to scan</param>
84
94
// / <returns> position of MSB that is set. LSB is defined to be at position 1, so no bits set returns 0</returns>
85
- inline constexpr unsigned int find_msb (signed char x) {
95
+ inline constexpr unsigned find_msb (signed char x) {
86
96
// find the first non-zero bit
87
97
uint8_t tmp = uint8_t (x);
88
98
unsigned int base = 0 ;
@@ -100,7 +110,7 @@ inline constexpr unsigned int find_msb(signed char x) {
100
110
// / </summary>
101
111
// / <param name="x">value to scan</param>
102
112
// / <returns> position of MSB that is set. LSB is defined to be at position 1, so no bits set returns 0</returns>
103
- inline constexpr unsigned int find_msb (short x) {
113
+ inline constexpr unsigned find_msb (short x) {
104
114
// find the first non-zero bit
105
115
uint16_t tmp = uint16_t (x);
106
116
unsigned int base = 0 ;
@@ -119,7 +129,7 @@ inline constexpr unsigned int find_msb(short x) {
119
129
// / </summary>
120
130
// / <param name="x">value to scan</param>
121
131
// / <returns> position of MSB that is set. LSB is defined to be at position 1, so no bits set returns 0</returns>
122
- inline constexpr unsigned int find_msb (int x) {
132
+ inline constexpr unsigned find_msb (int x) {
123
133
// find the first non-zero bit
124
134
uint32_t tmp = uint32_t (x);
125
135
unsigned int base = 0 ;
@@ -139,7 +149,7 @@ inline constexpr unsigned int find_msb(int x) {
139
149
// / </summary>
140
150
// / <param name="x">value to scan</param>
141
151
// / <returns> position of MSB that is set. LSB is defined to be at position 1, so no bits set returns 0</returns>
142
- inline constexpr unsigned int find_msb (long x) {
152
+ inline constexpr unsigned find_msb (long x) {
143
153
// find the first non-zero bit
144
154
uint32_t tmp = uint32_t (x);
145
155
unsigned int base = 0 ;
@@ -160,7 +170,7 @@ inline constexpr unsigned int find_msb(long x) {
160
170
// / </summary>
161
171
// / <param name="x">value to scan</param>
162
172
// / <returns> position of MSB that is set. LSB is defined to be at position 1, so no bits set returns 0</returns>
163
- inline constexpr unsigned int find_msb (long long x) {
173
+ constexpr unsigned find_msb (long long x) {
164
174
// find the first non-zero bit
165
175
uint64_t tmp = uint64_t (x);
166
176
unsigned int base = 0 ;
0 commit comments