How to use __builtin_clzll in Rust? (count leading zeros)

GCC has the __builtin_clzll builtin function that according to 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’s 

I think LLVM also has these functions, and Rust is compiled with LLVM. Is there a way to use this function in Rust?

There's leading_zeros method on integers. It most likely compiles to the same thing. Use rust.godbolt.org to verify (remember to add -O to options on godbolt)

3 Likes

Indeed it does.

1 Like

Moreover, it is implemented as direct call to the intrinsic.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.