I need a very fast implementation of the log2 (float x) function in C ++.
I found a very interesting implementation (and very fast!)
#include <intrin.h>
inline unsigned long log2(int x)
{
unsigned long y;
_BitScanReverse(&y, x);
return y;
}
But this function is only good for integer input values.
Question: Is it possible to convert this function to an input variable double ?
UPD :
I found this implementation:
typedef unsigned long uint32;
typedef long int32;
static inline int32 ilog2(float x)
{
uint32 ix = (uint32&)x;
uint32 exp = (ix >> 23) & 0xFF;
int32 log2 = int32(exp) - 127;
return log2;
}
which is much faster than the previous example, but the output is unsigned.
Is it possible for this function to return a double ?
Thanks in advance!
source
share