Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions src/ImageSharp/Common/Helpers/Shuffle/IPad3Layout.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;

namespace SixLabors.ImageSharp;

/// <summary>
/// Defines the byte shuffles that pad pixels of three samples to four samples, and slice them back to three samples.
/// </summary>
/// <remarks>
/// A 128-bit lane holds 16 bytes of padded pixels. The same pixels with three samples use 12 bytes. Thus three registers
/// of 3-sample pixels fill four registers of padded pixels. <c>SimdUtils.Pad3</c> and <c>SimdUtils.Slice3</c> move
/// the 12-byte groups between registers. The masks of the layout move the bytes inside each 128-bit lane.
/// </remarks>
internal interface IPad3Layout
{
/// <summary>
/// Gets the byte shuffle that pads the 3-sample pixels in bytes 0 to 11 of a lane to the 16 bytes of the lane.
/// Index 0x80 sets the fourth sample of each pixel to 0.
/// </summary>
public static abstract Vector128<byte> PadMask { get; }

/// <summary>
/// Gets the byte shuffle that slices the first three samples of each padded pixel in a lane to bytes 0 to 11 of the
/// lane. Index 0x80 sets bytes 12 to 15 to 0.
/// </summary>
public static abstract Vector128<byte> SliceMask { get; }

/// <summary>
/// Gets <see cref="SliceMask"/> moved four bytes up. It slices the first three samples of each padded pixel to
/// bytes 4 to 15 of the lane. Index 0x80 sets bytes 0 to 3 to 0.
/// </summary>
public static abstract Vector128<byte> SliceEndMask { get; }
}

/// <summary>
/// Pads pixels of three 8-bit samples to 32-bit lanes. Byte 3 of each lane is 0.
/// </summary>
internal readonly struct Pad3x8Layout : IPad3Layout
{
/// <inheritdoc />
public static Vector128<byte> PadMask
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => Vector128.Create((byte)0, 1, 2, 0x80, 3, 4, 5, 0x80, 6, 7, 8, 0x80, 9, 10, 11, 0x80);
}

/// <inheritdoc />
public static Vector128<byte> SliceMask
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => Vector128.Create((byte)0, 1, 2, 4, 5, 6, 8, 9, 10, 12, 13, 14, 0x80, 0x80, 0x80, 0x80);
}

/// <inheritdoc />
public static Vector128<byte> SliceEndMask
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => Vector128.Create((byte)0x80, 0x80, 0x80, 0x80, 0, 1, 2, 4, 5, 6, 8, 9, 10, 12, 13, 14);
}
}

/// <summary>
/// Pads pixels of three 16-bit samples to 64-bit lanes. The fourth 16-bit sample of each lane is 0.
/// </summary>
internal readonly struct Pad3x16Layout : IPad3Layout
{
/// <inheritdoc />
public static Vector128<byte> PadMask
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => Vector128.Create((byte)0, 1, 2, 3, 4, 5, 0x80, 0x80, 6, 7, 8, 9, 10, 11, 0x80, 0x80);
}

/// <inheritdoc />
public static Vector128<byte> SliceMask
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => Vector128.Create((byte)0, 1, 2, 3, 4, 5, 8, 9, 10, 11, 12, 13, 0x80, 0x80, 0x80, 0x80);
}

/// <inheritdoc />
public static Vector128<byte> SliceEndMask
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => Vector128.Create((byte)0x80, 0x80, 0x80, 0x80, 0, 1, 2, 3, 4, 5, 8, 9, 10, 11, 12, 13);
}
}
22 changes: 22 additions & 0 deletions src/ImageSharp/Common/Helpers/Shuffle/IShuffle4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,28 @@ internal interface IShuffle4 : IComponentShuffle
{
}

/// <summary>
/// Keeps XYZW components in XYZW order.
/// </summary>
internal readonly struct XYZWShuffle4 : IShuffle4
{
/// <inheritdoc />
[MethodImpl(InliningOptions.ShortMethod)]
public static uint Invoke(uint source) => source;

/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector128<byte> Invoke(Vector128<byte> source) => source;

/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector256<byte> Invoke(Vector256<byte> source) => source;

/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector512<byte> Invoke(Vector512<byte> source) => source;
}

/// <summary>
/// Reorders XYZW components to WXYZ.
/// </summary>
Expand Down
30 changes: 24 additions & 6 deletions src/ImageSharp/Common/Helpers/SimdUtils.Convert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public static void UInt16ToNormalizedFloat<TAlpha>(ReadOnlySpan<ushort> source,
/// <param name="source">The source span of bytes</param>
/// <param name="destination">The destination span of floats</param>
[MethodImpl(InliningOptions.ShortMethod)]
internal static void ByteToNormalizedFloat(ReadOnlySpan<byte> source, Span<float> destination)
public static void ByteToNormalizedFloat(ReadOnlySpan<byte> source, Span<float> destination)
=> ByteToNormalizedFloat<PixelFormats.PixelOperations.NoAlphaOperator>(source, destination);

/// <summary>
Expand All @@ -213,7 +213,7 @@ internal static void ByteToNormalizedFloat(ReadOnlySpan<byte> source, Span<float
/// <param name="source">The source span of bytes.</param>
/// <param name="destination">The destination span of floats.</param>
[MethodImpl(InliningOptions.ShortMethod)]
internal static void ByteToNormalizedFloat<TAlpha>(ReadOnlySpan<byte> source, Span<float> destination)
public static void ByteToNormalizedFloat<TAlpha>(ReadOnlySpan<byte> source, Span<float> destination)
where TAlpha : struct, PixelFormats.PixelOperations.IAlphaOperator
{
DebugGuard.IsTrue(source.Length == destination.Length, nameof(source), "Input spans must be of same length!");
Expand All @@ -236,7 +236,7 @@ internal static void ByteToNormalizedFloat<TAlpha>(ReadOnlySpan<byte> source, Sp
/// <param name="source">The source span of floats</param>
/// <param name="destination">The destination span of bytes</param>
[MethodImpl(InliningOptions.ShortMethod)]
internal static void NormalizedFloatToByteSaturate(ReadOnlySpan<float> source, Span<byte> destination)
public static void NormalizedFloatToByteSaturate(ReadOnlySpan<float> source, Span<byte> destination)
=> NormalizedFloatToByteSaturate<PixelFormats.PixelOperations.NoAlphaOperator>(source, destination);

/// <summary>
Expand All @@ -248,7 +248,7 @@ internal static void NormalizedFloatToByteSaturate(ReadOnlySpan<float> source, S
/// <param name="source">The source span of floats.</param>
/// <param name="destination">The destination span of bytes.</param>
[MethodImpl(InliningOptions.ShortMethod)]
internal static void NormalizedFloatToByteSaturate<TAlpha>(ReadOnlySpan<float> source, Span<byte> destination)
public static void NormalizedFloatToByteSaturate<TAlpha>(ReadOnlySpan<float> source, Span<byte> destination)
where TAlpha : struct, PixelFormats.PixelOperations.IAlphaOperator
{
DebugGuard.IsTrue(source.Length == destination.Length, nameof(source), "Input spans must be of same length!");
Expand All @@ -267,7 +267,7 @@ internal static void NormalizedFloatToByteSaturate<TAlpha>(ReadOnlySpan<float> s
/// <param name="source">The source byte magnitudes.</param>
/// <param name="destination">The destination bytes.</param>
[MethodImpl(InliningOptions.ShortMethod)]
internal static void FloatToByteSaturate(ReadOnlySpan<float> source, Span<byte> destination)
public static void FloatToByteSaturate(ReadOnlySpan<float> source, Span<byte> destination)
=> FloatToByteSaturate<PixelFormats.PixelOperations.NoAlphaOperator>(source, destination);

/// <summary>
Expand All @@ -279,7 +279,7 @@ internal static void FloatToByteSaturate(ReadOnlySpan<float> source, Span<byte>
/// <param name="source">The source components.</param>
/// <param name="destination">The destination bytes.</param>
[MethodImpl(InliningOptions.ShortMethod)]
internal static void FloatToByteSaturate<TAlpha>(ReadOnlySpan<float> source, Span<byte> destination)
public static void FloatToByteSaturate<TAlpha>(ReadOnlySpan<float> source, Span<byte> destination)
where TAlpha : struct, PixelFormats.PixelOperations.IAlphaOperator
{
DebugGuard.IsTrue(source.Length == destination.Length, nameof(source), "Input spans must be of same length!");
Expand All @@ -292,6 +292,11 @@ internal static void FloatToByteSaturate<TAlpha>(ReadOnlySpan<float> source, Spa
}
}

/// <summary>
/// Converts the bytes that the vector paths leave to floats from 0 to 1, one byte at a time.
/// </summary>
/// <param name="source">The bytes.</param>
/// <param name="destination">The floats. The length is the length of <paramref name="source"/>.</param>
[MethodImpl(MethodImplOptions.NoInlining)]
private static void ConvertByteToNormalizedFloatRemainder(ReadOnlySpan<byte> source, Span<float> destination)
{
Expand Down Expand Up @@ -357,6 +362,12 @@ private static void ConvertFloatToByteRemainder<TAlpha>(ReadOnlySpan<float> sour
}
}

/// <summary>
/// Converts the floats that the vector paths leave to bytes, one float at a time.
/// </summary>
/// <param name="source">The floats.</param>
/// <param name="destination">The bytes. The length is the length of <paramref name="source"/>.</param>
/// <param name="scale">The factor that multiplies each float before the conversion.</param>
[MethodImpl(MethodImplOptions.NoInlining)]
private static void ConvertFloatToByteRemainder(ReadOnlySpan<float> source, Span<byte> destination, float scale)
{
Expand All @@ -369,6 +380,13 @@ private static void ConvertFloatToByteRemainder(ReadOnlySpan<float> source, Span
}
}

/// <summary>
/// Scales a float, then rounds it to the nearest byte value. A value halfway between two bytes rounds up.
/// A value outside the byte range clamps to 0 or 255.
/// </summary>
/// <param name="value">The float.</param>
/// <param name="scale">The factor that multiplies the float.</param>
/// <returns>The byte.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static byte ConvertToByte(float value, float scale) => (byte)ColorNumerics.ClampToFinite((value * scale) + .5F, 0, byte.MaxValue);
}
20 changes: 10 additions & 10 deletions src/ImageSharp/Common/Helpers/SimdUtils.HwIntrinsics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static ushort TrailingZeroCount(ushort value)
/// <param name="source">The source buffer.</param>
/// <param name="destination">The destination buffer.</param>
[MethodImpl(InliningOptions.ShortMethod)]
internal static void ByteToNormalizedFloatReduce(
public static void ByteToNormalizedFloatReduce(
ref ReadOnlySpan<byte> source,
ref Span<float> destination)
=> ByteToNormalizedFloatReduce<PixelFormats.PixelOperations.NoAlphaOperator>(ref source, ref destination);
Expand All @@ -46,7 +46,7 @@ internal static void ByteToNormalizedFloatReduce(
/// <typeparam name="TAlpha">The alpha step that the conversion applies before each store.</typeparam>
/// <param name="source">The source buffer.</param>
/// <param name="destination">The destination buffer.</param>
internal static void ByteToNormalizedFloatReduce<TAlpha>(
public static void ByteToNormalizedFloatReduce<TAlpha>(
ref ReadOnlySpan<byte> source,
ref Span<float> destination)
where TAlpha : struct, PixelFormats.PixelOperations.IAlphaOperator
Expand Down Expand Up @@ -90,7 +90,7 @@ internal static void ByteToNormalizedFloatReduce<TAlpha>(
/// Implementation is based on MagicScaler code:
/// https://github.com/saucecontrol/PhotoSauce/blob/b5811908041200488aa18fdfd17df5fc457415dc/src/MagicScaler/Magic/Processors/ConvertersFloat.cs#L80-L182
/// </remarks>
internal static void ByteToNormalizedFloat(
public static void ByteToNormalizedFloat(
ReadOnlySpan<byte> source,
Span<float> destination)
=> ByteToNormalizedFloat<PixelFormats.PixelOperations.NoAlphaOperator>(source, destination);
Expand All @@ -105,7 +105,7 @@ internal static void ByteToNormalizedFloat(
/// <typeparam name="TAlpha">The alpha step that the conversion applies before each store.</typeparam>
/// <param name="source">The source buffer.</param>
/// <param name="destination">The destination buffer.</param>
internal static void ByteToNormalizedFloat<TAlpha>(
public static void ByteToNormalizedFloat<TAlpha>(
ReadOnlySpan<byte> source,
Span<float> destination)
where TAlpha : struct, PixelFormats.PixelOperations.IAlphaOperator
Expand Down Expand Up @@ -246,7 +246,7 @@ internal static void ByteToNormalizedFloat<TAlpha>(
/// <param name="source">The source buffer.</param>
/// <param name="destination">The destination buffer.</param>
[MethodImpl(InliningOptions.ShortMethod)]
internal static void NormalizedFloatToByteSaturateReduce(
public static void NormalizedFloatToByteSaturateReduce(
ref ReadOnlySpan<float> source,
ref Span<byte> destination)
=> FloatToByteSaturateReduce(ref source, ref destination, byte.MaxValue);
Expand All @@ -258,7 +258,7 @@ internal static void NormalizedFloatToByteSaturateReduce(
/// <param name="destination">The destination buffer.</param>
/// <param name="scaleFactor">The factor applied before conversion.</param>
[MethodImpl(InliningOptions.ShortMethod)]
internal static void FloatToByteSaturateReduce(
public static void FloatToByteSaturateReduce(
ref ReadOnlySpan<float> source,
ref Span<byte> destination,
float scaleFactor)
Expand All @@ -272,7 +272,7 @@ internal static void FloatToByteSaturateReduce(
/// <param name="source">The source buffer.</param>
/// <param name="destination">The destination buffer.</param>
/// <param name="scaleFactor">The factor applied before conversion.</param>
internal static void FloatToByteSaturateReduce<TAlpha>(
public static void FloatToByteSaturateReduce<TAlpha>(
ref ReadOnlySpan<float> source,
ref Span<byte> destination,
float scaleFactor)
Expand Down Expand Up @@ -321,7 +321,7 @@ internal static void FloatToByteSaturateReduce<TAlpha>(
/// Implementation is based on MagicScaler code:
/// https://github.com/saucecontrol/PhotoSauce/blob/b5811908041200488aa18fdfd17df5fc457415dc/src/MagicScaler/Magic/Processors/ConvertersFloat.cs#L541-L622
/// </remarks>
internal static void NormalizedFloatToByteSaturate(
public static void NormalizedFloatToByteSaturate(
ReadOnlySpan<float> source,
Span<byte> destination)
=> FloatToByteSaturate(source, destination, byte.MaxValue);
Expand All @@ -335,7 +335,7 @@ internal static void NormalizedFloatToByteSaturate(
/// </param>
/// <param name="destination">The destination buffer.</param>
/// <param name="scaleFactor">The factor applied before conversion.</param>
internal static void FloatToByteSaturate(
public static void FloatToByteSaturate(
ReadOnlySpan<float> source,
Span<byte> destination,
float scaleFactor)
Expand All @@ -355,7 +355,7 @@ internal static void FloatToByteSaturate(
/// </param>
/// <param name="destination">The destination buffer.</param>
/// <param name="scaleFactor">The factor applied before conversion.</param>
internal static void FloatToByteSaturate<TAlpha>(
public static void FloatToByteSaturate<TAlpha>(
ReadOnlySpan<float> source,
Span<byte> destination,
float scaleFactor)
Expand Down
50 changes: 25 additions & 25 deletions src/ImageSharp/Common/Helpers/SimdUtils.Interleave.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,31 +349,6 @@ public static void Interleave4(Vector512<ushort> x, Vector512<ushort> y, Vector5
StoreInterleaved4(row0, row1, row2, row3, ref Unsafe.As<ushort, byte>(ref destination));
}

/// <summary>
/// Transposes the 128-bit lanes of four interleaved registers into consecutive output blocks.
/// </summary>
/// <param name="row0">The first packed pixel group from each 128-bit lane.</param>
/// <param name="row1">The second packed pixel group from each 128-bit lane.</param>
/// <param name="row2">The third packed pixel group from each 128-bit lane.</param>
/// <param name="row3">The fourth packed pixel group from each 128-bit lane.</param>
/// <param name="destination">The first of 256 destination bytes.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void StoreInterleaved4(Vector512<int> row0, Vector512<int> row1, Vector512<int> row2, Vector512<int> row3, ref byte destination)
{
// Zips keep 128-bit lanes separate. The first two outputs need lane 0 from all four rows, then lane 1.
// Pair lanes 0/1 from rows 0/1 and 2/3. The second shuffle selects each complete output block.
Vector512<int> lower01 = Vector512_.Shuffle4x128(row0, row1, Shuffle.MMShuffle1010);
Vector512<int> lower23 = Vector512_.Shuffle4x128(row2, row3, Shuffle.MMShuffle1010);
Vector512_.Shuffle4x128(lower01, lower23, Shuffle.MMShuffle2020).AsByte().StoreUnsafe(ref destination);
Vector512_.Shuffle4x128(lower01, lower23, Shuffle.MMShuffle3131).AsByte().StoreUnsafe(ref destination, 64U);

// Repeat for lanes 2/3. Four unaligned stores cover the exact 256-byte output, with no padding.
Vector512<int> upper01 = Vector512_.Shuffle4x128(row0, row1, Shuffle.MMShuffle3232);
Vector512<int> upper23 = Vector512_.Shuffle4x128(row2, row3, Shuffle.MMShuffle3232);
Vector512_.Shuffle4x128(upper01, upper23, Shuffle.MMShuffle2020).AsByte().StoreUnsafe(ref destination, 128U);
Vector512_.Shuffle4x128(upper01, upper23, Shuffle.MMShuffle3131).AsByte().StoreUnsafe(ref destination, 192U);
}

/// <summary>
/// Interleaves 8 unsigned word triples without changing their stored bits.
/// </summary>
Expand Down Expand Up @@ -1759,4 +1734,29 @@ public static void PackedInvertNormalizeInterleave4(
Unsafe.Add(ref packedRef, packedOffset + 3) = (maxValue - Unsafe.Add(ref wLaneRef, sourceOffset)) * scale;
}
}

/// <summary>
/// Transposes the 128-bit lanes of four interleaved registers into consecutive output blocks.
/// </summary>
/// <param name="row0">The first packed pixel group from each 128-bit lane.</param>
/// <param name="row1">The second packed pixel group from each 128-bit lane.</param>
/// <param name="row2">The third packed pixel group from each 128-bit lane.</param>
/// <param name="row3">The fourth packed pixel group from each 128-bit lane.</param>
/// <param name="destination">The first of 256 destination bytes.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void StoreInterleaved4(Vector512<int> row0, Vector512<int> row1, Vector512<int> row2, Vector512<int> row3, ref byte destination)
{
// Zips keep 128-bit lanes separate. The first two outputs need lane 0 from all four rows, then lane 1.
// Pair lanes 0/1 from rows 0/1 and 2/3. The second shuffle selects each complete output block.
Vector512<int> lower01 = Vector512_.Shuffle4x128(row0, row1, Shuffle.MMShuffle1010);
Vector512<int> lower23 = Vector512_.Shuffle4x128(row2, row3, Shuffle.MMShuffle1010);
Vector512_.Shuffle4x128(lower01, lower23, Shuffle.MMShuffle2020).AsByte().StoreUnsafe(ref destination);
Vector512_.Shuffle4x128(lower01, lower23, Shuffle.MMShuffle3131).AsByte().StoreUnsafe(ref destination, 64U);

// Repeat for lanes 2/3. Four unaligned stores cover the exact 256-byte output, with no padding.
Vector512<int> upper01 = Vector512_.Shuffle4x128(row0, row1, Shuffle.MMShuffle3232);
Vector512<int> upper23 = Vector512_.Shuffle4x128(row2, row3, Shuffle.MMShuffle3232);
Vector512_.Shuffle4x128(upper01, upper23, Shuffle.MMShuffle2020).AsByte().StoreUnsafe(ref destination, 128U);
Vector512_.Shuffle4x128(upper01, upper23, Shuffle.MMShuffle3131).AsByte().StoreUnsafe(ref destination, 192U);
}
}
Loading
Loading