SSE intrinsics: masking a float and using bitwise and?

Basically the problem is with the x86 assembler, where you have a number that you want to set either to zero or to the number itself using and. If you and, this is a negative number, you will return the number itself, but if you are andwith zero, you will get zero.

Now the problem that I encountered in SSE is that the floats do not match the binaries (or maybe I'm wrong). Anyways is the code here, I tried to use all kinds of floats to mask the second and third numbers (127.0f and 99.0f respectively), but no luck.

#include <xmmintrin.h>
#include <stdio.h>

void print_4_bit_num(const char * label, __m128 var)
{
    float *val = (float *) &var;
    printf("%s: %f %f %f %f\n",
       label, val[3], val[2], val[1], val[0]);
}
int main()
{
    __m128 v1 = _mm_set_ps(1.0f, 127.0f,  99.0f, 1.0f);
    __m128 v2 = _mm_set_ps(1.0f, 65535.0f, 127.0f, 0.0f);
    __m128 v = _mm_and_ps(v1, v2);

    print_4_bit_num("v1", v1);
    print_4_bit_num("v2", v2);
    print_4_bit_num("v ", v);

    return 0;
}
+4
source share
3 answers

() , AND, , , , - :

__m128 v1 = _mm_set_ps(1.0f, 127.0f,  99.0f, 1.0f);
__m128 v2 = _mm_castsi128_ps(_mm_set_epi32(0, -1, 0, -1));
__m128 v = _mm_and_ps(v1, v2); // => v = { 0.0f, 127.0f, 0.0f, 1.0f }
+4

SSE SSE (128 256 ), , ; . , 4 2 , , float - , , .

+2

If you have SSE4.1 (I assume you are doing this), you should consider _mm_blendv_ps(a,b,mask). This uses only the sign bit of its argument maskand essentially implements the vector mask<0?b:a.

0
source

All Articles