linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: Trent Piepho <tpiepho@gmail.com>, Daniel Latypov <dlatypov@google.com>
Cc: linux-kernel@vger.kernel.org, andy@kernel.org,
	akpm@linux-foundation.org, oskar@scara.com,
	Yiyuan Guo <yguoaz@gmail.com>
Subject: Re: [PATCH] lib/math/rational.c: Fix divide by zero
Date: Mon, 24 May 2021 13:51:41 +0300	[thread overview]
Message-ID: <YKuFPeH0sIFqrBt6@smile.fi.intel.com> (raw)
In-Reply-To: <20210523001806.3927609-1-tpiepho@gmail.com>

On Sat, May 22, 2021 at 05:18:06PM -0700, Trent Piepho wrote:

Thanks for the fix! My comments below.

> If the input is out of the range of the allowed values, either larger
> than the largest value or closer to zero than the smallest non-zero
> allowed value, then a division by zero would occur.
> 
> In the case of input too large, the division by zero will occur on the
> first iteration.  The best result (largest allowed value) will be found
> by always choosing the semi-convergent and excluding the denominator
> based limit when finding it.
> 
> In the case of the input too small, the division by zero will occur on
> the second iteration.  The numerator based semi-convergent should not be
> calculated to avoid the division by zero.  But the semi-convergent vs
> previous convergent test is still needed, which effectively chooses
> between 0 (the previous convergent) vs the smallest allowed fraction
> (best semi-convergent) as the result.

This misses the test cases (*). Please, develop them with Daniel.

*) We usually don't accept changes in the generic libraries without test cases.

Fixes tag?

> Reported-by: Yiyuan Guo <yguoaz@gmail.com>
> Signed-off-by: Trent Piepho <tpiepho@gmail.com>

...

> +			/* This tests if the semi-convergent is closer than the previous
> +			 * convergent.  If d1 is zero there is no previous convergent as this
> +			 * is the 1st iteration, so always choose the semi-convergent.
>  			 */
> -			if (2u * t > a || (2u * t == a && d0 * dp > d1 * d)) {
> +			if (!d1 || 2u * t > a || (2u * t == a && d0 * dp > d1 * d)) {
>  				n1 = n0 + t * n1;
>  				d1 = d0 + t * d1;
>  			}

I think that refactoring may lead us to check first iteration before even going
into the loop. But it's another story and we may do it later (the algo uses
heavy division anyway, so couple of additional branches probably won't affect
performance too much).

-- 
With Best Regards,
Andy Shevchenko



  reply	other threads:[~2021-05-24 10:51 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-23  0:18 [PATCH] lib/math/rational.c: Fix divide by zero Trent Piepho
2021-05-24 10:51 ` Andy Shevchenko [this message]
2021-05-24 16:55   ` Daniel Latypov
2021-05-24 22:04     ` Randy Dunlap
2021-05-24 22:56       ` Daniel Latypov
2021-05-24 23:30         ` Randy Dunlap
2021-05-24 23:38           ` Daniel Latypov
2021-05-25  0:42             ` David Gow
2021-05-25  1:49               ` Randy Dunlap
2021-05-25  1:57                 ` David Gow
2021-05-25  5:08                 ` Trent Piepho
2021-05-24 20:17   ` Trent Piepho
2021-05-24 20:35     ` Daniel Latypov
2021-05-25  9:02     ` Andy Shevchenko
2021-05-25  9:21       ` Trent Piepho
2021-05-25 12:03         ` Andy Shevchenko
2021-05-25 17:10       ` Daniel Latypov

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=YKuFPeH0sIFqrBt6@smile.fi.intel.com \
    --to=andriy.shevchenko@intel.com \
    --cc=akpm@linux-foundation.org \
    --cc=andy@kernel.org \
    --cc=dlatypov@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=oskar@scara.com \
    --cc=tpiepho@gmail.com \
    --cc=yguoaz@gmail.com \
    /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;
as well as URLs for NNTP newsgroup(s).