Browse Source

Simplify checks

pull/2654/head
James Jackson-South 2 years ago
parent
commit
4e494d427f
  1. 7
      src/ImageSharp/Common/Helpers/SimdUtils.Convert.cs
  2. 26
      src/ImageSharp/Common/Helpers/SimdUtils.HwIntrinsics.cs

7
src/ImageSharp/Common/Helpers/SimdUtils.Convert.cs

@ -57,7 +57,7 @@ internal static partial class SimdUtils
for (int i = 0; i < source.Length; i++) for (int i = 0; i < source.Length; i++)
{ {
Unsafe.Add(ref dBase, i) = Unsafe.Add(ref sBase, i) / 255f; Unsafe.Add(ref dBase, (uint)i) = Unsafe.Add(ref sBase, (uint)i) / 255f;
} }
} }
@ -66,12 +66,13 @@ internal static partial class SimdUtils
{ {
ref float sBase = ref MemoryMarshal.GetReference(source); ref float sBase = ref MemoryMarshal.GetReference(source);
ref byte dBase = ref MemoryMarshal.GetReference(destination); ref byte dBase = ref MemoryMarshal.GetReference(destination);
for (int i = 0; i < source.Length; i++) for (int i = 0; i < source.Length; i++)
{ {
Unsafe.Add(ref dBase, i) = ConvertToByte(Unsafe.Add(ref sBase, i)); Unsafe.Add(ref dBase, (uint)i) = ConvertToByte(Unsafe.Add(ref sBase, (uint)i));
} }
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private static byte ConvertToByte(float f) => (byte)Numerics.Clamp((f * 255F) + 0.5F, 0, 255F); private static byte ConvertToByte(float f) => (byte)Numerics.Clamp((f * 255f) + 0.5f, 0, 255f);
} }

26
src/ImageSharp/Common/Helpers/SimdUtils.HwIntrinsics.cs

@ -761,13 +761,10 @@ internal static partial class SimdUtils
{ {
DebugGuard.IsTrue(source.Length == destination.Length, nameof(source), "Input spans must be of same length!"); DebugGuard.IsTrue(source.Length == destination.Length, nameof(source), "Input spans must be of same length!");
if ((Vector512.IsHardwareAccelerated && Avx512F.IsSupported) || if (Vector128.IsHardwareAccelerated)
Avx2.IsSupported ||
Sse2.IsSupported ||
AdvSimd.IsSupported)
{ {
int remainder; int remainder;
if (Avx512F.IsSupported) if (Vector512.IsHardwareAccelerated && Avx512F.IsSupported)
{ {
remainder = Numerics.ModuloP2(source.Length, Vector512<byte>.Count); remainder = Numerics.ModuloP2(source.Length, Vector512<byte>.Count);
} }
@ -805,7 +802,7 @@ internal static partial class SimdUtils
ReadOnlySpan<byte> source, ReadOnlySpan<byte> source,
Span<float> destination) Span<float> destination)
{ {
if (Avx512F.IsSupported) if (Vector512.IsHardwareAccelerated && Avx512F.IsSupported)
{ {
DebugVerifySpanInput(source, destination, Vector512<byte>.Count); DebugVerifySpanInput(source, destination, Vector512<byte>.Count);
@ -870,7 +867,7 @@ internal static partial class SimdUtils
Unsafe.Add(ref d, 3) = f3; Unsafe.Add(ref d, 3) = f3;
} }
} }
else if (Sse2.IsSupported || AdvSimd.IsSupported) else if (Vector128.IsHardwareAccelerated)
{ {
DebugVerifySpanInput(source, destination, Vector128<byte>.Count); DebugVerifySpanInput(source, destination, Vector128<byte>.Count);
@ -897,7 +894,7 @@ internal static partial class SimdUtils
} }
else else
{ {
// Sse2, AdvSimd // Sse2, AdvSimd, etc
Vector128<byte> b = Vector128.LoadUnsafe(ref sourceBase, si); Vector128<byte> b = Vector128.LoadUnsafe(ref sourceBase, si);
(Vector128<ushort> s0, Vector128<ushort> s1) = Vector128.Widen(b); (Vector128<ushort> s0, Vector128<ushort> s1) = Vector128.Widen(b);
(i0, i1) = Vector128.Widen(s0.AsInt16()); (i0, i1) = Vector128.Widen(s0.AsInt16());
@ -931,13 +928,11 @@ internal static partial class SimdUtils
{ {
DebugGuard.IsTrue(source.Length == destination.Length, nameof(source), "Input spans must be of same length!"); DebugGuard.IsTrue(source.Length == destination.Length, nameof(source), "Input spans must be of same length!");
if ((Vector512.IsHardwareAccelerated && Avx512BW.IsSupported) || if (Sse2.IsSupported || AdvSimd.IsSupported)
(Vector256.IsHardwareAccelerated && Avx2.IsSupported) ||
(Vector128.IsHardwareAccelerated && (Sse2.IsSupported || AdvSimd.IsSupported)))
{ {
int remainder; int remainder;
if (Avx512BW.IsSupported) if (Vector512.IsHardwareAccelerated && Avx512BW.IsSupported)
{ {
remainder = Numerics.ModuloP2(source.Length, Vector512<byte>.Count); remainder = Numerics.ModuloP2(source.Length, Vector512<byte>.Count);
} }
@ -977,7 +972,7 @@ internal static partial class SimdUtils
ReadOnlySpan<float> source, ReadOnlySpan<float> source,
Span<byte> destination) Span<byte> destination)
{ {
if (Avx512BW.IsSupported) if (Vector512.IsHardwareAccelerated && Avx512BW.IsSupported)
{ {
DebugVerifySpanInput(source, destination, Vector512<byte>.Count); DebugVerifySpanInput(source, destination, Vector512<byte>.Count);
@ -1011,8 +1006,7 @@ internal static partial class SimdUtils
Unsafe.Add(ref destinationBase, i) = b; Unsafe.Add(ref destinationBase, i) = b;
} }
} }
else else if (Avx2.IsSupported)
if (Avx2.IsSupported)
{ {
DebugVerifySpanInput(source, destination, Vector256<byte>.Count); DebugVerifySpanInput(source, destination, Vector256<byte>.Count);
@ -1046,7 +1040,7 @@ internal static partial class SimdUtils
Unsafe.Add(ref destinationBase, i) = b; Unsafe.Add(ref destinationBase, i) = b;
} }
} }
else else if (Sse2.IsSupported || AdvSimd.IsSupported)
{ {
// Sse, AdvSimd // Sse, AdvSimd
DebugVerifySpanInput(source, destination, Vector128<byte>.Count); DebugVerifySpanInput(source, destination, Vector128<byte>.Count);

Loading…
Cancel
Save