assembly - 使用 AVX-512 或 AVX-2 对大数据进行 1 位计数(总体计数)

标签 assembly avx2 avx512 bitcount population-count

我有一大块内存,比如说 256 KiB 或更长。我想计算整个 block 中 1 位的数量,或者换句话说:将所有字节的“总体计数”值相加。

我知道 AVX-512 有一个 VPOPCNTDQ instruction它计算 512 位向量中每个连续 64 位中 1 位的数量,并且 IIANM 应该可以在每个周期发出其中一个(如果有适当的 SIMD 向量寄存器可用) - 但我没有任何编写 SIMD 代码的经验(我更喜欢 GPU)。另外,我不能 100% 确定编译器对 AVX-512 目标的支持。

在大多数 CPU 上,仍然不(完全)支持 AVX-512;但 AVX-2 已广泛使用。我还没有找到类似于 VPOPCNTDQ 的小于 512 位向量化指令,所以即使理论上我也不确定如何使用支持 AVX-2 的 CPU 快速计算位;也许这样的事情存在,但我只是以某种方式错过了它?

无论如何,我希望为两个指令集提供一个简短的 C/C++ 函数 - 要么使用一些 intritics-wrapper 库,要么使用内联汇编。签名是

uint64_t count_bits(void* ptr, size_t size);

注释:

最佳答案

AVX-2

@HadiBreis 的评论链接到 article关于使用 SSSE3 进行快速人口统计,作者:Wojciech Muła;文章链接至this GitHub repository ;和存储库has以下 AVX-2 实现。它基于矢量化查找指令,并使用 16 值查找表来计算半字节的位数。

#   include <immintrin.h>
#   include <x86intrin.h>

std::uint64_t popcnt_AVX2_lookup(const uint8_t* data, const size_t n) {

    size_t i = 0;

    const __m256i lookup = _mm256_setr_epi8(
        /* 0 */ 0, /* 1 */ 1, /* 2 */ 1, /* 3 */ 2,
        /* 4 */ 1, /* 5 */ 2, /* 6 */ 2, /* 7 */ 3,
        /* 8 */ 1, /* 9 */ 2, /* a */ 2, /* b */ 3,
        /* c */ 2, /* d */ 3, /* e */ 3, /* f */ 4,

        /* 0 */ 0, /* 1 */ 1, /* 2 */ 1, /* 3 */ 2,
        /* 4 */ 1, /* 5 */ 2, /* 6 */ 2, /* 7 */ 3,
        /* 8 */ 1, /* 9 */ 2, /* a */ 2, /* b */ 3,
        /* c */ 2, /* d */ 3, /* e */ 3, /* f */ 4
    );

    const __m256i low_mask = _mm256_set1_epi8(0x0f);

    __m256i acc = _mm256_setzero_si256();

#define ITER { \
        const __m256i vec = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(data + i)); \
        const __m256i lo  = _mm256_and_si256(vec, low_mask); \
        const __m256i hi  = _mm256_and_si256(_mm256_srli_epi16(vec, 4), low_mask); \
        const __m256i popcnt1 = _mm256_shuffle_epi8(lookup, lo); \
        const __m256i popcnt2 = _mm256_shuffle_epi8(lookup, hi); \
        local = _mm256_add_epi8(local, popcnt1); \
        local = _mm256_add_epi8(local, popcnt2); \
        i += 32; \
    }

    while (i + 8*32 <= n) {
        __m256i local = _mm256_setzero_si256();
        ITER ITER ITER ITER
        ITER ITER ITER ITER
        acc = _mm256_add_epi64(acc, _mm256_sad_epu8(local, _mm256_setzero_si256()));
    }

    __m256i local = _mm256_setzero_si256();

    while (i + 32 <= n) {
        ITER;
    }

    acc = _mm256_add_epi64(acc, _mm256_sad_epu8(local, _mm256_setzero_si256()));

#undef ITER

    uint64_t result = 0;

    result += static_cast<uint64_t>(_mm256_extract_epi64(acc, 0));
    result += static_cast<uint64_t>(_mm256_extract_epi64(acc, 1));
    result += static_cast<uint64_t>(_mm256_extract_epi64(acc, 2));
    result += static_cast<uint64_t>(_mm256_extract_epi64(acc, 3));

    for (/**/; i < n; i++) {
        result += lookup8bit[data[i]];
    }

    return result;
}

AVX-512

同一存储库还有基于 VPOPCNT 的 AVX-512 实现。在列出它的代码之前,这里是简化且更易读的伪代码:

  • For every consecutive sequence of 64 bytes:

    • Load the sequence into a SIMD register with 64x8 = 512 bits
    • Perform 8 parallel population counts of 64 bits each on that register
    • Add the 8 population-count results in parallel, into an "accumulator" register holding 8 sums
  • Sum up the 8 values in the accumulator

  • If there's a tail of less than 64 bytes, count the bits there in some simpler way

  • Return the main sum plus the tail sum

现在是真正的交易:

#   include <immintrin.h>
#   include <x86intrin.h>

uint64_t avx512_vpopcnt(const uint8_t* data, const size_t size) {
    
    const size_t chunks = size / 64;

    uint8_t* ptr = const_cast<uint8_t*>(data);
    const uint8_t* end = ptr + size;

    // count using AVX512 registers
    __m512i accumulator = _mm512_setzero_si512();
    for (size_t i=0; i < chunks; i++, ptr += 64) {
        
        // Note: a short chain of dependencies, likely unrolling will be needed.
        const __m512i v = _mm512_loadu_si512((const __m512i*)ptr);
        const __m512i p = _mm512_popcnt_epi64(v);

        accumulator = _mm512_add_epi64(accumulator, p);
    }

    // horizontal sum of a register
    uint64_t tmp[8] __attribute__((aligned(64)));
    _mm512_store_si512((__m512i*)tmp, accumulator);

    uint64_t total = 0;
    for (size_t i=0; i < 8; i++) {
        total += tmp[i];
    }

    // popcount the tail
    while (ptr + 8 < end) {
        total += _mm_popcnt_u64(*reinterpret_cast<const uint64_t*>(ptr));
        ptr += 8;
    }

    while (ptr < end) {
        total += lookup8bit[*ptr++];
    }

    return total;
}

lookup8bit是字节而不是位的 popcnt 查找表,定义为 here编辑:正如评论者所指出的,最后使用 8 位查找表并不是一个好主意,可以改进。

关于assembly - 使用 AVX-512 或 AVX-2 对大数据进行 1 位计数(总体计数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50081465/

相关文章:

sse - SIMD:位包有符号整数

scala - 使用 Intel AVX 和 Spark

c - 使用 AVX2 查找元素索引 - 代码优化

c++11 - 将最佳 uint8_t 位图转换为 8 x 32 位 SIMD "bool"向量

c - 如何在 MIPS 汇编中初始化一个巨大的数组?

c++ - 如果用两个变量或一个变量和一个数字文字进行加法,为什么从 C++ 调用的汇编函数会在标志中给出不同的结果?

c++ - SIMD __m256i 到 __m256d 转换结果

c - 为什么 _mm512_store_pd 在这个矩阵乘法代码中 super 慢?

c - 如何解释 C 中的缓冲区溢出漏洞

performance - 如果寄存器如此之快,为什么我们没有更多的寄存器?