Skip to content

Commit 676f6d4

Browse files
Use std::popcount instead of __builtin_popcount (#7026)
1 parent f636dbf commit 676f6d4

File tree

1 file changed

+2
-32
lines changed

1 file changed

+2
-32
lines changed

include/util/bit_range.hpp

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,13 @@
22
#define OSRM_UTIL_BIT_RANGE_HPP
33

44
#include "util/msb.hpp"
5-
5+
#include <bit>
66
#include <boost/iterator/iterator_facade.hpp>
77
#include <boost/range/iterator_range.hpp>
88

99
namespace osrm::util
1010
{
1111

12-
namespace detail
13-
{
14-
template <typename T> std::size_t countOnes(T value)
15-
{
16-
static_assert(std::is_unsigned<T>::value, "Only unsigned types allowed");
17-
std::size_t number_of_ones = 0;
18-
while (value > 0)
19-
{
20-
auto index = msb(value);
21-
value = value & ~(T{1} << index);
22-
number_of_ones++;
23-
}
24-
return number_of_ones;
25-
}
26-
27-
#if (defined(__clang__) || defined(__GNUC__) || defined(__GNUG__))
28-
inline std::size_t countOnes(std::uint8_t value)
29-
{
30-
return __builtin_popcount(std::uint32_t{value});
31-
}
32-
inline std::size_t countOnes(std::uint16_t value)
33-
{
34-
return __builtin_popcount(std::uint32_t{value});
35-
}
36-
inline std::size_t countOnes(unsigned int value) { return __builtin_popcount(value); }
37-
inline std::size_t countOnes(unsigned long value) { return __builtin_popcountl(value); }
38-
inline std::size_t countOnes(unsigned long long value) { return __builtin_popcountll(value); }
39-
#endif
40-
} // namespace detail
41-
4212
// Investigate if we can replace this with
4313
// http://www.boost.org/doc/libs/1_64_0/libs/dynamic_bitset/dynamic_bitset.html
4414
template <typename DataT>
@@ -70,7 +40,7 @@ class BitIterator : public boost::iterator_facade<BitIterator<DataT>,
7040

7141
difference_type distance_to(const BitIterator &other) const
7242
{
73-
return detail::countOnes(m_value) - detail::countOnes(other.m_value);
43+
return std::popcount(m_value) - std::popcount(other.m_value);
7444
}
7545

7646
bool equal(const BitIterator &other) const { return m_value == other.m_value; }

0 commit comments

Comments
 (0)