Is there an efficient way to get the first nonzero element in the SIMD register using the built-in SIMD functions?

As the name implies, if a 256-bit SIMD register:

0 | 0 | 1 | 0 | 0 | 1 | 0 | 0 |

How can I effectively get the index of the first non-zero element (i.e. the index of the 2first 1)? The easiest way is to store in memory and check one by one, but it can cost a lot. Are there any nice ideas for this?

+4
source share
1 answer
  • PCMPEQB / W / D / Q with respect to a register with zero zero to obtain a vector with elements that are all-1 for zero elements and all-zero for zero elements.
  • PMOVMSKB, all-ones all-zero
  • , ( C ~, asm NOT), 1s , .
  • TZCNT BSF, . BSF, .

, PCMPEQ , .

, , 8 . TZCNT 64- .


oops, intrinsics. ASM C- , Finder Intel intrinsics by asm . (. ).

+7

Source: https://habr.com/ru/post/1657689/


All Articles