案例一
假设你有一个小班:
class Point3D
{
private:
float x,y,z;
public:
operator+=()
...etc
};
Point3D &Point3D::operator+=(Point3D &other)
{
this->x += other.x;
this->y += other.y;
this->z += other.z;
}
天真的使用 SSE 会简单地用一些内在函数替换这些函数体。但是我们会期望这会产生很大的不同吗?MMX 曾经涉及昂贵的状态变化 IIRC,SSE 还是它们就像其他指令一样?即使没有直接的“使用 SSE”开销,将值移入 SSE 寄存器并再次移出真的会更快吗?
案例二
相反,您使用的是较少基于 OO 的代码库。而不是 Point3D 对象的数组/向量,您只需拥有一个大的浮点数组:
float coordinateData[NUM_POINTS*3];
void add(int i,int j) //yes it's unsafe, no overlap check... example only
{
for (int x=0;x<3;++x)
{
coordinateData[i*3+x] += coordinateData[j*3+x];
}
}
在这里使用 SSE 怎么样?好点?
综上所述
尝试使用 SSE 优化单向量操作实际上是否值得,还是仅在进行批量操作时才有价值?