Flipping sign on packed SSE floats

跟風遠走 提交于 2019-11-30 14:44:56

问题


I'm looking for the most efficient method of flipping the sign on all four floats packed in an SSE register.

I have not found an intrinsic for doing this in the Intel Architecture software dev manual. Below are the things I've already tried.

For each case I looped over the code 10 billion times and got the wall-time indicated. I'm trying to at least match 4 seconds it takes my non-SIMD approach, which is using just the unary minus operator.


[48 sec]
_mm_sub_ps( _mm_setzero_ps(), vec );


[32 sec]
_mm_mul_ps( _mm_set1_ps( -1.0f ), vec );


[9 sec]

union NegativeMask {
    int   intRep;
    float fltRep;
} negMask;
negMask.intRep = 0x80000000;

_mm_xor_ps( _mm_set1_ps( negMask.fltRep ), vec );


The compiler is gcc 4.2 with -O3. The CPU is an Intel Core 2 Duo.


回答1:


Just to complete your own answer by the gcc documentation about these builtin vectors:

The types defined in this manner can be used with a subset of normal C
operations.  Currently, GCC will allow using the following operators on
these types: `+, -, *, /, unary minus, ^, |, &, ~'.

It is probably a good idea to always stick to these when possible. With very high chances gcc will always provide the most efficient code for this SSE stuff.

For your compiler options, add something more specific to your architecture, something like -march=native will do in most cases.




回答2:


That union is not really needed, best of all worlds (readability, speed and portability):

_mm_xor_ps(vec, _mm_set1_ps(-0.f))



回答3:


A life lesson about coding till 3am in the morning.....

I never tried just using the unary minus on my packed vector. That actually compiles and has the exact same performance as the non-SIMD approach.



来源:https://stackoverflow.com/questions/3361132/flipping-sign-on-packed-sse-floats

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!