Reputation: 360
I have the below code
if ( i < 0 ) {
i = i + 1
}
Using NEON vectorized instructions I need to perform the above. How do I compare a NEON register value with 0 and perform the above calculation?
Upvotes: 3
Views: 964
Reputation: 6354
You don't need many instructions for this. A single vsra instruction will do (vector shift right accumulate) :
vsra.u32 q0, q0, #31 // i += ((unsigned int) i) >> 31;
Note that it's u32
on purpose and not s32
.
NEON is easy to learn, but hard to master as you need to know many bit-hacking related techniques in order to write efficient codes like this which is many times faster than the traditional if-else approach.
Upvotes: 3
Reputation: 212959
You can just do a compare and then subtract the result, since a true
comparison result is equivalent to -1:
const int32x4_t vk0 = { 0 };
uint32x4_t vcmp = vcltq_s32(va, vk0); // a < 0 ?
va = vsubq_s32(va, (int32x4_t)vcmp); // subtract -1 (i.e. add 1) for
// each element where a < 0
If you want to do this at the assembly level then you can just use the following instructions:
Upvotes: 1