std::simd::min, std::simd::max, std::simd::minmax
| 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) |
i is std::min(a[i], b[i]) for every i in [0, a.size()).basic_vec whose element i is std::max(a[i], b[i]) for every i in [0, a.size()).std::pair{simd::min(a, b), simd::max(a, b)}.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,
pminubandpmaxubforstd::uint8_tandpminswandpmaxswforshort, whilefloatandintelements are compared and blended with bitwise operations; - with SSE4.1,
minpsandmaxpsforfloat, andpminsdandpmaxsdforint; - with AVX2, the same instructions with the
vprefix, such asvminpsandvpminsd; - with AVX-512,
vminpsandvmaxpsforfloat, while integer elements are compared into a mask register and blended (vpcmpdandvpblendmdforint).
| 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
(C++26) |
element-wise clamp operation for basic_vec (function template) |
(C++26)(C++26)(C++26) |
reduces all values in basic_vec over a specified binary operation to a single value (function template) |
(C++26) |
element-wise selection using conditional operator (function template) |
| returns the smaller of the given values (function template & algorithm function object) | |
(C++20) |
|
| returns the greater of the given values (function template & algorithm function object) | |
(C++20) |
|
(C++11) |
returns the smaller and larger of two elements (function template & algorithm function object) |
(C++20) |