diff --git a/src/ComputeSharp/Core/Imaging/Bgra32.cs b/src/ComputeSharp/Core/Imaging/Bgra32.cs index c1f1ab2a..ffe235b9 100644 --- a/src/ComputeSharp/Core/Imaging/Bgra32.cs +++ b/src/ComputeSharp/Core/Imaging/Bgra32.cs @@ -2,6 +2,10 @@ using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +#if NET6_0_OR_GREATER +using System.Runtime.Intrinsics; +using System.Runtime.Intrinsics.X86; +#endif namespace ComputeSharp; @@ -87,6 +91,22 @@ public struct Bgra32 : IEquatable, IPixel [MethodImpl(MethodImplOptions.AggressiveInlining)] public readonly Float4 ToPixel() { +#if NET6_0_OR_GREATER + if (Sse2.IsSupported) + { + int pack = Unsafe.As(ref Unsafe.AsRef(in this)); + Vector128 vZero = Vector128.Zero; + Vector128 vByte = Vector128.CreateScalarUnsafe(pack).AsByte(); + Vector128 vUShort = Sse2.UnpackLow(vByte, vZero).AsUInt16(); + Vector128 vInt = Sse2.UnpackLow(vUShort, vZero.AsUInt16()).AsInt32(); + Vector128 vShuffle = Sse2.Shuffle(vInt, 0b11_00_01_10); + Vector128 vFloat = Sse2.ConvertToVector128Single(vShuffle); + Vector128 vMax = Vector128.Create((float)byte.MaxValue); + Vector128 vNorm = Sse.Divide(vFloat, vMax); + + return vNorm.AsVector4(); + } +#endif Vector4 linear = new(this.R, this.G, this.B, this.A); Vector4 normalized = linear / byte.MaxValue; diff --git a/src/ComputeSharp/Core/Imaging/Rgba32.cs b/src/ComputeSharp/Core/Imaging/Rgba32.cs index 5daab793..595eea02 100644 --- a/src/ComputeSharp/Core/Imaging/Rgba32.cs +++ b/src/ComputeSharp/Core/Imaging/Rgba32.cs @@ -2,6 +2,10 @@ using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +#if NET6_0_OR_GREATER +using System.Runtime.Intrinsics; +using System.Runtime.Intrinsics.X86; +#endif namespace ComputeSharp; @@ -86,6 +90,21 @@ public struct Rgba32 : IEquatable, IPixel [MethodImpl(MethodImplOptions.AggressiveInlining)] public readonly Float4 ToPixel() { +#if NET6_0_OR_GREATER + if (Sse2.IsSupported) + { + int pack = Unsafe.As(ref Unsafe.AsRef(in this)); + Vector128 vZero = Vector128.Zero; + Vector128 vByte = Vector128.CreateScalarUnsafe(pack).AsByte(); + Vector128 vUShort = Sse2.UnpackLow(vByte, vZero).AsUInt16(); + Vector128 vInt = Sse2.UnpackLow(vUShort, vZero.AsUInt16()).AsInt32(); + Vector128 vFloat = Sse2.ConvertToVector128Single(vInt); + Vector128 vMax = Vector128.Create((float)byte.MaxValue); + Vector128 vNorm = Sse.Divide(vFloat, vMax); + + return vNorm.AsVector4(); + } +#endif Vector4 linear = new(this.R, this.G, this.B, this.A); Vector4 normalized = linear / byte.MaxValue; diff --git a/src/ComputeSharp/Core/Imaging/Rgba64.cs b/src/ComputeSharp/Core/Imaging/Rgba64.cs index 59f6dd1d..0652aac0 100644 --- a/src/ComputeSharp/Core/Imaging/Rgba64.cs +++ b/src/ComputeSharp/Core/Imaging/Rgba64.cs @@ -2,6 +2,10 @@ using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +#if NET6_0_OR_GREATER +using System.Runtime.Intrinsics; +using System.Runtime.Intrinsics.X86; +#endif namespace ComputeSharp; @@ -86,6 +90,19 @@ public struct Rgba64 : IEquatable, IPixel [MethodImpl(MethodImplOptions.AggressiveInlining)] public readonly Float4 ToPixel() { +#if NET6_0_OR_GREATER + if (Sse2.IsSupported) + { + long pack = Unsafe.As(ref Unsafe.AsRef(in this)); + Vector128 vUShort = Vector128.CreateScalarUnsafe(pack).AsUInt16(); + Vector128 vInt = Sse2.UnpackLow(vUShort, Vector128.Zero).AsInt32(); + Vector128 vFloat = Sse2.ConvertToVector128Single(vInt); + Vector128 vMax = Vector128.Create((float)ushort.MaxValue); + Vector128 vNorm = Sse.Divide(vFloat, vMax); + + return vNorm.AsVector4(); + } +#endif Vector4 linear = new(this.R, this.G, this.B, this.A); Vector4 normalized = linear / ushort.MaxValue;