Skip to contentSkip to conversation

Rework __ctz and __clz macros

PATCH Superseded Norbert Lange 2 participants 4 messages Mar 13, 2019 – Apr 1, 2019

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

Those are commonly used in Frameworks (eg libc++), This series renamed __ctz and replaces the __clz with something simpler that could be evaluated in constant contexts aswell (and doesnt use GCC statement expressions).

Ideally though these would be moved out of the public header into something private for the xenomai build.

#1 lore raw

PATCH 1/2 Rename __ctz to xenomai_count_trailing_zeros

Because of conflicts with libc++ (v1/bit include file). Simplify the macro as there shouldnt be any bad cornercases

---
 include/boilerplate/compiler.h    | 23 +++++------------------
 lib/boilerplate/heapmem.c         |  2 +-
 lib/copperplate/heapobj-pshared.c |  2 +-
 3 files changed, 7 insertions(+), 20 deletions(-)
include/boilerplate/compiler.h +5 -18
index 0fcc17be1..e25ab5d2c 100644
@@ -74,24 +74,11 @@ extern "C" {
 	
 void __invalid_operand_size(void);
 
-#define __ctz(__v)							\
-	({								\
-		int __ret;						\
-		if (!__v)						\
-			__ret = sizeof(__v) * 8;			\
-		else							\
-			switch (sizeof(__v)) {				\
-			case sizeof(int):				\
-				__ret = __builtin_ctz((unsigned int)__v); \
-				break;					\
-			case sizeof(long long):				\
-				__ret = __builtin_ctzll(__v);		\
-				break;					\
-			default:					\
-				__invalid_operand_size();		\
-			}						\
-		__ret;							\
-	})
+#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)				\
 	({								\
lib/boilerplate/heapmem.c +1 -1
index 8728e0d15..e6369c715 100644
@@ -476,7 +476,7 @@ void *heapmem_alloc(struct heap_memory *heap, size_t size)
 			bmask = ext->pagemap[pg].map;
 			if (bmask == -1U)
 				break;
-			b = __ctz(~bmask);
+			b = xenomai_count_trailing_zeros(~bmask);
 
 			/*
 			 * Got one block from the heading per-bucket
lib/copperplate/heapobj-pshared.c +1 -1
index 5310d9092..d6cc51ae9 100644
@@ -529,7 +529,7 @@ static void *sheapmem_alloc(struct shared_heap_memory *heap, size_t size)
 			bmask = ext->pagemap[pg].map;
 			if (bmask == -1U)
 				break;
-			b = __ctz(~bmask);
+			b = xenomai_count_trailing_zeros(~bmask);
 
 			/*
 			 * Got one block from the heading per-bucket
Signature · 1 line
2.20.1
#2 lore raw

PATCH 2/2 Simplify xenomai_count_leading_zeros

In reply to Norbert Lange

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

---
 include/boilerplate/compiler.h | 27 +++++++--------------------
 1 file changed, 7 insertions(+), 20 deletions(-)
include/boilerplate/compiler.h +7 -20
index e25ab5d2c..828755cc1 100644
@@ -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
 }
Signature · 1 line
2.20.1
#3 lore raw

Re: [PATCH 2/2] Simplify xenomai_count_leading_zeros

Quoted 43 lines · Norbert Lange via Xenomai

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

---
  include/boilerplate/compiler.h | 27 +++++++--------------------
  1 file changed, 7 insertions(+), 20 deletions(-)
include/boilerplate/compiler.h +0 -20
index e25ab5d2c..828755cc1 100644
@@ -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;							\
-	})

include/boilerplate/compiler.h line 90

Quoted 10 lines
include/boilerplate/compiler.h +7 -0
+#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
  }

Looks functionally ok, but why is it differently structured than patch 1? Can we use that formatting for this one as well?

Jan