### Float -> int speedup conversion

The basic & regular conversion method (int) is a good one, but also it may cause data lost (always truncates the float or double data.)
For example :
 ``1234`` ``````float f = 7.23; int i = int(f); //= 7 (Correct !!!) float f2 = 7.86; int i2 = int(f2);//= 7 - !?!?!?!? ``````

And solutions : The round(), floor() are very powerful functions, but it can only help you when you really want it to be rounded down or up....

I learnt the assembly language and I've found out the solution. It only uses two assembly code commands so we called it 'ultra-super-fast float to int conversion'. It automatically rounds the value up or down and most likely it should faster and more convenient than round(), floor()... ???
Now, the code is :
 ``12345`` ``````inline int floattoint(float f){int nResult; _asm fld f; _asm fistp nResult; return nResult:} // And double? - Simply change the float parameter slot -> double ``````

And another similar test :
 ``12345`` ``````//Copy the code and test float f = 9.26; int i = floattoint(f); //= 9 (Correct !) float f2 = 9.89; int i2 = floattoint(f2); //= 10 (Very good !!!) ``````

Also I found out a simple but powerful method to test & compare the code performance. It records the time elapsed after loop(s). And here, the testing algorithm :
 ``12345678910`` ``````//#include //int nInt; int nStart = clock(); //Try doing a billion loop for (long i = 0; i < 1000000000; i ++ ) {//Put your test code here nInt = floattoint(10.94); } int nEnd = clock(); printf("Time elapsed : %.3f - Result : %d \n", (nEnd - nStart) / 1000.0f, nInt);``````

You can record the peforming time of these above conversions. Or any your own code (If you doubt)...

And finally, a small note : Make sure the 'inline' word is always kept otherwise it will negative the result and kill the performance gain... :P

What do you think?
Last edited on
Interesting, but doesn't manually using floating point assembly disable some more global optimization the compiler could have performed with those registers? Did you benchmark the gain in a complete program? And won't your benchmark program perform the operation only once due to compiler optimization?

I personally use
 ``123`` ``````inline int32_t xxxxxxxx_round(const double val){ return (int32_t)(val+0.5); }``````
 http://relishgames.com/forum/index.php?p=/discussion/2883/ultra-fast-floatdouble-int-conversions-follow-up/p1

@OP:

If you read all of this, you will see that you are offering bad advice again. This method is not quicker than normal. To test it they had to turn off optimisation, because the compiler optimised the for loop with a null statement. Even then it was half the speed.

I doubt you have "learnt" assembler - more likely you have copied this into your post to provoke comment.

Disclaimer: I have sometimes offered advice that wasn't quite right - usually in the process of helping someone - not in behaving like a troll.

I am going to ignore your attention seeking posts from now on. Others can make up their own mind.
Last edited on
If you need, try forcing the optimization off by adding the 'volatile' word before testing variable (Speed testing algorithm) (In this example, 'nInt' is choosed and it also is a main target)
Last edited on
I'd say it's not correct. Look at this:

http://musicdsp.org/showone.php?id=170

And even that seems not to take negative values into account.
Topic archived. No new replies allowed.