Replacing 32-bit loop counter with 64-bit causes strange performance degradation with _mm_popcnt_u64 on Intel CPU
Hello everyone! I'm writing a C++ program to count set bits in a large data array. I'm using the _mm_popcnt_u64 intrinsic from the SSE4.2 instruction set. The code is simple: I iterate through a uint64_t array and accumulate the result.
Everything works fine, but I noticed a strange thing: if I change the loop counter type from 32-bit (unsigned int) to 64-bit (size_t), performance drops by 2-3 times! The data is the same, the array size is the same, the compiler is the same (MSVC 2019, flags /O2 /arch:AVX2).
Here's the simplified code:
#include
#include
#include
#include
int main() {
const size_t N = 1'000'000;
std::vector data(N, 0xFFFFFFFFFFFFFFFF); // all bits = 1
// Version 1: 32-bit counter
unsigned int sum32 = 0;
auto t1 =
No answers yet.
Log in to leave an answer
Blogs