Math Utils: Add bitscan 64bit version

This commit is contained in:
Clément Foucault 2020-09-05 17:37:05 +02:00
parent 44b3985a18
commit 6560a1c35e
2 changed files with 26 additions and 0 deletions

View File

@ -31,6 +31,7 @@ extern "C" {
/* Search the value from LSB to MSB for a set bit. Returns index of this bit. */
MINLINE int bitscan_forward_i(int a);
MINLINE unsigned int bitscan_forward_uint(unsigned int a);
MINLINE unsigned int bitscan_forward_uint64(unsigned long long a);
/* Similar to above, but also clears the bit. */
MINLINE int bitscan_forward_clear_i(int *a);
@ -39,6 +40,7 @@ MINLINE unsigned int bitscan_forward_clear_uint(unsigned int *a);
/* Search the value from MSB to LSB for a set bit. Returns index of this bit. */
MINLINE int bitscan_reverse_i(int a);
MINLINE unsigned int bitscan_reverse_uint(unsigned int a);
MINLINE unsigned int bitscan_reverse_uint64(unsigned long long a);
/* Similar to above, but also clears the bit. */
MINLINE int bitscan_reverse_clear_i(int *a);

View File

@ -40,6 +40,18 @@ MINLINE unsigned int bitscan_forward_uint(unsigned int a)
#endif
}
MINLINE unsigned int bitscan_forward_uint64(unsigned long long a)
{
BLI_assert(a != 0);
#ifdef _MSC_VER
unsigned long ctz;
_BitScanForward64(&ctz, a);
return ctz;
#else
return (unsigned int)__builtin_ctz(a);
#endif
}
MINLINE int bitscan_forward_i(int a)
{
return (int)bitscan_forward_uint((unsigned int)a);
@ -69,6 +81,18 @@ MINLINE unsigned int bitscan_reverse_uint(unsigned int a)
#endif
}
MINLINE unsigned int bitscan_reverse_uint64(unsigned long long a)
{
BLI_assert(a != 0);
#ifdef _MSC_VER
unsigned long clz;
_BitScanReverse64(&clz, a);
return 31 - clz;
#else
return (unsigned int)__builtin_clzll(a);
#endif
}
MINLINE int bitscan_reverse_i(int a)
{
return (int)bitscan_reverse_uint((unsigned int)a);