Background: GCC C's builtin vector extensions allow for a fairly natural representation of SIMD vectors as C "types." According to the documentation, many built-in operations are supported (+, -, etc). However, the ternary operator, as well as logical operators (&&, ||) for some reason only work in C++. This is an issue for an all=C codebase.
The question: In GCC C, how would one implement SIMD-compatible [branchless] conditionals of the form:
v4si a = {2,-1,3,4}, b, indicesLessThan0;
indicesLessThan0 = a < 0;
b = indicesLessThan0 ? a : 0;
And, more generally, how to perform an arbitrary independent block of statements based on that same result:
v4si c = {9,8,7,6}, d;
for (int i = 0; i < 4; i++) {
if (indicesLessThan0[i]) { // consider tests one by one
b[i] = a[i] // as the ternary operator does above
d[i] = c[i] + 1; // some other independent operation
}
else {
b[i] = 0; // as the ternary operator does above
d[i] = c[i] - 1; // another independent operation
}
}
If doing a block of statements is harder (SIMD branching is bad), it would be fine to perform the ternary test again for any additional statements at the cost (supposedly) of some efficiency:
d = indicesLessThan0 ? c + 1 : c - 1; // the other operation in the loop
But the ternary operator doesn't work in C for some reason the manual doesn't explain. Is there another easy way? Some way of using if statements?