[coreboot-gerrit] New patch to review for coreboot: Add DIV_ROUND_CLOSEST

Patrick Georgi (pgeorgi@google.com) gerrit at coreboot.org
Thu Sep 22 18:33:20 CEST 2016


Patrick Georgi (pgeorgi at google.com) just uploaded a new patch set to gerrit, which you can find at https://review.coreboot.org/16705

-gerrit

commit 6c0ac553762dca0c1412d6a75785bd96a0b09661
Author: Simon Glass <sjg at chromium.org>
Date:   Wed Sep 7 16:01:55 2016 -0600

    Add DIV_ROUND_CLOSEST
    
    Bring in this useful function from Linux 4.7.
    
    BUG=chrome-os-partner:56556
    BRANCH=none
    TEST=build on gru
    
    Change-Id: I1577309e3e9ddea794e7e77354d50e582b70a43c
    Signed-off-by: Patrick Georgi <pgeorgi at chromium.org>
    Original-Commit-Id: 99f22182c2be0a6d02204b40f8f792f36d31c745
    Original-Change-Id: I37617e35b4784d6cdc51e6910aa91f566caf971d
    Original-Signed-off-by: Simon Glass <sjg at chromium.org>
    Original-Reviewed-on: https://chromium-review.googlesource.com/382320
    Original-Reviewed-by: Julius Werner <jwerner at chromium.org>
---
 src/include/stdlib.h | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/src/include/stdlib.h b/src/include/stdlib.h
index d6e7faf..6200f1c 100644
--- a/src/include/stdlib.h
+++ b/src/include/stdlib.h
@@ -16,6 +16,22 @@ static inline unsigned long div_round_up(unsigned int n, unsigned int d)
 {
 	return (n + d - 1) / d;
 }
+
+/*
+ * 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));	\
+}							\
+)
 #endif
 
 



More information about the coreboot-gerrit mailing list