Namespaces
Variants

std::simd::min, std::simd::max, std::simd::minmax

From cppreference.com
 
 
 
Data-parallel types (SIMD)
 
Defined in header <simd>
template< class T, class Abi >
constexpr basic_vec<T, Abi> min( const basic_vec<T, Abi>& a,
                                 const basic_vec<T, Abi>& b ) noexcept;
(1) (since C++26)
template< class T, class Abi >
constexpr basic_vec<T, Abi> max( const basic_vec<T, Abi>& a,
                                 const basic_vec<T, Abi>& b ) noexcept;
(2) (since C++26)
template< class T, class Abi >
constexpr std::pair<basic_vec<T, Abi>, basic_vec<T, Abi>>
    minmax( const basic_vec<T, Abi>& a, const basic_vec<T, Abi>& b ) noexcept;
(3) (since C++26)
namespace std {
    using simd::min;
    using simd::max;
    using simd::minmax;
}
(4) (since C++26)
1) Returns a std::simd::basic_vec whose element i is std::min(a[i], b[i]) for every i in [0, a.size()).
2) Returns a basic_vec whose element i is std::max(a[i], b[i]) for every i in [0, a.size()).
3) Returns std::pair{simd::min(a, b), simd::max(a, b)}.
4) Makes (1-3) available as std::min, std::max, and std::minmax too, in the same overload sets as std::min, std::max, and std::minmax for scalars.

The overloads (1,2) participate in overload resolution only if T models totally_ordered, which excludes the std::complex element types. The overload (3) is not constrained, but a call to it with such a T does not compile.

Parameters

a, b - the basic_vec objects to compare element by element

Return value

As described above.

Notes

These functions compare two objects element by element. To find the smallest or the largest element of one basic_vec, use simd::reduce_min or simd::reduce_max. Because basic_vec is a range, std::ranges::min(v) also returns the smallest element of v, unlike std::min(a, b).

Both arguments have the same basic_vec type, because T and Abi are deduced from each of them, so simd::min(v, 0) does not compile: use the broadcast constructor, as in simd::min(v, V(0)).

Because of (4), std::min(a, b) and std::max(a, b) work both for scalars and for basic_vec, and code that is generic over both can call them that way. Unlike std::min and std::max for scalars, these functions return a value, not a reference to an argument: a function template that returns std::min(a, b) as const T& returns a dangling reference for a basic_vec. The overloads of std::min and std::max for std::initializer_list, as in std::min({a, b, c}), do not work for basic_vec: write simd::min(a, simd::min(b, c)) instead.

As with std::min and std::max, floating-point elements must not be NaN: floating-point types model totally_ordered only for values other than NaN. GCC computes simd::min(a, b) as simd::select(a < b, a, b), so for a NaN element, and for elements that compare equal, it returns the element of b, while std::min returns a. For example, simd::min(a, b) with -0.f in a and 0.f in b gives 0.f, while std::min(-0.f, 0.f) gives -0.f.

GCC on x86-64 uses one instruction per register for min and max, and one of each for minmax:

  • with SSE2, pminub and pmaxub for std::uint8_t and pminsw and pmaxsw for short, while float and int elements are compared and blended with bitwise operations;
  • with SSE4.1, minps and maxps for float, and pminsd and pmaxsd for int;
  • with AVX2, the same instructions with the v prefix, such as vminps and vpminsd;
  • with AVX-512, vminps and vmaxps for float, while integer elements are compared into a mask register and blended (vpcmpd and vpblendmd for int).
Feature-test macro Value Std Feature
__cpp_lib_simd 202411L (C++26) simd::min, simd::max, and simd::minmax

Example

#include <array>
#include <limits>
#include <print>
#include <simd>

namespace simd = std::simd;
using V = simd::vec<int, 8>;

int main()
{
    V a([](int i) { return i * 3 % 8; });
    V b([](int i) { return 7 - i; });
    std::println("a         = {}", a);
    std::println("b         = {}", b);
    std::println("min(a, b) = {}", simd::min(a, b));
    std::println("max(a, b) = {}", simd::max(a, b));
    auto [lo, hi] = simd::minmax(a, b);
    std::println("hi - lo   = {}", hi - lo);

    // the largest value in an array: an element-wise maximum per chunk,
    // and a single reduction at the end
    std::array<int, 64> data;
    for (int i = 0; i < 64; ++i)
        data[i] = i * 37 % 101;
    V m(std::numeric_limits<int>::min());
    for (std::size_t i = 0; i < data.size(); i += 8)
        m = simd::max(m, simd::unchecked_load<V>(data.begin() + i, 8));
    std::println("maxima per element = {}", m);
    std::println("largest value      = {}", simd::reduce_max(m));
}

Output:

a         = [0, 3, 6, 1, 4, 7, 2, 5]
b         = [7, 6, 5, 4, 3, 2, 1, 0]
min(a, b) = [0, 3, 5, 1, 3, 2, 1, 0]
max(a, b) = [7, 6, 6, 4, 4, 7, 2, 5]
hi - lo   = [7, 3, 1, 3, 1, 5, 1, 5]
maxima per element = [94, 96, 74, 97, 99, 84, 100, 57]
largest value      = 100

See also

element-wise clamp operation for basic_vec
(function template) [edit]
reduces all values in basic_vec over a specified binary operation to a single value
(function template) [edit]
element-wise selection using conditional operator
(function template) [edit]
returns the smaller of the given values
(function template & algorithm function object)[edit]
returns the greater of the given values
(function template & algorithm function object)[edit]
(C++11)
returns the smaller and larger of two elements
(function template & algorithm function object)[edit]