Bugs or features?
per at karlstrom.se
Sat Feb 16 02:05:01 CET 2008
Torbjorn Granlund wrote:
> I am just wondering if this is by design or if this is a bug?
> The question if this is the intended functionality of the >>= operator
> for negative numbers. This differs from how it is defined for native
> types like int for example.
> This is a sticky issue.
> In the C standard, the behaviour of right shift of negative numbers is
> undefined. But in practice, almost every compiler defines it as
> rounding towards -oo. The one exception I am aware of is Cray's
> compilers for their vector systems. (They shift the numbers logically
> always, which is not very helpful.)
> I am inclined to changing gmpxx.h to make it follow the common
> behaviour, although that is not formally standardized.
> What do people think? I am mainly interested in the opinion of people
> that actually use gmpxx.h.
> (The change is trivial, just change the t to an f in mpz_tdiv_q_2exp
> in gmpxx.h.)
Being a hardware oriented person I vote for the changing it. Following
the principle of least surprise (at least from my point of view) an
arithmetic right shift does one simple operation. And that is to shift
bits in a register to the right and copy the sign bit to the top most
bits. That it is the same as a division by 2 is just a side effect of
what it does. In all assembly languages I know about an arithmetic right
shift is defined this way.
More information about the gmp-discuss