[vlc-commits] Make C parity() handle all unsigned types

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


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sun Feb 11 13:05:39 2018 +0200| [d0d9c185fa1f2eaefb8a22c41ac981695d201ad8] | committer: Rémi Denis-Courmont

Make C parity() handle all unsigned types

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

 include/vlc_common.h | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/include/vlc_common.h b/include/vlc_common.h
index c7d143bf2d..1b03307e91 100644
--- a/include/vlc_common.h
+++ b/include/vlc_common.h
@@ -483,6 +483,14 @@ static inline uint8_t clip_uint8_vlc( int32_t a )
         unsigned long long: __builtin_popcountll(x), \
           signed long long: __builtin_popcountll(x))
 
+#  define parity(x) \
+    _Generic((x), \
+        unsigned char: __builtin_parity(x), \
+        unsigned short: __builtin_parity(x), \
+        unsigned: __builtin_parity(x), \
+        unsigned long: __builtin_parityl(x), \
+        unsigned long long: __builtin_parityll(x))
+
 # else
 VLC_USED static inline int ctz(unsigned x)
 {
@@ -614,8 +622,6 @@ VLC_USED static inline int popcount(unsigned long long x)
           signed long long: popcount(x))
 #endif
 
-#endif /* __GNUC__ */
-
 /**
  * Parity
  *
@@ -623,18 +629,15 @@ VLC_USED static inline int popcount(unsigned long long x)
  * \retval 0 if x has an even number of set bits.
  * \retval 1 if x has an odd number of set bits.
  */
-VLC_USED
-static inline unsigned (parity)(unsigned x)
+VLC_USED static inline int parity(unsigned long long x)
 {
-#ifdef __GNUC__
-    return __builtin_parity (x);
-#else
     for (unsigned i = 4 * sizeof (x); i > 0; i /= 2)
         x ^= x >> i;
     return x & 1;
-#endif
 }
 
+#endif /* __GNUC__ */
+
 /** Byte swap (16 bits) */
 VLC_USED
 static inline uint16_t (bswap16)(uint16_t x)



More information about the vlc-commits mailing list