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 11#ifndef OSRM_UTIL_MSB_HPP
22#define OSRM_UTIL_MSB_HPP
33
4+ #include < bit>
45#include < boost/assert.hpp>
5-
6- #include < climits>
76#include < cstdint>
8- #include < utility >
7+ #include < limits >
98
109namespace osrm ::util
1110{
1211
13- // get the msb of an integer
14- // return 0 for integers without msb
1512template <typename T> std::size_t msb (T value)
1613{
14+ BOOST_ASSERT (value > 0 );
15+
1716 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 ;
2718
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);
4620}
47- # endif
21+
4822} // namespace osrm::util
4923
5024#endif
You can’t perform that action at this time.
0 commit comments