You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用SIMD高效定位大字节数组中1比特的索引?

基于SIMD(128/256/512)高效查找超大字节数组中所有1比特的索引

如果有超大字节数组,需要从最左侧比特开始计数,找出所有1比特的索引,如何借助SIMD(128/256/512)实现高效处理?(注:若仅需查找首个1比特,可参考早期相关问题,本需求需输出所有索引而非单个索引。)

非SIMD的512位基础实现

已基于C++20实现非SIMD版本作为基础处理模块,代码如下:

#include <cstdint>
#include <iostream>
#include <bit>

int Find1s512(uint64_t const * p, uint16_t * idxs) {
    int rpos = 0;
    for (int i = 0; i < 8; ++i) {
        uint64_t n = p[i];
        while (true) {
            int const j = std::countr_zero(n);
            if (j >= 64)
                break;
            idxs[rpos++] = i * 64 + j;
            n &= n - 1;
        }
    }
    return rpos;
}

int main() {
    uint64_t a[8] = {(1ULL << 17) | (1ULL << 63),
        1ULL << 19, 1ULL << 23};
    uint16_t b[512] = {};
    int const cnt = Find1s512(a, b);
    for (int i = 0; i < cnt; ++i)
        std::cout << int(b[i]) << " ";
    // 输出结果: 17 63 83 151
}

需求

现寻求该需求的最优实现方案,尤其针对SIMD 128/256/512的应用。

内容的提问来源于stack exchange,提问作者Arty

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 21:45:37