Conditional (ternary) operator
An interesting addition to Blaze is a function, Select
, to represent the conditional (ternary)?:
operator, which unfortunately could not be overloaded in C++, but naturally translates to SIMD intrinsics using a mask (with the difference that short circuiting is not possible). (Note that short circuiting is also not possible for overloads of operator &&
and ||
, but that is never really a use case for an algebra library).
Select(V(bool), V(X), V(X));
Pseudocode:
result[i] = (V1[i] & mask[i])|(V2[i] & ~mask[i]);
Related intrinsics:
SSE: _mm_and_ps
, _mm_andnot_ps, _mm_or_ps
ARM NEON: vbslq_f32
Comments (5)
-
-
-
assigned issue to
-
assigned issue to
-
- changed status to open
-
- changed status to resolved
Summary
The feature has been implemented, tested, optimized, and documented as required. It is immediately available via cloning the Blaze repository and will be officially released in Blaze 3.7.
The select() Operation
The
select()
function performs a componentwise, conditional selection of elements. Given the three dense vectorscond
,a
, andb
, in case an element in thecond
vector evaluates totrue
, the according element ofa
is selected, in case thecond
element evaluates tofalse
, the according element ofb
is selected. The following example demonstrates the use of theselect()
function:blaze::DynamicVector<bool> cond{ true, false, true false }; blaze::DynamicVector<int> a{ 1, -1, 1, -1 }; blaze::DynamicVector<int> b{ -2, 2, -2, 2 }; blaze::DynamicVector<int> c; // ... Resizing and initialization c = select( cond, a, b ); // Results in ( 1, 2, 1, 2 )
Alternatively it is possible to use three dense matrices:
blaze::DynamicMatrix<bool> cond{ { true, false }, { true false } }; blaze::DynamicMatrix<int> A{ { 1, -1 }, { 1, -1 } }; blaze::DynamicMatrix<int> B{ { -2, 2 }, { -2, 2 } }; blaze::DynamicMatrix<int> C; // ... Resizing and initialization C = select( cond, A, B ); // Results in ( 1, 2 ) ( 1, 2 )
-
reporter Great! This is a super useful feature!
- Log in to comment
Hi Matthias!
Thanks creating this proposal. This indeed sounds like a reasonable and useful addition to Blaze. We will consider it for the next releases of Blaze. Thanks again,
Best regards,
Klaus!