Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 35 additions & 13 deletions immer/detail/hamts/bits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <cstdint>

#if defined(_MSC_VER)
#include <intrin.h> // __popcnt
#include <intrin.h> // __popcnt, __cpuid
#endif

namespace immer {
Expand Down Expand Up @@ -61,7 +61,30 @@ constexpr T max_depth = (sizeof(hash_t) * 8u + B - 1u) / B;
template <typename hash_t, bits_t B, typename T = count_t>
constexpr T max_shift = max_depth<hash_t, B, count_t> * B;

// __popcnt/__popcnt64 compile directly to the x86 POPCNT instruction, which
// only exists from Intel's Nehalem generation (2008) onward (and AMD
// equivalents) and has no compiler-inserted CPUID check, unlike
// __builtin_popcount on GCC/Clang. MSVC only exposes a POPCNT capability
// query (via CPUID) on x86/x64; other MSVC targets (e.g. ARM64) have no
// popcount intrinsic to speak of, so there's nothing to "have" there --
// always use the portable fallback on such targets.
#if defined(_MSC_VER) && !(defined(_M_IX86) || defined(_M_X64))
#define IMMER_HAS_BUILTIN_POPCOUNT 0
#else
#define IMMER_HAS_BUILTIN_POPCOUNT 1
#endif

#if IMMER_HAS_BUILTIN_POPCOUNT && defined(_MSC_VER)
inline bool has_popcnt_support()
{
static const bool result = [] {
int info[4] = {0, 0, 0, 0};
__cpuid(info, 1);
return (info[2] & (1 << 23)) != 0; // ECX bit 23 == POPCNT
}();
return result;
}
#endif

inline auto popcount_fallback(std::uint32_t x)
{
Expand All @@ -84,34 +107,33 @@ inline auto popcount_fallback(std::uint64_t x)

inline count_t popcount(std::uint32_t x)
{
#if IMMER_HAS_BUILTIN_POPCOUNT
#if defined(_MSC_VER)
return __popcnt(x);
#if !IMMER_HAS_BUILTIN_POPCOUNT
return popcount_fallback(x);
#elif defined(_MSC_VER)
return has_popcnt_support() ? __popcnt(x) : popcount_fallback(x);
#else
return __builtin_popcount(x);
#endif
#else
return popcount_fallback(x);
#endif
}

inline count_t popcount(std::uint64_t x)
{
#if IMMER_HAS_BUILTIN_POPCOUNT
#if defined(_MSC_VER)
#if !IMMER_HAS_BUILTIN_POPCOUNT
return popcount_fallback(x);
#elif defined(_MSC_VER)
#if defined(_WIN64)
return static_cast<count_t>(__popcnt64(x));
return has_popcnt_support() ? static_cast<count_t>(__popcnt64(x))
: popcount_fallback(x);
#else
// TODO: benchmark against popcount_fallback(std::uint64_t x)
// Safe on CPUs without POPCNT: popcount(std::uint32_t) already checks
// has_popcnt_support() before using the intrinsic.
return popcount(static_cast<std::uint32_t>(x >> 32)) +
popcount(static_cast<std::uint32_t>(x));
#endif
#else
return __builtin_popcountll(x);
#endif
#else
return popcount_fallback(x);
#endif
}

inline count_t popcount(std::uint16_t x)
Expand Down
83 changes: 83 additions & 0 deletions test/detail/hamts/bits.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//
// immer: immutable data structures for C++
// Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente
//
// This software is distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt
//

#include <catch2/catch_test_macros.hpp>
#include <cstdint>
#include <immer/detail/hamts/bits.hpp>

using namespace immer::detail::hamts;

TEST_CASE("popcount_fallback matches expected bit counts")
{
SECTION("32-bit")
{
CHECK(popcount_fallback(std::uint32_t{0}) == 0u);
CHECK(popcount_fallback(std::uint32_t{0xFFFFFFFFu}) == 32u);
CHECK(popcount_fallback(std::uint32_t{1}) == 1u);
CHECK(popcount_fallback(std::uint32_t{0xAAAAAAAAu}) == 16u);
CHECK(popcount_fallback(std::uint32_t{0x80000000u}) == 1u);
}

SECTION("64-bit")
{
CHECK(popcount_fallback(std::uint64_t{0}) == 0u);
CHECK(popcount_fallback(std::uint64_t{0xFFFFFFFFFFFFFFFFull}) == 64u);
CHECK(popcount_fallback(std::uint64_t{1}) == 1u);
CHECK(popcount_fallback(std::uint64_t{0xAAAAAAAAAAAAAAAAull}) == 32u);
CHECK(popcount_fallback(std::uint64_t{0x8000000000000000ull}) == 1u);
}
}

TEST_CASE("popcount agrees with popcount_fallback")
{
// popcount() may dispatch to a platform intrinsic (or, on MSVC, a
// runtime-checked intrinsic -- see has_popcnt_support() in bits.hpp);
// it must always agree with the portable fallback it is meant to be
// equivalent to.
SECTION("8-bit")
{
for (unsigned i = 0; i <= 0xFF; ++i) {
auto x = static_cast<std::uint8_t>(i);
CHECK(popcount(x) == popcount_fallback(static_cast<std::uint32_t>(x)));
}
}

SECTION("16-bit samples")
{
for (auto x : {std::uint16_t{0},
std::uint16_t{1},
std::uint16_t{0xFFFF},
std::uint16_t{0xAAAA},
std::uint16_t{0x8001}}) {
CHECK(popcount(x) ==
popcount_fallback(static_cast<std::uint32_t>(x)));
}
}

SECTION("32-bit samples")
{
for (auto x : {std::uint32_t{0},
std::uint32_t{1},
std::uint32_t{0xFFFFFFFFu},
std::uint32_t{0xAAAAAAAAu},
std::uint32_t{0x80000001u}}) {
CHECK(popcount(x) == popcount_fallback(x));
}
}

SECTION("64-bit samples")
{
for (auto x : {std::uint64_t{0},
std::uint64_t{1},
std::uint64_t{0xFFFFFFFFFFFFFFFFull},
std::uint64_t{0xAAAAAAAAAAAAAAAAull},
std::uint64_t{0x8000000000000001ull}}) {
CHECK(popcount(x) == popcount_fallback(x));
}
}
}