Lookup table and float to int conversion and performance

Cyrille Berger 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 [1]. 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 [2]. And 
using a lookup table is a possible solution see [3] and [4] 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 [2] 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 
not guaranteed.

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 [1].

[1] http://bitbucket.org/cyrille/lut
[2] http://www.mega-nerd.com/FPcast/
[3] http://www.gimpusers.com/forums/gegl-developer/227-speeding-up-GEGL-
[4] http://mysite.verizon.net/spitzak/conversion/sketches_0265.pdf
Cyrille Berger

More information about the kimageshop mailing list