c++ libstd compute sin and cos simultaneously

2019-04-22 17:45发布

In C library math.h, there was a sincos function which was pretty efficient, because it computed both sine and cosine in a time closer to a single call to sin() or cos() than to the total time of calling both.

Is there such function in C++ standard library?

2条回答
做自己的国王
2楼-- · 2019-04-22 18:18

Just use sin and cos separately and turn on optimizations. C compilers are pretty good at optimizing, and they will probably realize that you are computing both the sine and cosine of the same variable. If you want to make sure, you can allways inspect the resulting assembly (for gcc use the -S option) and see what did it generate.

The compiler will probably optimize away any calls to sin or cos in favour of simply using SSE intructions to calculate it. I'm not sure SSE has a sincos opcode but even calculating them separatly is faster than calling any sincos function that the compiler won't optimize out.

查看更多
Root(大扎)
3楼-- · 2019-04-22 18:22

Is there no such function in c++ standard library?

No, unfortunately there isn't.

In C library math.h, there was a sincos function

On Linux, it is available as GNU Extension. It's not standard in C either.

查看更多
登录 后发表回答