2017-03-05 15:31:45 -05:00
|
|
|
#ifndef OSRM_UTIL_MSB_HPP
|
|
|
|
#define OSRM_UTIL_MSB_HPP
|
|
|
|
|
2024-09-28 14:34:23 -04:00
|
|
|
#include <bit>
|
2017-03-05 15:31:45 -05:00
|
|
|
#include <boost/assert.hpp>
|
|
|
|
#include <cstdint>
|
2024-09-28 14:34:23 -04:00
|
|
|
#include <limits>
|
2017-03-05 15:31:45 -05:00
|
|
|
|
2022-12-11 04:10:26 -05:00
|
|
|
namespace osrm::util
|
2017-03-05 15:31:45 -05:00
|
|
|
{
|
|
|
|
|
|
|
|
template <typename T> std::size_t msb(T value)
|
|
|
|
{
|
2024-09-28 14:34:23 -04:00
|
|
|
BOOST_ASSERT(value > 0);
|
|
|
|
|
2017-03-07 07:34:42 -05:00
|
|
|
static_assert(std::is_integral<T>::value && !std::is_signed<T>::value, "Integer required.");
|
2024-09-28 14:34:23 -04:00
|
|
|
constexpr auto MSB_INDEX = std::numeric_limits<unsigned char>::digits * sizeof(T) - 1;
|
2017-03-05 15:31:45 -05:00
|
|
|
|
2024-09-28 14:34:23 -04:00
|
|
|
return MSB_INDEX - std::countl_zero(value);
|
2017-03-05 15:31:45 -05:00
|
|
|
}
|
2024-09-28 14:34:23 -04:00
|
|
|
|
2022-12-20 12:00:11 -05:00
|
|
|
} // namespace osrm::util
|
2017-03-05 15:31:45 -05:00
|
|
|
|
|
|
|
#endif
|