1

这家伙:http ://andrew-hoyer.com/experiments/cloth/做了一个 javascript 算法来模拟布料。他指出代码太慢了,所以他不得不使用泰勒级数优化平方根。

  1. 难道不能通过预先计算每个可能的值并使用查找表来优化它吗?

  2. 这个经常用吗?例如,在 3d 游戏中,他们是否真的执行计算,或者他们已经为每个 sin、cos、tg、sqrt 和类似函数提供了一个查找表?

  3. 为什么不将其预编程到处理器中?

4

3 回答 3

2

查找表几乎已被现代处理器所掩盖。特别是对于像 sqrt 这样的东西。大多数 FPU 可以在 9-20 个周期内完成它们,并且它们通常与其他计算交错。内存访问现在通常是 CPU 的瓶颈,缓存未命中需要数百个周期。即使是二级缓存也可能需要 20-30 个周期。通常进行计算比保存预先计算的值更快。

于 2012-02-01T04:55:47.567 回答
2

Because your average CPU by default defines its floating operations based on the IEEE-754 standard which defines rather strictly what the result of any mathematical operation should be. A lookup table by design is only an approximation and will only contain a certain range and granularity that you need for your specific problem - that makes it rather impossible to implement sensibly in hardware. If you wanted to store any possible value - do the math yourself.

That does not mean that we never approximate results - lookup tables just aren't such a great solution for this. Ie SSE has both sqrtss and rsqrtss - the later returns an approximation of the real result and is a good bit faster. Just a bit of math there.

于 2012-01-31T18:24:29.593 回答
1

使用查找表可能没有错,但可能是微优化。使用算法或专注于其他代码会更聪明。也许是因为没有硬编码到cpu中的截止日期。对数呢?cpu中有查找表吗?

于 2012-01-31T15:54:41.663 回答