summaryrefslogtreecommitdiff
path: root/include/common.h
diff options
context:
space:
mode:
authorTom Rini <[email protected]>2014-02-20 12:18:59 -0500
committerTom Rini <[email protected]>2014-02-20 12:18:59 -0500
commit6853e6aa77b388998a5368b400aee3ae7776b1c2 (patch)
treee91dfbc15a79c452fa50d8896d5e8eb10aa1771d /include/common.h
parent130fbeb1c51f19a2b81c4e27d23da735b5b235d4 (diff)
parent3e11350255d9c5d4bd03c2a65769da84c05d3294 (diff)
Merge branch 'master' of git://git.denx.de/u-boot-arm
Diffstat (limited to 'include/common.h')
-rw-r--r--include/common.h16
1 files changed, 16 insertions, 0 deletions
diff --git a/include/common.h b/include/common.h
index 08b294cfd91..033b5d946d1 100644
--- a/include/common.h
+++ b/include/common.h
@@ -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))