develooper Front page | perl.perl5.porters | Postings from June 2015

Re: Coverity finding: shift by negative

Thread Previous | Thread Next
From:
Zefram
Date:
June 23, 2015 13:55
Subject:
Re: Coverity finding: shift by negative
Message ID:
20150623135520.GM23010@fysh.org
Abigail wrote:
>Rotating, or shifting by "n module wordsize" seems like a bad thing
>to me -- that exposes the user to the size of the integer used
>internally.

That's already exposed by the overflow behaviour: left shifting by small
distances causes bits to drop off the high-order end of the word.

>   1) Promote the IV to an NV; that is, treat "<< $n" as "* 2 ** $n".

That's silly.  Should 3>>1 return 1.5?  And it still exposes parameters
of the NV format.  The only way to hide the word size is to use bignums
to simulate an infinitely large integer word.  Perl's native arithmetic
isn't otherwise bignumish, so that would be a bizarre exception.

>   3) Do what we now: shift by module wordbits.

No, what we do now is to invoke undefined behaviour.  In practice
this turns out to be the native behaviour of the CPU's variable shift
instructions, for which modulo behaviour is a popular (cheap) option.

-zefram

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About