Skip to contentSkip to conversation

linux/kernel.h: Fix DIV_ROUND_CLOSEST to support negative operands

PATCH Superseded Guenter Roeck 2 participants 3 messages Aug 31, 2012

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 linux/kernel.h: Fix DIV_ROUND_CLOSEST to support negative operands

DIV_ROUND_CLOSEST returns a bad result for dividends with different sign:

	DIV_ROUND_CLOSEST(-2, 2) = 0

Most of the time this does not matter. However, in the hardware monitoring subsystem, DIV_ROUND_CLOSEST is sometimes used on integers which can be negative (such as temperatures).

---
v3: Instead of adding a new macro, fix DIV_ROUND_CLOSEST.
This version works for negative dividend and divisor.

v2: v1 did not work if typeof(divisor) was an unsigned variable type
(which can obviously not be negative).
Rework to revert to DIV_ROUND_CLOSEST if the dividend is unsigned, or if it is signed but non-negative.

 include/linux/kernel.h |    7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
include/linux/kernel.h +5 -2
index 6043821..4b180de 100644
@@ -84,8 +84,11 @@
 )
 #define DIV_ROUND_CLOSEST(x, divisor)(			\
 {							\
-	typeof(divisor) __divisor = divisor;		\
-	(((x) + ((__divisor) / 2)) / (__divisor));	\
+	typeof(x) __x = x;				\
+	typeof(divisor) __d = divisor;			\
+	((__x) < 0) == ((__d) < 0) ?			\
+		(((__x) + ((__d) / 2)) / (__d)) :	\
+		(((__x) - ((__d) / 2)) / (__d));	\
 }							\
 )
 
Signature · 1 line
1.7.9.7
#1 lore raw
Quoted 14 lines · On Thu, 30 Aug 2012 17:10:47 -0700 Guenter Roeck <[email protected]> wrote:

DIV_ROUND_CLOSEST returns a bad result for dividends with different sign:

	DIV_ROUND_CLOSEST(-2, 2) = 0

Most of the time this does not matter. However, in the hardware monitoring subsystem, DIV_ROUND_CLOSEST is sometimes used on integers which can be negative (such as temperatures).

...

include/linux/kernel.h +0 -0
@@ -84,8 +84,11 @@
 )
 #define DIV_ROUND_CLOSEST(x, divisor)(			\

include/linux/kernel.h line 93

Quoted 10 lines
include/linux/kernel.h +5 -2
 {							\
-	typeof(divisor) __divisor = divisor;		\
-	(((x) + ((__divisor) / 2)) / (__divisor));	\
+	typeof(x) __x = x;				\
+	typeof(divisor) __d = divisor;			\
+	((__x) < 0) == ((__d) < 0) ?			\
+		(((__x) + ((__d) / 2)) / (__d)) :	\
+		(((__x) - ((__d) / 2)) / (__d));	\
 }							\
 )

Your v2 had that sneaky little "(typeof(x))-1 >= 0" trick in it, so half the code gets elided at compile time if `x' (why isn't this called "dividend") has an unsigned type.

Would retaining that be of any benefit? We do want to avoid doing the compare-and-branch in as many cases as possible.

Also, this would be a great opportunity to document the macro's beahviour (I do go on). That would be a useful thing to do, given that we're now handling the four +/+, +/-, -/+, -/- cases and the behaviour for each case isn't terribly obvious.

#2 lore raw
Quoted 31 lines · Andrew Morton
Quoted 14 lines · On Thu, 30 Aug 2012 17:10:47 -0700 Guenter Roeck <[email protected]> wrote:

DIV_ROUND_CLOSEST returns a bad result for dividends with different sign:

	DIV_ROUND_CLOSEST(-2, 2) = 0

Most of the time this does not matter. However, in the hardware monitoring subsystem, DIV_ROUND_CLOSEST is sometimes used on integers which can be negative (such as temperatures).

...

include/linux/kernel.h +0 -0
@@ -84,8 +84,11 @@
 )
 #define DIV_ROUND_CLOSEST(x, divisor)(			\

include/linux/kernel.h line 93

Quoted 10 lines
include/linux/kernel.h +5 -2
 {							\
-	typeof(divisor) __divisor = divisor;		\
-	(((x) + ((__divisor) / 2)) / (__divisor));	\
+	typeof(x) __x = x;				\
+	typeof(divisor) __d = divisor;			\
+	((__x) < 0) == ((__d) < 0) ?			\
+		(((__x) + ((__d) / 2)) / (__d)) :	\
+		(((__x) - ((__d) / 2)) / (__d));	\
 }							\
 )

Your v2 had that sneaky little "(typeof(x))-1 >= 0" trick in it, so half the code gets elided at compile time if `x' (why isn't this called "dividend") has an unsigned type.

Quoted 3 lines

Would retaining that be of any benefit? We do want to avoid doing the compare-and-branch in as many cases as possible.

DIV_ROUND_CLOSEST(0,-2)=1

This also happens if I keep the sneaky code. The v3 code does not have this problem. I know it is a bit theoretic, but still there. Of course, I could simply ignore the divisor's sign entirely, assuming (and documenting) that negative divisors are just too odd to deal with. Commentss welcome ...

Quoted 5 lines

Also, this would be a great opportunity to document the macro's beahviour (I do go on). That would be a useful thing to do, given that we're now handling the four +/+, +/-, -/+, -/- cases and the behaviour for each case isn't terribly obvious.

Ok.

Guenter