Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get index of first element that is not zero in a __m256 variable

Tags:

c++

c

avx

simd

sse

__m256  dst = _mm256_cmp_ps(value1, value2, _CMP_LE_OQ);

If dst is [0,0,0,-nan, 0,0,0,-nan]; I want to be able to know the first -nan index, in this case 3 without doing a for loop with 8 iterations. Is this possible?

like image 497
hidayat Avatar asked Mar 31 '19 09:03

hidayat


People also ask

What is __ m128?

The __m128 data type is used to represent the contents of a Intel® SSE register used by Intel® SSE intrinsics. The __m128 data type can hold four 32-bit floating-point values. The __m128d data type can hold two 64-bit floating-point values.

What is __ m256i?

__m256 Data Types The __m256 data type can hold eight 32-bit floating-point values, while the __m256d data type can hold four 64-bit double precision floating-point values, and the __m256i data type can hold thirty-two 8-bit, sixteen 16-bit, eight 32-bit, or four 64-bit integer values.


1 Answers

I would movmskps the result of the comparison and then do a bitscan forward.

Using intrinsics (this works with gcc/clang, see here for alternatives):

int pos = __builtin_ctz(_mm256_movemask_ps(dst));

Note that the result of bsf is unspecified if no bit is set. To work around this you can, e.g., write this to get 8, if no other bit is set:

int pos = __builtin_ctz(_mm256_movemask_ps(dst) | 0x100);
like image 135
chtz Avatar answered Sep 20 '22 08:09

chtz