Skip to content
This repository has been archived by the owner on Dec 26, 2023. It is now read-only.

Commit

Permalink
Use tzcnt instead of bsf for better performance on ZEN/ZEN2.
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfpld committed Jun 18, 2020
1 parent b217307 commit 7eff58f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
22 changes: 19 additions & 3 deletions src/include/robin_hood.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,23 @@ static Counts& counts() {
#endif

// count leading/trailing bits
#ifdef _MSC_VER
#if ( ( defined __i386 || defined __x86_64__ ) && defined __BMI__ ) || defined _M_IX86 || defined _M_X64
# ifdef _MSC_VER
# include <intrin.h>
# else
# include <x86intrin.h>
# endif
# if ROBIN_HOOD(BITNESS) == 32
# define ROBIN_HOOD_PRIVATE_DEFINITION_CTZ() _tzcnt_u32
# else
# define ROBIN_HOOD_PRIVATE_DEFINITION_CTZ() _tzcnt_u64
# endif
# if defined __AVX2__ || defined __BMI__
# define ROBIN_HOOD_COUNT_TRAILING_ZEROES(x) ROBIN_HOOD(CTZ)(x)
# else
# define ROBIN_HOOD_COUNT_TRAILING_ZEROES(x) ((x) ? ROBIN_HOOD(CTZ)(x) : ROBIN_HOOD(BITNESS))
# endif
#elif defined _MSC_VER
# if ROBIN_HOOD(BITNESS) == 32
# define ROBIN_HOOD_PRIVATE_DEFINITION_BITSCANFORWARD() _BitScanForward
# else
Expand Down Expand Up @@ -1249,9 +1265,9 @@ class Table
do {
auto const n = detail::unaligned_load<size_t>(mInfo);
#if ROBIN_HOOD(LITTLE_ENDIAN)
inc = ROBIN_HOOD_COUNT_TRAILING_ZEROES(n) / 8;
inc = static_cast<int>(ROBIN_HOOD_COUNT_TRAILING_ZEROES(n)) / 8;
#else
inc = ROBIN_HOOD_COUNT_LEADING_ZEROES(n) / 8;
inc = static_cast<int>(ROBIN_HOOD_COUNT_LEADING_ZEROES(n)) / 8;
#endif
mInfo += inc;
mKeyVals += inc;
Expand Down
2 changes: 1 addition & 1 deletion src/test/unit/unit_clz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace {

int clz(void const* data) {
auto const n = robin_hood::detail::unaligned_load<size_t>(data);
return ROBIN_HOOD_COUNT_TRAILING_ZEROES(n) / 8;
return static_cast<int>(ROBIN_HOOD_COUNT_TRAILING_ZEROES(n)) / 8;
}

} // namespace
Expand Down

0 comments on commit 7eff58f

Please sign in to comment.