File tree Expand file tree Collapse file tree 1 file changed +7
-33
lines changed Expand file tree Collapse file tree 1 file changed +7
-33
lines changed Original file line number Diff line number Diff line change 1
1
#ifndef OSRM_UTIL_MSB_HPP
2
2
#define OSRM_UTIL_MSB_HPP
3
3
4
+ #include < bit>
4
5
#include < boost/assert.hpp>
5
-
6
- #include < climits>
7
6
#include < cstdint>
8
- #include < utility >
7
+ #include < limits >
9
8
10
9
namespace osrm ::util
11
10
{
12
11
13
- // get the msb of an integer
14
- // return 0 for integers without msb
15
12
template <typename T> std::size_t msb (T value)
16
13
{
14
+ BOOST_ASSERT (value > 0 );
15
+
17
16
static_assert (std::is_integral<T>::value && !std::is_signed<T>::value, " Integer required." );
18
- std::size_t msb = 0 ;
19
- while (value > 0 )
20
- {
21
- value >>= 1u ;
22
- msb++;
23
- }
24
- BOOST_ASSERT (msb > 0 );
25
- return msb - 1 ;
26
- }
17
+ constexpr auto MSB_INDEX = std::numeric_limits<unsigned char >::digits * sizeof (T) - 1 ;
27
18
28
- #if (defined(__clang__) || defined(__GNUC__) || defined(__GNUG__))
29
- inline std::size_t msb (unsigned long long v)
30
- {
31
- BOOST_ASSERT (v > 0 );
32
- constexpr auto MSB_INDEX = CHAR_BIT * sizeof (unsigned long long ) - 1 ;
33
- return MSB_INDEX - __builtin_clzll (v);
34
- }
35
- inline std::size_t msb (unsigned long v)
36
- {
37
- BOOST_ASSERT (v > 0 );
38
- constexpr auto MSB_INDEX = CHAR_BIT * sizeof (unsigned long ) - 1 ;
39
- return MSB_INDEX - __builtin_clzl (v);
40
- }
41
- inline std::size_t msb (unsigned int v)
42
- {
43
- BOOST_ASSERT (v > 0 );
44
- constexpr auto MSB_INDEX = CHAR_BIT * sizeof (unsigned int ) - 1 ;
45
- return MSB_INDEX - __builtin_clz (v);
19
+ return MSB_INDEX - std::countl_zero (value);
46
20
}
47
- # endif
21
+
48
22
} // namespace osrm::util
49
23
50
24
#endif
You can’t perform that action at this time.
0 commit comments