diff --git a/src/ImageSharp/Common/Constants.cs b/src/ImageSharp/Common/Constants.cs
index fa2f72c74..d4640f133 100644
--- a/src/ImageSharp/Common/Constants.cs
+++ b/src/ImageSharp/Common/Constants.cs
@@ -1,4 +1,4 @@
-// Copyright (c) Six Labors.
+// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
namespace SixLabors.ImageSharp;
diff --git a/src/ImageSharp/Common/Helpers/SimdUtils.HwIntrinsics.cs b/src/ImageSharp/Common/Helpers/SimdUtils.HwIntrinsics.cs
index 4bc0040c6..7d2bab259 100644
--- a/src/ImageSharp/Common/Helpers/SimdUtils.HwIntrinsics.cs
+++ b/src/ImageSharp/Common/Helpers/SimdUtils.HwIntrinsics.cs
@@ -532,7 +532,8 @@ internal static partial class SimdUtils
}
///
- /// Performs a multiplication and an addition of the .
+ /// Performs a multiplication and an addition of the .
+ /// TODO: Fix. The arguments are in a different order to the FMA intrinsic.
///
/// ret = (vm0 * vm1) + va
/// The vector to add to the intermediate result.
@@ -549,22 +550,21 @@ internal static partial class SimdUtils
{
return Fma.MultiplyAdd(vm1, vm0, va);
}
- else
- {
- return Avx.Add(Avx.Multiply(vm0, vm1), va);
- }
+
+ return Avx.Add(Avx.Multiply(vm0, vm1), va);
}
///
- /// Performs a multiplication and a substraction of the .
+ /// Performs a multiplication and a subtraction of the .
+ /// TODO: Fix. The arguments are in a different order to the FMA intrinsic.
///
/// ret = (vm0 * vm1) - vs
- /// The vector to substract from the intermediate result.
+ /// The vector to subtract from the intermediate result.
/// The first vector to multiply.
/// The second vector to multiply.
/// The .
[MethodImpl(InliningOptions.ShortMethod)]
- public static Vector256 MultiplySubstract(
+ public static Vector256 MultiplySubtract(
in Vector256 vs,
in Vector256 vm0,
in Vector256 vm1)
@@ -573,10 +573,30 @@ internal static partial class SimdUtils
{
return Fma.MultiplySubtract(vm1, vm0, vs);
}
- else
+
+ return Avx.Subtract(Avx.Multiply(vm0, vm1), vs);
+ }
+
+ ///
+ /// Performs a multiplication and a negated addition of the .
+ ///
+ /// ret = c - (a * b)
+ /// The first vector to multiply.
+ /// The second vector to multiply.
+ /// The vector to add negated to the intermediate result.
+ /// The .
+ [MethodImpl(InliningOptions.ShortMethod)]
+ public static Vector256 MultiplyAddNegated(
+ in Vector256 a,
+ in Vector256 b,
+ in Vector256 c)
+ {
+ if (Fma.IsSupported)
{
- return Avx.Subtract(Avx.Multiply(vm0, vm1), vs);
+ return Fma.MultiplyAddNegated(a, b, c);
}
+
+ return Avx.Subtract(c, Avx.Multiply(a, b));
}
///
diff --git a/src/ImageSharp/Formats/Jpeg/Components/FloatingPointDCT.Intrinsic.cs b/src/ImageSharp/Formats/Jpeg/Components/FloatingPointDCT.Intrinsic.cs
index cae89fc3c..7e102f696 100644
--- a/src/ImageSharp/Formats/Jpeg/Components/FloatingPointDCT.Intrinsic.cs
+++ b/src/ImageSharp/Formats/Jpeg/Components/FloatingPointDCT.Intrinsic.cs
@@ -99,7 +99,7 @@ internal static partial class FloatingPointDCT
var mm256_F_1_4142 = Vector256.Create(1.414213562f);
Vector256 tmp13 = Avx.Add(tmp1, tmp3);
- Vector256 tmp12 = SimdUtils.HwIntrinsics.MultiplySubstract(tmp13, Avx.Subtract(tmp1, tmp3), mm256_F_1_4142);
+ Vector256 tmp12 = SimdUtils.HwIntrinsics.MultiplySubtract(tmp13, Avx.Subtract(tmp1, tmp3), mm256_F_1_4142);
tmp0 = Avx.Add(tmp10, tmp13);
tmp3 = Avx.Subtract(tmp10, tmp13);
diff --git a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultPixelBlenders.Generated.cs b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultPixelBlenders.Generated.cs
index cf1910121..2db61a06f 100644
--- a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultPixelBlenders.Generated.cs
+++ b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultPixelBlenders.Generated.cs
@@ -3,6 +3,10 @@
//
using System.Numerics;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+using System.Runtime.Intrinsics;
+using System.Runtime.Intrinsics.X86;
namespace SixLabors.ImageSharp.PixelFormats.PixelBlenders;
@@ -43,18 +47,85 @@ internal static class DefaultPixelBlenders
protected override void BlendFunction(Span destination, ReadOnlySpan background, ReadOnlySpan source, float amount)
{
amount = Numerics.Clamp(amount, 0, 1);
- for (int i = 0; i < destination.Length; i++)
+
+ if (Avx2.IsSupported && destination.Length >= 2)
+ {
+ // Divide by 2 as 4 elements per Vector4 and 8 per Vector256
+ ref Vector256 destinationBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(destination));
+ ref Vector256 destinationLast = ref Unsafe.Add(ref destinationBase, (IntPtr)((uint)destination.Length / 2u));
+
+ ref Vector256 backgroundBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(background));
+ ref Vector256 sourceBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(source));
+ Vector256 opacity = Vector256.Create(amount);
+
+ while (Unsafe.IsAddressLessThan(ref destinationBase, ref destinationLast))
+ {
+ destinationBase = PorterDuffFunctions.NormalSrc(backgroundBase, sourceBase, opacity);
+ destinationBase = ref Unsafe.Add(ref destinationBase, 1);
+ backgroundBase = ref Unsafe.Add(ref backgroundBase, 1);
+ sourceBase = ref Unsafe.Add(ref sourceBase, 1);
+ }
+
+ if (Numerics.Modulo2(destination.Length) != 0)
+ {
+ // Vector4 fits neatly in pairs. Any overlap has to be equal to 1.
+ int i = destination.Length - 1;
+ destination[i] = PorterDuffFunctions.NormalSrc(background[i], source[i], amount);
+ }
+ }
+ else
{
- destination[i] = PorterDuffFunctions.NormalSrc(background[i], source[i], amount);
+ for (int i = 0; i < destination.Length; i++)
+ {
+ destination[i] = PorterDuffFunctions.NormalSrc(background[i], source[i], amount);
+ }
}
}
///
protected override void BlendFunction(Span destination, ReadOnlySpan background, ReadOnlySpan source, ReadOnlySpan amount)
{
- for (int i = 0; i < destination.Length; i++)
+ if (Avx2.IsSupported && destination.Length >= 2)
+ {
+ // Divide by 2 as 4 elements per Vector4 and 8 per Vector256
+ ref Vector256 destinationBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(destination));
+ ref Vector256 destinationLast = ref Unsafe.Add(ref destinationBase, (IntPtr)((uint)destination.Length / 2u));
+
+ ref Vector256 backgroundBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(background));
+ ref Vector256 sourceBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(source));
+ ref float amountBase = ref MemoryMarshal.GetReference(amount);
+
+ Vector256 vOne = Vector256.Create(1F);
+
+ while (Unsafe.IsAddressLessThan(ref destinationBase, ref destinationLast))
+ {
+ // We need to create a Vector256 containing the current and next amount values
+ // taking up each half of the Vector256 and then clamp them.
+ Vector256 opacity = Vector256.Create(
+ Vector128.Create(amountBase),
+ Vector128.Create(Unsafe.Add(ref amountBase, 1)));
+ opacity = Avx.Min(Avx.Max(Vector256.Zero, opacity), vOne);
+
+ destinationBase = PorterDuffFunctions.NormalSrc(backgroundBase, sourceBase, opacity);
+ destinationBase = ref Unsafe.Add(ref destinationBase, 1);
+ backgroundBase = ref Unsafe.Add(ref backgroundBase, 1);
+ sourceBase = ref Unsafe.Add(ref sourceBase, 1);
+ amountBase = ref Unsafe.Add(ref amountBase, 2);
+ }
+
+ if (Numerics.Modulo2(destination.Length) != 0)
+ {
+ // Vector4 fits neatly in pairs. Any overlap has to be equal to 1.
+ int i = destination.Length - 1;
+ destination[i] = PorterDuffFunctions.NormalSrc(background[i], source[i], Numerics.Clamp(amount[i], 0, 1F));
+ }
+ }
+ else
{
- destination[i] = PorterDuffFunctions.NormalSrc(background[i], source[i], Numerics.Clamp(amount[i], 0, 1));
+ for (int i = 0; i < destination.Length; i++)
+ {
+ destination[i] = PorterDuffFunctions.NormalSrc(background[i], source[i], Numerics.Clamp(amount[i], 0, 1F));
+ }
}
}
}
@@ -81,18 +152,85 @@ internal static class DefaultPixelBlenders
protected override void BlendFunction(Span destination, ReadOnlySpan background, ReadOnlySpan source, float amount)
{
amount = Numerics.Clamp(amount, 0, 1);
- for (int i = 0; i < destination.Length; i++)
+
+ if (Avx2.IsSupported && destination.Length >= 2)
+ {
+ // Divide by 2 as 4 elements per Vector4 and 8 per Vector256
+ ref Vector256 destinationBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(destination));
+ ref Vector256 destinationLast = ref Unsafe.Add(ref destinationBase, (IntPtr)((uint)destination.Length / 2u));
+
+ ref Vector256 backgroundBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(background));
+ ref Vector256 sourceBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(source));
+ Vector256 opacity = Vector256.Create(amount);
+
+ while (Unsafe.IsAddressLessThan(ref destinationBase, ref destinationLast))
+ {
+ destinationBase = PorterDuffFunctions.MultiplySrc(backgroundBase, sourceBase, opacity);
+ destinationBase = ref Unsafe.Add(ref destinationBase, 1);
+ backgroundBase = ref Unsafe.Add(ref backgroundBase, 1);
+ sourceBase = ref Unsafe.Add(ref sourceBase, 1);
+ }
+
+ if (Numerics.Modulo2(destination.Length) != 0)
+ {
+ // Vector4 fits neatly in pairs. Any overlap has to be equal to 1.
+ int i = destination.Length - 1;
+ destination[i] = PorterDuffFunctions.MultiplySrc(background[i], source[i], amount);
+ }
+ }
+ else
{
- destination[i] = PorterDuffFunctions.MultiplySrc(background[i], source[i], amount);
+ for (int i = 0; i < destination.Length; i++)
+ {
+ destination[i] = PorterDuffFunctions.MultiplySrc(background[i], source[i], amount);
+ }
}
}
///
protected override void BlendFunction(Span destination, ReadOnlySpan background, ReadOnlySpan source, ReadOnlySpan amount)
{
- for (int i = 0; i < destination.Length; i++)
+ if (Avx2.IsSupported && destination.Length >= 2)
+ {
+ // Divide by 2 as 4 elements per Vector4 and 8 per Vector256
+ ref Vector256 destinationBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(destination));
+ ref Vector256 destinationLast = ref Unsafe.Add(ref destinationBase, (IntPtr)((uint)destination.Length / 2u));
+
+ ref Vector256 backgroundBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(background));
+ ref Vector256 sourceBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(source));
+ ref float amountBase = ref MemoryMarshal.GetReference(amount);
+
+ Vector256 vOne = Vector256.Create(1F);
+
+ while (Unsafe.IsAddressLessThan(ref destinationBase, ref destinationLast))
+ {
+ // We need to create a Vector256 containing the current and next amount values
+ // taking up each half of the Vector256 and then clamp them.
+ Vector256 opacity = Vector256.Create(
+ Vector128.Create(amountBase),
+ Vector128.Create(Unsafe.Add(ref amountBase, 1)));
+ opacity = Avx.Min(Avx.Max(Vector256.Zero, opacity), vOne);
+
+ destinationBase = PorterDuffFunctions.MultiplySrc(backgroundBase, sourceBase, opacity);
+ destinationBase = ref Unsafe.Add(ref destinationBase, 1);
+ backgroundBase = ref Unsafe.Add(ref backgroundBase, 1);
+ sourceBase = ref Unsafe.Add(ref sourceBase, 1);
+ amountBase = ref Unsafe.Add(ref amountBase, 2);
+ }
+
+ if (Numerics.Modulo2(destination.Length) != 0)
+ {
+ // Vector4 fits neatly in pairs. Any overlap has to be equal to 1.
+ int i = destination.Length - 1;
+ destination[i] = PorterDuffFunctions.MultiplySrc(background[i], source[i], Numerics.Clamp(amount[i], 0, 1F));
+ }
+ }
+ else
{
- destination[i] = PorterDuffFunctions.MultiplySrc(background[i], source[i], Numerics.Clamp(amount[i], 0, 1));
+ for (int i = 0; i < destination.Length; i++)
+ {
+ destination[i] = PorterDuffFunctions.MultiplySrc(background[i], source[i], Numerics.Clamp(amount[i], 0, 1F));
+ }
}
}
}
@@ -119,18 +257,85 @@ internal static class DefaultPixelBlenders
protected override void BlendFunction(Span destination, ReadOnlySpan background, ReadOnlySpan source, float amount)
{
amount = Numerics.Clamp(amount, 0, 1);
- for (int i = 0; i < destination.Length; i++)
+
+ if (Avx2.IsSupported && destination.Length >= 2)
+ {
+ // Divide by 2 as 4 elements per Vector4 and 8 per Vector256
+ ref Vector256 destinationBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(destination));
+ ref Vector256 destinationLast = ref Unsafe.Add(ref destinationBase, (IntPtr)((uint)destination.Length / 2u));
+
+ ref Vector256 backgroundBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(background));
+ ref Vector256 sourceBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(source));
+ Vector256 opacity = Vector256.Create(amount);
+
+ while (Unsafe.IsAddressLessThan(ref destinationBase, ref destinationLast))
+ {
+ destinationBase = PorterDuffFunctions.AddSrc(backgroundBase, sourceBase, opacity);
+ destinationBase = ref Unsafe.Add(ref destinationBase, 1);
+ backgroundBase = ref Unsafe.Add(ref backgroundBase, 1);
+ sourceBase = ref Unsafe.Add(ref sourceBase, 1);
+ }
+
+ if (Numerics.Modulo2(destination.Length) != 0)
+ {
+ // Vector4 fits neatly in pairs. Any overlap has to be equal to 1.
+ int i = destination.Length - 1;
+ destination[i] = PorterDuffFunctions.AddSrc(background[i], source[i], amount);
+ }
+ }
+ else
{
- destination[i] = PorterDuffFunctions.AddSrc(background[i], source[i], amount);
+ for (int i = 0; i < destination.Length; i++)
+ {
+ destination[i] = PorterDuffFunctions.AddSrc(background[i], source[i], amount);
+ }
}
}
///
protected override void BlendFunction(Span destination, ReadOnlySpan background, ReadOnlySpan source, ReadOnlySpan amount)
{
- for (int i = 0; i < destination.Length; i++)
+ if (Avx2.IsSupported && destination.Length >= 2)
{
- destination[i] = PorterDuffFunctions.AddSrc(background[i], source[i], Numerics.Clamp(amount[i], 0, 1));
+ // Divide by 2 as 4 elements per Vector4 and 8 per Vector256
+ ref Vector256 destinationBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(destination));
+ ref Vector256 destinationLast = ref Unsafe.Add(ref destinationBase, (IntPtr)((uint)destination.Length / 2u));
+
+ ref Vector256 backgroundBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(background));
+ ref Vector256 sourceBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(source));
+ ref float amountBase = ref MemoryMarshal.GetReference(amount);
+
+ Vector256 vOne = Vector256.Create(1F);
+
+ while (Unsafe.IsAddressLessThan(ref destinationBase, ref destinationLast))
+ {
+ // We need to create a Vector256 containing the current and next amount values
+ // taking up each half of the Vector256 and then clamp them.
+ Vector256 opacity = Vector256.Create(
+ Vector128.Create(amountBase),
+ Vector128.Create(Unsafe.Add(ref amountBase, 1)));
+ opacity = Avx.Min(Avx.Max(Vector256.Zero, opacity), vOne);
+
+ destinationBase = PorterDuffFunctions.AddSrc(backgroundBase, sourceBase, opacity);
+ destinationBase = ref Unsafe.Add(ref destinationBase, 1);
+ backgroundBase = ref Unsafe.Add(ref backgroundBase, 1);
+ sourceBase = ref Unsafe.Add(ref sourceBase, 1);
+ amountBase = ref Unsafe.Add(ref amountBase, 2);
+ }
+
+ if (Numerics.Modulo2(destination.Length) != 0)
+ {
+ // Vector4 fits neatly in pairs. Any overlap has to be equal to 1.
+ int i = destination.Length - 1;
+ destination[i] = PorterDuffFunctions.AddSrc(background[i], source[i], Numerics.Clamp(amount[i], 0, 1F));
+ }
+ }
+ else
+ {
+ for (int i = 0; i < destination.Length; i++)
+ {
+ destination[i] = PorterDuffFunctions.AddSrc(background[i], source[i], Numerics.Clamp(amount[i], 0, 1F));
+ }
}
}
}
@@ -157,18 +362,85 @@ internal static class DefaultPixelBlenders
protected override void BlendFunction(Span destination, ReadOnlySpan background, ReadOnlySpan source, float amount)
{
amount = Numerics.Clamp(amount, 0, 1);
- for (int i = 0; i < destination.Length; i++)
+
+ if (Avx2.IsSupported && destination.Length >= 2)
{
- destination[i] = PorterDuffFunctions.SubtractSrc(background[i], source[i], amount);
+ // Divide by 2 as 4 elements per Vector4 and 8 per Vector256
+ ref Vector256 destinationBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(destination));
+ ref Vector256 destinationLast = ref Unsafe.Add(ref destinationBase, (IntPtr)((uint)destination.Length / 2u));
+
+ ref Vector256 backgroundBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(background));
+ ref Vector256 sourceBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(source));
+ Vector256 opacity = Vector256.Create(amount);
+
+ while (Unsafe.IsAddressLessThan(ref destinationBase, ref destinationLast))
+ {
+ destinationBase = PorterDuffFunctions.SubtractSrc(backgroundBase, sourceBase, opacity);
+ destinationBase = ref Unsafe.Add(ref destinationBase, 1);
+ backgroundBase = ref Unsafe.Add(ref backgroundBase, 1);
+ sourceBase = ref Unsafe.Add(ref sourceBase, 1);
+ }
+
+ if (Numerics.Modulo2(destination.Length) != 0)
+ {
+ // Vector4 fits neatly in pairs. Any overlap has to be equal to 1.
+ int i = destination.Length - 1;
+ destination[i] = PorterDuffFunctions.SubtractSrc(background[i], source[i], amount);
+ }
+ }
+ else
+ {
+ for (int i = 0; i < destination.Length; i++)
+ {
+ destination[i] = PorterDuffFunctions.SubtractSrc(background[i], source[i], amount);
+ }
}
}
///
protected override void BlendFunction(Span destination, ReadOnlySpan background, ReadOnlySpan source, ReadOnlySpan amount)
{
- for (int i = 0; i < destination.Length; i++)
+ if (Avx2.IsSupported && destination.Length >= 2)
+ {
+ // Divide by 2 as 4 elements per Vector4 and 8 per Vector256
+ ref Vector256 destinationBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(destination));
+ ref Vector256 destinationLast = ref Unsafe.Add(ref destinationBase, (IntPtr)((uint)destination.Length / 2u));
+
+ ref Vector256 backgroundBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(background));
+ ref Vector256 sourceBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(source));
+ ref float amountBase = ref MemoryMarshal.GetReference(amount);
+
+ Vector256 vOne = Vector256.Create(1F);
+
+ while (Unsafe.IsAddressLessThan(ref destinationBase, ref destinationLast))
+ {
+ // We need to create a Vector256 containing the current and next amount values
+ // taking up each half of the Vector256 and then clamp them.
+ Vector256 opacity = Vector256.Create(
+ Vector128.Create(amountBase),
+ Vector128.Create(Unsafe.Add(ref amountBase, 1)));
+ opacity = Avx.Min(Avx.Max(Vector256.Zero, opacity), vOne);
+
+ destinationBase = PorterDuffFunctions.SubtractSrc(backgroundBase, sourceBase, opacity);
+ destinationBase = ref Unsafe.Add(ref destinationBase, 1);
+ backgroundBase = ref Unsafe.Add(ref backgroundBase, 1);
+ sourceBase = ref Unsafe.Add(ref sourceBase, 1);
+ amountBase = ref Unsafe.Add(ref amountBase, 2);
+ }
+
+ if (Numerics.Modulo2(destination.Length) != 0)
+ {
+ // Vector4 fits neatly in pairs. Any overlap has to be equal to 1.
+ int i = destination.Length - 1;
+ destination[i] = PorterDuffFunctions.SubtractSrc(background[i], source[i], Numerics.Clamp(amount[i], 0, 1F));
+ }
+ }
+ else
{
- destination[i] = PorterDuffFunctions.SubtractSrc(background[i], source[i], Numerics.Clamp(amount[i], 0, 1));
+ for (int i = 0; i < destination.Length; i++)
+ {
+ destination[i] = PorterDuffFunctions.SubtractSrc(background[i], source[i], Numerics.Clamp(amount[i], 0, 1F));
+ }
}
}
}
@@ -195,18 +467,85 @@ internal static class DefaultPixelBlenders
protected override void BlendFunction(Span destination, ReadOnlySpan background, ReadOnlySpan source, float amount)
{
amount = Numerics.Clamp(amount, 0, 1);
- for (int i = 0; i < destination.Length; i++)
+
+ if (Avx2.IsSupported && destination.Length >= 2)
+ {
+ // Divide by 2 as 4 elements per Vector4 and 8 per Vector256
+ ref Vector256 destinationBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(destination));
+ ref Vector256 destinationLast = ref Unsafe.Add(ref destinationBase, (IntPtr)((uint)destination.Length / 2u));
+
+ ref Vector256 backgroundBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(background));
+ ref Vector256 sourceBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(source));
+ Vector256 opacity = Vector256.Create(amount);
+
+ while (Unsafe.IsAddressLessThan(ref destinationBase, ref destinationLast))
+ {
+ destinationBase = PorterDuffFunctions.ScreenSrc(backgroundBase, sourceBase, opacity);
+ destinationBase = ref Unsafe.Add(ref destinationBase, 1);
+ backgroundBase = ref Unsafe.Add(ref backgroundBase, 1);
+ sourceBase = ref Unsafe.Add(ref sourceBase, 1);
+ }
+
+ if (Numerics.Modulo2(destination.Length) != 0)
+ {
+ // Vector4 fits neatly in pairs. Any overlap has to be equal to 1.
+ int i = destination.Length - 1;
+ destination[i] = PorterDuffFunctions.ScreenSrc(background[i], source[i], amount);
+ }
+ }
+ else
{
- destination[i] = PorterDuffFunctions.ScreenSrc(background[i], source[i], amount);
+ for (int i = 0; i < destination.Length; i++)
+ {
+ destination[i] = PorterDuffFunctions.ScreenSrc(background[i], source[i], amount);
+ }
}
}
///
protected override void BlendFunction(Span destination, ReadOnlySpan background, ReadOnlySpan source, ReadOnlySpan amount)
{
- for (int i = 0; i < destination.Length; i++)
+ if (Avx2.IsSupported && destination.Length >= 2)
+ {
+ // Divide by 2 as 4 elements per Vector4 and 8 per Vector256
+ ref Vector256 destinationBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(destination));
+ ref Vector256 destinationLast = ref Unsafe.Add(ref destinationBase, (IntPtr)((uint)destination.Length / 2u));
+
+ ref Vector256 backgroundBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(background));
+ ref Vector256 sourceBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(source));
+ ref float amountBase = ref MemoryMarshal.GetReference(amount);
+
+ Vector256 vOne = Vector256.Create(1F);
+
+ while (Unsafe.IsAddressLessThan(ref destinationBase, ref destinationLast))
+ {
+ // We need to create a Vector256 containing the current and next amount values
+ // taking up each half of the Vector256 and then clamp them.
+ Vector256 opacity = Vector256.Create(
+ Vector128.Create(amountBase),
+ Vector128.Create(Unsafe.Add(ref amountBase, 1)));
+ opacity = Avx.Min(Avx.Max(Vector256.Zero, opacity), vOne);
+
+ destinationBase = PorterDuffFunctions.ScreenSrc(backgroundBase, sourceBase, opacity);
+ destinationBase = ref Unsafe.Add(ref destinationBase, 1);
+ backgroundBase = ref Unsafe.Add(ref backgroundBase, 1);
+ sourceBase = ref Unsafe.Add(ref sourceBase, 1);
+ amountBase = ref Unsafe.Add(ref amountBase, 2);
+ }
+
+ if (Numerics.Modulo2(destination.Length) != 0)
+ {
+ // Vector4 fits neatly in pairs. Any overlap has to be equal to 1.
+ int i = destination.Length - 1;
+ destination[i] = PorterDuffFunctions.ScreenSrc(background[i], source[i], Numerics.Clamp(amount[i], 0, 1F));
+ }
+ }
+ else
{
- destination[i] = PorterDuffFunctions.ScreenSrc(background[i], source[i], Numerics.Clamp(amount[i], 0, 1));
+ for (int i = 0; i < destination.Length; i++)
+ {
+ destination[i] = PorterDuffFunctions.ScreenSrc(background[i], source[i], Numerics.Clamp(amount[i], 0, 1F));
+ }
}
}
}
@@ -233,18 +572,85 @@ internal static class DefaultPixelBlenders
protected override void BlendFunction(Span destination, ReadOnlySpan background, ReadOnlySpan source, float amount)
{
amount = Numerics.Clamp(amount, 0, 1);
- for (int i = 0; i < destination.Length; i++)
+
+ if (Avx2.IsSupported && destination.Length >= 2)
+ {
+ // Divide by 2 as 4 elements per Vector4 and 8 per Vector256
+ ref Vector256 destinationBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(destination));
+ ref Vector256 destinationLast = ref Unsafe.Add(ref destinationBase, (IntPtr)((uint)destination.Length / 2u));
+
+ ref Vector256 backgroundBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(background));
+ ref Vector256 sourceBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(source));
+ Vector256 opacity = Vector256.Create(amount);
+
+ while (Unsafe.IsAddressLessThan(ref destinationBase, ref destinationLast))
+ {
+ destinationBase = PorterDuffFunctions.DarkenSrc(backgroundBase, sourceBase, opacity);
+ destinationBase = ref Unsafe.Add(ref destinationBase, 1);
+ backgroundBase = ref Unsafe.Add(ref backgroundBase, 1);
+ sourceBase = ref Unsafe.Add(ref sourceBase, 1);
+ }
+
+ if (Numerics.Modulo2(destination.Length) != 0)
+ {
+ // Vector4 fits neatly in pairs. Any overlap has to be equal to 1.
+ int i = destination.Length - 1;
+ destination[i] = PorterDuffFunctions.DarkenSrc(background[i], source[i], amount);
+ }
+ }
+ else
{
- destination[i] = PorterDuffFunctions.DarkenSrc(background[i], source[i], amount);
+ for (int i = 0; i < destination.Length; i++)
+ {
+ destination[i] = PorterDuffFunctions.DarkenSrc(background[i], source[i], amount);
+ }
}
}
///
protected override void BlendFunction(Span destination, ReadOnlySpan background, ReadOnlySpan source, ReadOnlySpan amount)
{
- for (int i = 0; i < destination.Length; i++)
+ if (Avx2.IsSupported && destination.Length >= 2)
+ {
+ // Divide by 2 as 4 elements per Vector4 and 8 per Vector256
+ ref Vector256 destinationBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(destination));
+ ref Vector256 destinationLast = ref Unsafe.Add(ref destinationBase, (IntPtr)((uint)destination.Length / 2u));
+
+ ref Vector256 backgroundBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(background));
+ ref Vector256 sourceBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(source));
+ ref float amountBase = ref MemoryMarshal.GetReference(amount);
+
+ Vector256 vOne = Vector256.Create(1F);
+
+ while (Unsafe.IsAddressLessThan(ref destinationBase, ref destinationLast))
+ {
+ // We need to create a Vector256 containing the current and next amount values
+ // taking up each half of the Vector256 and then clamp them.
+ Vector256 opacity = Vector256.Create(
+ Vector128.Create(amountBase),
+ Vector128.Create(Unsafe.Add(ref amountBase, 1)));
+ opacity = Avx.Min(Avx.Max(Vector256.Zero, opacity), vOne);
+
+ destinationBase = PorterDuffFunctions.DarkenSrc(backgroundBase, sourceBase, opacity);
+ destinationBase = ref Unsafe.Add(ref destinationBase, 1);
+ backgroundBase = ref Unsafe.Add(ref backgroundBase, 1);
+ sourceBase = ref Unsafe.Add(ref sourceBase, 1);
+ amountBase = ref Unsafe.Add(ref amountBase, 2);
+ }
+
+ if (Numerics.Modulo2(destination.Length) != 0)
+ {
+ // Vector4 fits neatly in pairs. Any overlap has to be equal to 1.
+ int i = destination.Length - 1;
+ destination[i] = PorterDuffFunctions.DarkenSrc(background[i], source[i], Numerics.Clamp(amount[i], 0, 1F));
+ }
+ }
+ else
{
- destination[i] = PorterDuffFunctions.DarkenSrc(background[i], source[i], Numerics.Clamp(amount[i], 0, 1));
+ for (int i = 0; i < destination.Length; i++)
+ {
+ destination[i] = PorterDuffFunctions.DarkenSrc(background[i], source[i], Numerics.Clamp(amount[i], 0, 1F));
+ }
}
}
}
@@ -271,18 +677,85 @@ internal static class DefaultPixelBlenders
protected override void BlendFunction(Span destination, ReadOnlySpan background, ReadOnlySpan source, float amount)
{
amount = Numerics.Clamp(amount, 0, 1);
- for (int i = 0; i < destination.Length; i++)
+
+ if (Avx2.IsSupported && destination.Length >= 2)
+ {
+ // Divide by 2 as 4 elements per Vector4 and 8 per Vector256
+ ref Vector256 destinationBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(destination));
+ ref Vector256 destinationLast = ref Unsafe.Add(ref destinationBase, (IntPtr)((uint)destination.Length / 2u));
+
+ ref Vector256 backgroundBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(background));
+ ref Vector256 sourceBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(source));
+ Vector256 opacity = Vector256.Create(amount);
+
+ while (Unsafe.IsAddressLessThan(ref destinationBase, ref destinationLast))
+ {
+ destinationBase = PorterDuffFunctions.LightenSrc(backgroundBase, sourceBase, opacity);
+ destinationBase = ref Unsafe.Add(ref destinationBase, 1);
+ backgroundBase = ref Unsafe.Add(ref backgroundBase, 1);
+ sourceBase = ref Unsafe.Add(ref sourceBase, 1);
+ }
+
+ if (Numerics.Modulo2(destination.Length) != 0)
+ {
+ // Vector4 fits neatly in pairs. Any overlap has to be equal to 1.
+ int i = destination.Length - 1;
+ destination[i] = PorterDuffFunctions.LightenSrc(background[i], source[i], amount);
+ }
+ }
+ else
{
- destination[i] = PorterDuffFunctions.LightenSrc(background[i], source[i], amount);
+ for (int i = 0; i < destination.Length; i++)
+ {
+ destination[i] = PorterDuffFunctions.LightenSrc(background[i], source[i], amount);
+ }
}
}
///
protected override void BlendFunction(Span destination, ReadOnlySpan background, ReadOnlySpan source, ReadOnlySpan amount)
{
- for (int i = 0; i < destination.Length; i++)
+ if (Avx2.IsSupported && destination.Length >= 2)
+ {
+ // Divide by 2 as 4 elements per Vector4 and 8 per Vector256
+ ref Vector256 destinationBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(destination));
+ ref Vector256 destinationLast = ref Unsafe.Add(ref destinationBase, (IntPtr)((uint)destination.Length / 2u));
+
+ ref Vector256 backgroundBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(background));
+ ref Vector256 sourceBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(source));
+ ref float amountBase = ref MemoryMarshal.GetReference(amount);
+
+ Vector256 vOne = Vector256.Create(1F);
+
+ while (Unsafe.IsAddressLessThan(ref destinationBase, ref destinationLast))
+ {
+ // We need to create a Vector256 containing the current and next amount values
+ // taking up each half of the Vector256 and then clamp them.
+ Vector256 opacity = Vector256.Create(
+ Vector128.Create(amountBase),
+ Vector128.Create(Unsafe.Add(ref amountBase, 1)));
+ opacity = Avx.Min(Avx.Max(Vector256.Zero, opacity), vOne);
+
+ destinationBase = PorterDuffFunctions.LightenSrc(backgroundBase, sourceBase, opacity);
+ destinationBase = ref Unsafe.Add(ref destinationBase, 1);
+ backgroundBase = ref Unsafe.Add(ref backgroundBase, 1);
+ sourceBase = ref Unsafe.Add(ref sourceBase, 1);
+ amountBase = ref Unsafe.Add(ref amountBase, 2);
+ }
+
+ if (Numerics.Modulo2(destination.Length) != 0)
+ {
+ // Vector4 fits neatly in pairs. Any overlap has to be equal to 1.
+ int i = destination.Length - 1;
+ destination[i] = PorterDuffFunctions.LightenSrc(background[i], source[i], Numerics.Clamp(amount[i], 0, 1F));
+ }
+ }
+ else
{
- destination[i] = PorterDuffFunctions.LightenSrc(background[i], source[i], Numerics.Clamp(amount[i], 0, 1));
+ for (int i = 0; i < destination.Length; i++)
+ {
+ destination[i] = PorterDuffFunctions.LightenSrc(background[i], source[i], Numerics.Clamp(amount[i], 0, 1F));
+ }
}
}
}
@@ -309,18 +782,85 @@ internal static class DefaultPixelBlenders
protected override void BlendFunction(Span destination, ReadOnlySpan background, ReadOnlySpan source, float amount)
{
amount = Numerics.Clamp(amount, 0, 1);
- for (int i = 0; i < destination.Length; i++)
+
+ if (Avx2.IsSupported && destination.Length >= 2)
+ {
+ // Divide by 2 as 4 elements per Vector4 and 8 per Vector256
+ ref Vector256 destinationBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(destination));
+ ref Vector256 destinationLast = ref Unsafe.Add(ref destinationBase, (IntPtr)((uint)destination.Length / 2u));
+
+ ref Vector256 backgroundBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(background));
+ ref Vector256 sourceBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(source));
+ Vector256 opacity = Vector256.Create(amount);
+
+ while (Unsafe.IsAddressLessThan(ref destinationBase, ref destinationLast))
+ {
+ destinationBase = PorterDuffFunctions.OverlaySrc(backgroundBase, sourceBase, opacity);
+ destinationBase = ref Unsafe.Add(ref destinationBase, 1);
+ backgroundBase = ref Unsafe.Add(ref backgroundBase, 1);
+ sourceBase = ref Unsafe.Add(ref sourceBase, 1);
+ }
+
+ if (Numerics.Modulo2(destination.Length) != 0)
+ {
+ // Vector4 fits neatly in pairs. Any overlap has to be equal to 1.
+ int i = destination.Length - 1;
+ destination[i] = PorterDuffFunctions.OverlaySrc(background[i], source[i], amount);
+ }
+ }
+ else
{
- destination[i] = PorterDuffFunctions.OverlaySrc(background[i], source[i], amount);
+ for (int i = 0; i < destination.Length; i++)
+ {
+ destination[i] = PorterDuffFunctions.OverlaySrc(background[i], source[i], amount);
+ }
}
}
///
protected override void BlendFunction(Span destination, ReadOnlySpan background, ReadOnlySpan source, ReadOnlySpan amount)
{
- for (int i = 0; i < destination.Length; i++)
+ if (Avx2.IsSupported && destination.Length >= 2)
+ {
+ // Divide by 2 as 4 elements per Vector4 and 8 per Vector256
+ ref Vector256 destinationBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(destination));
+ ref Vector256 destinationLast = ref Unsafe.Add(ref destinationBase, (IntPtr)((uint)destination.Length / 2u));
+
+ ref Vector256 backgroundBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(background));
+ ref Vector256 sourceBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(source));
+ ref float amountBase = ref MemoryMarshal.GetReference(amount);
+
+ Vector256 vOne = Vector256.Create(1F);
+
+ while (Unsafe.IsAddressLessThan(ref destinationBase, ref destinationLast))
+ {
+ // We need to create a Vector256 containing the current and next amount values
+ // taking up each half of the Vector256 and then clamp them.
+ Vector256 opacity = Vector256.Create(
+ Vector128.Create(amountBase),
+ Vector128.Create(Unsafe.Add(ref amountBase, 1)));
+ opacity = Avx.Min(Avx.Max(Vector256.Zero, opacity), vOne);
+
+ destinationBase = PorterDuffFunctions.OverlaySrc(backgroundBase, sourceBase, opacity);
+ destinationBase = ref Unsafe.Add(ref destinationBase, 1);
+ backgroundBase = ref Unsafe.Add(ref backgroundBase, 1);
+ sourceBase = ref Unsafe.Add(ref sourceBase, 1);
+ amountBase = ref Unsafe.Add(ref amountBase, 2);
+ }
+
+ if (Numerics.Modulo2(destination.Length) != 0)
+ {
+ // Vector4 fits neatly in pairs. Any overlap has to be equal to 1.
+ int i = destination.Length - 1;
+ destination[i] = PorterDuffFunctions.OverlaySrc(background[i], source[i], Numerics.Clamp(amount[i], 0, 1F));
+ }
+ }
+ else
{
- destination[i] = PorterDuffFunctions.OverlaySrc(background[i], source[i], Numerics.Clamp(amount[i], 0, 1));
+ for (int i = 0; i < destination.Length; i++)
+ {
+ destination[i] = PorterDuffFunctions.OverlaySrc(background[i], source[i], Numerics.Clamp(amount[i], 0, 1F));
+ }
}
}
}
@@ -347,18 +887,85 @@ internal static class DefaultPixelBlenders
protected override void BlendFunction(Span