Get the latest tech news
Improving on std:count_if()'s auto-vectorization
The problem Let’s consider the problem with the following description: - We have an array of arbitrary length filled with uint8_tvalues; - We want to count the number of even values in the array; - Before doing the calculation we know that the number of even values in the array is between 0 and 2551. A typical solution To start off, we can leverage the STL for this calculation.
We can see that the assembly generated by Clang is vectorized, and iterates the array DWORD-by- DWORD(i.e. four 8-bit values at a time). However, considering the constraint that was specified in the problem description (the total number of even values in the array is between 0 and 255), we know that we do NOT need 64-bit precision when doing the increments. If you’re using a GCC version between 9.1 and 14.2 (most recent release as of March 8, 2025), there’s an interesting regression that has only been fixed after 5 years.
Or read this on Hacker News