Skip to content

Commit 44eee26

Browse files
committed
adding a generic find_msb() implementation for types that support bit patterns
1 parent f54385d commit 44eee26

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

include/universal/utility/find_msb.hpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,22 @@ namespace sw { namespace universal {
1717

1818
//static const unsigned int bval[] = { 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4 };
1919

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+
2030
/// <summary>
2131
/// find most significant bit that is set
2232
/// </summary>
2333
/// <param name="x">value to scan</param>
2434
/// <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) {
2636
// find the first non-zero bit
2737
unsigned int base = 0;
2838
if (x & 0xFFFF0000) { base += 16; x >>= 16; }
@@ -41,7 +51,7 @@ inline constexpr unsigned int find_msb(unsigned int x) {
4151
/// </summary>
4252
/// <param name="x">value to scan</param>
4353
/// <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) {
4555
// find the first non-zero bit
4656
unsigned int base = 0;
4757
if (x & 0xFFFF0000) { base += 16; x >>= 16; }
@@ -60,7 +70,7 @@ inline constexpr unsigned int find_msb(unsigned long x) {
6070
/// </summary>
6171
/// <param name="x">value to scan</param>
6272
/// <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) {
6474
// find the first non-zero bit
6575
unsigned int base = 0;
6676
if (x & 0xFFFFFFFF00000000) { base += 32; x >>= 32; }
@@ -82,7 +92,7 @@ inline constexpr unsigned int find_msb(unsigned long long x) {
8292
/// </summary>
8393
/// <param name="x">value to scan</param>
8494
/// <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) {
8696
// find the first non-zero bit
8797
uint8_t tmp = uint8_t(x);
8898
unsigned int base = 0;
@@ -100,7 +110,7 @@ inline constexpr unsigned int find_msb(signed char x) {
100110
/// </summary>
101111
/// <param name="x">value to scan</param>
102112
/// <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) {
104114
// find the first non-zero bit
105115
uint16_t tmp = uint16_t(x);
106116
unsigned int base = 0;
@@ -119,7 +129,7 @@ inline constexpr unsigned int find_msb(short x) {
119129
/// </summary>
120130
/// <param name="x">value to scan</param>
121131
/// <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) {
123133
// find the first non-zero bit
124134
uint32_t tmp = uint32_t(x);
125135
unsigned int base = 0;
@@ -139,7 +149,7 @@ inline constexpr unsigned int find_msb(int x) {
139149
/// </summary>
140150
/// <param name="x">value to scan</param>
141151
/// <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) {
143153
// find the first non-zero bit
144154
uint32_t tmp = uint32_t(x);
145155
unsigned int base = 0;
@@ -160,7 +170,7 @@ inline constexpr unsigned int find_msb(long x) {
160170
/// </summary>
161171
/// <param name="x">value to scan</param>
162172
/// <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) {
164174
// find the first non-zero bit
165175
uint64_t tmp = uint64_t(x);
166176
unsigned int base = 0;

0 commit comments

Comments
 (0)