Consider the function:
int log2di(double x) {
return (int)log2(x);
}
Since the log2 result is immediately truncated to int, this can be much more efficiently implemented using1 std::ilogb or std::frexp
.
How about this slight variation, where the log2 result is multiplied by an arbitrary double y
before being truncated:
int log2di_mul(double x, double y) {
return (int)(log2(x) * y);
}
Can it be efficiently implemented without an expensive general purpose log2
call?
1 Really this only applies to FLT_RADIX == 2
systems, but that's everything you are realistically going to care about these days.