+/** Count leading zeroes */
+VLC_USED
+static inline unsigned clz (unsigned x)
+{
+#if VLC_GCC_VERSION(3,4)
+ return __builtin_clz (x);
+#else
+ unsigned i = sizeof (x) * 8;
+
+ while (x)
+ {
+ x = x >> 1;
+ i--;
+ }
+ return i;
+#endif
+}
+
+#define clz8( x ) (clz(x) - ((sizeof(unsigned) - sizeof (uint8_t)) * 8))
+#define clz16( x ) (clz(x) - ((sizeof(unsigned) - sizeof (uint16_t)) * 8))
+/* XXX: this assumes that int is 32-bits or more */
+#define clz32( x ) (clz(x) - ((sizeof(unsigned) - sizeof (uint32_t)) * 8))
+
+/** Bit weight */
+VLC_USED
+static inline unsigned popcount (unsigned x)
+{
+#if VLC_GCC_VERSION(3,4)
+ return __builtin_popcount (x);
+#else
+ unsigned count = 0;
+ while (x)
+ {
+ count += x & 1;
+ x = x >> 1;
+ }
+ return count;
+#endif
+}
+
+VLC_USED
+static inline unsigned parity (unsigned x)
+{
+#if VLC_GCC_VERSION(3,4)
+ return __builtin_parity (x);
+#else
+ for (unsigned i = 4 * sizeof (x); i > 0; i /= 2)
+ x ^= x >> i;
+ return x & 1;
+#endif
+}
+
+#ifdef __OS2__
+# undef bswap16
+# undef bswap32
+# undef bswap64
+#endif
+
+/** Byte swap (16 bits) */
+VLC_USED
+static inline uint16_t bswap16 (uint16_t x)
+{
+ return (x << 8) | (x >> 8);
+}
+
+/** Byte swap (32 bits) */
+VLC_USED
+static inline uint32_t bswap32 (uint32_t x)
+{
+#if VLC_GCC_VERSION(4,3)
+ return __builtin_bswap32 (x);
+#else
+ return ((x & 0x000000FF) << 24)
+ | ((x & 0x0000FF00) << 8)
+ | ((x & 0x00FF0000) >> 8)
+ | ((x & 0xFF000000) >> 24);
+#endif
+}
+
+/** Byte swap (64 bits) */
+VLC_USED
+static inline uint64_t bswap64 (uint64_t x)
+{
+#if VLC_GCC_VERSION(4,3)
+ return __builtin_bswap64 (x);
+#elif !defined (__cplusplus)
+ return ((x & 0x00000000000000FF) << 56)
+ | ((x & 0x000000000000FF00) << 40)
+ | ((x & 0x0000000000FF0000) << 24)
+ | ((x & 0x00000000FF000000) << 8)
+ | ((x & 0x000000FF00000000) >> 8)
+ | ((x & 0x0000FF0000000000) >> 24)
+ | ((x & 0x00FF000000000000) >> 40)
+ | ((x & 0xFF00000000000000) >> 56);
+#else
+ return ((x & 0x00000000000000FFLLU) << 56)
+ | ((x & 0x000000000000FF00LLU) << 40)
+ | ((x & 0x0000000000FF0000LLU) << 24)
+ | ((x & 0x00000000FF000000LLU) << 8)
+ | ((x & 0x000000FF00000000LLU) >> 8)
+ | ((x & 0x0000FF0000000000LLU) >> 24)
+ | ((x & 0x00FF000000000000LLU) >> 40)
+ | ((x & 0xFF00000000000000LLU) >> 56);
+#endif
+}
+
+