gpt4 book ai didi

c - 如何在 gcc 中使用 lzcnt

转载 作者:太空宇宙 更新时间:2023-11-04 06:54:03 25 4
gpt4 key购买 nike

我正在查看一些汇编代码,我看到了 tzcntl。对该指令的搜索重定向到 lzcnt。这些是相同的指令吗?是否可以将 lzcnt 与 gcc 一起使用?

我看过这个例子: Intrinsic __lzcnt64 returns different values with different compile options

尽管我对是否需要使用 __lzcnt64 或者是否有 32 位版本感到困惑。

总结一下:

  1. tzcntl 和 lzcnt 之间有什么区别(如果有的话)?
  2. 如何在 gcc 中正确使用 lzcnt(代码、包含和编译)
  3. 我可以选择 32 位还是 64 位版本?

最佳答案

tzcnt 计算尾随 零,而lzcnt 计算前导 零。

x86 compiler built-ins提供对各种寄存器宽度的 lzcnt 指令的访问:

unsigned short __builtin_ia32_lzcnt_u16(unsigned short);
unsigned int __builtin_ia32_lzcnt_u32(unsigned int);
unsigned long long __builtin_ia32_lzcnt_u64 (unsigned long long);

但这些仅适用于 -mlzcnt,如果 CPU 不支持将 rep bsr 执行为 lzcnt,则会给出错误的结果。

但是您可以使用通用内置函数进行位计数。参见 the GCC documentation :

Built-in Function: int __builtin_clzll (unsigned long long)

Similar to __builtin_clz, except the argument type is unsigned long long.

关于c - 如何在 gcc 中使用 lzcnt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47245581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com