Remix.run Logo
mananaysiempre 4 days ago

The POSIX name for the function is clz() [the C23 name is stdc_leading_zeros(), because that's how the committee names things now, while the GCC intrinsic is __builtin_clz()]. The name of the x86 instruction, on the other hand, is BSR (80386+) or LZCNT (Nehalem+, K10+) depending on what semantics you want for zero inputs (keep in mind that early implementations of BSF/BSR are very slow and take time proportional to the output value). The compiled code uses BSR. (All of these are specified slightly differently, take care if you plan to actually use them.)

unwind 4 days ago | parent [-]

Got it, thanks. I suck at x86, even more than I thought. :/

Edit: it's CLZ on Arm [1], probably what I was looking for.

[1]: https://developer.arm.com/documentation/100069/0610/A32-and-...

mananaysiempre 3 days ago | parent [-]

In that case, I suck at POSIX—I could’ve sworn clz() was a standard or at least a conventional function, but no, that’s fls(), which is furthermore not universal across Unices. Either way, if you don’t feel your knowledge of the x86 instruction set is adequate, there’s always an option of taking an instruction listing and looking up anything that seems unfamilliar. It’s surprisingly effective. (You can either use an old listing[1] or skip all the vector stuff in a new one[2].)

[1] https://web.archive.org/web/20000819070651/http://www.quanta...

[2] https://www.felixcloutier.com/x86/