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.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of std

std

Photo of vectorization

vectorization

Photo of count_if

count_if

Related news:

News photo

US spent millions on STD prevention in Gaza. But it was a province in Mozambique

News photo

C++ String Conversion: Exploring std:from_chars in C++17 to C++26

News photo

Giving C++ std:regex a C makeover