Skip to content

Commit

Permalink
Branchfree bitmask calculation
Browse files Browse the repository at this point in the history
kimwalisch committed Jul 28, 2024
1 parent e7fba93 commit aacc5a0
Showing 2 changed files with 16 additions and 14 deletions.
5 changes: 3 additions & 2 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
Changes in primecount-7.14, 2024-07-01
Changes in primecount-7.14, 2024-07-28

* Move x86 cpuid code from cpuid.hpp to src/x86/cpuid.cpp.
* Move generate.hpp code to src/generate.cpp.
* Move generate.hpp code to src/generate_primes.cpp.
* Move generate_phi.hpp code to src/phi_vector.cpp.
* int128_t.hpp: Rename namespace port to pstd (portable std namespace).
* Sieve.hpp: Tune AVX512 code.
* Sieve.hpp: Branchfree bitmask calculation.
* cpu_supports_popcnt.hpp: Simplify, move preprocessor checks to new multiarch_x86_popcnt.cmake.
* multiarch_avx512_vpopcnt.cmake: Tune AVX512 code.
* multiarch_x86_popcnt.cmake: Detect x86 POPCNT.
25 changes: 13 additions & 12 deletions include/Sieve.hpp
Original file line number Diff line number Diff line change
@@ -161,20 +161,21 @@ class Sieve
uint64_t m2 = unset_larger[stop % 240];
const uint64_t* sieve64 = (const uint64_t*) sieve_.data();

if (start_idx == stop_idx)
return popcnt64(sieve64[start_idx] & m1 & m2);
else
{
uint64_t start_bits = sieve64[start_idx] & m1;
uint64_t stop_bits = sieve64[stop_idx] & m2;
uint64_t cnt = popcnt64(start_bits);
cnt += popcnt64(stop_bits);
// Branchfree bitmask calculation:
// m1 = (start_idx != stop_idx) ? m1 : m1 & m2;
m1 = (m1 * (start_idx != stop_idx)) | ((m1 & m2) * (start_idx == stop_idx));
// m2 = (start_idx != stop_idx) ? m2 : 0;
m2 *= (start_idx != stop_idx);

for (uint64_t i = start_idx + 1; i < stop_idx; i++)
cnt += popcnt64(sieve64[i]);
uint64_t start_bits = sieve64[start_idx] & m1;
uint64_t stop_bits = sieve64[stop_idx] & m2;
uint64_t cnt = popcnt64(start_bits);
cnt += popcnt64(stop_bits);

return cnt;
}
for (uint64_t i = start_idx + 1; i < stop_idx; i++)
cnt += popcnt64(sieve64[i]);

return cnt;
}

#endif

0 comments on commit aacc5a0

Please sign in to comment.