Skip to contentSkip to conversation

clk: fractional-divider: eliminate while-loop

PATCH In review Andy Shevchenko 2 participants 3 messages Mar 19, 2015 – Mar 20, 2015 Common clk

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 clk: fractional-divider: eliminate while-loop

We may do a calculation of the rounded rate based on (*prate / div) value in case it is not 0. It's as much higher as power of two of nearest value to (*prate / div / maxn). Thus, the patch replaces while-loop by direct calculations.

While here, fix off-by-one error. maxn is the maximum value that can be hold by a register which means all ones in it.

---
 drivers/clk/clk-fractional-divider.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
drivers/clk/clk-fractional-divider.c +6 -7
index 6aa72d9..4c0541b 100644
@@ -14,6 +14,7 @@
 #include <linux/device.h>
 #include <linux/slab.h>
 #include <linux/gcd.h>
+#include <linux/log2.h>
 
 #define to_clk_fd(_hw) container_of(_hw, struct clk_fractional_divider, hw)
 
@@ -49,18 +50,16 @@ static long clk_fd_round_rate(struct clk_hw *hw, unsigned long rate,
 			      unsigned long *prate)
 {
 	struct clk_fractional_divider *fd = to_clk_fd(hw);
-	unsigned maxn = (fd->nmask >> fd->nshift) + 1;
-	unsigned div;
+	unsigned maxn = fd->nmask >> fd->nshift;
+	unsigned long div, n;
 
 	if (!rate || rate >= *prate)
 		return *prate;
 
 	div = gcd(*prate, rate);
-
-	while ((*prate / div) > maxn) {
-		div <<= 1;
-		rate <<= 1;
-	}
+	n = *prate / div;
+	if (n > maxn)
+		return rate * roundup_pow_of_two(n / maxn);
 
 	return rate;
 }
Signature · 1 line
2.1.4
#1 lore raw
Quoted 39 lines · Andy Shevchenko

We may do a calculation of the rounded rate based on (*prate / div) value in case it is not 0. It's as much higher as power of two of nearest value to (*prate / div / maxn). Thus, the patch replaces while-loop by direct calculations.

While here, fix off-by-one error. maxn is the maximum value that can be hold by a register which means all ones in it.

---
 drivers/clk/clk-fractional-divider.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
drivers/clk/clk-fractional-divider.c +3 -3
index 6aa72d9..4c0541b 100644
@@ -14,6 +14,7 @@
 #include <linux/device.h>
 #include <linux/slab.h>
 #include <linux/gcd.h>
+#include <linux/log2.h>
 
 #define to_clk_fd(_hw) container_of(_hw, struct clk_fractional_divider, hw)
 
@@ -49,18 +50,16 @@ static long clk_fd_round_rate(struct clk_hw *hw, unsigned long rate,
 			      unsigned long *prate)
 {
 	struct clk_fractional_divider *fd = to_clk_fd(hw);
-	unsigned maxn = (fd->nmask >> fd->nshift) + 1;
-	unsigned div;
+	unsigned maxn = fd->nmask >> fd->nshift;
+	unsigned long div, n;
 
 	if (!rate || rate >= *prate)
 		return *prate;
 
 	div = gcd(*prate, rate);
-

drivers/clk/clk-fractional-divider.c static long clk_fd_round_rate(struct clk_hw *hw, unsigned long rate, · line 65

Quoted 10 lines
drivers/clk/clk-fractional-divider.c +3 -4
-	while ((*prate / div) > maxn) {
-		div <<= 1;
-		rate <<= 1;
-	}
+	n = *prate / div;
+	if (n > maxn)
+		return rate * roundup_pow_of_two(n / maxn);
 
 	return rate;
 }

Is there any reason why this code isn't using
rational_best_approximation() and mult_frac()?

Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
#2 lore raw
Quoted 48 lines · Stephen Boyd
Quoted 47 lines · Andy Shevchenko

We may do a calculation of the rounded rate based on (*prate / div) value in case it is not 0. It's as much higher as power of two of nearest value to (*prate / div / maxn). Thus, the patch replaces while-loop by direct calculations.

While here, fix off-by-one error. maxn is the maximum value that can be hold by a register which means all ones in it.

---
 drivers/clk/clk-fractional-divider.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
drivers/clk/clk-fractional-divider.c +6 -7
index 6aa72d9..4c0541b 100644
@@ -14,6 +14,7 @@
 #include <linux/device.h>
 #include <linux/slab.h>
 #include <linux/gcd.h>
+#include <linux/log2.h>
 
 #define to_clk_fd(_hw) container_of(_hw, struct clk_fractional_divider, hw)
 
@@ -49,18 +50,16 @@ static long clk_fd_round_rate(struct clk_hw *hw, unsigned long rate,
 			      unsigned long *prate)
 {
 	struct clk_fractional_divider *fd = to_clk_fd(hw);
-	unsigned maxn = (fd->nmask >> fd->nshift) + 1;
-	unsigned div;
+	unsigned maxn = fd->nmask >> fd->nshift;
+	unsigned long div, n;
 
 	if (!rate || rate >= *prate)
 		return *prate;
 
 	div = gcd(*prate, rate);
-
-	while ((*prate / div) > maxn) {
-		div <<= 1;
-		rate <<= 1;
-	}
+	n = *prate / div;
+	if (n > maxn)
+		return rate * roundup_pow_of_two(n / maxn);
 
Quoted 5 lines

drivers/clk/clk-fractional-divider.c static long clk_fd_round_rate(struct clk_hw *hw, unsigned long rate, · line 65

Quoted 2 lines
drivers/clk/clk-fractional-divider.c +0 -0
 	return rate;
 }

Is there any reason why this code isn't using
rational_best_approximation() and mult_frac()?

For fist glance it should be faster that rational_best_approximation(), though less precise.

I could switch to them.

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy