This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Casting and Optimization Flags
- From: John Love-Jensen <eljay at adobe dot com>
- To: Colin Law <c dot law at elec dot gla dot ac dot uk>, <gcc-help at gcc dot gnu dot org>
- Date: Wed, 11 Dec 2002 07:22:08 -0600
- Subject: Re: Casting and Optimization Flags
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