Skip to content

Commit 6b5392b

Browse files
Remove restrictions from vector utilities
1 parent 1a91ec9 commit 6b5392b

5 files changed

Lines changed: 21 additions & 77 deletions

File tree

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ public static void Shuffle4Reduce(
6666
ref Span<float> destination,
6767
[ConstantExpected] byte control)
6868
{
69-
if ((Vector512.IsHardwareAccelerated && Vector512_.SupportsShuffleNativeFloat) ||
70-
(Vector256.IsHardwareAccelerated && Vector256_.SupportsShuffleNativeFloat) ||
71-
Vector128.IsHardwareAccelerated)
69+
if (Vector512.IsHardwareAccelerated ||
70+
Vector256.IsHardwareAccelerated ||
71+
Vector128.IsHardwareAccelerated)
7272
{
7373
int remainder = 0;
7474
if (Vector512.IsHardwareAccelerated)
@@ -112,8 +112,8 @@ public static void Shuffle4Reduce(
112112
ref Span<byte> destination,
113113
[ConstantExpected] byte control)
114114
{
115-
if ((Vector512.IsHardwareAccelerated && Vector512_.SupportsShuffleNativeByte) ||
116-
(Vector256.IsHardwareAccelerated && Vector256_.SupportsShuffleNativeByte) ||
115+
if (Vector512.IsHardwareAccelerated ||
116+
Vector256.IsHardwareAccelerated ||
117117
Vector128.IsHardwareAccelerated)
118118
{
119119
int remainder = 0;
@@ -249,7 +249,7 @@ private static void Shuffle4(
249249
Span<float> destination,
250250
[ConstantExpected] byte control)
251251
{
252-
if (Vector512.IsHardwareAccelerated && Vector512_.SupportsShuffleNativeFloat)
252+
if (Vector512.IsHardwareAccelerated)
253253
{
254254
ref Vector512<float> sourceBase = ref Unsafe.As<float, Vector512<float>>(ref MemoryMarshal.GetReference(source));
255255
ref Vector512<float> destinationBase = ref Unsafe.As<float, Vector512<float>>(ref MemoryMarshal.GetReference(destination));
@@ -277,7 +277,7 @@ private static void Shuffle4(
277277
}
278278
}
279279
}
280-
else if (Vector256.IsHardwareAccelerated && Vector256_.SupportsShuffleNativeFloat)
280+
else if (Vector256.IsHardwareAccelerated)
281281
{
282282
ref Vector256<float> sourceBase = ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(source));
283283
ref Vector256<float> destinationBase = ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(destination));
@@ -341,7 +341,7 @@ private static void Shuffle4(
341341
Span<byte> destination,
342342
[ConstantExpected] byte control)
343343
{
344-
if (Vector512.IsHardwareAccelerated && Vector512_.SupportsShuffleNativeByte)
344+
if (Vector512.IsHardwareAccelerated)
345345
{
346346
Span<byte> temp = stackalloc byte[Vector512<byte>.Count];
347347
Shuffle.MMShuffleSpan(ref temp, control);
@@ -373,7 +373,7 @@ private static void Shuffle4(
373373
}
374374
}
375375
}
376-
else if (Vector256.IsHardwareAccelerated && Vector256_.SupportsShuffleNativeByte)
376+
else if (Vector256.IsHardwareAccelerated)
377377
{
378378
Span<byte> temp = stackalloc byte[Vector256<byte>.Count];
379379
Shuffle.MMShuffleSpan(ref temp, control);

src/ImageSharp/Common/Helpers/Vector128Utilities.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1323,7 +1323,9 @@ public static int MoveMask(Vector128<byte> value)
13231323
{
13241324
// https://stackoverflow.com/questions/11870910/sse-mm-movemask-epi8-equivalent-method-for-arm-neon
13251325
Vector128<byte> powers = Vector128.Create(1, 2, 4, 8, 16, 32, 64, 128, 1, 2, 4, 8, 16, 32, 64, 128);
1326-
Vector128<byte> masked = value & powers;
1326+
Vector128<byte> msbMask = Vector128.Create((byte)0x80);
1327+
Vector128<byte> normalized = AdvSimd.CompareEqual(value & msbMask, msbMask); // 0xFF or 0x00
1328+
Vector128<byte> masked = normalized & powers;
13271329

13281330
Vector128<ushort> sum8 = AdvSimd.AddPairwiseWidening(masked);
13291331
Vector128<uint> sum16 = AdvSimd.AddPairwiseWidening(sum8);

src/ImageSharp/Common/Helpers/Vector256Utilities.cs

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4-
using System.Diagnostics;
54
using System.Diagnostics.CodeAnalysis;
65
using System.Runtime.CompilerServices;
76
using System.Runtime.Intrinsics;
@@ -21,24 +20,6 @@ namespace SixLabors.ImageSharp.Common.Helpers;
2120
internal static class Vector256_
2221
#pragma warning restore SA1649 // File name should match first type name
2322
{
24-
/// <summary>
25-
/// Gets a value indicating whether shuffle byte operations are supported.
26-
/// </summary>
27-
public static bool SupportsShuffleNativeFloat
28-
{
29-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
30-
get => Avx.IsSupported;
31-
}
32-
33-
/// <summary>
34-
/// Gets a value indicating whether shuffle byte operations are supported.
35-
/// </summary>
36-
public static bool SupportsShuffleNativeByte
37-
{
38-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
39-
get => Avx2.IsSupported;
40-
}
41-
4223
/// <summary>
4324
/// Creates a new vector by selecting values from an input vector using a set of indices.
4425
/// </summary>
@@ -47,15 +28,7 @@ public static bool SupportsShuffleNativeByte
4728
/// <returns>The <see cref="Vector256{Single}"/>.</returns>
4829
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4930
public static Vector256<float> ShuffleNative(Vector256<float> vector, [ConstantExpected] byte control)
50-
{
51-
if (Avx.IsSupported)
52-
{
53-
return Avx.Shuffle(vector, vector, control);
54-
}
55-
56-
ThrowUnreachableException();
57-
return default;
58-
}
31+
=> Avx.Shuffle(vector, vector, control);
5932

6033
/// <summary>
6134
/// Creates a new vector by selecting values from an input vector using a set of indices.</summary>
@@ -73,8 +46,9 @@ public static Vector256<byte> ShuffleNative(Vector256<byte> vector, Vector256<by
7346
return Avx2.Shuffle(vector, indices);
7447
}
7548

76-
ThrowUnreachableException();
77-
return default;
49+
return Vector256.Create(
50+
Vector128_.ShuffleNative(vector.GetLower(), indices.GetLower()),
51+
Vector128_.ShuffleNative(vector.GetUpper(), indices.GetUpper()));
7852
}
7953

8054
/// <summary>
@@ -508,7 +482,4 @@ public static int MoveMask(Vector256<byte> value)
508482
int hiMask = Vector128_.MoveMask(value.GetUpper());
509483
return loMask | (hiMask << 16);
510484
}
511-
512-
[DoesNotReturn]
513-
private static void ThrowUnreachableException() => throw new UnreachableException();
514485
}

src/ImageSharp/Common/Helpers/Vector512Utilities.cs

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4-
using System.Diagnostics;
54
using System.Diagnostics.CodeAnalysis;
65
using System.Runtime.CompilerServices;
76
using System.Runtime.Intrinsics;
@@ -21,24 +20,6 @@ namespace SixLabors.ImageSharp.Common.Helpers;
2120
internal static class Vector512_
2221
#pragma warning restore SA1649 // File name should match first type name
2322
{
24-
/// <summary>
25-
/// Gets a value indicating whether shuffle float operations are supported.
26-
/// </summary>
27-
public static bool SupportsShuffleNativeFloat
28-
{
29-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
30-
get => Avx512F.IsSupported;
31-
}
32-
33-
/// <summary>
34-
/// Gets a value indicating whether shuffle byte operations are supported.
35-
/// </summary>
36-
public static bool SupportsShuffleNativeByte
37-
{
38-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
39-
get => Avx512BW.IsSupported;
40-
}
41-
4223
/// <summary>
4324
/// Creates a new vector by selecting values from an input vector using the control.
4425
/// </summary>
@@ -47,15 +28,7 @@ public static bool SupportsShuffleNativeByte
4728
/// <returns>The <see cref="Vector512{Single}"/>.</returns>
4829
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4930
public static Vector512<float> ShuffleNative(Vector512<float> vector, [ConstantExpected] byte control)
50-
{
51-
if (Avx512F.IsSupported)
52-
{
53-
return Avx512F.Shuffle(vector, vector, control);
54-
}
55-
56-
ThrowUnreachableException();
57-
return default;
58-
}
31+
=> Avx512F.Shuffle(vector, vector, control);
5932

6033
/// <summary>
6134
/// Creates a new vector by selecting values from an input vector using a set of indices.
@@ -73,8 +46,9 @@ public static Vector512<byte> ShuffleNative(Vector512<byte> vector, Vector512<by
7346
return Avx512BW.Shuffle(vector, indices);
7447
}
7548

76-
ThrowUnreachableException();
77-
return default;
49+
return Vector512.Create(
50+
Vector256_.ShuffleNative(vector.GetLower(), indices.GetLower()),
51+
Vector256_.ShuffleNative(vector.GetUpper(), indices.GetUpper()));
7852
}
7953

8054
/// <summary>
@@ -175,7 +149,4 @@ public static Vector512<float> MultiplyAdd(
175149
[MethodImpl(MethodImplOptions.AggressiveInlining)]
176150
public static Vector512<T> Clamp<T>(Vector512<T> value, Vector512<T> min, Vector512<T> max)
177151
=> Vector512.Min(Vector512.Max(value, min), max);
178-
179-
[DoesNotReturn]
180-
private static void ThrowUnreachableException() => throw new UnreachableException();
181152
}

src/ImageSharp/Formats/Webp/Lossless/ColorSpaceTransformUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ internal static class ColorSpaceTransformUtils
1212
{
1313
public static void CollectColorBlueTransforms(Span<uint> bgra, int stride, int tileWidth, int tileHeight, int greenToBlue, int redToBlue, Span<int> histo)
1414
{
15-
if (Vector256_.SupportsShuffleNativeByte && tileWidth >= 16)
15+
if (Vector256.IsHardwareAccelerated && tileWidth >= 16)
1616
{
1717
const int span = 16;
1818
Span<ushort> values = stackalloc ushort[span];

0 commit comments

Comments
 (0)