Skip to content

Commit 5b94795

Browse files
Address feedback
1 parent 277cf61 commit 5b94795

9 files changed

+42
-20
lines changed

src/ImageSharp/Common/Helpers/SimdUtils.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Numerics;
66
using System.Runtime.CompilerServices;
77
using System.Runtime.Intrinsics;
8+
using System.Runtime.Intrinsics.Arm;
89
using System.Runtime.Intrinsics.X86;
910

1011
namespace SixLabors.ImageSharp;
@@ -40,13 +41,28 @@ internal static Vector4 PseudoRound(this Vector4 v)
4041
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4142
internal static Vector<float> FastRound(this Vector<float> v)
4243
{
44+
// .NET9+ has a built-in method for this Vector.Round
4345
if (Avx2.IsSupported && Vector<float>.Count == Vector256<float>.Count)
4446
{
4547
ref Vector256<float> v256 = ref Unsafe.As<Vector<float>, Vector256<float>>(ref v);
4648
Vector256<float> vRound = Avx.RoundToNearestInteger(v256);
4749
return Unsafe.As<Vector256<float>, Vector<float>>(ref vRound);
4850
}
4951

52+
if (Sse41.IsSupported && Vector<float>.Count == Vector128<float>.Count)
53+
{
54+
ref Vector128<float> v128 = ref Unsafe.As<Vector<float>, Vector128<float>>(ref v);
55+
Vector128<float> vRound = Sse41.RoundToNearestInteger(v128);
56+
return Unsafe.As<Vector128<float>, Vector<float>>(ref vRound);
57+
}
58+
59+
if (AdvSimd.IsSupported && Vector<float>.Count == Vector128<float>.Count)
60+
{
61+
ref Vector128<float> v128 = ref Unsafe.As<Vector<float>, Vector128<float>>(ref v);
62+
Vector128<float> vRound = AdvSimd.RoundToNearest(v128);
63+
return Unsafe.As<Vector128<float>, Vector<float>>(ref vRound);
64+
}
65+
5066
// https://github.com/g-truc/glm/blob/master/glm/simd/common.h#L11
5167
Vector<float> sign = v & new Vector<float>(-0F);
5268
Vector<float> val_2p23_f32 = sign | new Vector<float>(8388608F);

src/ImageSharp/Common/Helpers/Vector128Utilities.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,11 @@ public static Vector128<float> MultiplyAdd(
244244
return Fma.MultiplyAdd(vm1, vm0, va);
245245
}
246246

247+
if (AdvSimd.IsSupported)
248+
{
249+
return AdvSimd.FusedMultiplyAdd(va, vm0, vm1);
250+
}
251+
247252
return va + (vm0 * vm1);
248253
}
249254

src/ImageSharp/Common/Helpers/Vector256Utilities.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,6 @@ public static Vector256<float> MultiplyAdd(
149149
return Fma.MultiplyAdd(vm0, vm1, va);
150150
}
151151

152-
if (Avx.IsSupported)
153-
{
154-
return Avx.Add(Avx.Multiply(vm0, vm1), va);
155-
}
156-
157152
return va + (vm0 * vm1);
158153
}
159154

src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.CmykVector128.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,12 @@ public static void ConvertFromRgb(in ComponentValues values, float maxValue, Spa
7878
Vector128<float> ytmp = scale - Unsafe.Add(ref srcB, i);
7979
Vector128<float> ktmp = Vector128.Min(ctmp, Vector128.Min(mtmp, ytmp));
8080

81-
Vector128<float> kMask = Vector128.Equals(ktmp, scale);
82-
ctmp = Vector128.AndNot((ctmp - ktmp) / (scale - ktmp), kMask);
83-
mtmp = Vector128.AndNot((mtmp - ktmp) / (scale - ktmp), kMask);
84-
ytmp = Vector128.AndNot((ytmp - ktmp) / (scale - ktmp), kMask);
81+
Vector128<float> kMask = ~Vector128.Equals(ktmp, scale);
82+
Vector128<float> divisor = scale - ktmp;
83+
84+
ctmp = ((ctmp - ktmp) / divisor) & kMask;
85+
mtmp = ((mtmp - ktmp) / divisor) & kMask;
86+
ytmp = ((ytmp - ktmp) / divisor) & kMask;
8587

8688
Unsafe.Add(ref destC, i) = scale - (ctmp * scale);
8789
Unsafe.Add(ref destM, i) = scale - (mtmp * scale);

src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.CmykVector256.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,12 @@ public static void ConvertFromRgb(in ComponentValues values, float maxValue, Spa
7878
Vector256<float> ytmp = scale - Unsafe.Add(ref srcB, i);
7979
Vector256<float> ktmp = Vector256.Min(ctmp, Vector256.Min(mtmp, ytmp));
8080

81-
Vector256<float> kMask = Vector256.Equals(ktmp, scale);
82-
ctmp = Vector256.AndNot((ctmp - ktmp) / (scale - ktmp), kMask);
83-
mtmp = Vector256.AndNot((mtmp - ktmp) / (scale - ktmp), kMask);
84-
ytmp = Vector256.AndNot((ytmp - ktmp) / (scale - ktmp), kMask);
81+
Vector256<float> kMask = ~Vector256.Equals(ktmp, scale);
82+
Vector256<float> divisor = scale - ktmp;
83+
84+
ctmp = ((ctmp - ktmp) / divisor) & kMask;
85+
mtmp = ((mtmp - ktmp) / divisor) & kMask;
86+
ytmp = ((ytmp - ktmp) / divisor) & kMask;
8587

8688
Unsafe.Add(ref destC, i) = scale - (ctmp * scale);
8789
Unsafe.Add(ref destM, i) = scale - (mtmp * scale);

src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverter.CmykVector512.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,12 @@ internal static void ConvertFromRgbVectorized(in ComponentValues values, float m
8686
Vector512<float> ytmp = scale - Unsafe.Add(ref srcB, i);
8787
Vector512<float> ktmp = Vector512.Min(ctmp, Vector512.Min(mtmp, ytmp));
8888

89-
Vector512<float> kMask = Vector512.Equals(ktmp, scale);
90-
ctmp = Vector512.AndNot((ctmp - ktmp) / (scale - ktmp), kMask);
91-
mtmp = Vector512.AndNot((mtmp - ktmp) / (scale - ktmp), kMask);
92-
ytmp = Vector512.AndNot((ytmp - ktmp) / (scale - ktmp), kMask);
89+
Vector512<float> kMask = ~Vector512.Equals(ktmp, scale);
90+
Vector512<float> divisor = scale - ktmp;
91+
92+
ctmp = ((ctmp - ktmp) / divisor) & kMask;
93+
mtmp = ((mtmp - ktmp) / divisor) & kMask;
94+
ytmp = ((ytmp - ktmp) / divisor) & kMask;
9395

9496
Unsafe.Add(ref destC, i) = scale - (ctmp * scale);
9597
Unsafe.Add(ref destM, i) = scale - (mtmp * scale);

src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverterVector128.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ protected JpegColorConverterVector128(JpegColorSpace colorSpace, int precision)
2525
{
2626
}
2727

28-
public static bool IsSupported => Vector128.IsHardwareAccelerated && Vector128<float>.IsSupported;
28+
public static bool IsSupported => Vector128.IsHardwareAccelerated;
2929

3030
public sealed override bool IsAvailable => IsSupported;
3131

src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverterVector256.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ protected JpegColorConverterVector256(JpegColorSpace colorSpace, int precision)
2525
{
2626
}
2727

28-
public static bool IsSupported => Vector256.IsHardwareAccelerated && Vector256<float>.IsSupported;
28+
public static bool IsSupported => Vector256.IsHardwareAccelerated;
2929

3030
public sealed override bool IsAvailable => IsSupported;
3131

src/ImageSharp/Formats/Jpeg/Components/ColorConverters/JpegColorConverterVector512.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ protected JpegColorConverterVector512(JpegColorSpace colorSpace, int precision)
1919
{
2020
}
2121

22-
public static bool IsSupported => Vector512.IsHardwareAccelerated && Vector512<float>.IsSupported;
22+
public static bool IsSupported => Vector512.IsHardwareAccelerated;
2323

2424
/// <inheritdoc/>
2525
public override bool IsAvailable => IsSupported;

0 commit comments

Comments
 (0)