Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is an __m128i variable zero?

Tags:

c++

c

intel

simd

sse

How do I test if a __m128i variable has any nonzero value on SSE-2-and-earlier processors?

like image 230
user541686 Avatar asked Nov 03 '11 03:11

user541686


2 Answers

In SSE2 you can do:

__m128i zero = _mm_setzero_si128();
if(_mm_movemask_epi8(_mm_cmpeq_epi32(x,zero)) == 0xFFFF)
{
    //the code...
}

this will test four int's vs zero then return a mask for each byte, so your bit-offsets of each corresponding int would be at 0, 4, 8 & 12, but the above test will catch if any bit is set, then if you preserve the mask you can work with the finer grained parts directly if need be.

like image 188
Necrolis Avatar answered Oct 22 '22 14:10

Necrolis


For the sake of completeness, with SSE4 one can use _mm_testz_si128.

const bool isAllZero = _mm_testz_si128(a,a);

Note that this is true when all bits are zero.

like image 22
Antonio Avatar answered Oct 22 '22 15:10

Antonio