public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] linux/kernel.h: Fix warning seen with W=1 due to change in DIV_ROUND_CLOSEST
@ 2012-09-19  4:00 Guenter Roeck
  2012-09-19  8:33 ` Mauro Carvalho Chehab
  2012-09-19 11:26 ` Jean Delvare
  0 siblings, 2 replies; 3+ messages in thread
From: Guenter Roeck @ 2012-09-19  4:00 UTC (permalink / raw)
  To: linux-kernel
  Cc: Andrew Morton, Jean Delvare, Mauro Carvalho Chehab, lm-sensors,
	Guenter Roeck

After commit b6d86d3d (Fix DIV_ROUND_CLOSEST to support negative dividends),
the following warning is seen if the kernel is compiled with W=1 (-Wextra):

warning: comparison of unsigned expression >= 0 is always true

The warning is due to the test '((typeof(x))-1) >= 0', which is used to detect
if the variable type is unsigned. Research on the web suggests that the warning
disappears if '>' instead of '>=' is used for the comparison.

Tests after changing the macro along that line show that the warning is gone,
and that the result is still correct:

i=-4: DIV_ROUND_CLOSEST(i, 2)=-2
i=-3: DIV_ROUND_CLOSEST(i, 2)=-2
i=-2: DIV_ROUND_CLOSEST(i, 2)=-1
i=-1: DIV_ROUND_CLOSEST(i, 2)=-1
i=0: DIV_ROUND_CLOSEST(i, 2)=0
i=1: DIV_ROUND_CLOSEST(i, 2)=1
i=2: DIV_ROUND_CLOSEST(i, 2)=1
i=3: DIV_ROUND_CLOSEST(i, 2)=2
i=4: DIV_ROUND_CLOSEST(i, 2)=2

Code size is the same as before.

Signed-off-by: Guenter Roeck <linux@roeck-us.net>
---
 include/linux/kernel.h |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 594b419..2451f1f 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -91,7 +91,7 @@
 {							\
 	typeof(x) __x = x;				\
 	typeof(divisor) __d = divisor;			\
-	(((typeof(x))-1) >= 0 || (__x) >= 0) ?		\
+	(((typeof(x))-1) > 0 || (__x) > 0) ?		\
 		(((__x) + ((__d) / 2)) / (__d)) :	\
 		(((__x) - ((__d) / 2)) / (__d));	\
 }							\
-- 
1.7.9.7


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

* Re: [PATCH] linux/kernel.h: Fix warning seen with W=1 due to change in DIV_ROUND_CLOSEST
  2012-09-19  4:00 [PATCH] linux/kernel.h: Fix warning seen with W=1 due to change in DIV_ROUND_CLOSEST Guenter Roeck
@ 2012-09-19  8:33 ` Mauro Carvalho Chehab
  2012-09-19 11:26 ` Jean Delvare
  1 sibling, 0 replies; 3+ messages in thread
From: Mauro Carvalho Chehab @ 2012-09-19  8:33 UTC (permalink / raw)
  To: Guenter Roeck
  Cc: linux-kernel, Andrew Morton, Jean Delvare, Mauro Carvalho Chehab,
	lm-sensors

Em 19-09-2012 01:00, Guenter Roeck escreveu:
> After commit b6d86d3d (Fix DIV_ROUND_CLOSEST to support negative dividends),
> the following warning is seen if the kernel is compiled with W=1 (-Wextra):
> 
> warning: comparison of unsigned expression >= 0 is always true
> 
> The warning is due to the test '((typeof(x))-1) >= 0', which is used to detect
> if the variable type is unsigned. Research on the web suggests that the warning
> disappears if '>' instead of '>=' is used for the comparison.
> 
> Tests after changing the macro along that line show that the warning is gone,
> and that the result is still correct:
> 
> i=-4: DIV_ROUND_CLOSEST(i, 2)=-2
> i=-3: DIV_ROUND_CLOSEST(i, 2)=-2
> i=-2: DIV_ROUND_CLOSEST(i, 2)=-1
> i=-1: DIV_ROUND_CLOSEST(i, 2)=-1
> i=0: DIV_ROUND_CLOSEST(i, 2)=0
> i=1: DIV_ROUND_CLOSEST(i, 2)=1
> i=2: DIV_ROUND_CLOSEST(i, 2)=1
> i=3: DIV_ROUND_CLOSEST(i, 2)=2
> i=4: DIV_ROUND_CLOSEST(i, 2)=2
> 
> Code size is the same as before.

It is great to have this warning disappearing.

Tested-by: Mauro Carvalho Chehab <mchehab@redhat.com>
> 
> Signed-off-by: Guenter Roeck <linux@roeck-us.net>
> ---
>  include/linux/kernel.h |    2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/include/linux/kernel.h b/include/linux/kernel.h
> index 594b419..2451f1f 100644
> --- a/include/linux/kernel.h
> +++ b/include/linux/kernel.h
> @@ -91,7 +91,7 @@
>  {							\
>  	typeof(x) __x = x;				\
>  	typeof(divisor) __d = divisor;			\
> -	(((typeof(x))-1) >= 0 || (__x) >= 0) ?		\
> +	(((typeof(x))-1) > 0 || (__x) > 0) ?		\
>  		(((__x) + ((__d) / 2)) / (__d)) :	\
>  		(((__x) - ((__d) / 2)) / (__d));	\
>  }							\
> 


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

* Re: [PATCH] linux/kernel.h: Fix warning seen with W=1 due to change in DIV_ROUND_CLOSEST
  2012-09-19  4:00 [PATCH] linux/kernel.h: Fix warning seen with W=1 due to change in DIV_ROUND_CLOSEST Guenter Roeck
  2012-09-19  8:33 ` Mauro Carvalho Chehab
@ 2012-09-19 11:26 ` Jean Delvare
  1 sibling, 0 replies; 3+ messages in thread
From: Jean Delvare @ 2012-09-19 11:26 UTC (permalink / raw)
  To: Guenter Roeck
  Cc: linux-kernel, Andrew Morton, Mauro Carvalho Chehab, lm-sensors

On Tue, 18 Sep 2012 21:00:09 -0700, Guenter Roeck wrote:
> After commit b6d86d3d (Fix DIV_ROUND_CLOSEST to support negative dividends),
> the following warning is seen if the kernel is compiled with W=1 (-Wextra):
> 
> warning: comparison of unsigned expression >= 0 is always true
> 
> The warning is due to the test '((typeof(x))-1) >= 0', which is used to detect
> if the variable type is unsigned. Research on the web suggests that the warning
> disappears if '>' instead of '>=' is used for the comparison.
> 
> Tests after changing the macro along that line show that the warning is gone,
> and that the result is still correct:
> 
> i=-4: DIV_ROUND_CLOSEST(i, 2)=-2
> i=-3: DIV_ROUND_CLOSEST(i, 2)=-2
> i=-2: DIV_ROUND_CLOSEST(i, 2)=-1
> i=-1: DIV_ROUND_CLOSEST(i, 2)=-1
> i=0: DIV_ROUND_CLOSEST(i, 2)=0
> i=1: DIV_ROUND_CLOSEST(i, 2)=1
> i=2: DIV_ROUND_CLOSEST(i, 2)=1
> i=3: DIV_ROUND_CLOSEST(i, 2)=2
> i=4: DIV_ROUND_CLOSEST(i, 2)=2
> 
> Code size is the same as before.
> 
> Signed-off-by: Guenter Roeck <linux@roeck-us.net>
> ---
>  include/linux/kernel.h |    2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/include/linux/kernel.h b/include/linux/kernel.h
> index 594b419..2451f1f 100644
> --- a/include/linux/kernel.h
> +++ b/include/linux/kernel.h
> @@ -91,7 +91,7 @@
>  {							\
>  	typeof(x) __x = x;				\
>  	typeof(divisor) __d = divisor;			\
> -	(((typeof(x))-1) >= 0 || (__x) >= 0) ?		\
> +	(((typeof(x))-1) > 0 || (__x) > 0) ?		\
>  		(((__x) + ((__d) / 2)) / (__d)) :	\
>  		(((__x) - ((__d) / 2)) / (__d));	\
>  }							\

Very nice. Let's push this upstream ASAP.

Acked-by: Jean Delvare <khali@linux-fr.org>

-- 
Jean Delvare

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

end of thread, other threads:[~2012-09-19 11:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-09-19  4:00 [PATCH] linux/kernel.h: Fix warning seen with W=1 due to change in DIV_ROUND_CLOSEST Guenter Roeck
2012-09-19  8:33 ` Mauro Carvalho Chehab
2012-09-19 11:26 ` Jean Delvare

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox