Skip to content

Commit f65122b

Browse files
Merge branch 'main' into fixed-improvements
2 parents df4ae9e + bab277c commit f65122b

17 files changed

+129
-39
lines changed

src/ImageSharp/Common/Helpers/Numerics.cs

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,4 +949,94 @@ public static int EvenReduceSum(Vector256<int> accumulator)
949949
[MethodImpl(MethodImplOptions.AggressiveInlining)]
950950
public static bool IsOutOfRange(int value, int min, int max)
951951
=> (uint)(value - min) > (uint)(max - min);
952+
953+
/// <summary>
954+
/// Gets the count of vectors that safely fit into the given span.
955+
/// </summary>
956+
/// <typeparam name="TVector">The type of the vector.</typeparam>
957+
/// <param name="span">The given span.</param>
958+
/// <returns>Count of vectors that safely fit into the span.</returns>
959+
public static nuint VectorCount<TVector>(this Span<byte> span)
960+
where TVector : struct
961+
=> (uint)span.Length / (uint)Vector<TVector>.Count;
962+
963+
/// <summary>
964+
/// Gets the count of vectors that safely fit into the given span.
965+
/// </summary>
966+
/// <typeparam name="TVector">The type of the vector.</typeparam>
967+
/// <param name="span">The given span.</param>
968+
/// <returns>Count of vectors that safely fit into the span.</returns>
969+
public static nuint Vector128Count<TVector>(this Span<byte> span)
970+
where TVector : struct
971+
=> (uint)span.Length / (uint)Vector128<TVector>.Count;
972+
973+
/// <summary>
974+
/// Gets the count of vectors that safely fit into the given span.
975+
/// </summary>
976+
/// <typeparam name="TVector">The type of the vector.</typeparam>
977+
/// <param name="span">The given span.</param>
978+
/// <returns>Count of vectors that safely fit into the span.</returns>
979+
public static nuint Vector128Count<TVector>(this ReadOnlySpan<byte> span)
980+
where TVector : struct
981+
=> (uint)span.Length / (uint)Vector128<TVector>.Count;
982+
983+
/// <summary>
984+
/// Gets the count of vectors that safely fit into the given span.
985+
/// </summary>
986+
/// <typeparam name="TVector">The type of the vector.</typeparam>
987+
/// <param name="span">The given span.</param>
988+
/// <returns>Count of vectors that safely fit into the span.</returns>
989+
public static nuint Vector256Count<TVector>(this Span<byte> span)
990+
where TVector : struct
991+
=> (uint)span.Length / (uint)Vector256<TVector>.Count;
992+
993+
/// <summary>
994+
/// Gets the count of vectors that safely fit into the given span.
995+
/// </summary>
996+
/// <typeparam name="TVector">The type of the vector.</typeparam>
997+
/// <param name="span">The given span.</param>
998+
/// <returns>Count of vectors that safely fit into the span.</returns>
999+
public static nuint Vector256Count<TVector>(this ReadOnlySpan<byte> span)
1000+
where TVector : struct
1001+
=> (uint)span.Length / (uint)Vector256<TVector>.Count;
1002+
1003+
/// <summary>
1004+
/// Gets the count of vectors that safely fit into the given span.
1005+
/// </summary>
1006+
/// <typeparam name="TVector">The type of the vector.</typeparam>
1007+
/// <param name="span">The given span.</param>
1008+
/// <returns>Count of vectors that safely fit into the span.</returns>
1009+
public static nuint VectorCount<TVector>(this Span<float> span)
1010+
where TVector : struct
1011+
=> (uint)span.Length / (uint)Vector<TVector>.Count;
1012+
1013+
/// <summary>
1014+
/// Gets the count of vectors that safely fit into the given span.
1015+
/// </summary>
1016+
/// <typeparam name="TVector">The type of the vector.</typeparam>
1017+
/// <param name="span">The given span.</param>
1018+
/// <returns>Count of vectors that safely fit into the span.</returns>
1019+
public static nuint Vector128Count<TVector>(this Span<float> span)
1020+
where TVector : struct
1021+
=> (uint)span.Length / (uint)Vector128<TVector>.Count;
1022+
1023+
/// <summary>
1024+
/// Gets the count of vectors that safely fit into the given span.
1025+
/// </summary>
1026+
/// <typeparam name="TVector">The type of the vector.</typeparam>
1027+
/// <param name="span">The given span.</param>
1028+
/// <returns>Count of vectors that safely fit into the span.</returns>
1029+
public static nuint Vector256Count<TVector>(this Span<float> span)
1030+
where TVector : struct
1031+
=> (uint)span.Length / (uint)Vector256<TVector>.Count;
1032+
1033+
/// <summary>
1034+
/// Gets the count of vectors that safely fit into length.
1035+
/// </summary>
1036+
/// <typeparam name="TVector">The type of the vector.</typeparam>
1037+
/// <param name="length">The given length.</param>
1038+
/// <returns>Count of vectors that safely fit into the length.</returns>
1039+
public static nuint Vector256Count<TVector>(int length)
1040+
where TVector : struct
1041+
=> (uint)length / (uint)Vector256<TVector>.Count;
9521042
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ internal static void ByteToNormalizedFloat(ReadOnlySpan<byte> source, Span<float
9797
{
9898
VerifySpanInput(source, dest, Vector<byte>.Count);
9999

100-
nuint n = (uint)dest.Length / (uint)Vector<byte>.Count;
100+
nuint n = dest.VectorCount<byte>();
101101

102102
ref Vector<byte> sourceBase = ref Unsafe.As<byte, Vector<byte>>(ref MemoryMarshal.GetReference(source));
103103
ref Vector<float> destBase = ref Unsafe.As<float, Vector<float>>(ref MemoryMarshal.GetReference(dest));
@@ -132,7 +132,7 @@ internal static void NormalizedFloatToByteSaturate(
132132
{
133133
VerifySpanInput(source, dest, Vector<byte>.Count);
134134

135-
nuint n = (uint)dest.Length / (uint)Vector<byte>.Count;
135+
nuint n = dest.VectorCount<byte>();
136136

137137
ref Vector<float> sourceBase =
138138
ref Unsafe.As<float, Vector<float>>(ref MemoryMarshal.GetReference(source));

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ private static void Shuffle4(
222222
ref Vector256<float> destBase =
223223
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(dest));
224224

225-
nint n = (nint)(uint)(dest.Length / Vector256<float>.Count);
225+
nint n = (nint)dest.Vector256Count<float>();
226226
nint m = Numerics.Modulo4(n);
227227
nint u = n - m;
228228

@@ -392,7 +392,7 @@ private static void Shuffle3(
392392
ref Vector128<byte> destBase =
393393
ref Unsafe.As<byte, Vector128<byte>>(ref MemoryMarshal.GetReference(dest));
394394

395-
nuint n = (uint)source.Length / (uint)Vector128<byte>.Count;
395+
nuint n = source.Vector128Count<byte>();
396396

397397
for (nuint i = 0; i < n; i += 3)
398398
{
@@ -455,7 +455,7 @@ private static void Pad3Shuffle4(
455455
ref Vector128<byte> destBase =
456456
ref Unsafe.As<byte, Vector128<byte>>(ref MemoryMarshal.GetReference(dest));
457457

458-
nuint n = (uint)source.Length / (uint)Vector128<byte>.Count;
458+
nuint n = source.Vector128Count<byte>();
459459

460460
for (nuint i = 0, j = 0; i < n; i += 3, j += 4)
461461
{
@@ -499,7 +499,7 @@ private static void Shuffle4Slice3(
499499
ref Vector128<byte> destBase =
500500
ref Unsafe.As<byte, Vector128<byte>>(ref MemoryMarshal.GetReference(dest));
501501

502-
nuint n = (uint)source.Length / (uint)Vector128<byte>.Count;
502+
nuint n = source.Vector128Count<byte>();
503503

504504
for (nuint i = 0, j = 0; i < n; i += 4, j += 3)
505505
{
@@ -679,7 +679,7 @@ internal static unsafe void ByteToNormalizedFloat(
679679
{
680680
VerifySpanInput(source, dest, Vector256<byte>.Count);
681681

682-
nuint n = (uint)dest.Length / (uint)Vector256<byte>.Count;
682+
nuint n = dest.Vector256Count<byte>();
683683

684684
ref Vector256<float> destBase =
685685
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(dest));
@@ -712,7 +712,7 @@ internal static unsafe void ByteToNormalizedFloat(
712712
// Sse
713713
VerifySpanInput(source, dest, Vector128<byte>.Count);
714714

715-
nuint n = (uint)dest.Length / (uint)Vector128<byte>.Count;
715+
nuint n = dest.Vector128Count<byte>();
716716

717717
ref Vector128<float> destBase =
718718
ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(dest));
@@ -811,7 +811,7 @@ internal static void NormalizedFloatToByteSaturate(
811811
{
812812
VerifySpanInput(source, dest, Vector256<byte>.Count);
813813

814-
nuint n = (uint)dest.Length / (uint)Vector256<byte>.Count;
814+
nuint n = dest.Vector256Count<byte>();
815815

816816
ref Vector256<float> sourceBase =
817817
ref Unsafe.As<float, Vector256<float>>(ref MemoryMarshal.GetReference(source));
@@ -850,7 +850,7 @@ internal static void NormalizedFloatToByteSaturate(
850850
// Sse
851851
VerifySpanInput(source, dest, Vector128<byte>.Count);
852852

853-
nuint n = (uint)dest.Length / (uint)Vector128<byte>.Count;
853+
nuint n = dest.Vector128Count<byte>();
854854

855855
ref Vector128<float> sourceBase =
856856
ref Unsafe.As<float, Vector128<float>>(ref MemoryMarshal.GetReference(source));
@@ -893,7 +893,7 @@ internal static void PackFromRgbPlanesAvx2Reduce(
893893
ref Vector256<byte> bBase = ref Unsafe.As<byte, Vector256<byte>>(ref MemoryMarshal.GetReference(blueChannel));
894894
ref byte dBase = ref Unsafe.As<Rgb24, byte>(ref MemoryMarshal.GetReference(destination));
895895

896-
nuint count = (uint)redChannel.Length / (uint)Vector256<byte>.Count;
896+
nuint count = redChannel.Vector256Count<byte>();
897897

898898
ref byte control1Bytes = ref MemoryMarshal.GetReference(PermuteMaskEvenOdd8x32);
899899
Vector256<uint> control1 = Unsafe.As<byte, Vector256<uint>>(ref control1Bytes);
@@ -965,7 +965,7 @@ internal static void PackFromRgbPlanesAvx2Reduce(
965965
ref Vector256<byte> bBase = ref Unsafe.As<byte, Vector256<byte>>(ref MemoryMarshal.GetReference(blueChannel));
966966
ref Vector256<byte> dBase = ref Unsafe.As<Rgba32, Vector256<byte>>(ref MemoryMarshal.GetReference(destination));
967967

968-
nuint count = (uint)redChannel.Length / (uint)Vector256<byte>.Count;
968+
nuint count = redChannel.Vector256Count<byte>();
969969
ref byte control1Bytes = ref MemoryMarshal.GetReference(PermuteMaskEvenOdd8x32);
970970
Vector256<uint> control1 = Unsafe.As<byte, Vector256<uint>>(ref control1Bytes);
971971
var a = Vector256.Create((byte)255);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public override void ConvertToRgbInplace(in ComponentValues values)
3232
// Used for the color conversion
3333
var scale = Vector256.Create(1 / (this.MaximumValue * this.MaximumValue));
3434

35-
nuint n = (uint)values.Component0.Length / (uint)Vector256<float>.Count;
35+
nuint n = values.Component0.Vector256Count<float>();
3636
for (nuint i = 0; i < n; i++)
3737
{
3838
ref Vector256<float> c = ref Unsafe.Add(ref c0Base, i);
@@ -71,7 +71,7 @@ public static void ConvertFromRgb(in ComponentValues values, float maxValue, Spa
7171

7272
var scale = Vector256.Create(maxValue);
7373

74-
nuint n = (uint)values.Component0.Length / (uint)Vector256<float>.Count;
74+
nuint n = values.Component0.Vector256Count<float>();
7575
for (nuint i = 0; i < n; i++)
7676
{
7777
Vector256<float> ctmp = Avx.Subtract(scale, Unsafe.Add(ref srcR, i));

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ protected override void ConvertToRgbInplaceVectorized(in ComponentValues values)
3030

3131
var scale = new Vector<float>(1 / (this.MaximumValue * this.MaximumValue));
3232

33-
nuint n = (uint)values.Component0.Length / (uint)Vector<float>.Count;
33+
nuint n = values.Component0.VectorCount<float>();
3434
for (nuint i = 0; i < n; i++)
3535
{
3636
ref Vector<float> c = ref Unsafe.Add(ref cBase, i);
@@ -78,7 +78,7 @@ public static void ConvertFromRgbInplaceVectorized(in ComponentValues values, fl
7878
// Used for the color conversion
7979
var scale = new Vector<float>(maxValue);
8080

81-
nuint n = (uint)values.Component0.Length / (uint)Vector<float>.Count;
81+
nuint n = values.Component0.VectorCount<float>();
8282
for (nuint i = 0; i < n; i++)
8383
{
8484
Vector<float> ctmp = scale - Unsafe.Add(ref srcR, i);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public override void ConvertToRgbInplace(in ComponentValues values)
2727
// Used for the color conversion
2828
var scale = Vector128.Create(1 / this.MaximumValue);
2929

30-
nuint n = (uint)values.Component0.Length / (uint)Vector128<float>.Count;
30+
nuint n = values.Component0.Vector128Count<float>();
3131
for (nuint i = 0; i < n; i++)
3232
{
3333
ref Vector128<float> c0 = ref Unsafe.Add(ref c0Base, i);
@@ -53,7 +53,7 @@ public override void ConvertFromRgb(in ComponentValues values, Span<float> rLane
5353
var f0587 = Vector128.Create(0.587f);
5454
var f0114 = Vector128.Create(0.114f);
5555

56-
nuint n = (uint)values.Component0.Length / (uint)Vector128<float>.Count;
56+
nuint n = values.Component0.Vector128Count<float>();
5757
for (nuint i = 0; i < n; i++)
5858
{
5959
ref Vector128<float> r = ref Unsafe.Add(ref srcRed, i);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public override void ConvertToRgbInplace(in ComponentValues values)
2727
// Used for the color conversion
2828
var scale = Vector256.Create(1 / this.MaximumValue);
2929

30-
nuint n = (uint)values.Component0.Length / (uint)Vector256<float>.Count;
30+
nuint n = values.Component0.Vector256Count<float>();
3131
for (nuint i = 0; i < n; i++)
3232
{
3333
ref Vector256<float> c0 = ref Unsafe.Add(ref c0Base, i);
@@ -53,7 +53,7 @@ public override void ConvertFromRgb(in ComponentValues values, Span<float> rLane
5353
var f0587 = Vector256.Create(0.587f);
5454
var f0114 = Vector256.Create(0.114f);
5555

56-
nuint n = (uint)values.Component0.Length / (uint)Vector256<float>.Count;
56+
nuint n = values.Component0.Vector256Count<float>();
5757
for (nuint i = 0; i < n; i++)
5858
{
5959
ref Vector256<float> r = ref Unsafe.Add(ref srcRed, i);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ protected override void ConvertToRgbInplaceVectorized(in ComponentValues values)
2424

2525
var scale = new Vector<float>(1 / this.MaximumValue);
2626

27-
nuint n = (uint)values.Component0.Length / (uint)Vector<float>.Count;
27+
nuint n = values.Component0.VectorCount<float>();
2828
for (nuint i = 0; i < n; i++)
2929
{
3030
ref Vector<float> c0 = ref Unsafe.Add(ref cBase, i);
@@ -53,7 +53,7 @@ protected override void ConvertFromRgbVectorized(in ComponentValues values, Span
5353
var gMult = new Vector<float>(0.587f);
5454
var bMult = new Vector<float>(0.114f);
5555

56-
nuint n = (uint)values.Component0.Length / (uint)Vector<float>.Count;
56+
nuint n = values.Component0.VectorCount<float>();
5757
for (nuint i = 0; i < n; i++)
5858
{
5959
Vector<float> r = Unsafe.Add(ref srcR, i);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public override void ConvertToRgbInplace(in ComponentValues values)
3030

3131
// Used for the color conversion
3232
var scale = Vector128.Create(1 / this.MaximumValue);
33-
nuint n = (uint)values.Component0.Length / (uint)Vector128<float>.Count;
33+
nuint n = values.Component0.Vector128Count<float>();
3434
for (nuint i = 0; i < n; i++)
3535
{
3636
ref Vector128<float> r = ref Unsafe.Add(ref rBase, i);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public override void ConvertToRgbInplace(in ComponentValues values)
2929

3030
// Used for the color conversion
3131
var scale = Vector256.Create(1 / this.MaximumValue);
32-
nuint n = (uint)values.Component0.Length / (uint)Vector256<float>.Count;
32+
nuint n = values.Component0.Vector256Count<float>();
3333
for (nuint i = 0; i < n; i++)
3434
{
3535
ref Vector256<float> r = ref Unsafe.Add(ref rBase, i);

0 commit comments

Comments
 (0)