我正在尝试规范化 4d 矢量。
我的第一个方法是使用 SSE 内在函数——它为我的向量算术提供了 2 倍的速度提升。这是基本代码:(v.v4 是输入)(使用 GCC)(所有这些都是内联的)
//find squares
v4sf s = __builtin_ia32_mulps(v.v4, v.v4);
//set t to square
v4sf t = s;
//add the 4 squares together
s = __builtin_ia32_shufps(s, s, 0x1B);
t = __builtin_ia32_addps(t, s);
s = __builtin_ia32_shufps(s, s, 0x4e);
t = __builtin_ia32_addps(t, s);
s = __builtin_ia32_shufps(s, s, 0x1B);
t = __builtin_ia32_addps(t, s);
//find 1/sqrt of t
t = __builtin_ia32_rsqrtps(t);
//multiply to get normal
return Vec4(__builtin_ia32_mulps(v.v4, t));
我检查了反汇编,它看起来像我所期望的那样。我看不出有什么大问题。
无论如何,然后我尝试使用近似值:(我从谷歌得到这个)
float x = (v.w*v.w) + (v.x*v.x) + (v.y*v.y) + (v.z*v.z);
float xhalf = 0.5f*x;
int i = *(int*)&x; // get bits for floating value
i = 0x5f3759df - (i>>1); // give initial guess y0
x = *(float*)&i; // convert bits back to float
x *= 1.5f - xhalf*x*x; // newton step, repeating this step
// increases accuracy
//x *= 1.5f - xhalf*x*x;
return Vec4(v.w*x, v.x*x, v.y*x, v.z*x);
它的运行速度比 SSE 版本稍快!(大约快 5-10%)它的结果也非常准确 - 找到长度时我会说 0.001! 但是.. 由于类型双关语,GCC 给了我蹩脚的严格别名规则。
所以我修改它:
union {
float fa;
int ia;
};
fa = (v.w*v.w) + (v.x*v.x) + (v.y*v.y) + (v.z*v.z);
float faHalf = 0.5f*fa;
ia = 0x5f3759df - (ia>>1);
fa *= 1.5f - faHalf*fa*fa;
//fa *= 1.5f - faHalf*fa*fa;
return Vec4(v.w*fa, v.x*fa, v.y*fa, v.z*fa);
现在修改后的版本(没有警告)运行速度变慢了!!它的运行速度几乎是 SSE 版本运行速度的 60%(但结果相同)!为什么是这样?
所以这里有问题:
- 我的 SSE 实施是否正确?
- SSE 真的比正常的 fpu 操作慢吗?
- 为什么第三个代码这么慢?