[llvm-commits] patch: chrec * chrec = chrec.

Nick Lewycky nlewycky at google.com
Mon Oct 3 14:10:45 PDT 2011


On 3 October 2011 13:51, Joerg Sonnenberger <joerg at britannica.bec.de> wrote:

> On Sun, Oct 02, 2011 at 04:50:59PM -0700, Nick Lewycky wrote:
> > Index: lib/Analysis/ScalarEvolution.cpp
> > ===================================================================
> > --- lib/Analysis/ScalarEvolution.cpp  (revision 140963)
> > +++ lib/Analysis/ScalarEvolution.cpp  (working copy)
> > @@ -1812,6 +1812,29 @@
> >    return S;
> >  }
> >
> > +static unsigned umul_ov(unsigned i, unsigned j, bool &Overflow) {
> > +  unsigned k = i*j;
> > +  if (j > 1 && k / j != i) Overflow = true;
> > +  return k;
> > +}
> > +
> > +static unsigned Choose(unsigned n, unsigned k, bool &Overflow) {
> > +  // This method can overflow internally even if the final result would
> fit.
> > +
> > +  if (n == 0) return 1;
> > +  if (k >= n) return 1;
> > +
> > +  if (k > n/2)
> > +    k = n-k;
> > +
> > +  unsigned r = 1;
> > +  for (unsigned i = 1; i <= k; ++i) {
> > +    r = umul_ov(r, n-(i-1), Overflow);
> > +    r /= i;
> > +  }
> > +  return r;
> > +}
> > +
>
> What exactly is the desired behavior in case of overflow?
> E.g. do you want to get C(n,k) % (UINTMAX + 1)? In that case, your
> formula doesn't compute correct results.
>

In overflow the return value is garbage and Overflow is set. This method
deserves a comment.

Nick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20111003/a9cf75e4/attachment.html>


More information about the llvm-commits mailing list