ozma
ozma

Reputation: 399

C++ and inline assembly code have same output in one program, but different in another

I'm working on an assignment where I have to convert a snippet of C code to inline assembly. The code is part of a program that renders a Julia fractal.

I've tested the outputs of both code snippets and they match exactly, yet my program still outputs a different image (a proper Julia fractal for the C code, a flat pink screen for the inline assembly code).

This is the starting part of the function as well as the return

COLORREF render_point(const double &a, 
                      const double &b, int N) {
  double cRe = -0.5;
  double cIm = -0.05;
  double x(a), y(b);
  double norm = x*x+y*y;
  int n;
  double three = 3.0;

  (loop goes here)

  return HSVtoRGB(n % 256, 255 , 255 *(n<N));
}

Here's the C code

for (n = 0; norm < 4.0 && n < N; ++n) 
{
    double old_x = x;
    double old_y = y;

    x = (old_x * old_x * old_x) - (3 * old_y * old_y * old_x) + cRe;
    y = (3 * old_y * old_x * old_x) - (old_y * old_y * old_y) + cIm;

    norm = x*x+y*y;
}

and the inline assembly code:

for (n = 0; norm < 4.0 && n < N; ++n) 
  {
    __asm {
        // Create (old_x * old_x * old_x)
        fld x;
        fmul x;
        fmul x;

        // Create (3 * old_y * old_y * old_x)
        fld three;
        fmul y;
        fmul y;
        fmul x;

        // Create the full equation for x
        fsubp st(1), st(0);
        fadd cRe;

        // Create (3 * old_y * old_x * old_x) + cIm
        fld three;
        fmul y;
        fmul x;
        fmul x;
        fadd cIm;

        // Create (old_y * old_y * old_y)
        fld y;
        fmul y;
        fmul y;

        fsubp st(1), st(0); // Create the full equation for y

        fst y;              // Store in y to use for next loop
        fmul st(0), st(0);  // Get y*y

        fxch st(1);         // Swap places of y*y with newly calculated x
        fst x;              // Store in x to use for next loop

        fmul st(0), st(0);  // Get x*x

        faddp st(1), st(0); // Get x*x + y*y
        fst norm;           // Set loop variable
    }
  }

Is there a difference between the two loops that might result in a differing output in the program?

Upvotes: 2

Views: 253

Answers (1)

ozma
ozma

Reputation: 399

As 1201ProgramAlarm mentioned in a comment, just had to pop the remaining value norm off the FPU at the end of each loop iteration.

Upvotes: 2

Related Questions