本文介绍了一起计算 sin 和 cos 的最快方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我想一起计算一个值的正弦和余弦(例如创建一个旋转矩阵).当然,我可以像 a = cos(x); 这样一个接一个地分别计算它们.b = sin(x);,但我想知道在需要两个值时是否有更快的方法.

I would like to compute both the sine and co-sine of a value together (for example to create a rotation matrix). Of course I could compute them separately one after another like a = cos(x); b = sin(x);, but I wonder if there is a faster way when needing both values.

总结到目前为止的答案:

To summarize the answers so far:

  • Vlad 说,有asm命令FSINCOS 计算它们(与单独调用 FSIN 几乎同时)

  • Vlad said, that there is the asm command FSINCOS computing both of them (in almost the same time as a call to FSIN alone)

就像 Chi 注意到的那样,这种优化有时已经由编译器完成(当使用优化标志).

Like Chi noticed, this optimization is sometimes already done by the compiler (when using optimization flags).

caf 指出,函数 sincossincosf 可能是可用的,只需包含 math.h

caf pointed out, that functions sincos and sincosf are probably available and can be called directly by just including math.h

tanascius 使用查找表的方法存在争议.(但是,在我的计算机和基准测试场景中,它的运行速度比 sincos 快 3 倍,对于 32 位浮点的精度几乎相同.)

tanascius approach of using a look-up table is discussed controversial. (However on my computer and in a benchmark scenario it runs 3x faster than sincos with almost the same accuracy for 32-bit floating points.)

Joel Goodwin 链接到一种非常快速的近似技术的有趣方法,具有相当准确率高(对我来说,这比查表还要快)

Joel Goodwin linked to an interesting approach of an extremly fast approximation technique with quite good accuray (for me, this is even faster then the table look-up)

推荐答案

现代 Intel/AMD 处理器具有指令 FSINCOS 用于同时计算正弦和余弦函数.如果您需要强大的优化,也许您应该使用它.

Modern Intel/AMD processors have instruction FSINCOS for calculating sine and cosine functions simultaneously. If you need strong optimization, perhaps you should use it.

这是一个小例子:http://home.broadpark.no/~alein/fsincos.html

这是另一个示例(对于 MSVC):http://www.codeguru.com/forum/showthread.php?t=328669

Here is another example (for MSVC): http://www.codeguru.com/forum/showthread.php?t=328669

这是另一个例子(使用 gcc):http://www.allegro.cc/论坛/线程/588470

Here is yet another example (with gcc): http://www.allegro.cc/forums/thread/588470

希望其中之一有所帮助.(我自己没有使用这个指令,抱歉.)

Hope one of them helps.(I didn't use this instruction myself, sorry.)

由于它们在处理器级别得到支持,我希望它们比表查找快得多.

As they are supported on processor level, I expect them to be way much faster than table lookups.


维基百科建议在 387 个处理器上添加 FSINCOS,因此您几乎无法找到不支持它的处理器.


Wikipedia suggests that FSINCOS was added at 387 processors, so you can hardly find a processor which doesn't support it.


Intel 的文档 指出 FSINCOS 差不多比FDIV(即浮点除法)慢5倍.


Intel's documentation states that FSINCOS is just about 5 times slower than FDIV (i.e., floating point division).


请注意,并非所有现代编译器都将正弦和余弦的计算优化为对 FSINCOS 的调用.特别是,我的 VS 2008 没有这样做.


Please note that not all modern compilers optimize calculation of sine and cosine into a call to FSINCOS. In particular, my VS 2008 didn't do it that way.


第一个示例链接已死,但有 仍然是 Wayback Machine 上的一个版本.

这篇关于一起计算 sin 和 cos 的最快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 02:21