<div class="gmail_quote">On 3 October 2011 13:51, Joerg Sonnenberger <span dir="ltr"><<a href="mailto:joerg@britannica.bec.de">joerg@britannica.bec.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

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

</div>