Testing equality between two __m128i variables

前端 未结 3 1234
悲&欢浪女
悲&欢浪女 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:28

    You can use a compare and then extract a mask from the comparison result:

    __m128i vcmp = _mm_cmpeq_epi8(v0, v1);       // PCMPEQB
    uint16_t vmask = _mm_movemask_epi8(vcmp);    // PMOVMSKB
    if (vmask == 0xffff)
    {
        // v0 == v1
    }
    

    This works with SSE2 and later.

    As noted by @Zboson, if you have SSE 4.1 then you can do it like this, which may be slightly more efficient, as it's two SSE instructions and then a test on a flag (ZF):

    __m128i vcmp = _mm_xor_si128(v0, v1);        // PXOR
    if (_mm_testz_si128(vcmp, vcmp))             // PTEST (requires SSE 4.1)
    {
        // v0 == v1
    }
    

    FWIW I just benchmarked both of these implementations on a Haswell Core i7 using clang to compile the test harness and the timing results were very similar - the SSE4 implementation appears to be very slightly faster but it's hard to measure the difference.

提交回复
热议问题