SSE comparison and extraction

What is the best way to pairwise compare two integer registers and extract equal elements using SSE instructions? For example, if a = [6 4 7 2]and b = [2 4 9 2](each register contains four 32-bit integers), the result should be [4 2 x x]. An alternative form of this question is how to get a binary mask of equal elements ( ..0101b), which can be used for shuffling or as an index for finding a parameter for a shuffle command in a pre-computed table.

+3
source share
2 answers

I would probably use a variant of what drhirschoffers:

int index = _mm_movemask_ps((__m128)_mm_cmp_epi32(a, b));

, , .

+2

. pcmpeqd:

__m128i zero = _mm_set1_epi32(0);
__m128i a = _mm_set_epi32(6, 4, 7, 2);
__m128i b = _mm_set_epi32(2, 4, 9, 2);

__m128i mask = _mm_cmp_epi32(a, b);     // mask is now 0, -1, 0, -1
mask = _mm_sub_epi32(zero, mask);       // mask is now 0,  1, 0,  1

: - , .

static const __m128i zero = _mm_set1_epi32(0);
static const __m128i bits = _mm_set_epi32(1,2,4,8);

__m128i a = _mm_set_epi32(6, 4, 7, 2);
__m128i b = _mm_set_epi32(2, 4, 9, 2);

__m128i bitvector = _mm_and_si128(bits, _mm_cmp_epi32(a, b));
bitvector = _mm_hadd_epi32(bitvector, bitvector);
bitvector = _mm_hadd_epi32(bitvector, bitvector);
// now a index from 0...15 is the the low 32 bit of bitvector

, , , De Bruijn. OTOH, 4 ints , 4 int phaddd.

+3

All Articles