How to efficiently call _BitScanReverse or __builtin_clz in constexpr functions?

It seems that _BitScanReverse, although it is an integral and not a real function, cannot be called in the constexpr function in Visual C ++. I know that I can implement this operation on my own much more slowly, which would be good for the case when it was evaluated at compile time, but it is unfortunate that it will not be just a single-cylinder, one processor (BSR) for the case when it was evaluated at runtime. I have not tried __builtin_clz in GCC / Clang yet, but it may or may not have the same problem and I want this code to work with main compilers (with slow backup for non-GCC, non-Clang, non- VC compilers).

Ideas / Questions:

Is there an easy way to have a function that uses one block of code when evaluating at compile time so that it can be constexpr-safe and another block of code for runtime so it can be fast? (If so, this will also be relevant to a few other issues that I have.)

Alternatively, is there a way to trick the compiler so that it can evaluate _BitScanReverse for constexpr code?

Side question:

Are there any words about plans to eventually add this to the C ++ standard? They added std :: log2 and std :: ilogb, but both of them go through floating point numbers, and not just do one BSR (or CLZ and subtraction on ARM chips).

+4
source share

Source: https://habr.com/ru/post/1687174/


All Articles