Original Post
Standard C++ functions to convert a float or double to int, or to take the integer floor of a float or double, have always been very slow.
I've seen various alternatives in the past, but I'm wondering: what is generally considered the best (as in fastest) solution these days?
This one (using standard functions) obviously sucks:
When limiting to x86, I guess some inline asm FISTP approach might be faster, although I guess that depends on the current rounding mode (and changing + restoring that kinda defeats the purpose).
I've also seen several methods using 'magic numbers': interpreting the binary float representation as integer (i.e. no conversion takes place), adding some magic value, and you have a valid integer. Probably limited precision, but it seemed to do the job.
What should I use? (besides optimizing my entire algorithm in the first place, and only consider this kind of optimizations at the end - I know)
I've seen various alternatives in the past, but I'm wondering: what is generally considered the best (as in fastest) solution these days?
This one (using standard functions) obviously sucks:
inline int MyFloor( double x )
{
return static_cast<int> (floor(x));
}When limiting to x86, I guess some inline asm FISTP approach might be faster, although I guess that depends on the current rounding mode (and changing + restoring that kinda defeats the purpose).
I've also seen several methods using 'magic numbers': interpreting the binary float representation as integer (i.e. no conversion takes place), adding some magic value, and you have a valid integer. Probably limited precision, but it seemed to do the job.
What should I use? (besides optimizing my entire algorithm in the first place, and only consider this kind of optimizations at the end - I know)