Skip to content

Commit 4e494d4

Browse files
Simplify checks
1 parent 0ec839f commit 4e494d4

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private static void ConvertByteToNormalizedFloatRemainder(ReadOnlySpan<byte> sou
5757

5858
for (int i = 0; i < source.Length; i++)
5959
{
60-
Unsafe.Add(ref dBase, i) = Unsafe.Add(ref sBase, i) / 255f;
60+
Unsafe.Add(ref dBase, (uint)i) = Unsafe.Add(ref sBase, (uint)i) / 255f;
6161
}
6262
}
6363

@@ -66,12 +66,13 @@ private static void ConvertNormalizedFloatToByteRemainder(ReadOnlySpan<float> so
6666
{
6767
ref float sBase = ref MemoryMarshal.GetReference(source);
6868
ref byte dBase = ref MemoryMarshal.GetReference(destination);
69+
6970
for (int i = 0; i < source.Length; i++)
7071
{
71-
Unsafe.Add(ref dBase, i) = ConvertToByte(Unsafe.Add(ref sBase, i));
72+
Unsafe.Add(ref dBase, (uint)i) = ConvertToByte(Unsafe.Add(ref sBase, (uint)i));
7273
}
7374
}
7475

7576
[MethodImpl(MethodImplOptions.AggressiveInlining)]
76-
private static byte ConvertToByte(float f) => (byte)Numerics.Clamp((f * 255F) + 0.5F, 0, 255F);
77+
private static byte ConvertToByte(float f) => (byte)Numerics.Clamp((f * 255f) + 0.5f, 0, 255f);
7778
}

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

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -761,13 +761,10 @@ internal static void ByteToNormalizedFloatReduce(
761761
{
762762
DebugGuard.IsTrue(source.Length == destination.Length, nameof(source), "Input spans must be of same length!");
763763

764-
if ((Vector512.IsHardwareAccelerated && Avx512F.IsSupported) ||
765-
Avx2.IsSupported ||
766-
Sse2.IsSupported ||
767-
AdvSimd.IsSupported)
764+
if (Vector128.IsHardwareAccelerated)
768765
{
769766
int remainder;
770-
if (Avx512F.IsSupported)
767+
if (Vector512.IsHardwareAccelerated && Avx512F.IsSupported)
771768
{
772769
remainder = Numerics.ModuloP2(source.Length, Vector512<byte>.Count);
773770
}
@@ -805,7 +802,7 @@ internal static unsafe void ByteToNormalizedFloat(
805802
ReadOnlySpan<byte> source,
806803
Span<float> destination)
807804
{
808-
if (Avx512F.IsSupported)
805+
if (Vector512.IsHardwareAccelerated && Avx512F.IsSupported)
809806
{
810807
DebugVerifySpanInput(source, destination, Vector512<byte>.Count);
811808

@@ -870,7 +867,7 @@ internal static unsafe void ByteToNormalizedFloat(
870867
Unsafe.Add(ref d, 3) = f3;
871868
}
872869
}
873-
else if (Sse2.IsSupported || AdvSimd.IsSupported)
870+
else if (Vector128.IsHardwareAccelerated)
874871
{
875872
DebugVerifySpanInput(source, destination, Vector128<byte>.Count);
876873

@@ -897,7 +894,7 @@ internal static unsafe void ByteToNormalizedFloat(
897894
}
898895
else
899896
{
900-
// Sse2, AdvSimd
897+
// Sse2, AdvSimd, etc
901898
Vector128<byte> b = Vector128.LoadUnsafe(ref sourceBase, si);
902899
(Vector128<ushort> s0, Vector128<ushort> s1) = Vector128.Widen(b);
903900
(i0, i1) = Vector128.Widen(s0.AsInt16());
@@ -931,13 +928,11 @@ internal static void NormalizedFloatToByteSaturateReduce(
931928
{
932929
DebugGuard.IsTrue(source.Length == destination.Length, nameof(source), "Input spans must be of same length!");
933930

934-
if ((Vector512.IsHardwareAccelerated && Avx512BW.IsSupported) ||
935-
(Vector256.IsHardwareAccelerated && Avx2.IsSupported) ||
936-
(Vector128.IsHardwareAccelerated && (Sse2.IsSupported || AdvSimd.IsSupported)))
931+
if (Sse2.IsSupported || AdvSimd.IsSupported)
937932
{
938933
int remainder;
939934

940-
if (Avx512BW.IsSupported)
935+
if (Vector512.IsHardwareAccelerated && Avx512BW.IsSupported)
941936
{
942937
remainder = Numerics.ModuloP2(source.Length, Vector512<byte>.Count);
943938
}
@@ -977,7 +972,7 @@ internal static void NormalizedFloatToByteSaturate(
977972
ReadOnlySpan<float> source,
978973
Span<byte> destination)
979974
{
980-
if (Avx512BW.IsSupported)
975+
if (Vector512.IsHardwareAccelerated && Avx512BW.IsSupported)
981976
{
982977
DebugVerifySpanInput(source, destination, Vector512<byte>.Count);
983978

@@ -1011,8 +1006,7 @@ internal static void NormalizedFloatToByteSaturate(
10111006
Unsafe.Add(ref destinationBase, i) = b;
10121007
}
10131008
}
1014-
else
1015-
if (Avx2.IsSupported)
1009+
else if (Avx2.IsSupported)
10161010
{
10171011
DebugVerifySpanInput(source, destination, Vector256<byte>.Count);
10181012

@@ -1046,7 +1040,7 @@ internal static void NormalizedFloatToByteSaturate(
10461040
Unsafe.Add(ref destinationBase, i) = b;
10471041
}
10481042
}
1049-
else
1043+
else if (Sse2.IsSupported || AdvSimd.IsSupported)
10501044
{
10511045
// Sse, AdvSimd
10521046
DebugVerifySpanInput(source, destination, Vector128<byte>.Count);

0 commit comments

Comments
 (0)