Casting and Optimization Flags

John Love-Jensen eljay@adobe.com
Wed Dec 11 05:24:00 GMT 2002


Hi Colin,

Floating point numbers are not exact numbers, and slight rounding and/or
truncation issues should be expected.

There are much better (i.e., exacting) algorithms to calculate your desired
value width.  On modern architectures with 1-cycle FPU log10 and division,
the performance will be similar.  On slightly older architectures, these
algorithms are more efficient.

Check out this excellent Stanford page of Bit Twiddling Hacks, kudos to Sean
Eron Anderson.  <http://graphics.stanford.edu/~seander/bithacks.html>

In particular, the "Find the log base 2 of an N-bit integer in O(lg(N))
operations".  Or perhaps the "Round up to the next highest power of 2" is
more suited to your needs.

Note:  I'd avoid the "Find integer log base 2 of a 32-bit IEEE float", since
the algorithm presumes (and relies upon) the IEEE layout in memory.  But
that's because I like to write platform agnostic C++ code.

--Eljay



More information about the Gcc-help mailing list