Replacing 32-bit loop counter with 64-bit introduces crazy performance deviations with _mm_popcnt_u64 on Intel CPU
Hello everyone! I'm writing a high-performance function for counting set bits (popcount) in a large array of 64-bit integers. I'm using the `_mm_popcnt_u64` intrinsic from SSE4.2. The code is simple — a loop summing the popcount for each element.
I discovered strange behavior: if I declare the loop variable as `uint32_t`, the function runs very fast. But if I change it to `uint64_t` (just by changing the type of `i`), performance drops by almost half! I tested this on several Intel Core i7 processors (Sandy Bridge, Haswell, Skylake) — the effect reproduces. The compiler is MSVC 2010, though GCC 4.6.3 shows a similar pattern. Optimizations are enabled (/O2 or -O2).
Here's a minimal code example:
```cpp
#include
#include
#include
#include
// Fast version with