summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2012-09-06 20:39:47 +0400
committerLinus Torvalds <torvalds@linux-foundation.org>2012-09-06 20:39:47 +0400
commitc7c6bf1e8c72b94d73756d5dc4f7a6d7c4d1e290 (patch)
tree05a749ebde9f76985f2f6ded042cf42d0a8fe588 /include
parentbd12ce8cc23a5ab7eafb1edb3f2cd311d2b89e7e (diff)
parentb6d86d3d6d6e4c9b588d81615c81b5a8292b62ed (diff)
downloadlinux-c7c6bf1e8c72b94d73756d5dc4f7a6d7c4d1e290.tar.xz
Merge tag 'hwmon-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/groeck/linux-staging
Pull a hwmon fix from Guenter Roeck: "One patch, fixing DIV_ROUND_CLOSEST to support negative dividends. While the changes are not in the drivers/hwmon directory, the problem primarily affects hwmon drivers, and it makes sense to push the patch through the hwmon tree." * tag 'hwmon-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/groeck/linux-staging: linux/kernel.h: Fix DIV_ROUND_CLOSEST to support negative dividends
Diffstat (limited to 'include')
-rw-r--r--include/linux/kernel.h12
1 files changed, 10 insertions, 2 deletions
diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 604382143bcf..594b419b7d20 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -82,10 +82,18 @@
__x - (__x % (y)); \
} \
)
+
+/*
+ * Divide positive or negative dividend by positive divisor and round
+ * to closest integer. Result is undefined for negative divisors.
+ */
#define DIV_ROUND_CLOSEST(x, divisor)( \
{ \
- typeof(divisor) __divisor = divisor; \
- (((x) + ((__divisor) / 2)) / (__divisor)); \
+ typeof(x) __x = x; \
+ typeof(divisor) __d = divisor; \
+ (((typeof(x))-1) >= 0 || (__x) >= 0) ? \
+ (((__x) + ((__d) / 2)) / (__d)) : \
+ (((__x) - ((__d) / 2)) / (__d)); \
} \
)