use SIMDE intrinsic to count leading zeroes and make rounding exact
This commit is contained in:
committed by
francescomani
parent
d304865489
commit
4e09353b43
@@ -19,18 +19,19 @@
|
|||||||
* contact@openairinterface.org
|
* contact@openairinterface.org
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <simde/x86/avx512/lzcnt.h>
|
||||||
|
|
||||||
unsigned char log2_approx(unsigned int x)
|
unsigned char log2_approx(unsigned int x)
|
||||||
{
|
{
|
||||||
unsigned char l2 = 0;
|
const unsigned int round_val = 3037000499U; // 2^31.5
|
||||||
unsigned int tmp = x >> 1;
|
if (!x)
|
||||||
|
return 0;
|
||||||
|
|
||||||
while (tmp) {
|
int l2 = simde_x_clz32(x);
|
||||||
l2++;
|
if (x > (round_val >> l2))
|
||||||
tmp >>= 1;
|
l2 = 32 - l2;
|
||||||
}
|
else
|
||||||
|
l2 = 31 - l2;
|
||||||
if (l2)
|
|
||||||
l2 += (x >> (l2 - 1)) & 1;
|
|
||||||
|
|
||||||
return l2;
|
return l2;
|
||||||
}
|
}
|
||||||
@@ -50,16 +51,15 @@ unsigned char factor2(unsigned int x)
|
|||||||
|
|
||||||
unsigned char log2_approx64(unsigned long long int x)
|
unsigned char log2_approx64(unsigned long long int x)
|
||||||
{
|
{
|
||||||
unsigned char l2 = 0;
|
const unsigned long long round_val = 13043817825332782212ULL; // 2^63.5
|
||||||
unsigned long long int tmp = x >> 1;
|
if (!x)
|
||||||
|
return 0;
|
||||||
|
|
||||||
while (tmp) {
|
int l2 = simde_x_clz64(x);
|
||||||
l2++;
|
if (x > (round_val >> l2))
|
||||||
tmp >>= 1;
|
l2 = 64 - l2;
|
||||||
}
|
else
|
||||||
|
l2 = 63 - l2;
|
||||||
if (l2)
|
|
||||||
l2 += (x >> (l2 - 1)) & 1;
|
|
||||||
|
|
||||||
return l2;
|
return l2;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user