]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - include/common.h
Merge branch 'u-boot/master' into 'u-boot-arm/master'
[karo-tx-uboot.git] / include / common.h
index 08b294cfd9124f9bcac11e7fda85163fe60c1a2a..033b5d946d11b76040a37bf9db13ef16df129ba9 100644 (file)
@@ -971,6 +971,22 @@ static inline phys_addr_t map_to_sysmem(const void *ptr)
 #define DIV_ROUND_UP(n,d)      (((n) + (d) - 1) / (d))
 #define roundup(x, y)          ((((x) + ((y) - 1)) / (y)) * (y))
 
+/*
+ * Divide positive or negative dividend by positive divisor and round
+ * to closest integer. Result is undefined for negative divisors and
+ * for negative dividends if the divisor variable type is unsigned.
+ */
+#define DIV_ROUND_CLOSEST(x, divisor)(                 \
+{                                                      \
+       typeof(x) __x = x;                              \
+       typeof(divisor) __d = divisor;                  \
+       (((typeof(x))-1) > 0 ||                         \
+        ((typeof(divisor))-1) > 0 || (__x) > 0) ?      \
+               (((__x) + ((__d) / 2)) / (__d)) :       \
+               (((__x) - ((__d) / 2)) / (__d));        \
+}                                                      \
+)
+
 #define ALIGN(x,a)             __ALIGN_MASK((x),(typeof(x))(a)-1)
 #define __ALIGN_MASK(x,mask)   (((x)+(mask))&~(mask))