【问题标题】:Fastest way to strip trailing zeroes from an unsigned int从无符号整数中去除尾随零的最快方法
【发布时间】:2022-01-27 23:06:01
【问题描述】:

假设我们试图从某个无符号变量中删除尾随零。

uint64_t a = ...
uint64_t last_bit = a & -a; // Two's complement trick: last_bit holds the trailing bit of a
a /= last_bit; // Removing all trailing zeroes from a.

我注意到手动计算位数和移位速度更快。 (开启优化的 MSVC 编译器)

uint64_t a = ...
uint64_t last_bit = a & -a;
size_t last_bit_index = _BitScanForward64( last_bit );
a >>= last_bit_index

假设编译器内在 _BitScanForward64 比任何替代方案都快,是否有任何进一步的快速技巧可以使这更快?

【问题讨论】:

  • 你不能直接在a上使用_BitScanForward64吗?
  • 可能是this?

标签: c++ performance optimization bit-manipulation


【解决方案1】:

在 x86 上,_tzcnt_u64_BitScanForward64 的更快替代,如果它可用(它适用于 BMI 指令集)。

此外,您可以直接在输入上使用它,您不需要隔离最低位集,正如@AlanBirtles 在评论中指出的那样。

除此之外,可以对单个变量进行注释。对于它们的数组,可能有一个 SIMD 解决方案。

【讨论】:

    【解决方案2】:

    您可以使用std::countr_zero (c++20) 并依赖编译器对其进行优化。

    a >>= std::countr_zero(a);
    

    (奖励:您不需要指定宽度,它适用于任何无符号整数类型)

    【讨论】:

      猜你喜欢
      • 2020-02-06
      • 2019-03-25
      • 2021-10-15
      • 1970-01-01
      • 2014-09-02
      • 1970-01-01
      • 2011-11-01
      • 2022-01-19
      • 2015-08-13
      相关资源
      最近更新 更多