mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-05-13 17:38:39 +00:00
Use the GCC built-in for PopulationCount when it's available, and use a faster
algorithm when it's not. This should be particularly noticeable in the 64-bit case. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@34776 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
af8fb19846
commit
2a934cb607
@ -208,22 +208,27 @@ inline unsigned CountTrailingZeros_64(uint64_t Value) {
|
|||||||
/// CountPopulation_32 - this function counts the number of set bits in a value.
|
/// CountPopulation_32 - this function counts the number of set bits in a value.
|
||||||
/// Ex. CountPopulation(0xF000F000) = 8
|
/// Ex. CountPopulation(0xF000F000) = 8
|
||||||
/// Returns 0 if the word is zero.
|
/// Returns 0 if the word is zero.
|
||||||
inline unsigned CountPopulation_32(unsigned Value) {
|
inline unsigned CountPopulation_32(uint32_t Value) {
|
||||||
unsigned x, t;
|
#if __GNUC__ >= 4
|
||||||
x = Value - ((Value >> 1) & 0x55555555);
|
return __builtin_popcount(Value);
|
||||||
t = ((x >> 2) & 0x33333333);
|
#else
|
||||||
x = (x & 0x33333333) + t;
|
uint32_t v = v - ((v >> 1) & 0x55555555);
|
||||||
x = (x + (x >> 4)) & 0x0F0F0F0F;
|
v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
|
||||||
x = x + (x << 8);
|
return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
|
||||||
x = x + (x << 16);
|
#endif
|
||||||
return x >> 24;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// CountPopulation_64 - this function counts the number of set bits in a value,
|
/// CountPopulation_64 - this function counts the number of set bits in a value,
|
||||||
/// (64 bit edition.)
|
/// (64 bit edition.)
|
||||||
inline unsigned CountPopulation_64(uint64_t Value) {
|
inline unsigned CountPopulation_64(uint64_t Value) {
|
||||||
return CountPopulation_32(unsigned(Value >> 32)) +
|
#if __GNUC__ >= 4
|
||||||
CountPopulation_32(unsigned(Value));
|
return __builtin_popcountll(Value);
|
||||||
|
#else
|
||||||
|
uint64_t v = Value - ((Value >> 1) & 0x5555555555555555ULL);
|
||||||
|
v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
|
||||||
|
v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
|
||||||
|
return (uint64_t)(v * 0x0101010101010101ULL) >> 56;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Log2_32 - This function returns the floor log base 2 of the specified value,
|
/// Log2_32 - This function returns the floor log base 2 of the specified value,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user