[U-Boot] [PATCH 1/8] common: Provide DIV_ROUND_CLOSEST macro

Michal Simek michal.simek at xilinx.com
Wed Jan 22 11:50:53 CET 2014


From: Soren Brinkmann <soren.brinkmann at xilinx.com>

Provide the DIV_ROUND_CLOSEST macro - taken from the Linux kernel - in
common.h

Signed-off-by: Soren Brinkmann <soren.brinkmann at xilinx.com>
Signed-off-by: Michal Simek <michal.simek at xilinx.com>
---

 include/common.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/include/common.h b/include/common.h
index d49c514..ae45836 100644
--- a/include/common.h
+++ b/include/common.h
@@ -964,6 +964,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))

--
1.8.2.3

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: not available
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20140122/3a71f1ce/attachment.pgp>


More information about the U-Boot mailing list