So, I want to do these operations using inline assembly. I'm using visual studio 2008 and I'm getting weird results towards the end. I think it has to do with the fpu register stack. norm2 comes out as garbage values, while c, b and a are what they are supposed to be. Anyway, this is my c code and my asm is beneath it.
double a = 1.0, b = 2.0, norm2 = 0.0, two = 2.0, x = 3.0, y = 4.0, c;
c = a*a - b*b + x;
b = 2.0*a*b + y;
a = c;
norm2 = a*a + b*b;
__asm
{
fld b ; st: b
fmul st(0), st(0) ; st: b * b
fld a ; st: a, b * b
fmul st(0), st(0) ; st: a * a, b * b
fsub st(0), st(1) ; st: a * a - b * b, b * b
fld x ; st: x, a * a - b * b, b * b
fadd st(0), st(1) ; st: x + a * a - b * b,a * a - b * b, b * b
fstp c ; our new c st: a * a - b * b, b * b
fld b ; st: b
fld a ; st: a, b
fmul st(0), st(1) ; a * b, b
fld two ; 2, a * b, b
fmul st(0), st(1) ; a * b * 2, a * b, b
fld y ; y, a * b * 2, a * b, b
fadd st(0), st(1)
fstp b
fld c
fst a
fmul st(0), st(0)
fld b
fmul st(0), st(0)
fadd st(0), st(1)
fstp norm2
}
Is it important to pop the stack? Does that slow down the program?
Thank you