[vlc-commits] Make clz() type-generic, accept long and long long

Rémi Denis-Courmont git at videolan.org
Sun Feb 11 12:49:20 CET 2018


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sun Feb 11 11:51:37 2018 +0200| [4f58c049f092b31883703cd47a9160f32620c367] | committer: Rémi Denis-Courmont

Make clz() type-generic, accept long and long long

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=4f58c049f092b31883703cd47a9160f32620c367
---

 include/vlc_common.h | 47 +++++++++++++++++++++++++++++++++++------------
 1 file changed, 35 insertions(+), 12 deletions(-)

diff --git a/include/vlc_common.h b/include/vlc_common.h
index 88aef3330a..78fdfadfbb 100644
--- a/include/vlc_common.h
+++ b/include/vlc_common.h
@@ -443,14 +443,28 @@ static inline uint8_t clip_uint8_vlc( int32_t a )
     else           return a;
 }
 
-/** Count leading zeroes */
-VLC_USED
-static inline unsigned (clz)(unsigned x)
+
+#ifndef __cplusplus
+# ifdef __GNUC__
+/**
+ * Count leading zeroes
+ *
+ * (assumes CHAR_BIT==8)
+ */
+#  define clz(x) \
+    _Generic((x), \
+        unsigned char: (__builtin_clz(x) \
+		- (sizeof (unsigned) - 1) * 8), \
+        unsigned short: (__builtin_clz(x) \
+		- (sizeof (unsigned) - sizeof (unsigned short)) * 8), \
+        unsigned: __builtin_clz(x), \
+        unsigned long: __builtin_clzl(x), \
+        unsigned long long: __builtin_clzll(x))
+
+# else
+VLC_USED static inline int clzbits(unsigned long long x, int maxbits)
 {
-#ifdef __GNUC__
-    return __builtin_clz (x);
-#else
-    unsigned i = sizeof (x) * 8;
+    int i = maxbits;
 
     while (x)
     {
@@ -458,13 +472,22 @@ static inline unsigned (clz)(unsigned x)
         i--;
     }
     return i;
-#endif
 }
+#  define clztype(x, type) clzbits(x, sizeof (type) * 8)
+
+#  define clz(x) \
+    _Generic((x), \
+        unsigned char: clzbits(x, char), \
+        unsigned short: clzbits(x, short), \
+        unsigned: clzbits(x, int), \
+        unsigned long: clzbits(x, long), \
+        unsigned long long: clzbits(x, long long))
+# endif
+#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))
+#define clz8(x)  clz((uint8_t)(x))
+#define clz16(x) clz((uint16_t)(x))
+#define clz32(x) clz((uint32_t)(x))
 
 /** Count trailing zeroes */
 VLC_USED



More information about the vlc-commits mailing list