Skip to content

Commit 505ecce

Browse files
Update ShuffleNative (byte)
1 parent 6238f00 commit 505ecce

File tree

4 files changed

+36
-22
lines changed

4 files changed

+36
-22
lines changed

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public static void Shuffle4Reduce(
113113
[ConstantExpected] byte control)
114114
{
115115
if ((Vector512.IsHardwareAccelerated && Vector512_.SupportsShuffleNativeByte) ||
116-
(Vector256.IsHardwareAccelerated && Vector256_.SupportsShuffleByte) ||
116+
(Vector256.IsHardwareAccelerated && Vector256_.SupportsShuffleNativeByte) ||
117117
(Vector128.IsHardwareAccelerated && Vector128_.SupportsShuffleNativeByte))
118118
{
119119
int remainder = 0;
@@ -158,7 +158,7 @@ public static void Shuffle3Reduce(
158158
ref Span<byte> destination,
159159
[ConstantExpected] byte control)
160160
{
161-
if (Vector128.IsHardwareAccelerated && Vector128_.SupportsShuffleNativeByte && Vector128_.SupportsRightAlign)
161+
if (Vector128.IsHardwareAccelerated && Vector128_.SupportsShuffleNativeByte && Vector128_.SupportsAlignRight)
162162
{
163163
int remainder = source.Length % (Vector128<byte>.Count * 3);
164164

@@ -373,7 +373,7 @@ private static void Shuffle4(
373373
}
374374
}
375375
}
376-
else if (Vector256.IsHardwareAccelerated && Vector256_.SupportsShuffleByte)
376+
else if (Vector256.IsHardwareAccelerated && Vector256_.SupportsShuffleNativeByte)
377377
{
378378
Span<byte> temp = stackalloc byte[Vector256<byte>.Count];
379379
Shuffle.MMShuffleSpan(ref temp, control);
@@ -445,7 +445,9 @@ private static void Shuffle3(
445445
Span<byte> destination,
446446
[ConstantExpected] byte control)
447447
{
448-
if (Vector128.IsHardwareAccelerated && Vector128_.SupportsShuffleNativeByte && Vector128_.SupportsRightAlign)
448+
if (Vector128.IsHardwareAccelerated &&
449+
Vector128_.SupportsShuffleNativeByte &&
450+
Vector128_.SupportsAlignRight)
449451
{
450452
Vector128<byte> maskPad4Nx16 = ShuffleMaskPad4Nx16();
451453
Vector128<byte> maskSlice4Nx16 = ShuffleMaskSlice4Nx16();
@@ -505,7 +507,10 @@ private static void Pad3Shuffle4(
505507
Span<byte> destination,
506508
[ConstantExpected] byte control)
507509
{
508-
if (Vector128.IsHardwareAccelerated && Vector128_.SupportsShuffleNativeByte && Vector128_.SupportsShiftByte)
510+
if (Vector128.IsHardwareAccelerated &&
511+
Vector128_.SupportsShuffleNativeByte &&
512+
Vector128_.SupportsShiftByte &&
513+
Vector128_.SupportsAlignRight)
509514
{
510515
Vector128<byte> maskPad4Nx16 = ShuffleMaskPad4Nx16();
511516
Vector128<byte> fill = Vector128.Create(0xff000000ff000000ul).AsByte();
@@ -548,7 +553,10 @@ private static void Shuffle4Slice3(
548553
Span<byte> destination,
549554
[ConstantExpected] byte control)
550555
{
551-
if (Vector128.IsHardwareAccelerated && Vector128_.SupportsShuffleNativeByte && Vector128_.SupportsShiftByte)
556+
if (Vector128.IsHardwareAccelerated &&
557+
Vector128_.SupportsShuffleNativeByte &&
558+
Vector128_.SupportsShiftByte &&
559+
Vector128_.SupportsAlignRight)
552560
{
553561
Vector128<byte> maskSlice4Nx16 = ShuffleMaskSlice4Nx16();
554562
Vector128<byte> maskE = Vector128_.AlignRight(maskSlice4Nx16, maskSlice4Nx16, 12);

src/ImageSharp/Common/Helpers/Vector128Utilities.cs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Diagnostics;
55
using System.Diagnostics.CodeAnalysis;
66
using System.Runtime.CompilerServices;
7+
using System.Runtime.InteropServices;
78
using System.Runtime.Intrinsics;
89
using System.Runtime.Intrinsics.Arm;
910
using System.Runtime.Intrinsics.Wasm;
@@ -38,13 +39,26 @@ public static bool SupportsShuffleNativeFloat
3839
public static bool SupportsShuffleNativeByte
3940
{
4041
[MethodImpl(MethodImplOptions.AggressiveInlining)]
41-
get => Ssse3.IsSupported || AdvSimd.Arm64.IsSupported || PackedSimd.IsSupported;
42+
get
43+
{
44+
if (Vector128.IsHardwareAccelerated)
45+
{
46+
if (RuntimeInformation.ProcessArchitecture is Architecture.X86 or Architecture.X64)
47+
{
48+
return Ssse3.IsSupported;
49+
}
50+
51+
return true;
52+
}
53+
54+
return false;
55+
}
4256
}
4357

4458
/// <summary>
4559
/// Gets a value indicating whether right align operations are supported.
4660
/// </summary>
47-
public static bool SupportsRightAlign
61+
public static bool SupportsAlignRight
4862
{
4963
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5064
get => Ssse3.IsSupported || AdvSimd.IsSupported;
@@ -91,23 +105,16 @@ public static Vector128<float> ShuffleNative(Vector128<float> vector, [ConstantE
91105
[MethodImpl(MethodImplOptions.AggressiveInlining)]
92106
public static Vector128<byte> ShuffleNative(Vector128<byte> vector, Vector128<byte> indices)
93107
{
108+
// For x64 we use the SSSE3 shuffle intrinsic to avoid additional instructions. 3 vs 1.
94109
if (Ssse3.IsSupported)
95110
{
96111
return Ssse3.Shuffle(vector, indices);
97112
}
98113

99-
if (AdvSimd.Arm64.IsSupported)
100-
{
101-
return AdvSimd.Arm64.VectorTableLookup(vector, indices);
102-
}
103-
104-
if (PackedSimd.IsSupported)
105-
{
106-
return PackedSimd.Swizzle(vector, indices);
107-
}
108-
109-
ThrowUnreachableException();
110-
return default;
114+
// For ARM and WASM, codegen will be optimal.
115+
// We don't throw for x86/x64 so we should never use this method without
116+
// checking for support.
117+
return Vector128.Shuffle(vector, indices);
111118
}
112119

113120
/// <summary>

src/ImageSharp/Common/Helpers/Vector256Utilities.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static bool SupportsShuffleNativeFloat
3333
/// <summary>
3434
/// Gets a value indicating whether shuffle byte operations are supported.
3535
/// </summary>
36-
public static bool SupportsShuffleByte
36+
public static bool SupportsShuffleNativeByte
3737
{
3838
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3939
get => Avx2.IsSupported;

src/ImageSharp/Formats/Jpeg/Components/Block8x8F.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Runtime.CompilerServices;
66
using System.Runtime.InteropServices;
77
using System.Runtime.Intrinsics;
8-
using System.Runtime.Intrinsics.X86;
98
using System.Text;
109
using SixLabors.ImageSharp.Common.Helpers;
1110

0 commit comments

Comments
 (0)