linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Trent Piepho <tpiepho@gmail.com>
To: Andy Shevchenko <andriy.shevchenko@intel.com>
Cc: Daniel Latypov <dlatypov@google.com>,
	linux-kernel@vger.kernel.org, andy@kernel.org,
	Andrew Morton <akpm@linux-foundation.org>,
	Oskar Schirmer <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:17:48 -0700	[thread overview]
Message-ID: <CA+7tXiiogw+bWCj2=QiRBc+sp01dUh1j_mfLJC19CB6Wch0nuQ@mail.gmail.com> (raw)
In-Reply-To: <YKuFPeH0sIFqrBt6@smile.fi.intel.com>

On Mon, May 24, 2021 at 3:51 AM Andy Shevchenko
<andriy.shevchenko@intel.com> wrote:
> On Sat, May 22, 2021 at 05:18:06PM -0700, Trent Piepho wrote:
>
> 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?

Is there a bug report on a tracker?  I just got the email from Yigua.

> > +                     /* 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

I started that, but it had no advantages and some disadvantages.

Basically, there are three cases: too large, too small & closest to
zero, too small & closest to non-zero.  This code can handle those
three cases by adding three branches, if(d1), if(n1), and if(!d1).
The truth values we need already exist at this point the algorithm.

If it's at the start, then there still needs to be the three branches
for each case.  But the values to test must be calculated too.

What's more, it's possible that the value is exactly representable in
the allowed range.  That's actual appears to be the most common use
case, reducing a fraction to lowest terms (*).  By putting the tests
in the "terminate because of limits" case, they don't need to happen
when "terminate because exact value find" is the result. If the check
was first, then it would always happen, even if it wouldn't have been
necessary.

And the time it took to find this bug shows us that out of bounds
inputs are not a common case, so putting that on the hot path by
checking it first at the expense of the reducing to lowest terms path
doesn't make sense.

(*)  One could write a reduce to lowest terms function with an easier
interface.  It could be a trivial one expression wrapper around
rational_best_approximation().  It could also be a simpler function,
but I think it would still perform the exact same sequence of
divisions and moduli, so it wouldn't really make any difference.

  parent reply	other threads:[~2021-05-24 20:18 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
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 [this message]
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='CA+7tXiiogw+bWCj2=QiRBc+sp01dUh1j_mfLJC19CB6Wch0nuQ@mail.gmail.com' \
    --to=tpiepho@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=andriy.shevchenko@intel.com \
    --cc=andy@kernel.org \
    --cc=dlatypov@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=oskar@scara.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).