Namespaces
Variants

std::simd::reduce, std::simd::reduce_min, std::simd::reduce_max

From cppreference.com
 
 
 
Data-parallel types (SIMD)
 
Defined in header <simd>
template< class T, class Abi, class BinaryOperation = std::plus<> >
constexpr T reduce( const basic_vec<T, Abi>& x, BinaryOperation binary_op = {} );
(1) (since C++26)
template< class T, class Abi, class BinaryOperation = std::plus<> >
constexpr T reduce( const basic_vec<T, Abi>& x,
                    const typename basic_vec<T, Abi>::mask_type& mask,
                    BinaryOperation binary_op = {},
                    std::type_identity_t<T> identity_element = /* see below */ );
(2) (since C++26)
template< class T, class BinaryOperation = std::plus<> >
constexpr T reduce( const T& x, BinaryOperation binary_op = {} );
(3) (since C++26)
template< class T, class BinaryOperation = std::plus<> >
constexpr T reduce( const T& x, std::same_as<bool> auto mask,
                    BinaryOperation binary_op = {},
                    std::type_identity_t<T> identity_element = /* see below */ );
(4) (since C++26)
template< class T, class Abi >
constexpr T reduce_min( const basic_vec<T, Abi>& x ) noexcept;
template< class T, class Abi >
constexpr T reduce_max( const basic_vec<T, Abi>& x ) noexcept;
(5) (since C++26)
template< class T, class Abi >
constexpr T reduce_min( const basic_vec<T, Abi>& x,
                        const typename basic_vec<T, Abi>::mask_type& mask ) noexcept;
template< class T, class Abi >
constexpr T reduce_max( const basic_vec<T, Abi>& x,
                        const typename basic_vec<T, Abi>::mask_type& mask ) noexcept;
(6) (since C++26)
template< class T >
constexpr T reduce_min( const T& x ) noexcept;
template< class T >
constexpr T reduce_max( const T& x ) noexcept;
(7) (since C++26)
template< class T >
constexpr T reduce_min( const T& x, std::same_as<bool> auto mask ) noexcept;
template< class T >
constexpr T reduce_max( const T& x, std::same_as<bool> auto mask ) noexcept;
(8) (since C++26)

Reduces the elements of a std::simd::basic_vec to a single value.

1) Combines all elements of x with binary_op and returns the result. The order in which the elements are combined, and how they are grouped, is unspecified. Each element is passed to binary_op as a vec<T, 1>, and the result of binary_op is a basic_vec as well.
2) Same as (1), but only for the elements of x that are selected by mask. If no element is selected, returns identity_element.
3,4) Overloads for scalars: returns x, or identity_element if mask is false. These overloads participate in overload resolution only if T is a vectorizable type.
5) Returns the smallest or the largest element of x.
6) Returns the smallest or the largest element of x among those selected by mask. If no element is selected, returns std::numeric_limits<T>::max() for reduce_min and std::numeric_limits<T>::lowest() for reduce_max.
7,8) Overloads for scalars: returns x, or, if mask is false, std::numeric_limits<T>::max() for reduce_min and std::numeric_limits<T>::lowest() for reduce_max. These overloads participate in overload resolution only if T is a vectorizable type.

(1-4) participate in overload resolution only if binary_op can be called with two arguments of type vec<T, 1> and returns a vec<T, 1>. binary_op must also accept two basic_vec objects of an unspecified other width and return the same type, and it must be an element-wise operation that is commutative, otherwise the program is ill-formed, no diagnostic required. If it is not associative, the result is unspecified.

If BinaryOperation is one of std::plus<>, std::multiplies<>, std::bit_and<>, std::bit_or<>, and std::bit_xor<>, identity_element defaults to T(), T(1), T(~T()), T(), and T(), respectively. For other operations, (2) and (4) participate in overload resolution only if identity_element is provided. If binary_op applied to identity_element and any finite value y, in either order, does not return y, the behavior is undefined.

(5-8) participate in overload resolution only if T models totally_ordered, which excludes std::complex.

Parameters

x - the basic_vec or scalar to reduce
mask - the elements to reduce
binary_op - the operation that combines two elements
identity_element - the value that binary_op leaves unchanged, and the result if no element is selected

Return value

As described above.

Exceptions

1,2) Any exception thrown by binary_op.
3,4) Throws nothing.

Notes

Since the order of operations is unspecified, a floating-point sum can be rounded differently than a sum of the elements from first to last. Unlike std::reduce, these functions take no initial value: to add one, add it to the result.

binary_op receives basic_vec objects, not elements, so a lambda expression that takes elements, such as [](int a, int b) { return a + b; }, does not work, while a generic lambda such as [](auto a, auto b) { return a + b; } does. For the same reason, std::plus<> works, while std::plus<int> does not.

The scalar overloads (3,4,7,8) allow code that is generic over basic_vec and scalar types, such as a function template that returns simd::reduce(a * b) and is called with either.

GCC on x86-64 implements a reduction of a full register as a tree of log2(size()) vector operations on halves: with AVX2, the sum of a vec<float, 8> compiles to vextractf128, vaddps, two shuffles (vpermilps and vshufps) each followed by an addition, and reduce_max of a vec<int, 8> to the same steps with vpmaxsd. A masked reduction first replaces the unselected elements with the identity element, with vpand with AVX2 and a masked move with AVX-512.

Feature-test macro Value Std Feature
__cpp_lib_simd 202411L (C++26) simd::reduce, simd::reduce_min, and simd::reduce_max for basic_vec
202603L (C++26) Overloads for scalars (3,4,7,8)

Example

#include <functional>
#include <limits>
#include <print>
#include <simd>

int main()
{
    const std::simd::vec<int, 8> v([](int i) { return i * i - 10; });
    std::println("v = {}", v);
    std::println("sum: {}, product: {}, min: {}, max: {}", std::simd::reduce(v),
                 std::simd::reduce(v, std::multiplies<>{}), std::simd::reduce_min(v),
                 std::simd::reduce_max(v));

    // masked: only the selected elements take part
    const std::simd::mask<int, 8> pos = v > 0;
    std::println("positive: sum {}, product {}, min {}", std::simd::reduce(v, pos),
                 std::simd::reduce(v, pos, std::multiplies<>{}), std::simd::reduce_min(v, pos));

    // no element selected: the identity element, or the limits of int
    const std::simd::mask<int, 8> none = v > 100;
    std::println("none selected: sum {}, product {}, min {}", std::simd::reduce(v, none),
                 std::simd::reduce(v, none, std::multiplies<>{}), std::simd::reduce_min(v, none));

    // a custom operation takes basic_vec arguments, so a generic lambda works,
    // but [](int a, int b) { ... } does not; with a mask, it needs an identity element
    auto max = [](auto a, auto b) { return std::simd::max(a, b); };
    std::println("max: {}, max of the negative elements: {}", std::simd::reduce(v, max),
                 std::simd::reduce(v, v < 0, max, std::numeric_limits<int>::lowest()));

    // the order of the operations is unspecified, which changes rounding
    const std::simd::vec<float, 8> f([](int i) { return i == 0 ? 1e8f : i == 4 ? -1e8f : 1.f; });
    float sequential = 0;
    for (float x : f)
        sequential += x;
    std::println("f = {}: reduce {}, sequential sum {}", f, std::simd::reduce(f), sequential);
}

Output:

v = [-10, -9, -6, -1, 6, 15, 26, 39]
sum: 60, product: 49280400, min: -10, max: 39
positive: sum 86, product 91260, min 6
none selected: sum 0, product 1, min 2147483647
max: 39, max of the negative elements: -1
f = [1e+08, 1, 1, 1, -1e+08, 1, 1, 1]: reduce 6, sequential sum 3

See also

reduction of basic_mask to number of true values
(function template) [edit]
reductions of basic_mask to the index of first or last true value
(function template) [edit]
reductions of basic_mask to bool
(function template) [edit]
(C++26)(C++26)(C++26)
element-wise min/max operations for basic_vec
(function template) [edit]
(C++17)
similar to std::accumulate, except out of order
(function template) [edit]