Visual C++ Developer Center >
Visual C++ Forums
>
Visual C++ General
>
fyl2x modifying bottom of stack
fyl2x modifying bottom of stack
- Hello, I don't know if this is the right forum to post this in, but I'll try here anyway. I have some inline assembly code I'm using to calculate the y^x but after several iterations it's returning a QNaN. I narrowed down the cause to a stack fault because the fpu stack is incorrectly modified by the fyl2x instruction. I'm just wondering if there is something wierd I'm missing here. I'm using VS2008.
Here is a sample from my code:
#include <stdio.h> int main() { __asm { fld1 // Nothing on fpu stack fld1 // st(0) = 1 fyl2x // st(0) = 1, st(1) = 1 wait // st(0) = 0, st(7) = 1 ?????? } return 0; }
Answers
- Never mind I found the problem. I was leaving a value on the stack at the end. I just had to add fstp st(1) after everything else. Thanks for the help.
- Marked As Answer bySonyt2 Wednesday, November 04, 2009 11:55 PM
All Replies
In our experiments this fragment shows a correct result:
double x = 8.0;
double y = 10.0;
double result;
__asm
{
fld y
fld x
fyl2x // y * log2(x)
fwait
fstp result // result = 10 * log2(8) = 30
}
It uses two elements of the stack; therefore two bottom values are lost.
- Thank you for your help. I realize now that I had a misconception about the nature of the floating point stack. I thought that fyl2x was the source of the fpu stack corruption in my original source, but I am now trying to pin the true source down.
My Original Code:
This prints out 25 the first 5 iterations but then starts to print -1.#IND00.#include <stdio.h> double Pow(double x, double y) { bool neg = false; if(x < 0) { if(y != double((int)y)) throw "For negative bases y must be an integer"; if(int(y) % 2 != 0) neg = true; } short controlword; __asm { fld y fld x fabs fyl2x lea eax, controlword fstcw word ptr [eax] or byte ptr [eax+1], 0Ch fldcw word ptr [eax] fld st(0) fld st(0) frndint fsubp st(1), st(0) f2xm1 fld1 fadd fscale fstp x lea eax, controlword fldcw word ptr [eax] } return (neg)? -x : x; } int main() { for(int i = 0;;i++) { printf("%f", Pow(5, 2)); getchar(); } return 0; }
- Never mind I found the problem. I was leaving a value on the stack at the end. I just had to add fstp st(1) after everything else. Thanks for the help.
- Marked As Answer bySonyt2 Wednesday, November 04, 2009 11:55 PM


