gpt4 book ai didi

C++ 和内联汇编代码在一个程序中输出相同,但在另一个程序中输出不同

转载 作者:搜寻专家 更新时间:2023-10-31 02:09:38 24 4
gpt4 key购买 nike

我正在处理一项作业,我必须将一段 C 代码转换为内联汇编。该代码是呈现 Julia 分形的程序的一部分。

我已经测试了两个代码片段的输出并且它们完全匹配,但我的程序仍然输出不同的图像(C 代码的正确 Julia 分形,内联汇编代码的扁平粉红色屏幕)。

这是函数的开始部分,也是返回

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));
}

这是C代码

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;
}

和内联汇编代码:

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
}
}

这两个循环之间是否存在可能导致程序输出不同的差异?

最佳答案

正如 1201ProgramAlarm 在评论中提到的那样,只需在每次循环迭代结束时从 FPU 中弹出剩余值范数。

关于C++ 和内联汇编代码在一个程序中输出相同,但在另一个程序中输出不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46386567/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com