Improve pbitops performance by enabling GCC intrinsics when available

This commit is contained in:
rdb 2014-10-29 21:18:28 +00:00
parent 747e1fe381
commit 10a72e595b
4 changed files with 2843 additions and 11 deletions

View File

@ -218,13 +218,4 @@ init_libputil() {
BamCacheIndex::register_with_read_factory();
BamCacheRecord::register_with_read_factory();
// Initialize the num_bits_on table, for BitMask::get_num_on_bits().
for (int bit = 0; bit < 16; ++bit) {
int w = (1 << bit);
for (int i = 0; i < w; ++i) {
num_bits_on[i + w] = num_bits_on[i] + 1;
}
}
}

View File

@ -28,7 +28,11 @@ count_bits_in_word(PN_uint16 x) {
////////////////////////////////////////////////////////////////////
INLINE int
count_bits_in_word(PN_uint32 x) {
#if defined(__GNUC__) && defined(__POPCNT__)
return __builtin_popcount((unsigned int) x);
#else
return (int)num_bits_on[x & 0xffff] + (int)num_bits_on[(x >> 16) & 0xffff];
#endif
}
////////////////////////////////////////////////////////////////////
@ -37,7 +41,11 @@ count_bits_in_word(PN_uint32 x) {
////////////////////////////////////////////////////////////////////
INLINE int
count_bits_in_word(PN_uint64 x) {
#if defined(__GNUC__) && defined(__POPCNT__)
return __builtin_popcountll((unsigned long long) x);
#else
return count_bits_in_word((PN_uint32)x) + count_bits_in_word((PN_uint32)(x >> 32));
#endif
}
////////////////////////////////////////////////////////////////////
@ -137,12 +145,16 @@ flood_bits_up(PN_uint64 x) {
////////////////////////////////////////////////////////////////////
INLINE int
get_lowest_on_bit(PN_uint16 x) {
#ifdef __GNUC__
return __builtin_ffs((unsigned int) x) - 1;
#else
if (x == 0) {
return -1;
}
PN_uint16 w = (x & (~x + 1));
return (int)num_bits_on[w - 1];
#endif
}
////////////////////////////////////////////////////////////////////
@ -152,12 +164,16 @@ get_lowest_on_bit(PN_uint16 x) {
////////////////////////////////////////////////////////////////////
INLINE int
get_lowest_on_bit(PN_uint32 x) {
#ifdef __GNUC__
return __builtin_ffs((unsigned int) x) - 1;
#else
if (x == 0) {
return -1;
}
PN_uint32 w = (x & (~x + 1));
return count_bits_in_word(w - 1);
#endif
}
////////////////////////////////////////////////////////////////////
@ -167,12 +183,16 @@ get_lowest_on_bit(PN_uint32 x) {
////////////////////////////////////////////////////////////////////
INLINE int
get_lowest_on_bit(PN_uint64 x) {
#ifdef __GNUC__
return __builtin_ffsll((unsigned long long) x) - 1;
#else
if (x == 0) {
return -1;
}
PN_uint64 w = (x & (~x + 1));
return count_bits_in_word(w - 1);
#endif
}
////////////////////////////////////////////////////////////////////
@ -182,8 +202,12 @@ get_lowest_on_bit(PN_uint64 x) {
////////////////////////////////////////////////////////////////////
INLINE int
get_highest_on_bit(PN_uint16 x) {
#ifdef __GNUC__
return (x == 0) ? -1 : 31 - __builtin_clz((unsigned int) x);
#else
PN_uint16 w = flood_bits_down(x);
return count_bits_in_word(w) - 1;
#endif
}
////////////////////////////////////////////////////////////////////
@ -193,8 +217,12 @@ get_highest_on_bit(PN_uint16 x) {
////////////////////////////////////////////////////////////////////
INLINE int
get_highest_on_bit(PN_uint32 x) {
#ifdef __GNUC__
return (x == 0) ? -1 : 31 - __builtin_clz((unsigned int) x);
#else
PN_uint32 w = flood_bits_down(x);
return count_bits_in_word(w) - 1;
#endif
}
////////////////////////////////////////////////////////////////////
@ -204,8 +232,12 @@ get_highest_on_bit(PN_uint32 x) {
////////////////////////////////////////////////////////////////////
INLINE int
get_highest_on_bit(PN_uint64 x) {
#ifdef __GNUC__
return (x == 0) ? -1 : 63 - __builtin_clzll((unsigned long long) x);
#else
PN_uint64 w = flood_bits_down(x);
return count_bits_in_word(w) - 1;
#endif
}
////////////////////////////////////////////////////////////////////

File diff suppressed because it is too large Load Diff

View File

@ -46,7 +46,7 @@ INLINE int get_next_higher_bit(PN_uint32 x);
INLINE int get_next_higher_bit(PN_uint64 x);
// This table precomputes the number of on bits in each 16-bit word.
extern EXPCL_PANDA_PUTIL unsigned char num_bits_on[65536];
extern EXPCL_PANDA_PUTIL const unsigned char num_bits_on[65536];
#include "pbitops.I"