简体   繁体   中英

How to use SSE logical instructions

I need some help on how to use the SSE logical instructions on Visual Studio. Based on the MSDN website ,

__m128 _mm_and_ps(__m128 a , __m128 b );
ANDPS

can be used to perform logical AND on the operands a and b . But I couldn't figure out the exact steps needed to get the results back (memory load and initialization, set, store, etc).

Specifically, I'd like to know how to:

  1. perform the operation,
  2. get the result back, and
  3. obtain the size of the result (number of significant bits).

Could someone please show me in an example how this can be done?

Thanks!

Here's an example - inverting the sign of all the elements in a float vector using XOR:

__m128 v1 = _mm_set_ps(0.0f, 1.0f, -1.0f, -2.0f);
                                       //   v1 =  0.0f,  1.0f, -1.0f, -2.0f
__m128 sign = _mm_set1_ps(-0.0f);      // sign = -0.0f, -0.0f, -0.0f, -0.0f
__m128 v2 = _mm_xor_ps(v1, sign);      //   v2 = -0.0f, -1.0f,  1.0f,  2.0f

So, supposing you wanted to negate all the values in an array, you could do it 4 elements at a time, like this:

const int N = 1024;
float a[N];
const __m128 sign = _mm_set1_ps(-0.0f);// sign = -0.0f, -0.0f, -0.0f, -0.0f
for (int i = 0; i < N; i += 4)
{
    v = _mm_loadu_ps(&a[i]);           // load 4 elements (unaligned) from a[]
    v = _mm_xor_ps(v1, sign);          // invert sign bit (i.e. negate)
    _mm_storeu_ps(&a[i], v);           // store 4 elements (unaligned) back to a[]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM