Use std::popcount instead of __builtin_popcount (#7026)

This commit is contained in:
Siarhei Fedartsou 2024-09-28 20:34:39 +02:00 committed by GitHub
parent f636dbfd44
commit 676f6d4704
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,43 +2,13 @@
#define OSRM_UTIL_BIT_RANGE_HPP
#include "util/msb.hpp"
#include <bit>
#include <boost/iterator/iterator_facade.hpp>
#include <boost/range/iterator_range.hpp>
namespace osrm::util
{
namespace detail
{
template <typename T> std::size_t countOnes(T value)
{
static_assert(std::is_unsigned<T>::value, "Only unsigned types allowed");
std::size_t number_of_ones = 0;
while (value > 0)
{
auto index = msb(value);
value = value & ~(T{1} << index);
number_of_ones++;
}
return number_of_ones;
}
#if (defined(__clang__) || defined(__GNUC__) || defined(__GNUG__))
inline std::size_t countOnes(std::uint8_t value)
{
return __builtin_popcount(std::uint32_t{value});
}
inline std::size_t countOnes(std::uint16_t value)
{
return __builtin_popcount(std::uint32_t{value});
}
inline std::size_t countOnes(unsigned int value) { return __builtin_popcount(value); }
inline std::size_t countOnes(unsigned long value) { return __builtin_popcountl(value); }
inline std::size_t countOnes(unsigned long long value) { return __builtin_popcountll(value); }
#endif
} // namespace detail
// Investigate if we can replace this with
// http://www.boost.org/doc/libs/1_64_0/libs/dynamic_bitset/dynamic_bitset.html
template <typename DataT>
@ -70,7 +40,7 @@ class BitIterator : public boost::iterator_facade<BitIterator<DataT>,
difference_type distance_to(const BitIterator &other) const
{
return detail::countOnes(m_value) - detail::countOnes(other.m_value);
return std::popcount(m_value) - std::popcount(other.m_value);
}
bool equal(const BitIterator &other) const { return m_value == other.m_value; }