Lookup table and float to int conversion and performance
cberger at cberger.net
Tue Apr 20 23:31:37 CEST 2010
Since pippin has been poking us on irc on using lookup table with floats, I
wrote a class that do exactly that, code is in . I did a few tests, and I
get a 4 times improvement on computation of sinus, and 2.5 times improvement
on computation of sqrt.
An other fact with floating point that have come to my attention recently is
that conversion to integer is painfully slow, for more details see . And
using a lookup table is a possible solution see  and  for what gegl
people did. Using the floating point lookup table, I noticed 19 times
improvement on float->uint8, and 1.22 times improvement on float->uint32. But
more interestingly for our case is the suggestion by  to use the 'lrintf'
function, which is part of the C99 standard, this function gives 44 times
improvement on float->uint8 and 2.5 times on float->uint32, of course it is
not totally a replacement for cast since it is a round function, but I think
in most of our uses, it is good enough. Also 'lrintf' is not available under
MSVC, so we will probably have to wrap it to be portable to Windows (most
likely in KoColorSpaceMaths).
It is worth to note that there is an even faster way, but it involves
assembly, and I am not sure the gain is worth the pain, not counting that it
is assuming that the FPU is in a good state, which is generally the case, but
The conclusion I take from this is that we need to be careful in conversion
from float to integer, check when it happen, and only do it when it is
absolutely needed. And for 2.3, I intend to make the color spaces use
'lrintf', and start using the lut .
More information about the kimageshop