Skip to content

Commit a59c900

Browse files
More optimizations based on feedback
1 parent 55a8c73 commit a59c900

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,8 @@ internal static void NormalizedFloatToByteSaturate(
10081008
ref Vector128<byte> destinationBase = ref Unsafe.As<byte, Vector128<byte>>(ref MemoryMarshal.GetReference(destination));
10091009

10101010
Vector128<float> scale = Vector128.Create((float)byte.MaxValue);
1011+
Vector128<int> min = Vector128<int>.Zero;
1012+
Vector128<int> max = Vector128.Create((int)byte.MaxValue);
10111013

10121014
for (nuint i = 0; i < n; i++)
10131015
{
@@ -1023,10 +1025,15 @@ internal static void NormalizedFloatToByteSaturate(
10231025
Vector128<int> w2 = Vector128_.ConvertToInt32RoundToEven(f2);
10241026
Vector128<int> w3 = Vector128_.ConvertToInt32RoundToEven(f3);
10251027

1026-
Vector128<short> u0 = Vector128_.PackSignedSaturate(w0, w1);
1027-
Vector128<short> u1 = Vector128_.PackSignedSaturate(w2, w3);
1028+
w0 = Vector128_.Clamp(w0, min, max);
1029+
w1 = Vector128_.Clamp(w1, min, max);
1030+
w2 = Vector128_.Clamp(w2, min, max);
1031+
w3 = Vector128_.Clamp(w3, min, max);
10281032

1029-
Unsafe.Add(ref destinationBase, i) = Vector128_.PackUnsignedSaturate(u0, u1);
1033+
Vector128<short> u0 = Vector128.Narrow(w0, w1);
1034+
Vector128<short> u1 = Vector128.Narrow(w2, w3);
1035+
1036+
Unsafe.Add(ref destinationBase, i) = Vector128.Narrow(u0, u1).AsByte();
10301037
}
10311038
}
10321039
}

src/ImageSharp/Common/Helpers/Vector128Utilities.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ public static Vector128<int> ConvertToInt32RoundToEven(Vector128<float> vector)
205205
return AdvSimd.ConvertToInt32RoundToEven(vector);
206206
}
207207

208+
if (PackedSimd.IsSupported)
209+
{
210+
return PackedSimd.ConvertToInt32Saturate(PackedSimd.RoundToNearest(vector));
211+
}
212+
208213
Vector128<float> sign = vector & Vector128.Create(-0F);
209214
Vector128<float> val_2p23_f32 = sign | Vector128.Create(8388608F);
210215

@@ -230,6 +235,11 @@ public static Vector128<float> RoundToNearestInteger(Vector128<float> vector)
230235
return AdvSimd.RoundToNearest(vector);
231236
}
232237

238+
if (PackedSimd.IsSupported)
239+
{
240+
return PackedSimd.RoundToNearest(vector);
241+
}
242+
233243
Vector128<float> sign = vector & Vector128.Create(-0F);
234244
Vector128<float> val_2p23_f32 = sign | Vector128.Create(8388608F);
235245

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

Lines changed: 0 additions & 1 deletion
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.Numerics;
54
using System.Runtime.CompilerServices;
65
using System.Runtime.Intrinsics;
76
using SixLabors.ImageSharp.Common.Helpers;

0 commit comments

Comments
 (0)