]> git.sesse.net Git - vlc/commitdiff
popcount(): compute bit weight
authorRémi Denis-Courmont <remi@remlab.net>
Tue, 15 Feb 2011 16:53:00 +0000 (18:53 +0200)
committerRémi Denis-Courmont <remi@remlab.net>
Tue, 15 Feb 2011 17:20:36 +0000 (19:20 +0200)
include/vlc_common.h

index b312a72ea9300b924ca860c68b4244f6679596aa..2fbcdc3f85a742ff2743a414a4858fdb2cebe5ec 100644 (file)
@@ -625,6 +625,23 @@ 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))
 
+/* Bit weight */
+LIBVLC_USED
+static inline unsigned popcount (unsigned x)
+{
+#ifdef __GNUC_
+    return __builtin_popcount (x);
+#else
+    unsigned count = 0;
+    while (x)
+    {
+        count += x & 1;
+        x = x >> 1;
+    }
+    return count;
+#endif
+}
+
 /* Free and set set the variable to NULL */
 #define FREENULL(a) do { free( a ); a = NULL; } while(0)