From fb7ed7262917f7307c91d1f834a36f663347d1b5 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sun, 26 Jul 2026 01:46:29 +1000 Subject: [PATCH] Use AVX-512 for byte tensor addition --- src/ImageSharp/Common/Helpers/TensorPrimitives.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ImageSharp/Common/Helpers/TensorPrimitives.cs b/src/ImageSharp/Common/Helpers/TensorPrimitives.cs index 4bff7882f..4be4ab3aa 100644 --- a/src/ImageSharp/Common/Helpers/TensorPrimitives.cs +++ b/src/ImageSharp/Common/Helpers/TensorPrimitives.cs @@ -204,15 +204,15 @@ internal static partial class TensorPrimitives_ ref T yRef = ref MemoryMarshal.GetReference(y); ref T destinationRef = ref MemoryMarshal.GetReference(destination); nuint length = (uint)x.Length; + nuint vector512Threshold = Unsafe.SizeOf() == 1 ? (uint)Vector512.Count : 512; - // AVX-512 setup only pays off for larger multi-byte inputs. Byte addition remains on AVX2 because direct - // PNG/WebP measurements show that its higher lane count does not recover the wider dispatch cost. + // Match the runtime's AVX-512 selection for byte-sized elements once one complete vector is available. + // Wider elements retain the measured crossover point where their 512-bit setup cost becomes worthwhile. // Each pipeline preloads its final inputs when a tail overlaps so same-start in-place operation remains correct. if (TOperator.Vectorizable && Vector512.IsHardwareAccelerated && Vector512.IsSupported - && Unsafe.SizeOf() > 1 - && length >= 512) + && length >= vector512Threshold) { InvokeVectorized512(ref xRef, ref yRef, ref destinationRef, length); return;