summaryrefslogtreecommitdiffstats
path: root/include/llvm/Support
diff options
context:
space:
mode:
authorNate Begeman <natebegeman@mac.com>2005-08-31 00:25:01 +0000
committerNate Begeman <natebegeman@mac.com>2005-08-31 00:25:01 +0000
commit16d6ea526482e733fe3bc63929e94c9e88b6708d (patch)
tree4e206a80197fd5eb747d29e90d44710d0daf7a21 /include/llvm/Support
parenteb80fe8ff684121c8130db716fe4a7cb5ca3ac0d (diff)
downloadexternal_llvm-16d6ea526482e733fe3bc63929e94c9e88b6708d.zip
external_llvm-16d6ea526482e733fe3bc63929e94c9e88b6708d.tar.gz
external_llvm-16d6ea526482e733fe3bc63929e94c9e88b6708d.tar.bz2
Add support for count trailing zeroes, and population count. These are
needed for folding constants in the upcoming dag combiner. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23163 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include/llvm/Support')
-rw-r--r--include/llvm/Support/MathExtras.h42
1 files changed, 37 insertions, 5 deletions
diff --git a/include/llvm/Support/MathExtras.h b/include/llvm/Support/MathExtras.h
index 8209445..1dbcf43 100644
--- a/include/llvm/Support/MathExtras.h
+++ b/include/llvm/Support/MathExtras.h
@@ -83,10 +83,6 @@ inline bool isPowerOf2_64(uint64_t Value) {
// counting the number of zeros from the most significant bit to the first one
// bit. Ex. CountLeadingZeros_32(0x00F000FF) == 8.
// Returns 32 if the word is zero.
-// CountLeadingZeros_32 - this function performs the platform optimal form
-// of counting the number of zeros from the most significant bit to the first
-// one bit. Ex. CountLeadingZeros_32(0x00F000FF) == 8.
-// Returns 32 if the word is zero.
inline unsigned CountLeadingZeros_32(unsigned Value) {
unsigned Count; // result
#if __GNUC__ >= 4
@@ -155,12 +151,48 @@ inline unsigned CountLeadingZeros_64(uint64_t Value) {
return Count;
}
+// CountTrailingZeros_32 - this function performs the platform optimal form of
+// counting the number of zeros from the least significant bit to the first one
+// bit. Ex. CountTrailingZeros_32(0xFF00FF00) == 8.
+// Returns 32 if the word is zero.
+inline unsigned CountTrailingZeros_32(unsigned Value) {
+ return 32 - CountLeadingZeros_32(~Value & (Value - 1));
+}
+
+// CountTrailingZeros_64 - This function performs the platform optimal form
+// of counting the number of zeros from the least significant bit to the first
+// one bit (64 bit edition.)
+// Returns 64 if the word is zero.
+inline unsigned CountTrailingZeros_64(uint64_t Value) {
+ return 64 - CountLeadingZeros_64(~Value & (Value - 1));
+}
+
+// CountPopulation_32 - this function counts the number of set bits in a value.
+// Ex. CountPopulation(0xF000F000) = 8
+// Returns 0 if the word is zero.
+inline unsigned CountPopulation_32(unsigned Value) {
+ unsigned x, t;
+ x = Value - ((Value >> 1) & 0x55555555);
+ t = ((x >> 2) & 0x33333333);
+ x = (x & 0x33333333) + t;
+ x = (x + (x >> 4)) & 0x0F0F0F0F;
+ x = x + (x << 8);
+ x = x + (x << 16);
+ return x >> 24;
+}
+
+// CountPopulation_64 - this function counts the number of set bits in a value,
+// (64 bit edition.)
+inline unsigned CountPopulation_64(uint64_t Value) {
+ return CountPopulation_32(Value >> 32) + CountPopulation_32(Value);
+}
+
// Log2_32 - This function returns the floor log base 2 of the specified value,
// -1 if the value is zero. (32 bit edition.)
// Ex. Log2_32(32) == 5, Log2_32(1) == 0, Log2_32(0) == -1
inline unsigned Log2_32(unsigned Value) {
return 31 - CountLeadingZeros_32(Value);
- }
+}
// Log2_64 - This function returns the floor log base 2 of the specified value,
// -1 if the value is zero. (64 bit edition.)