]> git.sesse.net Git - vlc/commitdiff
vlc_common: add ctz() to count trailing zeroes
authorRémi Denis-Courmont <remi@remlab.net>
Sun, 13 Jan 2013 17:54:59 +0000 (19:54 +0200)
committerRémi Denis-Courmont <remi@remlab.net>
Sun, 13 Jan 2013 17:55:20 +0000 (19:55 +0200)
include/vlc_common.h

index d09bf78b15bc3eb50ab24b5a12c6883ed1b5ec71..6a3dd9bbf4be84f8bda5191e04c6f496a32ac703 100644 (file)
@@ -524,7 +524,7 @@ static inline unsigned clz (unsigned x)
 
     while (x)
     {
-        x = x >> 1;
+        x >>= 1;
         i--;
     }
     return i;
@@ -536,6 +536,24 @@ static inline unsigned clz (unsigned x)
 /* XXX: this assumes that int is 32-bits or more */
 #define clz32( x ) (clz(x) - ((sizeof(unsigned) - sizeof (uint32_t)) * 8))
 
+/** Count trailing zeroes */
+VLC_USED
+static inline unsigned ctz (unsigned x)
+{
+#if VLC_GCC_VERSION(3,4)
+    return __builtin_ctz (x);
+#else
+    unsigned i = sizeof (x) * 8;
+
+    while (x)
+    {
+        x <<= 1;
+        i--;
+    }
+    return i;
+#endif
+}
+
 /** Bit weight */
 VLC_USED
 static inline unsigned popcount (unsigned x)