Ways preventing floating point value conversion to int

Started by
3 comments, last by Craazer 21 years, 1 month ago
Hi I desperedly need help whit optimizing this... Consider following situation where I have floating number and I need convert it to integer and it needs to be really fast. So the queston is how do you optimize this code:
      
// way 1 floating point

float fn = 1.4;
int x;
f = fn; // terrible slow conversion

  
// So I tryed to optimize by using fixed point math:

// way 2 fixed point      

int fp_fn = 1.4*100;
int x;
f = fn/100; // still slow

      
This suprised me today becose I didnt know that divining is equally slow as float conversion (it seemed to be equally slow) And lastly the third way. Using fixed point math whit << and >> operator but that gives unaccurate results. So what is left? How do I optimize that code? [edited by - Craazer on March 2, 2003 10:18:56 AM]
Advertisement
dividing is slow yeah..

when converting from float to fixed point, you do this:

fixvalue = int(floatvalue*65536.0f); //for 16.16 fixed point
and then intvalue = fixvalue>>16;

but it looks like in your case you just try to convert a float to an integer.. which is a quite slow operation. try adding -Qifist to your compiler command line in Settings for your project (assuming MSVC here), it will enable a faster convertor.
quote:Original post by Anonymous Poster
dividing is slow yeah..

when converting from float to fixed point, you do this:

fixvalue = int(floatvalue*65536.0f); //for 16.16 fixed point
and then intvalue = fixvalue>>16;

Hmmm so doin it that way (using >>16) you dont lose any numbers?
quote:
but it looks like in your case you just try to convert a float to an integer.. which is a quite slow operation.

Actualy I try to avoid conversion
quote:
try adding -Qifist to your compiler command line in Settings for your project (assuming MSVC here), it will enable a faster convertor.

Thanks Il try that.







You could also trick the FPU with a bit of assembly language. As far as I can remember you fmul the float you want to convert with some magical magic number then you store as float but the result is int ...

EDIT: Add link
Double to Int
Float-to-int, coverting an array of floats.

Hope it helps


[edited by - cnstrnd on March 3, 2003 7:49:01 AM]
quote:Original post by cnstrnd
You could also trick the FPU with a bit of assembly language. As far as I can remember you fmul the float you want to convert with some magical magic number then you store as float but the result is int ...

EDIT: Add link
Double to Int
Float-to-int, coverting an array of floats.

Hope it helps


[edited by - cnstrnd on March 3, 2003 7:49:01 AM]


Thanks for the tip, and intresting links!

This topic is closed to new replies.

Advertisement