how to round an odd integer towards the nearest power of two
Add one to or subtract one from an odd integer such that the even result
is closer to the nearest power of two.
if ( ??? ) x += 1; else x -= 1;// x > 2 and odd
For example, 25 through 47 round towards 32, adding one to 25 through 31
and subtracting one from 33 through 47. 23 rounds down towards 16 to 22
and 49 rounds up towards 64 to 50.
Is there a way to do this without finding the specific power of two that
is being rounded towards. I know how to use a logarithm or count bits to
get the specific power of two.
My specific use case for this is in splitting odd sized inputs to
karatsuba multiplication.
No comments:
Post a Comment