Jump to content
  • Advertisement
Sign in to follow this  
Malc0lm

fastest way to do (int) floor(double) nowadays ?

This topic is 2465 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:
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)

Share this post


Link to post
Share on other sites
Advertisement
Unless I was doing lots of these inside a really tight deeply nested loop (in which case I'd be asking serious questions about my design), and unless I knew it was a measurable bottleneck, I'd generally just trust the compiler to do the right thing here.

Share this post


Link to post
Share on other sites
Just assign the double to an int? The floating point value is instantly truncated without casts or (formal) conversions.


inline int MyFloor(double d) {
int f = d;
return f;
}

Share this post


Link to post
Share on other sites

Just assign the double to an int? The floating point value is instantly truncated without casts or conversions.


inline int MyFloor(double d) {
int f = d;
return f;
}


If your compiler isn't giving you a warning about that, then you're using your compiler wrong. Casting definitely should be used.

Although I will ask (in partial agreement with forb's comment about truncation, though there should be a cast), is there a reason for flooring before casting?

Share this post


Link to post
Share on other sites
Casting a floating point value to an integer rounds towards zero, but the floor-function rounds towards negative infinity. Thus, simply casting the value to an integer does not round the value in the same way as the OP requested.

Share this post


Link to post
Share on other sites

Casting a floating point value to an integer rounds towards zero, but the floor-function rounds towards negative infinity. Thus, simply casting the value to an integer does not round the value in the same way as the OP requested.

Ah, I see. Subtle difference. Thanks.

Share this post


Link to post
Share on other sites
Smart arse answer here tongue.png -- but the fastest way is probably to keep the result in floating-point and avoid mixing integer and float math together, especially on PPC CPUs wink.png

Share this post


Link to post
Share on other sites
I agree with Hodgman. The biggest problem here is the LHS caused by moving the value from the FPU to integer registers. For the biggest bang for your buck, leave the value in floating-point or move the math to use SIMD or some other registers that can store both floats and ints (if your hardware supports it).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!