Sign in to follow this  

Inline assembly and returning a float

This topic is 4843 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
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

This topic is 4843 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this