Archived

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

Z01

Performance costs of casting number

Recommended Posts

Z01    134
Hi I''m writing some code for traversing a quad-tree that needs to be fast performance-wise. I''m trying to choose basic types for my variables right now, and I''m curious if there is any performance cost with casting or its just a compile time thing. (I need to learn some assembly language soon so I can answer these questions for myself ) Specifically, I''m interested in:
  
// I''m mostly interested in these two

float newFloat = (float)anInt;
float newFloat = (float)anUnsignedChar;

// but I would like to know about this one also if possible

int newInt = (int)aFloat;
  
I think floating point adds take the same time as adding ints on P3 and higher, but I''m not sure exactly what the speed of these opcodes is. Maybe someone knows? Is casting a variable to a float actually reorganizing the memory contents of that memory address, or is it just the program is looking at the memory contents in a different way? As an example of what I''m thinking about:
  
// stride should be an int because I use for array indexing

const int stride = 10; 
float xUpperLeft = 20.0f;
float xUpperRight = xUpperLeft + (float)stride;

// ***************** VERSUS


// but this might be faster because its a float and there''s no cast

const float stride = 10.0f;
float xUpperLeft = 20.0f;
float xUpperRight = xUpperLeft + stride;
  
Thanks Guys

Share this post


Link to post
Share on other sites
BeerNutts    4401
The only casting that will slow down the code is casting to/from a float and integer type since the compiler calls a function to do the converting.

So, for your problem, set stride as a float.

Share this post


Link to post
Share on other sites
Beer Hunter    712
For the i386 architecture, the cpu instruction to convert a float to an int (FIST) takes 6 cycles. But it takes about 40 cycles to change the cpu''s rounding mode... which is usually done both before and after converting the float to an int.

It''s better to call FIST directly.

Intel syntax: (tested under borland c++)
  
inline int fast_round(float number) {
int answer;
__asm {
FLD number
FIST answer
}
return answer;
}


AT&T syntax: (tested under g++)
  
inline int fast_round(float number) {
int answer;
__asm__ __volatile__ (
"FLD %0;"
"FIST (%1);"
: : "m" (number), "r" (&answer));
return answer;
}


I don''t know about converting ints to floats, but I seriously doubt that there''d be a performance issue there.

Share this post


Link to post
Share on other sites
jenova    122
to be perfectly clean.

use "fistp" to pop the stack as well.

To the vast majority of mankind, nothing is more agreeable than to escape the need for mental exertion... To most people, nothing is more troublesome than the effort of thinking.

Share this post


Link to post
Share on other sites
I''m pretty sure there''s an instruction to load an int32 to the FPU registers, maybe FLDI. I can''t remember though. But I''m almost positive float-to-int takes the same amount of time as int-to-float, because either way it has to format to or from the float data type. Floats are to ints as zip files are to text files, in a way.

~CGameProgrammer( );

Share this post


Link to post
Share on other sites
Yes, you just need to change FISt to FIStP. FLD loads data onto one of the FPU registers. FIStP returns the integer form of it and removes the float from the FPU. FISt just returns the integer form of it, but the float remains on the FPU registers.

~CGameProgrammer( );

Share this post


Link to post
Share on other sites
Beer Hunter    712
Thanks.

But converting an int to a float shouldn''t require the cpu''s rounding mode to be changed. Here''s the code anyway...

  
inline float itof(const int number) {
float answer;
__asm__ __volatile__ (
"FILD (%0);"
"FSTP (%1);"
: : "r" (&number), "r" (&answer));
return answer;
}

(can''t be bothered to do an intel version right now)

Share this post


Link to post
Share on other sites
Steveyboy    122
Theres a section in Game Programming Gems 2 about speeding stuff up - where it shows a fast way to convert a float to an int and back.

Oh, and also, if you want to cast the actual bits of the float to an int, you''d do this:
  
float fFloat = 10.23f;
long lFloatBits = *((long*)(&fFloat));


Steve

Share this post


Link to post
Share on other sites
Beer Hunter    712
Those of us who have the book would already know about it.
Those of us who don''t have the book have learned nothing just from reading your post.

Just tell us: what method is suggested by Game Programming Gems 2?

Share this post


Link to post
Share on other sites