[llvm-commits] patch: chrec * chrec = chrec.
Joerg Sonnenberger
joerg at britannica.bec.de
Mon Oct 3 13:51:57 PDT 2011
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.
Joerg
More information about the llvm-commits
mailing list