Numerics.Vector.SinCos() bug

Open
#113,295 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp
Domain
performance

Research direction

Start by running the supplied C# reproduction for Vector.SinCos with the lane-3 value changed between 0.0f and float.NaN. Trace the Vector.SinCos entry point and add a regression test covering the shown inputs; done means unaffected lanes produce identical sine and cosine results in both vectors.

Written by the indexing model from the issue text.

Description

area-System.Numerics
Description

I am seeing Vector.SinCos() producing different results for identical inputs in a given lane when introducing a NaN in some other lane. Changing lane 3 from 0.0 to NaN causes Cos on lane 0 to change value.

Reproduction Steps
public void TestVectorSinCos()
{
    Vector<float> x = new Vector<float>([0.029831253f, 0.035057854f, 2.5535614f, 0.0f]);
    (var sinx, var cosx) = Vector.SinCos(x);

    Vector<float> y = new Vector<float>([0.029831253f, 0.035057854f, 2.5535614f, float.NaN]);
    (var siny, var cosy) = Vector.SinCos(y);

    for (int i = 0; i < 3; i++)
    {
        Assert.That(sinx[i], Is.EqualTo(siny[i]));
        Assert.That(cosx[i], Is.EqualTo(cosy[i]));
    }
}
Expected behavior

SIMD lanes should produce same the result for the same input.

Actual behavior

Cos on lane 0 changes from 0.9995551 to 0.99955505 when lane 3 changes from 0.0 to NaN. Each lane should operate independently.

Regression?

No response

Known Workarounds

Replace Vector sin and cos operations with scalar versions. But this has a negative performance impact.

Configuration

dotnet --info

.NET SDK:
 Version:           9.0.101
 Commit:            eedb237549
 Workload version:  9.0.100-manifests.4a280210
 MSBuild version:   17.12.12+1cce77968

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  15.3
 OS Platform: Darwin
 RID:         osx-arm64
 Base Path:   /usr/local/share/dotnet/sdk/9.0.101/

.NET workloads installed:
There are no installed workloads to display.
Configured to use loose manifests when installing new manifests.

Host:
  Version:      9.0.0
  Architecture: arm64
  Commit:       9d5a6a9aa4

.NET SDKs installed:
  6.0.301 [/usr/local/share/dotnet/sdk]
  8.0.203 [/usr/local/share/dotnet/sdk]
  9.0.101 [/usr/local/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.6 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.3 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 9.0.0 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.6 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.3 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 9.0.0 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

Other architectures found:
  None

Environment variables:
  Not set

global.json file:
  Not found

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download
Other information

No response

Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.