http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46186
--- Comment #19 from joseph at codesourcery dot com <joseph at codesourcery dot com> 2010-10-26 20:29:56 UTC --- On Tue, 26 Oct 2010, dominiq at lps dot ens.fr wrote: > --- Comment #13 from Dominique d'Humieres <dominiq at lps dot ens.fr> > 2010-10-26 16:36:05 UTC --- > > This multiplication transformation is incorrect if the loop wraps > > (unsigned always wraps; never overflows). > > I think this is wrong: wrapping is nothing but a modulo 2^n operation (n=64 > here) which "works" for additions and multiplications, so if there is > wrapping, > the result is sum=(b*(b-1)-a*(a-1))/2 modulo 2^n, i.e. correctly wrapped. It's a bit more complicated than that, in that you can't just compute (b*(b-1)-a*(a-1)) mod 2^n, then divide by 2, as that will lose the top bit. (I haven't checked exactly what the generated code is doing here.)