All of lore.kernel.org
 help / color / mirror / Atom feed
* Rework __ctz and __clz macros
@ 2019-03-13 17:16 Norbert Lange
  2019-03-13 17:16 ` [PATCH 1/2] Rename __ctz to xenomai_count_trailing_zeros Norbert Lange
  2019-03-13 17:16 ` [PATCH 2/2] Simplify xenomai_count_leading_zeros Norbert Lange
  0 siblings, 2 replies; 4+ messages in thread
From: Norbert Lange @ 2019-03-13 17:16 UTC (permalink / raw)
  To: xenomai

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.




^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] Rename __ctz to xenomai_count_trailing_zeros
  2019-03-13 17:16 Rework __ctz and __clz macros Norbert Lange
@ 2019-03-13 17:16 ` Norbert Lange
  2019-03-13 17:16 ` [PATCH 2/2] Simplify xenomai_count_leading_zeros Norbert Lange
  1 sibling, 0 replies; 4+ messages in thread
From: Norbert Lange @ 2019-03-13 17:16 UTC (permalink / raw)
  To: xenomai

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

Signed-off-by: Norbert Lange <norbert.lange@andritz.com>
---
 include/boilerplate/compiler.h    | 23 +++++------------------
 lib/boilerplate/heapmem.c         |  2 +-
 lib/copperplate/heapobj-pshared.c |  2 +-
 3 files changed, 7 insertions(+), 20 deletions(-)

diff --git a/include/boilerplate/compiler.h b/include/boilerplate/compiler.h
index 0fcc17be1..e25ab5d2c 100644
--- a/include/boilerplate/compiler.h
+++ b/include/boilerplate/compiler.h
@@ -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)				\
 	({								\
diff --git a/lib/boilerplate/heapmem.c b/lib/boilerplate/heapmem.c
index 8728e0d15..e6369c715 100644
--- a/lib/boilerplate/heapmem.c
+++ b/lib/boilerplate/heapmem.c
@@ -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
diff --git a/lib/copperplate/heapobj-pshared.c b/lib/copperplate/heapobj-pshared.c
index 5310d9092..d6cc51ae9 100644
--- a/lib/copperplate/heapobj-pshared.c
+++ b/lib/copperplate/heapobj-pshared.c
@@ -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
-- 
2.20.1



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] Simplify xenomai_count_leading_zeros
  2019-03-13 17:16 Rework __ctz and __clz macros Norbert Lange
  2019-03-13 17:16 ` [PATCH 1/2] Rename __ctz to xenomai_count_trailing_zeros Norbert Lange
@ 2019-03-13 17:16 ` Norbert Lange
  2019-04-01 17:50   ` Jan Kiszka
  1 sibling, 1 reply; 4+ messages in thread
From: Norbert Lange @ 2019-03-13 17:16 UTC (permalink / raw)
  To: xenomai

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

Signed-off-by: Norbert Lange <norbert.lange@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



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] Simplify xenomai_count_leading_zeros
  2019-03-13 17:16 ` [PATCH 2/2] Simplify xenomai_count_leading_zeros Norbert Lange
@ 2019-04-01 17:50   ` Jan Kiszka
  0 siblings, 0 replies; 4+ messages in thread
From: Jan Kiszka @ 2019-04-01 17:50 UTC (permalink / raw)
  To: Norbert Lange, xenomai

On 13.03.19 18:16, Norbert Lange via Xenomai wrote:
> Covers now all standard integer types,
> no need for corner cases.
>
> Signed-off-by: Norbert Lange <norbert.lange@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
>   }
>

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

Jan


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-04-01 17:50 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-13 17:16 Rework __ctz and __clz macros Norbert Lange
2019-03-13 17:16 ` [PATCH 1/2] Rename __ctz to xenomai_count_trailing_zeros Norbert Lange
2019-03-13 17:16 ` [PATCH 2/2] Simplify xenomai_count_leading_zeros Norbert Lange
2019-04-01 17:50   ` Jan Kiszka

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.