If you want to round a single-precision floating point value down to the next power of 2, you can do this (as long as the number is "normalized", which it always is unless it's infinity, NaN, or extremely small) by making all the significand bits zero. Something like this would probably work:
float truncateSgand(float value) {
return (float) ((uint32_t) value & 0xFF800000);
}
(You might have to worry about endianness.)
For a double-precision floating point, the equivalent code would be this. (I've never seen a 16-digit hexadecimal literal in C; I don't know if they're actually valid.)
double truncateSgand(double value) {
return (double) ((uint64_t) value & 0xFFF0000000000000;
}
I haven't tested either of these functions.
floor(float x)
function or alog_2(float x)
function that we can use? If you do then this isn't too hard (as per user SMF's answer). Otherwise this could be much harder. (also, what language are you using to write this program? this may be important for how people choose to answer.) $\endgroup$ – Mike Pierce May 21 '15 at 16:53