std::simd::select
| Defined in header <simd>
|
||
template< class T, class U >
constexpr auto select( bool c, const T& a, const U& b )
-> std::remove_cvref_t<decltype(c ? a : b)>;
|
(1) | (since C++26) |
template< std::size_t Bytes, class Abi, class T, class U >
constexpr auto select( const basic_mask<Bytes, Abi>& c, const T& a, const U& b )
noexcept -> /* see below */;
|
(2) | (since C++26) |
The conditional operator for std::simd::basic_mask conditions, and for bool conditions in code that is generic over std::simd::basic_vec and scalar types.
c ? a : b, as a value of the type of that expression without references and cv-qualifiers.i is c[i] ? a[i] : b[i] for every i in [0, c.size()), where a scalar argument stands for the same value in every element. The type of the result depends on a and b:
- If
aorbis abasic_vecof typeVwhosemask_typeis the type ofc, and the other argument converts implicitly toV, the result is of typeV. - If
aandbare both of the type ofc, or are both of typebool, the result is of the type ofc. - If
aandbare of the same typeT, which can be the element type of abasic_vec, andsizeof(T) == Bytes, the result is of typesimd::vec<T, c.size()>. - Otherwise, this overload does not participate in overload resolution.
Parameters
| c | - | the condition |
| a | - | the value, or the elements, selected where c is true
|
| b | - | the value, or the elements, selected where c is false
|
Return value
As described above.
Notes
The operators of basic_vec and basic_mask work element by element, but the conditional operator cannot be overloaded, and its condition must convert to bool. simd::select takes its place: simd::select(x < 0, -x, x) computes the absolute value of each element of x.
Unlike the conditional operator, simd::select is a function, so both a and b are evaluated before the call, and every element of each is computed, including the elements that are not selected. An expression that is invalid for some elements has to be made safe for all of them: for integer elements, simd::select(d != 0, n / d, 0) has undefined behavior if an element of d is zero, while n / simd::select(d != 0, d, 1) does not.
A scalar argument of overload (2) is converted to the basic_vec by its broadcast constructor, which accepts an arithmetic value only if its conversion to the element type is value-preserving. For a simd::vec<float> x, simd::select(x > 0, x, 1) does not compile, and simd::select(x > 0, x, 1.f) does.
The overload (1) allows code that is generic over basic_vec and scalar types, such as a function template that returns simd::select(x > 0.f, x, 0.f) and is called with either a float or a simd::vec<float>.
In std::experimental::simd, the elements of an object were replaced through a std::experimental::where expression, as in where(k, v) = x;. The C++26 equivalent is v = simd::select(k, x, v);.
GCC on x86-64 takes each element of the result from a or b according to the mask in a few instructions:
- with SSE2, the bitwise operations
andps,andnps, andorps, orpand,pandn, andporfor integer elements; - with SSE4.1, a blend instruction,
blendvpsorpblendvb; - with AVX2,
vblendvpsorvpblendvb; - with AVX-512, where the mask is a bit mask in a mask register,
vblendmpsorvpblendmd.
If b is zero, a single pand is enough, or a zero-masking move with AVX-512. With AVX-512, a selection between the result of an operation and one of its operands, such as simd::select(k, a + b, a), becomes a single masked instruction, such as vaddps with a mask register.
| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
__cpp_lib_simd |
202411L |
(C++26) | simd::select
|
Example
#include <print>
#include <simd>
namespace simd = std::simd;
using V = simd::vec<int, 8>;
// works with float and with vec<float>
auto relu(auto x)
{
return simd::select(x > 0.f, x, 0.f);
}
int main()
{
V v([](int i) { return i * 5 % 11 - 5; });
std::println("v = {}", v);
std::println("select(v < 0, -v, v) = {}", simd::select(v < 0, -v, v));
std::println("select(v > 0, 1, 0) = {}", simd::select(v > 0, 1, 0));
// the same function for vec<float> and for float
simd::vec<float, 8> f([](int i) { return i - 3.5f; });
std::println("relu({}) = {}", f, relu(f));
std::println("relu(-2.5f) = {}, relu(1.5f) = {}", relu(-2.5f), relu(1.5f));
// both arguments are evaluated: divide only by the nonzero elements
V n([](int i) { return 100 + i; });
auto nonzero = v != 0;
V q = simd::select(nonzero, n / simd::select(nonzero, v, 1), 0);
std::println("n = {}", n);
std::println("n / v, or 0 where v is 0 = {}", q);
}
Output:
v = [-5, 0, 5, -1, 4, -2, 3, -3]
select(v < 0, -v, v) = [5, 0, 5, 1, 4, 2, 3, 3]
select(v > 0, 1, 0) = [0, 0, 1, 0, 1, 0, 1, 0]
relu([-3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5]) = [0, 0, 0, 0, 0.5, 1.5, 2.5, 3.5]
relu(-2.5f) = 0, relu(1.5f) = 1.5
n = [100, 101, 102, 103, 104, 105, 106, 107]
n / v, or 0 where v is 0 = [-20, 0, 20, -103, 26, -52, 35, -35]
See also
(C++26)(C++26)(C++26) |
element-wise min/max operations for basic_vec (function template) |
(C++26) |
element-wise clamp operation for basic_vec (function template) |
(C++26)(C++26)(C++26) |
reductions of basic_mask to bool (function template) |