linux/kernel.h: Fix warning seen with W=1 due to change in DIV_ROUND_CLOSEST
PATCH 1/1 linux/kernel.h: Fix warning seen with W=1 due to change in DIV_ROUND_CLOSEST
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.
--- include/linux/kernel.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/kernel.h +1 -1
@@ -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)); \ } \
Signature · 1 line
1.7.9.7
Em 19-09-2012 01:00, Guenter Roeck escreveu:
Quoted 22 lines
After commit
b6d86d3d(FixDIV_ROUND_CLOSESTto 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
Quoted 1 line
Code size is the same as before.
It is great to have this warning disappearing.
Quoted 12 lines
Signed-off-by Guenter Roeck <[email protected]>--- include/linux/kernel.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/linux/kernel.h +0
-0
include/linux/kernel.h line 97
Quoted 8 lines
include/linux/kernel.h +1
-1@@ -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)); \ } \
_______________________________________________
lm-sensors mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/lm-sensors
Quoted 34 lines · Guenter Roeck
After commit
b6d86d3d(FixDIV_ROUND_CLOSESTto 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)=2Code size is the same as before.
Signed-off-by Guenter Roeck <[email protected]>--- include/linux/kernel.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/linux/kernel.h +0
-0
include/linux/kernel.h line 97
Quoted 8 lines
include/linux/kernel.h +1
-1@@ -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.
Signature · 6 lines
Jean Delvare _______________________________________________ lm-sensors mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/lm-sensors