Alex Rivera | Logout

Check XMM register for all zeroes

Asked 2012-04-16T14:08:15.900
10

Is there a way to check if all bits/bytes/words etc. in a __m128i variable are 0?
In my app I have to check if all integers packed in a in a __m128i variable are zeroes. Will I have to extract them and compare each separately?


Edit:

What I am doing now is:

int next = 0;
do{
    //some code

    next = idata.m128i_i32[0] + idata.m128i_i32[1] + idata.m128i_i32[2] + idata.m128i_i32[3];
}while(next > 0);

What I need is to check if idata is all zeroes without having to access each individual element, and quit the loop if they are...


Based on Harold's comment this is the solution:

__m128i idata = _mm_setr_epi32(i,j,k,l);
do{
    //some code
}while( !_mm_testz_si128(idata, idata) );

This will exit the loop if all low bits of each DW in idata are 0... thanks harold!

Edit
Report

1 Answer

9

_mm_testz_si128 is SSE4.1 which isn't supported on some CPUs (e.g. Intel Atom, AMD Phenom)

Here is an SSE2-compatible variant

inline bool isAllZeros(__m128i xmm) {
    return _mm_movemask_epi8(_mm_cmpeq_epi8(xmm, _mm_setzero_si128())) == 0xFFFF;
}
answered 2012-04-20T16:49:48.773

Your Answer