I converted it into a simple routine:
/** * Function for rounding a number up to the next power of two. * n - an integer number to be rounded * returns - n rounded up to the next power of two*/int roundUpToPow2(int n){ int e; frexp((float)n-1,&e); return (int)pow(2,e);}
it requires the inclusion of math.h
and the basic idea of how it works, is you use frexp to get the exponent of the incomming number, then you do 2 to the e'th (exponent) and it gives you back the proper pow2 number, quite handy =)
you put in 255, it gives you 256
you put in 257, it gives you 512
you put in 256, it gives you 256
Edit:
Just ran some speed tests, (so that I'd know if I'd need to steal your version) and it looks like the version I've been using is just a touch faster. Over ten million iterations, it's about half a second faster. Not worth much, just thought I'd share.