在结果冒险和数据冒险中,可以发现,所有的流水线停顿的操作都要从指令执行阶段开始。流水线的前两个阶段,也就是取取指令(IF)和指令译码(ID)阶段,是不需要停顿的。CPU会在流水线里面去取下一条指令,然后进行译码。
取指令和指令译码不会需要遇到任何瓶颈,这是基于一个假设。这个假设就是,所有的指令代码都是顺序加载执行的。不过这个假设,在执行的代码中,一旦遇到if…else这样的条件分支,或者for/while循环,就会不成立。
#include<stdio.h>
#include<string.h>
#include<time.h>
#include<math.h>
#include<stdlib.h>
int qwzsf(float* p1, float* p2, float* p3);
float xz(float x);
float a = 0, b = 0, c = 0, d = 0;
int main()
{
float* p1=NULL, * p2=NULL, * p3=NULL;
float x1, x2, x3;
p1 = &x1;
p2 = &x2;
p3 = &x3;
scanf("%f %f %f %f", &a, &b, &c, &d);
qwzsf( p1, p2, p3);
printf("%.2f %.2f %.2f", *p1, *p2, *p3);
return 0;
}
int qwzsf(float* p1, float* p2, float* p3)
{
float xfirst1=-100, xlast3=100;
float xfirst2, xlast1, xfirst3, xlast2;
float xmid;
float m,l,f;
xfirst2 = xlast1 = (-b - sqrt(b * b - 3 * a * c)) / 3 * a;
xfirst3 = xlast2 = (-b + sqrt(b * b - 3 * a * c)) / 3 * a;
if (xz(100) == 0)
{
*p3 = 100;
}
if (xz(-100) == 0)
{
*p1 = -100;
}
for (; xlast1 - xfirst1 > 0.005;)
{
xmid = (xfirst1 + xlast1) / 2;
m = xz(xmid);
f = xz(xfirst1);
l = xz(xlast1);
if (m == 0)
{
*p1 = xmid;
}
else if (l * m < 0)
{
xfirst1 = xmid;
}
else
{
xlast1 = xmid;
}
}
*p1 = xmid;
for (; xlast2 - xfirst2 > 0.005;)
{
xmid = (xfirst2 + xlast2) / 2;
m = xz(xmid);
f = xz(xfirst2);
l = xz(xlast2);
if (m == 0)
{
*p1 = xmid;
}
else if (l * m < 0)
{
xfirst2 = xmid;
}
else
{
xlast2 = xmid;
}
}
*p2 = xmid;
for (; xlast3 - xfirst3 > 0.005;)
{
xmid = (xfirst3 + xlast3) / 2;
m = xz(xmid);
f = xz(xfirst3);
l = xz(xlast3);
if (m == 0)
{
*p1 = xmid;
}
else if (l * m < 0)
{
xfirst3 = xmid;
}
else
{
xlast3 = xmid;
}
}
*p3 = xmid;
return 0;
}
float xz(float x)
{
return a * x * x * x + b * x * x + c * x + d;
}