不使用BMI2的便携式有效替代PDEP?

mar*_*964 5 algorithm x86 assembly bit-manipulation bmi

英特尔位操作指令集2(BMI2)中的并行存款指令(PDEP)的文档描述了该指令的以下串行实现(类似C的伪代码):

U64 _pdep_u64(U64 val, U64 mask) {
  U64 res = 0;
  for (U64 bb = 1; mask; bb += bb) {
    if (val & bb)
      res |= mask & -mask;
    mask &= mask - 1;
  }
  return res;
}
Run Code Online (Sandbox Code Playgroud)

另请参阅英特尔的pdepinsn参考手册.

该算法是O(n),其中n是设置位的数量mask,这显然具有O(k)的最坏情况,其中k是总的位数mask.

更有效的最坏情况算法是否可行?

是否有可能制作一个更快的版本,假设val最多有一个位设置,即等于0或等于0到63之间的1<<r某个值r

nju*_*ffa 6

问题的第二部分是关于 1 位存款的特殊情况,需要两个步骤。第一步,我们需要确定r中单个 1 位的位索引,在为零的val情况下使用合适的响应。val这可以通过 POSIX 函数轻松完成ffs,或者如果r通过其他方式知道,如提问者在评论中提到的那样。在第二步中,我们需要识别中第 1 位i的位索引(如果存在)。然后我们可以存入at bit的第 -th 位。rmaskrvali

r查找第 1 位的索引的一种方法是使用基于二进制分区的经典总体计数mask算法来计算 1 位,并记录所有中间的按组位计数。然后,我们对记录的位计数数据执行二分搜索,以识别所需位的位置。

以下C代码使用 64 位数据演示了这一点。这实际上是否比迭代方法更快将在很大程度上取决于mask和的典型值val

#include <stdint.h>

/* Find the index of the n-th 1-bit in mask, n >= 0
   The index of the least significant bit is 0 
   Return -1 if there is no such bit
*/
int find_nth_set_bit (uint64_t mask, int n)
{
    int t, i = n, r = 0;
    const uint64_t m1 = 0x5555555555555555ULL; // even bits
    const uint64_t m2 = 0x3333333333333333ULL; // even 2-bit groups
    const uint64_t m4 = 0x0f0f0f0f0f0f0f0fULL; // even nibbles
    const uint64_t m8 = 0x00ff00ff00ff00ffULL; // even bytes
    uint64_t c1 = mask;
    uint64_t c2 = c1 - ((c1 >> 1) & m1);
    uint64_t c4 = ((c2 >> 2) & m2) + (c2 & m2);
    uint64_t c8 = ((c4 >> 4) + c4) & m4;
    uint64_t c16 = ((c8 >> 8) + c8) & m8;
    uint64_t c32 = (c16 >> 16) + c16;
    int c64 = (int)(((c32 >> 32) + c32) & 0x7f);
    t = (c32    ) & 0x3f; if (i >= t) { r += 32; i -= t; }
    t = (c16>> r) & 0x1f; if (i >= t) { r += 16; i -= t; }
    t = (c8 >> r) & 0x0f; if (i >= t) { r +=  8; i -= t; }
    t = (c4 >> r) & 0x07; if (i >= t) { r +=  4; i -= t; }
    t = (c2 >> r) & 0x03; if (i >= t) { r +=  2; i -= t; }
    t = (c1 >> r) & 0x01; if (i >= t) { r +=  1;         }
    if (n >= c64) r = -1;
    return r; 
}

/* val is either zero or has a single 1-bit.
   Return -1 if val is zero, otherwise the index of the 1-bit
   The index of the least significant bit is 0
*/
int find_bit_index (uint64_t val)
{
    return ffsll (val) - 1;
}

uint64_t deposit_single_bit (uint64_t val, uint64_t mask)
{
    uint64_t res = (uint64_t)0;
    int r = find_bit_index (val);
    if (r >= 0) {
        int i = find_nth_set_bit (mask, r);
        if (i >= 0) res = (uint64_t)1 << i;
    } 
    return res;
}
Run Code Online (Sandbox Code Playgroud)


Pse*_*nym 3

作为补充说明,由于这个问题再次出现在我面前,我发现Sebastiano Vigna查找第 n 个集合位的方法在实践中更快。它不包含分支或条件移动。

请注意,leq_bytesgt_zero_bytes可能可以使用 SSE 指令来实现,但此版本的优点是完全可移植。

const uint64_t sMSBs8 = 0x8080808080808080ull;
const uint64_t sLSBs8 = 0x0101010101010101ull;
    
inline uint64_t
leq_bytes(uint64_t pX, uint64_t pY)
{
    return ((((pY | sMSBs8) - (pX & ~sMSBs8)) ^ pX ^ pY) & sMSBs8) >> 7;
}
    
    
inline uint64_t
gt_zero_bytes(uint64_t pX)
{
    return ((pX | ((pX | sMSBs8) - sLSBs8)) & sMSBs8) >> 7;
}
    
    
inline uint64_t find_nth_set_bit(uint64_t pWord, uint64_t pR)
{
    const uint64_t sOnesStep4  = 0x1111111111111111ull;
    const uint64_t sIncrStep8  = 0x8040201008040201ull;

    uint64_t byte_sums = pWord - ((pWord & 0xA*sOnesStep4) >> 1);
    byte_sums = (byte_sums & 3*sOnesStep4) + ((byte_sums >> 2) & 3*sOnesStep4);
    byte_sums = (byte_sums + (byte_sums >> 4)) & 0xF*sLSBs8;
    byte_sums *= sLSBs8;
    
    const uint64_t k_step_8 = pR * sLSBs8;
    const uint64_t place
        = (leq_bytes( byte_sums, k_step_8 ) * sLSBs8 >> 53) & ~0x7;
    const int byte_rank = pR - (((byte_sums << 8) >> place) & 0xFF);
    const uint64_t spread_bits = (pWord >> place & 0xFF) * sLSBs8 & sIncrStep8;
    const uint64_t bit_sums = gt_zero_bytes(spread_bits) * sLSBs8;
    const uint64_t byte_rank_step_8 = byte_rank * sLSBs8;
    return place + (leq_bytes( bit_sums, byte_rank_step_8 ) * sLSBs8 >> 56);
}
Run Code Online (Sandbox Code Playgroud)