在 2022/11/30 15:23, bootmgr--- via Gcc-help 写道: > > And I want to port it to Linux, so I am looking for any functions like _BitScanForward. I trying to use > __builtin_ctz instead _BitScanForward, but it is undefined when x is 0 and it can't > determine whether x is 0 or not. Then I found __bsfd in x86intrin.h, but it's the same as > __builtin_ctz, which can't determine x is 0. > > > So is there any solution, preferably cross-platform? > > ``` __attribute__((__gnu_inline__, __nothrow__, __pure__)) extern __inline__ bool _BitScanForward(uint32_t* index, uint32_t value) { bool zf; __asm__ ( "{ bsfl %2, %1 | bsf %1, %2 }" : "=@ccz"(zf), "=r"(*index) : "rm"(value) ); return !zf; } ``` Godbolt: https://gcc.godbolt.org/z/vzEjGj11e -- Best regards, LIU Hao