You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In G++, there exists some intrinsic function that can make the function find_last_bit_set more efficiently, i.e. __builtin_clz
If under g++/clang, maybe we can replace the function like this. Or even simply using the BSR(bit scan reverse) instructions. Besides, the VSStudio also provides _BitScanReverse, _BitScanReverse64 if needed.
template <typename T>
constexprunsignedfind_last_bit_set(T val) {
// Or we can replace it with bit scan reverse with extra 1 plus.ifconstexpr (sizeof(T) == sizeof(unsignedint)) {
returnsizeof(T) * 8 - __builtin_clz(val);
} elseifconstexpr (sizeof(T) == sizeof(unsignedlong)){
returnsizeof(T) * 8 - __builtin_clzl(val);
} elseifconstexpr (sizeof(T) == sizeof(unsignedlonglong)){
returnsizeof(T) * 8 - __builtin_clzll(val);
} else {
unsigned result = 0;
for (; val != 0; val >>= 1)
++result;
return result;
}
}
The text was updated successfully, but these errors were encountered:
Thanks for the tip. I know about the intrinsics for bitscan forward/reverse, I thought I even added a TODO to adapt the function. Unfortunately I don't think the intrinsisc are constexpr, but I will look into this.
In G++, there exists some intrinsic function that can make the function find_last_bit_set more efficiently, i.e. __builtin_clz
If under g++/clang, maybe we can replace the function like this. Or even simply using the BSR(bit scan reverse) instructions. Besides, the VSStudio also provides _BitScanReverse, _BitScanReverse64 if needed.
The text was updated successfully, but these errors were encountered: