develooper Front page | perl.perl5.porters | Postings from February 2014

sv_grow and powers of two (or multiples of 256)

Thread Next
Father Chrysostomos
February 22, 2014 14:10
sv_grow and powers of two (or multiples of 256)
Message ID:
sv_grow has this comment:

     * If the new size is a big power of two, don't bother: we assume the
     * caller wanted a nice 2^N sized block and will be annoyed at getting
     * 2^N+1 */
    if (newlen & 0xff)

But 'newlen & 0xff' checks that it is not a multiple of 256, not that
it is not a power of 2, so the chances of skipping COW are greater
than they ought to be.  Is there any easy and efficient way to check
for powers of 2 in C?  The only way I can think of offhand is to loop
and shift, checking whether 'foo>>n == 1 && foo>>n<<n == foo'.  Is it
worth that?

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About