#### Archived

This topic is now archived and is closed to further replies.

# Ways preventing floating point value conversion to int

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

## Recommended Posts

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]

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
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 ...

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]

##### Share on other sites
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 ...

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!

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5
frob
12

• 9
• 21
• 11
• 9
• 17
• ### Forum Statistics

• Total Topics
632606
• Total Posts
3007381

×