Skip to content

[API Proposal]: Vector128/256 AddSaturate/RemoveSaturate #82559

Closed
@EgorBo

Description

@EgorBo

Background and motivation

I hit lack of these crossplatform helpers when I was doing "parse hex" work in #82521 - ended up with local functions. These APIs seem to present in SSE/AVX and AdvSimd and can be easily exposed as cross-platform helpers. Some places in BCL might benefit from this too, e.g.

if (AdvSimd.IsSupported)
{
charIsThreeByteUtf8Encoded = AdvSimd.AddSaturate(utf16Data, vector7800);
mask = GetNonAsciiBytes(AdvSimd.Or(charIsNonAscii, charIsThreeByteUtf8Encoded).AsByte(), bitMask128);
}
else
{
charIsThreeByteUtf8Encoded = Sse2.AddSaturate(utf16Data, vector7800);
mask = (uint)Sse2.MoveMask(Sse2.Or(charIsNonAscii, charIsThreeByteUtf8Encoded).AsByte());
}

API Proposal

namespace System.Numerics;

public static class Vector
{
+     public static Vector<T> AddSaturate<T>     (Vector<T> left, Vector<T> right) where T : struct;
+     public static Vector<T> SubtractSaturate<T>(Vector<T> left, Vector<T> right) where T : struct;
}

namespace System.Runtime.Intrinsics;

public static class Vector64
{
+     public static Vector64<T> AddSaturate<T>     (Vector64<T> left, Vector64<T> right) where T : struct;
+     public static Vector64<T> SubtractSaturate<T>(Vector64<T> left, Vector64<T> right) where T : struct;
}

public static class Vector128
{
+     public static Vector128<T> AddSaturate<T>     (Vector128<T> left, Vector128<T> right) where T : struct;
+     public static Vector128<T> SubtractSaturate<T>(Vector128<T> left, Vector128<T> right) where T : struct;
}

public static class Vector256
{
+     public static Vector256<T> AddSaturate<T>     (Vector256<T> left, Vector256<T> right) where T : struct;
+     public static Vector256<T> SubtractSaturate<T>(Vector256<T> left, Vector256<T> right) where T : struct;
}

public static class Vector512
{
+     public static Vector512<T> AddSaturate<T>     (Vector512<T> left, Vector512<T> right) where T : struct;
+     public static Vector512<T> SubtractSaturate<T>(Vector512<T> left, Vector512<T> right) where T : struct;
}

NOTE: SSE and AVX only accelerate byte, sbyte, short and ushort. AdvSimd supports more types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions