# 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.

## 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 on other sites
there is a fast cast with SIMD (float to int) with 2 instructions, there should be also a double floor to int.

##### Share on other sites
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 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 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 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 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 on other sites
Smart arse answer here -- 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

##### 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).

1. 1
2. 2
Rutin
17
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633702
• Total Posts
3013448
×