Skip to content

Commit

Permalink
Rewrite intrinsics to cross-platform ones
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Nov 25, 2024
1 parent f4a0b3e commit 142b6cc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 59 deletions.
31 changes: 10 additions & 21 deletions src/ComputeSharp/Imaging/Bgra32.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;

namespace ComputeSharp;

Expand Down Expand Up @@ -86,25 +84,16 @@ public uint PackedValue
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Float4 ToPixel()
{
if (Sse2.IsSupported)
{
int pack = Unsafe.As<Bgra32, int>(ref Unsafe.AsRef(in this));
Vector128<byte> vZero = Vector128<byte>.Zero;
Vector128<byte> vByte = Vector128.CreateScalarUnsafe(pack).AsByte();
Vector128<ushort> vUShort = Sse2.UnpackLow(vByte, vZero).AsUInt16();
Vector128<int> vInt = Sse2.UnpackLow(vUShort, vZero.AsUInt16()).AsInt32();
Vector128<int> vShuffle = Sse2.Shuffle(vInt, 0b11_00_01_10);
Vector128<float> vFloat = Sse2.ConvertToVector128Single(vShuffle);
Vector128<float> vMax = Vector128.Create((float)byte.MaxValue);
Vector128<float> vNorm = Sse.Divide(vFloat, vMax);

return vNorm.AsVector4();
}

Vector4 linear = new(this.R, this.G, this.B, this.A);
Vector4 normalized = linear / byte.MaxValue;

return normalized;
int pack = Unsafe.As<Bgra32, int>(ref Unsafe.AsRef(in this));
Vector128<byte> vByte = Vector128.CreateScalarUnsafe(pack).AsByte();
Vector128<ushort> vUShort = Vector128.WidenLower(vByte);
Vector128<int> vInt = Vector128.WidenLower(vUShort).AsInt32();
Vector128<int> vShuffle = Vector128.Shuffle(vInt, Vector128.Create(2, 1, 0, 3));
Vector128<float> vFloat = Vector128.ConvertToSingle(vShuffle);
Vector128<float> vMax = Vector128.Create((float)byte.MaxValue);
Vector128<float> vNorm = Vector128.Divide(vFloat, vMax);

return vNorm.AsVector4();
}

/// <summary>
Expand Down
29 changes: 9 additions & 20 deletions src/ComputeSharp/Imaging/Rgba32.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;

namespace ComputeSharp;

Expand Down Expand Up @@ -85,24 +83,15 @@ public uint PackedValue
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Float4 ToPixel()
{
if (Sse2.IsSupported)
{
int pack = Unsafe.As<Rgba32, int>(ref Unsafe.AsRef(in this));
Vector128<byte> vZero = Vector128<byte>.Zero;
Vector128<byte> vByte = Vector128.CreateScalarUnsafe(pack).AsByte();
Vector128<ushort> vUShort = Sse2.UnpackLow(vByte, vZero).AsUInt16();
Vector128<int> vInt = Sse2.UnpackLow(vUShort, vZero.AsUInt16()).AsInt32();
Vector128<float> vFloat = Sse2.ConvertToVector128Single(vInt);
Vector128<float> vMax = Vector128.Create((float)byte.MaxValue);
Vector128<float> vNorm = Sse.Divide(vFloat, vMax);

return vNorm.AsVector4();
}

Vector4 linear = new(this.R, this.G, this.B, this.A);
Vector4 normalized = linear / byte.MaxValue;

return normalized;
int pack = Unsafe.As<Rgba32, int>(ref Unsafe.AsRef(in this));
Vector128<byte> vByte = Vector128.CreateScalarUnsafe(pack).AsByte();
Vector128<ushort> vUShort = Vector128.WidenLower(vByte);
Vector128<int> vInt = Vector128.WidenLower(vUShort).AsInt32();
Vector128<float> vFloat = Vector128.ConvertToSingle(vInt);
Vector128<float> vMax = Vector128.Create((float)byte.MaxValue);
Vector128<float> vNorm = Vector128.Divide(vFloat, vMax);

return vNorm.AsVector4();
}

/// <summary>
Expand Down
26 changes: 8 additions & 18 deletions src/ComputeSharp/Imaging/Rgba64.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;

namespace ComputeSharp;

Expand Down Expand Up @@ -85,22 +83,14 @@ public ulong PackedValue
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Float4 ToPixel()
{
if (Sse2.IsSupported)
{
long pack = Unsafe.As<Rgba64, long>(ref Unsafe.AsRef(in this));
Vector128<ushort> vUShort = Vector128.CreateScalarUnsafe(pack).AsUInt16();
Vector128<int> vInt = Sse2.UnpackLow(vUShort, Vector128<ushort>.Zero).AsInt32();
Vector128<float> vFloat = Sse2.ConvertToVector128Single(vInt);
Vector128<float> vMax = Vector128.Create((float)ushort.MaxValue);
Vector128<float> vNorm = Sse.Divide(vFloat, vMax);

return vNorm.AsVector4();
}

Vector4 linear = new(this.R, this.G, this.B, this.A);
Vector4 normalized = linear / ushort.MaxValue;

return normalized;
long pack = Unsafe.As<Rgba64, long>(ref Unsafe.AsRef(in this));
Vector128<ushort> vUShort = Vector128.CreateScalarUnsafe(pack).AsUInt16();
Vector128<int> vInt = Vector128.WidenLower(vUShort).AsInt32();
Vector128<float> vFloat = Vector128.ConvertToSingle(vInt);
Vector128<float> vMax = Vector128.Create((float)ushort.MaxValue);
Vector128<float> vNorm = Vector128.Divide(vFloat, vMax);

return vNorm.AsVector4();
}

/// <summary>
Expand Down

0 comments on commit 142b6cc

Please sign in to comment.