From e6f09d66167acae42f180d566e21acac99fe0ffc Mon Sep 17 00:00:00 2001 From: Erik White <26148654+Erik-White@users.noreply.github.com> Date: Wed, 27 May 2026 15:00:07 +0200 Subject: [PATCH] Use constant or static values where possible --- .../Formats/Png/PngCgbiProcessor.cs | 45 +++++++++---------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/src/ImageSharp/Formats/Png/PngCgbiProcessor.cs b/src/ImageSharp/Formats/Png/PngCgbiProcessor.cs index 73af0253eb..749e4a6823 100644 --- a/src/ImageSharp/Formats/Png/PngCgbiProcessor.cs +++ b/src/ImageSharp/Formats/Png/PngCgbiProcessor.cs @@ -47,17 +47,17 @@ internal static class PngCgbiProcessor Span pixels = MemoryMarshal.Cast(scanline); int i = 0; - if (Vector512.IsHardwareAccelerated && pixels.Length >= 16) + if (Vector512.IsHardwareAccelerated && pixels.Length >= Vector512.Count) { i = ApplyTransformVector512(scanline, pixels.Length); } - if (Vector256.IsHardwareAccelerated && Avx2.IsSupported && (pixels.Length - i) >= 8) + if (Vector256.IsHardwareAccelerated && Avx2.IsSupported && (pixels.Length - i) >= Vector256.Count) { i = ApplyTransformVector256(scanline, i, pixels.Length); } - if (Vector128.IsHardwareAccelerated && (pixels.Length - i) >= 4) + if (Vector128.IsHardwareAccelerated && (pixels.Length - i) >= Vector128.Count) { i = ApplyTransformVector128(scanline, i, pixels.Length); } @@ -106,11 +106,9 @@ internal static class PngCgbiProcessor Vector512 zero = Vector512.Zero; Vector512 one = Vector512.One; - Vector512 byteMask = Vector512.Create(0xFF); - Vector512 opaque = Vector512.Create(0xFF); Vector512 byteMax = Vector512.Create((int)byte.MaxValue); - for (; i <= pixelCount - 16; i += 16) + for (; i <= pixelCount - Vector512.Count; i += Vector512.Count) { ref byte blockRef = ref Unsafe.Add(ref scanlineRef, i * Unsafe.SizeOf()); Vector512 bgra = Unsafe.ReadUnaligned>(ref blockRef); @@ -121,11 +119,11 @@ internal static class PngCgbiProcessor // Fully transparent and fully opaque pixels are identity cases for // unpremultiplication. Masking them keeps the scalar behavior and lets // safeAlpha avoid dividing by zero for alpha == 0. - Vector512 partialMask = ~(Vector512.Equals(alpha, zero) | Vector512.Equals(alpha, opaque)); + Vector512 partialMask = ~(Vector512.Equals(alpha, zero) | Vector512.Equals(alpha, byteMax)); - Vector512 r = packed & byteMask; - Vector512 g = Vector512.ShiftRightLogical(packed, 8) & byteMask; - Vector512 b = Vector512.ShiftRightLogical(packed, 16) & byteMask; + Vector512 r = packed & byteMax; + Vector512 g = Vector512.ShiftRightLogical(packed, 8) & byteMax; + Vector512 b = Vector512.ShiftRightLogical(packed, 16) & byteMax; Vector512 safeAlpha = Vector512.ConditionalSelect(partialMask, alpha, one); Vector512 halfAlpha = Vector512.ShiftRightLogical(safeAlpha, 1); @@ -180,11 +178,9 @@ internal static class PngCgbiProcessor Vector256 zero = Vector256.Zero; Vector256 one = Vector256.One; - Vector256 byteMask = Vector256.Create(0xFF); - Vector256 opaque = Vector256.Create(0xFF); Vector256 byteMax = Vector256.Create((int)byte.MaxValue); - for (; i <= pixelCount - 8; i += 8) + for (; i <= pixelCount - Vector256.Count; i += Vector256.Count) { ref byte blockRef = ref Unsafe.Add(ref scanlineRef, i * Unsafe.SizeOf()); Vector256 bgra = Unsafe.ReadUnaligned>(ref blockRef); @@ -195,11 +191,11 @@ internal static class PngCgbiProcessor // Fully transparent and fully opaque pixels are identity cases for // unpremultiplication. Masking them keeps the scalar behavior and lets // safeAlpha avoid dividing by zero for alpha == 0. - Vector256 partialMask = ~(Vector256.Equals(alpha, zero) | Vector256.Equals(alpha, opaque)); + Vector256 partialMask = ~(Vector256.Equals(alpha, zero) | Vector256.Equals(alpha, byteMax)); - Vector256 r = packed & byteMask; - Vector256 g = Vector256.ShiftRightLogical(packed, 8) & byteMask; - Vector256 b = Vector256.ShiftRightLogical(packed, 16) & byteMask; + Vector256 r = packed & byteMax; + Vector256 g = Vector256.ShiftRightLogical(packed, 8) & byteMax; + Vector256 b = Vector256.ShiftRightLogical(packed, 16) & byteMax; Vector256 safeAlpha = Vector256.ConditionalSelect(partialMask, alpha, one); Vector256 halfAlpha = Vector256.ShiftRightLogical(safeAlpha, 1); @@ -251,11 +247,9 @@ internal static class PngCgbiProcessor Vector128 zero = Vector128.Zero; Vector128 one = Vector128.One; - Vector128 byteMask = Vector128.Create(0xFF); - Vector128 opaque = Vector128.Create(0xFF); Vector128 byteMax = Vector128.Create((int)byte.MaxValue); - for (; i <= pixelCount - 4; i += 4) + for (; i <= pixelCount - Vector128.Count; i += Vector128.Count) { ref byte blockRef = ref Unsafe.Add(ref scanlineRef, i * Unsafe.SizeOf()); Vector128 bgra = Unsafe.ReadUnaligned>(ref blockRef); @@ -266,11 +260,11 @@ internal static class PngCgbiProcessor // Fully transparent and fully opaque pixels are identity cases for // unpremultiplication. Masking them keeps the scalar behavior and lets // safeAlpha avoid dividing by zero for alpha == 0. - Vector128 partialMask = ~(Vector128.Equals(alpha, zero) | Vector128.Equals(alpha, opaque)); + Vector128 partialMask = ~(Vector128.Equals(alpha, zero) | Vector128.Equals(alpha, byteMax)); - Vector128 r = packed & byteMask; - Vector128 g = Vector128.ShiftRightLogical(packed, 8) & byteMask; - Vector128 b = Vector128.ShiftRightLogical(packed, 16) & byteMask; + Vector128 r = packed & byteMax; + Vector128 g = Vector128.ShiftRightLogical(packed, 8) & byteMax; + Vector128 b = Vector128.ShiftRightLogical(packed, 16) & byteMax; Vector128 safeAlpha = Vector128.ConditionalSelect(partialMask, alpha, one); Vector128 halfAlpha = Vector128.ShiftRightLogical(safeAlpha, 1); @@ -315,9 +309,10 @@ internal static class PngCgbiProcessor private static byte[] BuildShuffleBytes() { - byte[] bytes = new byte[64]; + byte[] bytes = new byte[Vector512.Count]; Span span = bytes; Shuffle.MMShuffleSpan(ref span, Shuffle.MMShuffle3012); + return bytes; } }