Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare operation using NEON Instructions

Tags:

simd

arm

neon

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?

like image 897
mario Avatar asked Jan 29 '26 18:01

mario


2 Answers

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.

like image 194
Jake 'Alquimista' LEE Avatar answered Jan 31 '26 18:01

Jake 'Alquimista' LEE


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:

  • vcltq_s32 ==> VCLT.S32
  • vsubq_s32 ==> VSUB.I32
like image 34
Paul R Avatar answered Jan 31 '26 18:01

Paul R



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!