mario
mario

Reputation: 360

Compare operation using NEON Instructions

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

Answers (2)

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

Paul R
Paul R

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

Related Questions