Skip to content

TensorPrimitives.IndexOfMax produces incorrect results with vectorized paths #124233

@vcsjones

Description

@vcsjones

For TensorPrimitives.IndexOfMax<T> where the number of elements is larger that the largest value T can represent and can be vectorized, the incorrect index of can be calculated.

Consider the following example

using System;
using System.Numerics.Tensors;

byte[] data = new byte[(int)byte.MaxValue + 3];
data[(int)byte.MaxValue + 2] = 255;

int result = TensorPrimitives.IndexOfMax(data);
Console.WriteLine(result);

This will print "1". ((255 + 2) % 256).

Another example, using a signed short

using System;
using System.Numerics.Tensors;

short[] data = new short[(int)short.MaxValue + 3];
data[(int)short.MaxValue + 2] = 255;

int result = TensorPrimitives.IndexOfMax(data);
Console.WriteLine(result);

This will print -32767.

This likely affects IndexOfMin, IndexOfMaxMagnitude, and IndexOfMinMagnitude since they share IndexOfMinMaxCore.

This also only impacts vectorized code paths. When .NET is forced to using scalar code paths using DOTNET_EnableHWIntrinsic=0, then the outputs are 257 and 32769, respectively.

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions