【发布时间】:2012-06-22 21:58:51
【问题描述】:
我有以下功能(来自开源项目"recast navigation"):
/// Derives the dot product of two vectors on the xz-plane. (@p u . @p v)
/// @param[in] u A vector [(x, y, z)]
/// @param[in] v A vector [(x, y, z)]
/// @return The dot product on the xz-plane.
///
/// The vectors are projected onto the xz-plane, so the y-values are ignored.
inline float dtVdot2D(const float* u, const float* v)
{
return u[0]*v[0] + u[2]*v[2];
}
我通过 VS2010 CPU 性能测试运行它,它告诉我在这个函数u[0]*v[0] + u[2]*v[2] 的所有重铸代码库代码行中 CPU 最热。
我如何 CPU 优化(例如通过 SSE 或 GLSL like GLM (if it is easier or faster and appropriate in such case))这一行?
编辑:调用出现的上下文:
bool dtClosestHeightPointTriangle(const float* p, const float* a, const float* b, const float* c, float& h) {
float v0[3], v1[3], v2[3];
dtVsub(v0, c,a);
dtVsub(v1, b,a);
dtVsub(v2, p,a);
const float dot00 = dtVdot2D(v0, v0);
const float dot01 = dtVdot2D(v0, v1);
const float dot02 = dtVdot2D(v0, v2);
const float dot11 = dtVdot2D(v1, v1);
const float dot12 = dtVdot2D(v1, v2);
// Compute barycentric coordinates
const float invDenom = 1.0f / (dot00 * dot11 - dot01 * dot01);
const float u = (dot11 * dot02 - dot01 * dot12) * invDenom;
const float v = (dot00 * dot12 - dot01 * dot02) * invDenom;
【问题讨论】:
-
我怀疑这个叶子函数可以进一步优化,因为它只进行 3 次 FP 操作。可能在调用站点进行优化。
-
如果这个函数被调用了很多,如果有意义的话尝试使用OpenMP。
-
IMO 这不是正确的方法。 SSE 并不是真正用于水平操作。有一些水平指令,但它们(几乎)都很慢。使用 SSE,一次计算 4 件事情几乎总是比尝试以 4 倍的速度做一件事情要好。
-
我添加了更多信息。显然代码可以使用 SSE 进行优化,同样明显的是数据布局对于这种工作来说不是很聪明,并且会从重组中受益。
-
u[0]、v[0]、u[2] 和 v[2] 的值范围是多少?另外,您需要的最低精度是多少(即可以接受多少粒度误差)?
标签: c++ c optimization sse glm-math