How to cancel (change the sign) of floating point elements in a variable of type __m128?

Is there any single instruction or function that can invert the sign of each float inside __m128? those. a = r0:r1:r2:r3 ===> a = -r0:-r1:-r2:-r3 ?

I know this can be done with _mm_sub_ps(_mm_set1_ps(0.0),a) , but isn't it that slow since _mm_set1_ps(0.0) is a function with multiple instructions?

+6
source share
1 answer

In practice, your compiler should do a good job of creating a constant vector for 0.0. It probably just uses _mm_xor_ps , and if your code is in a loop, it should still push the constant generation out of the loop. So, on the bottom line, use the original idea:

 v = _mm_sub_ps(_mm_set1_ps(0.0), v); 

or another common trick that:

 v = _mm_xor_ps(v, _mm_set1_ps(-0.0)); 

which simply flips the sign bits instead of performing the subtraction (not as safe as the first method, since it does not do the right thing with NaNs, but may be more efficient in some cases).

+14
source

Source: https://habr.com/ru/post/958427/


All Articles