From 267802a4b659b2ebc5abfc37a14d25e14396bc74 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sun, 26 Jul 2026 00:20:39 +1000 Subject: [PATCH] Optimize ICC LUT normalization --- .../Metadata/Profiles/ICC/Various/IccLut.cs | 14 +- .../Profiles/ICC/Various/IccLutNormalizer.cs | 253 ++++++++++++++++++ .../Profiles/ICC/Various/IccLutTests.cs | 108 ++++++++ 3 files changed, 363 insertions(+), 12 deletions(-) create mode 100644 src/ImageSharp/Metadata/Profiles/ICC/Various/IccLutNormalizer.cs create mode 100644 tests/ImageSharp.Tests/Metadata/Profiles/ICC/Various/IccLutTests.cs diff --git a/src/ImageSharp/Metadata/Profiles/ICC/Various/IccLut.cs b/src/ImageSharp/Metadata/Profiles/ICC/Various/IccLut.cs index 5f07e5589..fb47409be 100644 --- a/src/ImageSharp/Metadata/Profiles/ICC/Various/IccLut.cs +++ b/src/ImageSharp/Metadata/Profiles/ICC/Various/IccLut.cs @@ -23,13 +23,8 @@ internal readonly struct IccLut : IEquatable { Guard.NotNull(values, nameof(values)); - const float max = ushort.MaxValue; - this.Values = new float[values.Length]; - for (int i = 0; i < values.Length; i++) - { - this.Values[i] = values[i] / max; - } + IccLutNormalizer.Normalize(values, this.Values); } /// @@ -40,13 +35,8 @@ internal readonly struct IccLut : IEquatable { Guard.NotNull(values, nameof(values)); - const float max = byte.MaxValue; - this.Values = new float[values.Length]; - for (int i = 0; i < values.Length; i++) - { - this.Values[i] = values[i] / max; - } + IccLutNormalizer.Normalize(values, this.Values); } /// diff --git a/src/ImageSharp/Metadata/Profiles/ICC/Various/IccLutNormalizer.cs b/src/ImageSharp/Metadata/Profiles/ICC/Various/IccLutNormalizer.cs new file mode 100644 index 000000000..67796cad3 --- /dev/null +++ b/src/ImageSharp/Metadata/Profiles/ICC/Various/IccLutNormalizer.cs @@ -0,0 +1,253 @@ +// Copyright (c) Six Labors. +// Licensed under the Six Labors Split License. + +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.Intrinsics; + +namespace SixLabors.ImageSharp.Metadata.Profiles.Icc; + +/// +/// Converts integer ICC lookup-table entries to their normalized single-precision representation. +/// +internal static class IccLutNormalizer +{ + /// + /// Defines the scalar and SIMD conversion for an integer lookup-table element type. + /// + /// The integer element type. + private interface INormalizeOperator + where T : unmanaged + { + /// + /// Gets the divisor that maps the integer range to [0, 1]. + /// + public static abstract float Divisor { get; } + + /// + /// Converts one scalar value. + /// + /// The integer value. + /// The normalized value. + public static abstract float Invoke(T source); + + /// + /// Converts one 128-bit input vector and stores the expanded single-precision results. + /// + /// The packed integer values. + /// The normalization divisor. + /// The first destination element. + public static abstract void Invoke(Vector128 source, Vector128 divisor, ref float destination); + + /// + /// Converts one 256-bit input vector and stores the expanded single-precision results. + /// + /// The packed integer values. + /// The normalization divisor. + /// The first destination element. + public static abstract void Invoke(Vector256 source, Vector256 divisor, ref float destination); + + /// + /// Converts one 512-bit input vector and stores the expanded single-precision results. + /// + /// The packed integer values. + /// The normalization divisor. + /// The first destination element. + public static abstract void Invoke(Vector512 source, Vector512 divisor, ref float destination); + } + + /// + /// Converts byte lookup-table entries to normalized single-precision values. + /// + /// The integer lookup-table entries. + /// The normalized destination values. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void Normalize(ReadOnlySpan source, Span destination) + => Normalize(source, destination); + + /// + /// Converts unsigned-short lookup-table entries to normalized single-precision values. + /// + /// The integer lookup-table entries. + /// The normalized destination values. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void Normalize(ReadOnlySpan source, Span destination) + => Normalize(source, destination); + + /// + /// Converts an integer lookup table using the widest available portable SIMD width, followed by narrower + /// widths and a scalar remainder. + /// + /// The integer element type. + /// The conversion implementation. + /// The integer lookup-table entries. + /// The normalized destination values. + private static void Normalize(ReadOnlySpan source, Span destination) + where T : unmanaged + where TOperator : struct, INormalizeOperator + { + ref T sourceRef = ref MemoryMarshal.GetReference(source); + ref float destinationRef = ref MemoryMarshal.GetReference(destination); + nuint length = (uint)source.Length; + nuint index = 0; + + if (Vector512.IsHardwareAccelerated) + { + Vector512 divisor = Vector512.Create(TOperator.Divisor); + nuint count = (uint)Vector512.Count; + + while (length - index >= count) + { + ref float destinationStart = ref Unsafe.Add(ref destinationRef, index); + TOperator.Invoke(Vector512.LoadUnsafe(ref sourceRef, index), divisor, ref destinationStart); + index += count; + } + } + + if (Vector256.IsHardwareAccelerated) + { + Vector256 divisor = Vector256.Create(TOperator.Divisor); + nuint count = (uint)Vector256.Count; + + while (length - index >= count) + { + ref float destinationStart = ref Unsafe.Add(ref destinationRef, index); + TOperator.Invoke(Vector256.LoadUnsafe(ref sourceRef, index), divisor, ref destinationStart); + index += count; + } + } + + if (Vector128.IsHardwareAccelerated) + { + Vector128 divisor = Vector128.Create(TOperator.Divisor); + nuint count = (uint)Vector128.Count; + + while (length - index >= count) + { + ref float destinationStart = ref Unsafe.Add(ref destinationRef, index); + TOperator.Invoke(Vector128.LoadUnsafe(ref sourceRef, index), divisor, ref destinationStart); + index += count; + } + } + + // Preserve the scalar division expression for the final partial vector. Multiplication by a reciprocal + // is not bit-equivalent for every input and would change the values stored in the ICC profile model. + while (index < length) + { + Unsafe.Add(ref destinationRef, index) = TOperator.Invoke(Unsafe.Add(ref sourceRef, index)); + index++; + } + } + + /// + /// Converts packed byte entries to normalized single-precision values. + /// + private readonly struct ByteNormalizeOperator : INormalizeOperator + { + /// + public static float Divisor => byte.MaxValue; + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static float Invoke(byte source) + => source / (float)byte.MaxValue; + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void Invoke(Vector128 source, Vector128 divisor, ref float destination) + { + // [b0..b15] becomes four ordered groups of four UInt32 values. Every widened value is at most + // 255, so reinterpreting UInt32 as Int32 before conversion preserves its numeric value. + (Vector128 lower16, Vector128 upper16) = Vector128.Widen(source); + (Vector128 values0, Vector128 values1) = Vector128.Widen(lower16); + (Vector128 values2, Vector128 values3) = Vector128.Widen(upper16); + + (Vector128.ConvertToSingle(values0.AsInt32()) / divisor).StoreUnsafe(ref destination); + (Vector128.ConvertToSingle(values1.AsInt32()) / divisor).StoreUnsafe(ref destination, 4); + (Vector128.ConvertToSingle(values2.AsInt32()) / divisor).StoreUnsafe(ref destination, 8); + (Vector128.ConvertToSingle(values3.AsInt32()) / divisor).StoreUnsafe(ref destination, 12); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void Invoke(Vector256 source, Vector256 divisor, ref float destination) + { + // [b0..b31] becomes four ordered groups of eight UInt32 values, matching four contiguous + // Vector256 stores without shuffling the converted results. + (Vector256 lower16, Vector256 upper16) = Vector256.Widen(source); + (Vector256 values0, Vector256 values1) = Vector256.Widen(lower16); + (Vector256 values2, Vector256 values3) = Vector256.Widen(upper16); + + (Vector256.ConvertToSingle(values0.AsInt32()) / divisor).StoreUnsafe(ref destination); + (Vector256.ConvertToSingle(values1.AsInt32()) / divisor).StoreUnsafe(ref destination, 8); + (Vector256.ConvertToSingle(values2.AsInt32()) / divisor).StoreUnsafe(ref destination, 16); + (Vector256.ConvertToSingle(values3.AsInt32()) / divisor).StoreUnsafe(ref destination, 24); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void Invoke(Vector512 source, Vector512 divisor, ref float destination) + { + // [b0..b63] becomes four ordered groups of sixteen UInt32 values, matching four contiguous + // Vector512 stores. The portable widening APIs map to zero-extension instructions. + (Vector512 lower16, Vector512 upper16) = Vector512.Widen(source); + (Vector512 values0, Vector512 values1) = Vector512.Widen(lower16); + (Vector512 values2, Vector512 values3) = Vector512.Widen(upper16); + + (Vector512.ConvertToSingle(values0.AsInt32()) / divisor).StoreUnsafe(ref destination); + (Vector512.ConvertToSingle(values1.AsInt32()) / divisor).StoreUnsafe(ref destination, 16); + (Vector512.ConvertToSingle(values2.AsInt32()) / divisor).StoreUnsafe(ref destination, 32); + (Vector512.ConvertToSingle(values3.AsInt32()) / divisor).StoreUnsafe(ref destination, 48); + } + } + + /// + /// Converts packed unsigned-short entries to normalized single-precision values. + /// + private readonly struct UInt16NormalizeOperator : INormalizeOperator + { + /// + public static float Divisor => ushort.MaxValue; + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static float Invoke(ushort source) + => source / (float)ushort.MaxValue; + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void Invoke(Vector128 source, Vector128 divisor, ref float destination) + { + // [u0..u7] becomes two ordered groups of four UInt32 values. Every value is at most 65535, + // so signed conversion after reinterpretation is numerically identical to unsigned conversion. + (Vector128 lower, Vector128 upper) = Vector128.Widen(source); + + (Vector128.ConvertToSingle(lower.AsInt32()) / divisor).StoreUnsafe(ref destination); + (Vector128.ConvertToSingle(upper.AsInt32()) / divisor).StoreUnsafe(ref destination, 4); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void Invoke(Vector256 source, Vector256 divisor, ref float destination) + { + // [u0..u15] becomes two ordered groups of eight UInt32 values, matching two contiguous + // Vector256 stores without a result shuffle. + (Vector256 lower, Vector256 upper) = Vector256.Widen(source); + + (Vector256.ConvertToSingle(lower.AsInt32()) / divisor).StoreUnsafe(ref destination); + (Vector256.ConvertToSingle(upper.AsInt32()) / divisor).StoreUnsafe(ref destination, 8); + } + + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void Invoke(Vector512 source, Vector512 divisor, ref float destination) + { + // [u0..u31] becomes two ordered groups of sixteen UInt32 values, matching two contiguous + // Vector512 stores. The portable widening APIs map to zero-extension instructions. + (Vector512 lower, Vector512 upper) = Vector512.Widen(source); + + (Vector512.ConvertToSingle(lower.AsInt32()) / divisor).StoreUnsafe(ref destination); + (Vector512.ConvertToSingle(upper.AsInt32()) / divisor).StoreUnsafe(ref destination, 16); + } + } +} diff --git a/tests/ImageSharp.Tests/Metadata/Profiles/ICC/Various/IccLutTests.cs b/tests/ImageSharp.Tests/Metadata/Profiles/ICC/Various/IccLutTests.cs new file mode 100644 index 000000000..f6705a1d0 --- /dev/null +++ b/tests/ImageSharp.Tests/Metadata/Profiles/ICC/Various/IccLutTests.cs @@ -0,0 +1,108 @@ +// Copyright (c) Six Labors. +// Licensed under the Six Labors Split License. + +using SixLabors.ImageSharp.Metadata.Profiles.Icc; + +namespace SixLabors.ImageSharp.Tests.Metadata.Profiles.ICC.Various; + +[Trait("Profile", "Icc")] +public class IccLutTests +{ + /// + /// Gets lengths that exercise scalar execution, every SIMD width, mixed-width remainders, and multiple vectors. + /// + public static TheoryData LutLengths => new() + { + 0, + 1, + 7, + 8, + 9, + 15, + 16, + 17, + 31, + 32, + 33, + 63, + 64, + 65, + 255, + 256, + 257 + }; + + /// + /// Verifies that byte LUT construction preserves the scalar conversion result for every traversal shape. + /// + /// The number of LUT entries. + [Theory] + [MemberData(nameof(LutLengths))] + public void ByteConstructorMatchesScalarFormula(int length) + { + byte[] values = new byte[length]; + + for (int i = 0; i < values.Length; i++) + { + values[i] = (byte)((i * 73) + 19); + } + + IccLut actual = new(values); + + Assert.Equal(values.Length, actual.Values.Length); + + for (int i = 0; i < values.Length; i++) + { + float expected = values[i] / (float)byte.MaxValue; + Assert.Equal(BitConverter.SingleToInt32Bits(expected), BitConverter.SingleToInt32Bits(actual.Values[i])); + } + } + + /// + /// Verifies that unsigned-short LUT construction preserves the scalar conversion result for every traversal shape. + /// + /// The number of LUT entries. + [Theory] + [MemberData(nameof(LutLengths))] + public void UInt16ConstructorMatchesScalarFormula(int length) + { + ushort[] values = new ushort[length]; + + for (int i = 0; i < values.Length; i++) + { + values[i] = (ushort)((i * 12_347) + 1_019); + } + + IccLut actual = new(values); + + Assert.Equal(values.Length, actual.Values.Length); + + for (int i = 0; i < values.Length; i++) + { + float expected = values[i] / (float)ushort.MaxValue; + Assert.Equal(BitConverter.SingleToInt32Bits(expected), BitConverter.SingleToInt32Bits(actual.Values[i])); + } + } + + /// + /// Verifies bit-exact normalization for every possible unsigned-short input. + /// + [Fact] + public void UInt16ConstructorMatchesScalarFormulaForEveryValue() + { + ushort[] values = new ushort[ushort.MaxValue + 1]; + + for (int i = 0; i < values.Length; i++) + { + values[i] = (ushort)i; + } + + IccLut actual = new(values); + + for (int i = 0; i < values.Length; i++) + { + float expected = values[i] / (float)ushort.MaxValue; + Assert.Equal(BitConverter.SingleToInt32Bits(expected), BitConverter.SingleToInt32Bits(actual.Values[i])); + } + } +}