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

sv_grow and powers of two (or multiples of 256)

Thread Next
From:
Father Chrysostomos
Date:
February 22, 2014 14:10
Subject:
sv_grow and powers of two (or multiples of 256)
Message ID:
20140222141013.18269.qmail@lists-nntp.develooper.com
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)
        newlen++;

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


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