Jump to content
  • Advertisement
Sign in to follow this  
birdjames

Inline assembly and returning a float

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

At the moment I am writing a function that returns a float. All of the code to calculate the return value (the entire function body) is written in inline assembly. The question that I have focusses on how to return a value stored at the top of the floating point stack register back to the calling function. My first method was to declare a temporary float variable before the inline assembly starts, store the result in that variable, and then return its value: float MyFunction(void) { float fTempValue = 0; __asm { // Calculating code goes here fstp fTempValue }; return fTempValue; } Although this works without a problem, it is slower than it needs to be (the value that is popped to fTempValue is pushed back onto the FPU stack by the return statement). My second version simply leaves the result at the top of the stack and doesn't have a return statement. This speeds up the function but I would like to know if this method is legal in standardized C++, whether this would work on different compilers, and whether there were any other ways of doing this. I know I could use the __declspec(naked) function declarator under Microsoft's C++ compiler and write my own function pro/epilog code but the function that I am programming needs to be cross-compiler...

Share this post


Link to post
Share on other sites
Advertisement
Good point antareus - i didn't think of that! Although i suppose it is only a very small portion of code that is platform dependant. I suppose i could put in a #define that switches between AT+T style (GCC) assembly blocks and intel style (MSVC) but the way the function works will essentially be the same (same assembly instructions, algorithms, etc).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!