[PATCH 02/14] Add macro for correct rounding in integer devisions

Juergen Beisert jbe at pengutronix.de
Mon Dec 13 05:53:25 EST 2010


Just stolen from Linux.

Signed-off-by: Juergen Beisert <jbe at pengutronix.de>
---
 drivers/mci/stm378x.c |    2 +-
 include/common.h      |    7 +++++++
 2 files changed, 8 insertions(+), 1 deletions(-)

diff --git a/drivers/mci/stm378x.c b/drivers/mci/stm378x.c
index 039fbb9..a2802af 100644
--- a/drivers/mci/stm378x.c
+++ b/drivers/mci/stm378x.c
@@ -484,7 +484,7 @@ static unsigned setup_clock_speed(struct device_d *hw_dev, unsigned nc)
 	ssp = imx_get_sspclk(0) * 1000;
 
 	for (div = 2; div < 255; div += 2) {
-		rate = (((ssp + (nc >> 1) ) / nc) + (div >> 1)) / div;
+		rate = DIV_ROUND_CLOSEST(DIV_ROUND_CLOSEST(ssp, nc), div);
 		if (rate <= 0x100)
 			break;
 	}
diff --git a/include/common.h b/include/common.h
index be83a31..1bea10c 100644
--- a/include/common.h
+++ b/include/common.h
@@ -224,4 +224,11 @@ extern const char version_string[];
 
 #define DIV_ROUND_UP(n,d)	(((n) + (d) - 1) / (d))
 
+#define DIV_ROUND_CLOSEST(x, divisor)(			\
+{							\
+	typeof(divisor) __divisor = divisor;		\
+	(((x) + ((__divisor) / 2)) / (__divisor));	\
+}							\
+)
+
 #endif	/* __COMMON_H_ */
-- 
1.7.2.3




More information about the barebox mailing list