c++ libstd compute sin and cos simultaneously

10,253

Solution 1

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.

Solution 2

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.

Solution 3

While there is no standard C++ library function, you can define a template function pretty quickly:

template <class S>
std::pair<S,S> sincos(S arg) { return { std::sin(arg), std::cos(arg) }; }

You can then get the result on a single line (with C++ 17) with:

auto [s, c] = sincos(arg);

It's very convenient if you are doing this often, saves space, and is self-documenting, so I would highly recommend it. If you're worried about performance, don't. When compiled with optimizations, it should produce the exact same code as calling sin and cos separately. You can confirm this is the case with clang++ -std=c++17 -S -o - -c -O3 sincos.cpp on the following test code:

#include <cmath>
#include <utility>
#include <iostream>

template <class S>
std::pair<S,S> sincos(S arg) { return { std::sin(arg), std::cos(arg) }; }

void testPair(double a) {
    auto [s,c] = sincos(a);
    std::cout << s << ", " << c << '\n';
}

void testSeparate(double a) {
    double s = std::sin(a);
    double c = std::cos(a);
    std::cout << s << ", " << c << '\n';
}

On MacOS with clang, both test functions compile to the exact same assembly (minus the name changes) that call ___sincos_stret to perform the combined computation (see https://stackoverflow.com/a/19017286/973580).

Share:
10,253
galinette
Author by

galinette

Updated on June 06, 2022

Comments

  • galinette
    galinette almost 2 years

    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?

  • Mysticial
    Mysticial almost 10 years
    SSE has no support for sin/cos or any "fancy" function beyond division and square root. But some compilers (such as the Intel Compiler) will have a vectorized implementation that uses SSE.
  • galinette
    galinette almost 10 years
    Are compilers really able to do high level optimizations? sin and cos are call to functions, not arithmetic operators.
  • galinette
    galinette almost 10 years
    SSE has no sin, cos or sincos instructions. Only base arithmetics.
  • Evan Dark
    Evan Dark almost 10 years
    @galinette: sin and cos are treated specially by the compiler and optimized out when optimizations are enabled, and yes it seems SSE has no sin or cos functions (I remembered wrong), so it's the x87 instructions of course.
  • Abhi
    Abhi almost 6 years
    Using godbolt I confirmed that newer GCC versions at high optimization levels do manage to coalesce sin + cos to sincos, but clang and VS2017 have no such optimization.
  • Royi
    Royi almost 5 years
    @DanOlson, I think MSVC 2017 and above does optimize Sine and Cosine calls: devblogs.microsoft.com/cppblog/…
  • alfC
    alfC over 4 years
    @DanOlson, confirmed here godbolt.org/z/wuS26Z for GCC and intel at a fairly low level of optimization. clang does the optimization only in -Ofast (fast-math). This hints me that sincos may not guarantee the exact same result as sin and cos called separately.
  • alfC
    alfC over 4 years
    This answer is more useful than the other one. It is a pity that it was downvoted for a technical reason. I would suggest removing the controversial assertion about SSE.
  • m2j
    m2j over 3 years
    As more you digg into it as more it gets complicated... I've tested various combinations on quick-bench.com. By default sin() and cos() may have side effects (error codes), so they will be executed twize in the expression x=sin(x)+sin(x);. This behaviour is also disabled with -Ofast. gcc seems to disable it also in -O3 for sin and cos. Thus it is 14x faster to evaluate cos(x) * cos(x) compared to sin(x) * cos(x) (gcc 10.1 with -O3).
  • user14717
    user14717 about 2 years
    godbolt showing that at -O3 we get the desired assembly: godbolt.org/z/KdT9fjEEr