This repository has been archived by the owner on Jun 18, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
scx: Branchless implementation of highest_bit
Origin implementation of function highest_bit utilize the function of "fls()" to calculate the most significant bit of the input parameter "flags". Normally we can return the mask with "1 << (fls(flags) - 1)", but "fls(flags)" will return 0 if the value of "flags" is 0, which will cause the evaluation become "1 << (-1)" and it's illegal. So we use a branch to determine whether the return value of "fls(flags)" is 0. We can remove the use of branch first left shift "fls(flags)" number of bits and then right shift 1 bit. When the value of "fls(flags)" is 0 then this evaluation will simply become 0 without any error. As more values other than 0, evaluation is the same as it did for "1 << (fls(flags) - 1)". This implementation can prevent any possible branch prediction fault and pure shift operations are cheaper than branch operation for if-else statements.
- Loading branch information