GCC 有__builtin_clzll
内置函数,根据https://www.geeksforgeeks.org/builtin-functions-gcc-compiler/:
__builtin_clz(x): This function is used to count the leading zeros of the integer. Note : clz = count leading zero\xe2\x80\x99s \n
Run Code Online (Sandbox Code Playgroud)\n我想LLVM也有这些功能,Rust就是用LLVM编译的。有没有办法在 Rust 中使用这个函数?
\nRustleading_zeros
对所有内置整数类型都有该方法。如果在支持高效硬件前导零指令的 CPU 上编译,它将使用:
pub fn foo(n: u32) -> u32 {
n.leading_zeros()
}
Run Code Online (Sandbox Code Playgroud)
rustc -O -C target-cpu=broadwell
:
example::foo:
lzcnt eax, edi
ret
Run Code Online (Sandbox Code Playgroud)
rustc -O --target arm-unknown-linux-gnueabihf -C target-cpu=cortex-a76
:
example::foo:
clz r0, r0
bx lr
Run Code Online (Sandbox Code Playgroud)