2

我正在开发一种__builtin_ffsll()uint64_t类型一起使用的算法。

我想使用 boost 多精度库切换到 512 位字段(我在支持 avx512 的机器上运行)。

是否有与提到的内置功能类似的功能?或者,我怎样才能有效地为 512 位整数实现这样的功能?

4

1 回答 1

3

文档中

unsigned lsb(const number-or-expression-template-type& x);

返回设置为 1 的最低有效位的(从零开始的)索引。

std::range_error如果参数是 ,则抛出 a <= 0

ffs()是从 1 开始的,因此将 1 加到lsb()的返回值将使其等价。编辑:正如所指出的,考虑到被传递 0 的情况。

也许像

unsigned ffs512(const boost::multiprecision::uint512_t &n) {
  if (n.is_zero()) {
    return 0;
  } else {
    return boost::multiprecision::lsb(n) + 1;
  }
}
于 2019-10-02T22:09:11.223 回答