Skip to content

Math.SinCos can be slower than Sin + Cos in some scenarios. #48776

@tannergooding

Description

@tannergooding

The codegen for Math.SinCos can currently be problematic on GCC and MSVC leading to decreased perf.

https://godbolt.org/z/K5dad3 shows the codegen for:

#include <math.h>

#ifdef _MSC_VER
#pragma float_control(precise, off)
#endif

void SinCos1(float x, float* pSin, float* pCos)
{
#ifdef _MSC_VER
    *pSin = sinf(x);
    *pCos = cosf(x);
#else
    sincosf(x, pSin, pCos);
#endif
}

In particular, GCC and MSVC end up with many shuffles between registers and to/from the stack causing a noticeable perf slowdown across hundreds of calls.

https://godbolt.org/z/sG6dTW shows comparison of several other possible variants, but none is "ideal" across all 3 compilers. We should investigate this further to determine what can be done to ensure that SinCos is at least as fast as calling Sin and Cos independently, possibly following up with the MSVC team to improve the codegen here.

category:performance
theme:expression-opts

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMItenet-performancePerformance related issue

    Type

    No type

    Projects

    Status

    Performance

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions