public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Gabriel Paubert <paubert@iram.es>
To: Yoann Vandoorselaere <yoann@prelude-ids.org>
Cc: cpufreq@lists.arm.linux.org, cpufreq@www.linux.org.uk,
	linux-kernel@vger.kernel.org,
	"benh@kernel.crashing.org" <benh@kernel.crashing.org>
Subject: Re: [PATCH]: fix 32bits integer overflow in loops_per_jiffy calculation
Date: Thu, 22 Aug 2002 10:21:57 +0000	[thread overview]
Message-ID: <3D64BB45.4020907@iram.es> (raw)
In-Reply-To: 1030009840.15429.109.camel@alph

Yoann Vandoorselaere wrote:
> Hi,
> 
> The "low_part * mult" multiplication of the old function may overflow a
> 32bits integer...
> 
> This patch both fix the overflow issue (tested with frequencies up to
> 20Ghz), and make the result of the function lose less precision.
> 
> Please apply, 
> 
> 
> 
> ------------------------------------------------------------------------
> 
> --- linux-benh/kernel/cpufreq.c	2002-08-21 17:27:52.000000000 +0200
> +++ linux-yoann/kernel/cpufreq.c	2002-08-22 11:27:09.000000000 +0200
> @@ -78,14 +78,16 @@ static unsigned int             cpufreq_
>   */
>  static unsigned long scale(unsigned long old, u_int div, u_int mult)
>  {
> -	unsigned long low_part, high_part;
> -
> -	high_part  = old / div;
> -	low_part   = (old % div) / 100;
> -	high_part *= mult;
> -	low_part   = low_part * mult / div;
> -
> -	return high_part + low_part * 100;
> +        unsigned long val, carry = 0;
> +        
> +        mult /= 100;
> +        div  /= 100;

if(abs(div)<100) div=0;

> +        val = old / div * mult;

Now happily divide by zero.

> +
> +        carry = old % div;

Again.

> +        carry = carry * mult / div;

Again.

> +                
> +        return val + carry;
>  }
>  

And I can't see how it can be more precise, you divide the numerator and
denominator of the fraction by 100 and then proceed forgetting 
everything about the rest. Basically this looses about 7 bits of precision.

Now altogether I believe that such a function pertains to a per arch 
optimized routine.







  reply	other threads:[~2002-08-22 10:44 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-08-22  9:50 [PATCH]: fix 32bits integer overflow in loops_per_jiffy calculation Yoann Vandoorselaere
2002-08-22 10:21 ` Gabriel Paubert [this message]
2002-08-22 13:00   ` Benjamin Herrenschmidt
  -- strict thread matches above, loose matches on Subject: below --
2002-08-22 13:02 Benjamin Herrenschmidt
2002-08-22 12:12 ` Gabriel Paubert
2002-08-22 14:31   ` Benjamin Herrenschmidt
2002-08-22 15:23     ` Gabriel Paubert
2002-08-22 15:59       ` Yoann Vandoorselaere
2002-08-22 16:51       ` Dominik Brodowski
2002-08-22 19:35         ` Benjamin Herrenschmidt
2002-08-22 17:46           ` Dominik Brodowski
2002-08-22 18:02             ` Yoann Vandoorselaere
2002-08-22 20:00             ` Benjamin Herrenschmidt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3D64BB45.4020907@iram.es \
    --to=paubert@iram.es \
    --cc=benh@kernel.crashing.org \
    --cc=cpufreq@lists.arm.linux.org \
    --cc=cpufreq@www.linux.org.uk \
    --cc=linux-kernel@vger.kernel.org \
    --cc=yoann@prelude-ids.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox