Fix the non-gcc 4.0 paths for countleadingzeros
authorChris Lattner <sabre@nondot.org>
Tue, 2 Aug 2005 20:21:33 +0000 (20:21 +0000)
committerChris Lattner <sabre@nondot.org>
Tue, 2 Aug 2005 20:21:33 +0000 (20:21 +0000)
Patch fixed by Jim Laskey

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@22598 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/MathExtras.h

index d8cdda8e6684f6263a7ff20d0bb7c7d45adf0ca3..6cd44682fc0e94018bc33d7a1cb8293ddc41d427 100644 (file)
@@ -72,36 +72,41 @@ inline bool isPowerOf2_32(unsigned Value) {
   return Value && !(Value & (Value - 1));
 }
 
   return Value && !(Value & (Value - 1));
 }
 
-// isPowerOf2_64 - This function returns true if the argument is a power of two > 0
-// (64 bit edition.)
+// isPowerOf2_64 - This function returns true if the argument is a power of two
+// > 0 (64 bit edition.)
 inline bool isPowerOf2_64(uint64_t Value) {
   return Value && !(Value & (Value - 1LL));
 }
 
 inline bool isPowerOf2_64(uint64_t Value) {
   return Value && !(Value & (Value - 1LL));
 }
 
+// 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.
 // CountLeadingZeros_32 - this function performs the platform optimal form
 // 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.
+// 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
 // Returns 32 if the word is zero.
 inline unsigned CountLeadingZeros_32(unsigned Value) {
   unsigned Count; // result
-  #if __GNUC__ >= 4
-    // PowerPC is defined for __builtin_clz(0)
-    #if !defined(__ppc__) && !defined(__ppc64__)
-      if (!Value) return 32;
-    #endif
-    Count = __builtin_clz(Value);
-  #else
-    if (!Value) return 32;
-    Count = 0;
-    // bisecton method for count leading zeros
-    for (unsigned Shift = 32 >> 1; Shift; Shift >>= 1) {
-        unsigned Tmp = Value >> Shift;
-        if (Tmp) {
-            Count |= Shift;
-            Value = Tmp;
-        }
+#if __GNUC__ >= 4
+  // PowerPC is defined for __builtin_clz(0)
+#if !defined(__ppc__) && !defined(__ppc64__)
+  if (!Value) return 32;
+#endif
+  Count = __builtin_clz(Value);
+#else
+  if (!Value) return 32;
+  Count = 0;
+  // bisecton method for count leading zeros
+  for (unsigned Shift = 32 >> 1; Shift; Shift >>= 1) {
+    unsigned Tmp = Value >> Shift;
+    if (Tmp) {
+      Value = Tmp;
+    } else {
+      Count |= Shift;
     }
     }
-  #endif
+  }
+#endif
   return Count;
 }
 
   return Count;
 }
 
@@ -111,13 +116,13 @@ inline unsigned CountLeadingZeros_32(unsigned Value) {
 // Returns 64 if the word is zero.
 inline unsigned CountLeadingZeros_64(uint64_t Value) {
   unsigned Count; // result
 // Returns 64 if the word is zero.
 inline unsigned CountLeadingZeros_64(uint64_t Value) {
   unsigned Count; // result
-  #if __GNUC__ >= 4
-    // PowerPC is defined for __builtin_clzll(0)
-    #if !defined(__ppc__) && !defined(__ppc64__)
-      if (!Value) return 64;
-    #endif
-    Count = __builtin_clzll(Value);
-  #else
+#if __GNUC__ >= 4
+  // PowerPC is defined for __builtin_clzll(0)
+#if defined(__ppc__) || defined(__ppc64__)
+  if (!Value) return 64;
+#endif
+  Count = __builtin_clzll(Value);
+#else
   if (sizeof(long) == sizeof(int64_t)) {
     if (!Value) return 64;
     Count = 0;
   if (sizeof(long) == sizeof(int64_t)) {
     if (!Value) return 64;
     Count = 0;
@@ -125,8 +130,9 @@ inline unsigned CountLeadingZeros_64(uint64_t Value) {
     for (uint64_t Shift = 64 >> 1; Shift; Shift >>= 1) {
       uint64_t Tmp = Value >> Shift;
       if (Tmp) {
     for (uint64_t Shift = 64 >> 1; Shift; Shift >>= 1) {
       uint64_t Tmp = Value >> Shift;
       if (Tmp) {
-        Count |= Shift;
         Value = Tmp;
         Value = Tmp;
+      } else {
+        Count |= Shift;
       }
     }
   } else {
       }
     }
   } else {