Testing equality between two __m128i variables

前端 未结 3 1209
悲&欢浪女
悲&欢浪女 2020-12-09 20:01

If I want to do a bitwise equality test between two __m128i variables, am I required to use an SSE instruction or can I use ==? If not, which SSE i

3条回答
  •  离开以前
    2020-12-09 20:12

    Although using _mm_movemask_epi8 is one solution, if you have a processor with SSE4.1 I think a better solution is to use an instruction which sets the zero or carry flag in the FLAGS register. This saves a test or cmp instruction.

    To do this you could do this:

    if(_mm_test_all_ones(_mm_cmpeq_epi8(v1,v2))) {
        //v0 == v1
    }
    

    Edit: as Paul R pointed out _mm_test_all_ones generates two instructions: pcmpeqd and ptest. With _mm_cmpeq_epi8 that's three instructions total. Here's a better solution which only uses two instructions in total:

    __m128i neq = _mm_xor_si128(v1,v2);
    if(_mm_test_all_zeros(neq,neq)) {
        //v0 == v1
    }
    

    This generates

    pxor    %xmm1, %xmm0
    ptest   %xmm0, %xmm0
    

提交回复
热议问题