linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1 1/1] clk: fractional-divider: eliminate while-loop
@ 2015-03-19 18:54 Andy Shevchenko
  2015-03-20  6:34 ` Stephen Boyd
  0 siblings, 1 reply; 3+ messages in thread
From: Andy Shevchenko @ 2015-03-19 18:54 UTC (permalink / raw)
  To: linux-kernel, Stephen Boyd, Heikki Krogerus; +Cc: 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.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/clk/clk-fractional-divider.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/drivers/clk/clk-fractional-divider.c b/drivers/clk/clk-fractional-divider.c
index 6aa72d9..4c0541b 100644
--- a/drivers/clk/clk-fractional-divider.c
+++ b/drivers/clk/clk-fractional-divider.c
@@ -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;
 }
-- 
2.1.4


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v1 1/1] clk: fractional-divider: eliminate while-loop
  2015-03-19 18:54 [PATCH v1 1/1] clk: fractional-divider: eliminate while-loop Andy Shevchenko
@ 2015-03-20  6:34 ` Stephen Boyd
  2015-03-20 13:03   ` Andy Shevchenko
  0 siblings, 1 reply; 3+ messages in thread
From: Stephen Boyd @ 2015-03-20  6:34 UTC (permalink / raw)
  To: Andy Shevchenko; +Cc: linux-kernel, Heikki Krogerus

On 03/19, Andy Shevchenko wrote:
> 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.
> 
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> ---
>  drivers/clk/clk-fractional-divider.c | 13 ++++++-------
>  1 file changed, 6 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/clk/clk-fractional-divider.c b/drivers/clk/clk-fractional-divider.c
> index 6aa72d9..4c0541b 100644
> --- a/drivers/clk/clk-fractional-divider.c
> +++ b/drivers/clk/clk-fractional-divider.c
> @@ -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;
>  }

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

-- 
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v1 1/1] clk: fractional-divider: eliminate while-loop
  2015-03-20  6:34 ` Stephen Boyd
@ 2015-03-20 13:03   ` Andy Shevchenko
  0 siblings, 0 replies; 3+ messages in thread
From: Andy Shevchenko @ 2015-03-20 13:03 UTC (permalink / raw)
  To: Stephen Boyd; +Cc: linux-kernel, Heikki Krogerus

On Thu, 2015-03-19 at 23:34 -0700, Stephen Boyd wrote:
> On 03/19, Andy Shevchenko wrote:
> > 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.
> > 
> > Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> > ---
> >  drivers/clk/clk-fractional-divider.c | 13 ++++++-------
> >  1 file changed, 6 insertions(+), 7 deletions(-)
> > 
> > diff --git a/drivers/clk/clk-fractional-divider.c b/drivers/clk/clk-fractional-divider.c
> > index 6aa72d9..4c0541b 100644
> > --- a/drivers/clk/clk-fractional-divider.c
> > +++ b/drivers/clk/clk-fractional-divider.c
> > @@ -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;
> >  }
> 
> 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.

-- 
Andy Shevchenko <andriy.shevchenko@intel.com>
Intel Finland Oy


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-03-20 13:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-03-19 18:54 [PATCH v1 1/1] clk: fractional-divider: eliminate while-loop Andy Shevchenko
2015-03-20  6:34 ` Stephen Boyd
2015-03-20 13:03   ` Andy Shevchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).