SSE/AVX寄存器的非零字节索引 [英] The indices of non-zero bytes of an SSE/AVX register

查看:100
本文介绍了SSE/AVX寄存器的非零字节索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果SSE/AVX寄存器的值使得其所有字节均为0或1,是否有任何方法可以有效地获取所有非零元素的索引?

If an SSE/AVX register's value is such that all its bytes are either 0 or 1, is there any way to efficiently get the indices of all non zero elements?

例如,如果xmm值为 | r0 = 0 | r1 = 1 | r2 = 0 | r3 = 1 | r4 = 0 | r5 = 1 | r6 = 0 | ... | r14 = 0 | r15 = 1 | 结果应该类似于(1、3、5,...,15).结果应放在另一个_m128i变量或char [16]数组中.

For example, if xmm value is | r0=0 | r1=1 | r2=0 | r3=1 | r4=0 | r5=1 | r6=0 |...| r14=0 | r15=1 | the result should be something like (1, 3, 5, ... , 15). The result should be placed in another _m128i variable or char[16] array.

如果有帮助,我们可以假设寄存器的值是所有字节均为0或某个恒定的非零值(不必要为1).

If it helps, we can assume that register's value is such that all bytes are either 0 or some constant nonzero value (not necessary 1).

我非常想知道是否有针对该指令的指令,或者最好是C/C ++内在指令.在任何SSE或AVX指令集中.

I am pretty much wondering if there is an instruction for that or preferably C/C++ intrinsic. In any SSE or AVX set of instructions.

它是正确的 @ zx485 观察到原始问题还不够清楚.我正在寻找任何连续"解决方案.

It was correctly observed by @zx485 that original question was not clear enough. I was looking for any "consecutive" solution.

上面的示例0 1 0 1 0 1 0 1...应导致以下任一情况:

The example 0 1 0 1 0 1 0 1... above should result in either of the following:

  • 如果我们假设索引从1开始,那么0将是一个终止字节,结果可能是
  • If we assume that indices start from 1, then 0 would be a termination byte and the result might be

002 004 006 008 010 012 014 016 00000000000000000000000000000

002 004 006 008 010 012 014 016 000 000 000 000 000 000 000 000

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆