[PATCH 2/2] Simplify xenomai_count_leading_zeros

Norbert Lange nolange79 at gmail.com
Wed Mar 13 18:16:32 CET 2019


Covers now all standard integer types,
no need for corner cases.

Signed-off-by: Norbert Lange <norbert.lange at andritz.com>
---
 include/boilerplate/compiler.h | 27 +++++++--------------------
 1 file changed, 7 insertions(+), 20 deletions(-)

diff --git a/include/boilerplate/compiler.h b/include/boilerplate/compiler.h
index e25ab5d2c..828755cc1 100644
--- a/include/boilerplate/compiler.h
+++ b/include/boilerplate/compiler.h
@@ -72,32 +72,19 @@
 extern "C" {
 #endif
 
-void __invalid_operand_size(void);
-
 #define xenomai_count_trailing_zeros(x)					\
 	( (x) == 0 ? (int)(sizeof(x) * __CHAR_BIT__)			\
 	: sizeof(x) <= sizeof(unsigned) ? __builtin_ctz(x)		\
 	: sizeof(x) <= sizeof(unsigned long) ? __builtin_ctzl(x)	\
 	: __builtin_ctzll(x))
 
-#define xenomai_count_leading_zeros(__v)				\
-	({								\
-		int __ret;						\
-		if (!__v)						\
-			__ret = sizeof(__v) * 8;			\
-		else							\
-			switch (sizeof(__v)) {				\
-			case sizeof(int):				\
-				__ret = __builtin_clz((unsigned int)__v); \
-				break;					\
-			case sizeof(long long):				\
-				__ret = __builtin_clzll(__v);		\
-				break;					\
-			default:					\
-				__invalid_operand_size();		\
-			}						\
-		__ret;							\
-	})
+#define xenomai_count_leading_zeros(x)					\
+	(int)( (x) == 0 ? sizeof(x) * __CHAR_BIT__			\
+	: ( sizeof(x) <= sizeof(unsigned) ?				\
+			__builtin_clz(x) + sizeof(unsigned) - sizeof(x)	\
+		: sizeof(x) <= sizeof(unsigned long) ?			\
+			__builtin_clzl(x) + sizeof(unsigned long) - sizeof(x) \
+		: __builtin_clzll(x) + sizeof(unsigned long long) - sizeof(x)))
 
 #ifdef __cplusplus
 }
-- 
2.20.1




More information about the Xenomai mailing list