On Saturday, December 24, 2011 9:20:38 PM Alexander E. Patrakov wrote:
> Andrew Eikum converted the buffer position calculation code from fixed
> point to floating point, because floating-point code is more readable.
> I agree with him.
FWIW, I'm not sure using floating point for the position index is that good of
an idea since it needs to be converted to integer all the time. Float-to-int
conversions are very slow because it involves reading the FPU control word,
setting a new control word with the proper rounding mode (which takes several
cycles), storing the float value as an integer, then setting the old control
word back (which again takes several cycles). It does this every time a float
gets converted to an int, and does something similar when you truncate/floor
the float value.
In addition, you have potential rounding errors caused by precision loss as
the exponent gets larger. Fixed-point doesn't have this issue since the
precision is fixed, and the result will always be consistent. E.g.,
val + step*3 == val + step + step + step
is true with fixed-point, but may not be true with floating-point and can
result in a value that's larger or smaller than you expect. In some
circumstances, it could even cause you to read past the end of the buffer.
If you're concerned about cleanliness, perhaps some simple inline functions
could be used to hide the bit-manipulation.