Help stabilising mini-gmp

Marc Glisse marc.glisse at inria.fr
Fri Nov 25 16:28:13 UTC 2016


On Fri, 25 Nov 2016, Torbjörn Granlund wrote:

> Marc Glisse <marc.glisse at inria.fr> writes:
>
>  Fixed. It was unhappy about (-13) << 2. I am a bit surprised it
>  doesn't complain about (-13) >> 2 on the next line, we'll see if it
>  ever becomes an issue.
>
> This must be a compiler problem.  At least in C, shifting negative
> integers is undefined.  It is a pity that it is, but the standard say
> so.
>
> I am less sure about C++.  Some googling did not provide a definitive
> answer.
>
> But if left shift of negative integers is undefined, surely right shift
> is too!

Surprisingly, shifting negative numbers left is undefined, while
shifting them right it implementation-defined.


The value of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are zero-filled. If E1 has an unsigned type, the value of the result is E1 × 2^E2 , reduced modulo one more than the maximum value representable in the result type. Otherwise, if E1 has a signed type and non-negative value, and E1 × 2^E2 is representable in the corresponding unsigned type of the result type, then that value, converted to the result type, is the resulting value; otherwise, the behavior is undefined.

The value of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a non-negative value, the value of the result is the integral part of the quotient of E1/2^E2 . If E1 has a signed type and a negative value, the resulting value is implementation-defined.

-- 
Marc Glisse


More information about the gmp-devel mailing list