std::experimental::popcount
From cppreference.com
< cpp | experimental | simd
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Experimental
Filesystem library (filesystem TS)
Library fundamentals (library fundamentals TS)
Library fundamentals 2 (library fundamentals TS v2)
Library fundamentals 3 (library fundamentals TS v3)
Extensions for parallelism (parallelism TS)
Extensions for parallelism 2 (parallelism TS v2)
Extensions for concurrency (concurrency TS)
Extensions for concurrency 2 (concurrency TS v2)
Concepts (concepts TS)
Ranges (ranges TS)
Reflection (reflection TS)
Mathematical special functions (special functions TR)
Extensions for parallelism v2
Parallel exceptions
Additional execution policies
Algorithms
Task blocks
Defined in header
<experimental/simd>
template< class T, class Abi >
int popcount( const simd_mask<T, Abi>& k ) noexcept;
(parallelism TS v2)
int popcount( const simd_mask<T, Abi>& k ) noexcept;
Returns the number of true values in the mask k.
[edit] Parameters
k
-
the
simd_mask
to apply the reduction to
[edit] Return value
An int in the range [
0,
simd_size_v<T, Abi>)
.
[edit] Example
Run this code
#include <cstddef> #include <experimental/simd> #include <iostream> namespace stdx = std::experimental; template<typename Abi> int count_zeros(stdx::simd<int, Abi> v) { return stdx::popcount(v == 0); // v == 0 returns a simd_mask } void println(auto rem, auto const v) { std::cout << rem << ": "; for (std::size_t i = 0; i != v.size(); ++i) std::cout << v[i] << ' '; } int main() { stdx::simd<int> x{8}; println("x", x); std::cout << " zeros: " << count_zeros(x) << '\n'; x[3] = x[1] = false; println("x", x); std::cout << " zeros: " << count_zeros(x) << '\n'; }
Possible output:
x: 8 8 8 8 zeros: 0 x: 8 0 8 0 zeros: 2