什么是计算正弦和余弦在一起最快的方法是什么?余弦、正弦、最快、方法

2023-09-10 22:51:16 作者:吃污藕丑

我想同时计算正弦和共同的正弦值在一起(例如,创建一个旋转矩阵)。当然,我可以分别计算它们一个接一个像 A = COS(X); B = SIN(X); ,但我不知道是否有需要这两个数值时更快的方法

编辑: 总结的答案迄今:

的弗拉德的说,有ASM的命令 FSINCOS (作为单独调用 FSIN 在几乎相同的时间)计算两者

如的志的注意到,这种优化有时已经完成由编译器(当采用优化的标志)。

的 CAF 的指出,其功能正余弦 sincosf 可能是有效的,可直接通过只包括文件math.h 关于正弦余弦问题

的 tanascius 的使用查表的方法进行了讨论争议。 (不过在我的电脑,并在基准情况下它的运行速度比正余弦 3倍速度更快,几乎相同的精度为32位浮点。)

的乔尔·古德温的链接到有着非常快逼近技术与一个相当有趣的方法良好的ACCURAY(对我来说,这是更快然后查表)

解决方案

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

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

下面是另一个例子(对于MSVC):http://www.$c$cguru.com/forum/showthread.php?t=328669

下面是另一个例子(与海湾合作委员会): http://www.allegro.cc/forums/thread/588470

希望他们中的一个帮助。 (我没有使用这个指令我自己,对不起。)

,因为它们是在处理器层面的支持,我希望他们会比查表的方式要快得多。

编辑: 维基百科表明, FSINCOS 溶液中加入387处理器,这样你就可以很难找到一个处理器,该处理器不支持它。

编辑: Intel的文档指出 FSINCOS 只是约5慢于 FDIV 倍(即,浮点除法)。

编辑: 请注意,并非所有的现代编译器优化的正弦和余弦的计算成一个呼叫 FSINCOS 。特别是,我的VS 2008并没有这样做的。

编辑: 第一个例子链接是死的,但还是一个版本在Wayback机器。

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.

Edit: To summarize the answers so far:

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

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

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

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 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)

解决方案

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

Here is a small example: http://home.broadpark.no/~alein/fsincos.html

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

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.

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

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

Edit: 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.

Edit: The first example link is dead, but there is still a version at the Wayback Machine.