marc.glisse at inria.fr
Mon Feb 11 02:03:14 CET 2013
On Mon, 11 Feb 2013, Vincent Lefevre wrote:
> On 2013-02-11 00:24:12 +0100, Marc Glisse wrote:
>> You should use i*=2 instead of i<<=1 to maximize optimization
> Why? I would say that a good compiler has more chances to optimize
> i<<=1 as the only difference between these two forms is that in
> the latter one, i is required to be nonnegative. Thus i<<=1 gives
> more information to the compiler.
Overflow of * for int is undefined behavior, so the compiler assumes that
a product of non-negative numbers is non-negative. On the other hand, for
<<, looking at the gcc doc:
"GCC does not use the latitude given in C99 only to treat certain aspects
of signed `<<' as undefined"
And C++ recently specified that when the result of a signed << would fit
in the unsigned type, then it is computed unsigned and cast to signed, so
it can be negative (I don't like this change).
More information about the gmp-devel