From 92bfb8db8f1b4537e9d103532690724f755734ae Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 27 Feb 2020 02:45:01 +0100 Subject: [PATCH 1/3] Added generic Octet type --- src/ImageSharp/Common/Tuples/Octet{T}.cs | 74 ++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 src/ImageSharp/Common/Tuples/Octet{T}.cs diff --git a/src/ImageSharp/Common/Tuples/Octet{T}.cs b/src/ImageSharp/Common/Tuples/Octet{T}.cs new file mode 100644 index 0000000000..9db28e3f97 --- /dev/null +++ b/src/ImageSharp/Common/Tuples/Octet{T}.cs @@ -0,0 +1,74 @@ +// Copyright (c) Six Labors and contributors. +// Licensed under the Apache License, Version 2.0. + +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using SixLabors.ImageSharp.Tuples; + +namespace SixLabors.ImageSharp.Common.Tuples +{ + /// + /// Contains 8 element value tuples of various types. + /// + [StructLayout(LayoutKind.Sequential)] + internal struct Octet + where T : unmanaged + { + public T V0; + public T V1; + public T V2; + public T V3; + public T V4; + public T V5; + public T V6; + public T V7; + + /// + public override readonly string ToString() + { + return $"{nameof(Octet)}<{typeof(T)}>({this.V0},{this.V1},{this.V2},{this.V3},{this.V4},{this.V5},{this.V6},{this.V7})"; + } + } + + /// + /// Extension methods for the type. + /// + internal static class OctetExtensions + { + /// + /// Loads the fields in a target of from one of type. + /// + /// The target of instance. + /// The source of instance. + [MethodImpl(InliningOptions.ShortMethod)] + public static void LoadFrom(ref this Octet destination, ref Octet source) + { + destination.V0 = source.V0; + destination.V1 = source.V1; + destination.V2 = source.V2; + destination.V3 = source.V3; + destination.V4 = source.V4; + destination.V5 = source.V5; + destination.V6 = source.V6; + destination.V7 = source.V7; + } + + /// + /// Loads the fields in a target of from one of type. + /// + /// The target of instance. + /// The source of instance. + [MethodImpl(InliningOptions.ShortMethod)] + public static void LoadFrom(ref this Octet destination, ref Octet source) + { + destination.V0 = (byte)source.V0; + destination.V1 = (byte)source.V1; + destination.V2 = (byte)source.V2; + destination.V3 = (byte)source.V3; + destination.V4 = (byte)source.V4; + destination.V5 = (byte)source.V5; + destination.V6 = (byte)source.V6; + destination.V7 = (byte)source.V7; + } + } +} From f6f004690fe72b38d19b73dd14420eacbc144f42 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 27 Feb 2020 02:54:38 +0100 Subject: [PATCH 2/3] Refactored code to use new Octet type --- .../Helpers/SimdUtils.BasicIntrinsics256.cs | 30 +++++++++---------- src/ImageSharp/Common/Tuples/Octet{T}.cs | 5 ++-- .../PixelConversion_Rgba32_To_Bgra32.cs | 10 +++---- .../Vectorization/WidenBytesToUInt32.cs | 4 +-- 4 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/ImageSharp/Common/Helpers/SimdUtils.BasicIntrinsics256.cs b/src/ImageSharp/Common/Helpers/SimdUtils.BasicIntrinsics256.cs index bc07fbf317..690bf83095 100644 --- a/src/ImageSharp/Common/Helpers/SimdUtils.BasicIntrinsics256.cs +++ b/src/ImageSharp/Common/Helpers/SimdUtils.BasicIntrinsics256.cs @@ -94,17 +94,17 @@ internal static void BulkConvertByteToNormalizedFloat(ReadOnlySpan source, var magicInt = new Vector(1191182336); // reinterpreted value of 32768.0f var mask = new Vector(255); - ref Octet.OfByte sourceBase = ref Unsafe.As(ref MemoryMarshal.GetReference(source)); - ref Octet.OfUInt32 destBaseAsWideOctet = ref Unsafe.As(ref MemoryMarshal.GetReference(dest)); + ref Octet sourceBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(source)); + ref Octet destBaseAsWideOctet = ref Unsafe.As>(ref MemoryMarshal.GetReference(dest)); - ref Vector destBaseAsFloat = ref Unsafe.As>(ref destBaseAsWideOctet); + ref Vector destBaseAsFloat = ref Unsafe.As, Vector>(ref destBaseAsWideOctet); int n = dest.Length / 8; for (int i = 0; i < n; i++) { - ref Octet.OfByte s = ref Unsafe.Add(ref sourceBase, i); - ref Octet.OfUInt32 d = ref Unsafe.Add(ref destBaseAsWideOctet, i); + ref Octet s = ref Unsafe.Add(ref sourceBase, i); + ref Octet d = ref Unsafe.Add(ref destBaseAsWideOctet, i); d.LoadFrom(ref s); } @@ -137,17 +137,17 @@ internal static void BulkConvertNormalizedFloatToByteClampOverflows(ReadOnlySpan } ref Vector srcBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(source)); - ref Octet.OfByte destBase = ref Unsafe.As(ref MemoryMarshal.GetReference(dest)); + ref Octet destBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(dest)); int n = source.Length / 8; var magick = new Vector(32768.0f); var scale = new Vector(255f) / new Vector(256f); // need to copy to a temporary struct, because - // SimdUtils.Octet.OfUInt32 temp = Unsafe.As, SimdUtils.Octet.OfUInt32>(ref x) + // SimdUtils.Octet temp = Unsafe.As, SimdUtils.Octet>(ref x) // does not work. TODO: This might be a CoreClr bug, need to ask/report - var temp = default(Octet.OfUInt32); - ref Vector tempRef = ref Unsafe.As>(ref temp); + var temp = default(Octet); + ref Vector tempRef = ref Unsafe.As, Vector>(ref temp); for (int i = 0; i < n; i++) { @@ -161,7 +161,7 @@ internal static void BulkConvertNormalizedFloatToByteClampOverflows(ReadOnlySpan x = (x * scale) + magick; tempRef = x; - ref Octet.OfByte d = ref Unsafe.Add(ref destBase, i); + ref Octet d = ref Unsafe.Add(ref destBase, i); d.LoadFrom(ref temp); } } @@ -186,17 +186,17 @@ internal static void BulkConvertNormalizedFloatToByte(ReadOnlySpan source } ref Vector srcBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(source)); - ref Octet.OfByte destBase = ref Unsafe.As(ref MemoryMarshal.GetReference(dest)); + ref Octet destBase = ref Unsafe.As>(ref MemoryMarshal.GetReference(dest)); int n = source.Length / 8; var magick = new Vector(32768.0f); var scale = new Vector(255f) / new Vector(256f); // need to copy to a temporary struct, because - // SimdUtils.Octet.OfUInt32 temp = Unsafe.As, SimdUtils.Octet.OfUInt32>(ref x) + // SimdUtils.Octet temp = Unsafe.As, SimdUtils.Octet>(ref x) // does not work. TODO: This might be a CoreClr bug, need to ask/report - var temp = default(Octet.OfUInt32); - ref Vector tempRef = ref Unsafe.As>(ref temp); + var temp = default(Octet); + ref Vector tempRef = ref Unsafe.As, Vector>(ref temp); for (int i = 0; i < n; i++) { @@ -207,7 +207,7 @@ internal static void BulkConvertNormalizedFloatToByte(ReadOnlySpan source x = (x * scale) + magick; tempRef = x; - ref Octet.OfByte d = ref Unsafe.Add(ref destBase, i); + ref Octet d = ref Unsafe.Add(ref destBase, i); d.LoadFrom(ref temp); } } diff --git a/src/ImageSharp/Common/Tuples/Octet{T}.cs b/src/ImageSharp/Common/Tuples/Octet{T}.cs index 9db28e3f97..71e7da801e 100644 --- a/src/ImageSharp/Common/Tuples/Octet{T}.cs +++ b/src/ImageSharp/Common/Tuples/Octet{T}.cs @@ -3,9 +3,8 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using SixLabors.ImageSharp.Tuples; -namespace SixLabors.ImageSharp.Common.Tuples +namespace SixLabors.ImageSharp.Tuples { /// /// Contains 8 element value tuples of various types. @@ -26,7 +25,7 @@ internal struct Octet /// public override readonly string ToString() { - return $"{nameof(Octet)}<{typeof(T)}>({this.V0},{this.V1},{this.V2},{this.V3},{this.V4},{this.V5},{this.V6},{this.V7})"; + return $"Octet<{typeof(T)}>({this.V0},{this.V1},{this.V2},{this.V3},{this.V4},{this.V5},{this.V6},{this.V7})"; } } diff --git a/tests/ImageSharp.Benchmarks/General/PixelConversion/PixelConversion_Rgba32_To_Bgra32.cs b/tests/ImageSharp.Benchmarks/General/PixelConversion/PixelConversion_Rgba32_To_Bgra32.cs index 4b09dd81e1..e24dd48900 100644 --- a/tests/ImageSharp.Benchmarks/General/PixelConversion/PixelConversion_Rgba32_To_Bgra32.cs +++ b/tests/ImageSharp.Benchmarks/General/PixelConversion/PixelConversion_Rgba32_To_Bgra32.cs @@ -233,8 +233,8 @@ public void Bitops_SingleTuple() // [Benchmark] public void Bitops_Simd() { - ref Octet.OfUInt32 sBase = ref Unsafe.As(ref this.source[0]); - ref Octet.OfUInt32 dBase = ref Unsafe.As(ref this.dest[0]); + ref Octet sBase = ref Unsafe.As>(ref this.source[0]); + ref Octet dBase = ref Unsafe.As>(ref this.dest[0]); for (int i = 0; i < this.Count / 8; i++) { @@ -257,9 +257,9 @@ private struct C #pragma warning restore SA1132 // Do not combine fields [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static void BitopsSimdImpl(ref Octet.OfUInt32 s, ref Octet.OfUInt32 d) + private static void BitopsSimdImpl(ref Octet s, ref Octet d) { - Vector sVec = Unsafe.As>(ref s); + Vector sVec = Unsafe.As, Vector>(ref s); var aMask = new Vector(0xFF00FF00); var bMask = new Vector(0x00FF00FF); @@ -282,7 +282,7 @@ private static void BitopsSimdImpl(ref Octet.OfUInt32 s, ref Octet.OfUInt32 d) Vector cc = Unsafe.As>(ref c); Vector dd = aa + cc; - d = Unsafe.As, Octet.OfUInt32>(ref dd); + d = Unsafe.As, Octet>(ref dd); } // [Benchmark] diff --git a/tests/ImageSharp.Benchmarks/General/Vectorization/WidenBytesToUInt32.cs b/tests/ImageSharp.Benchmarks/General/Vectorization/WidenBytesToUInt32.cs index 870fe3271b..beac94269d 100644 --- a/tests/ImageSharp.Benchmarks/General/Vectorization/WidenBytesToUInt32.cs +++ b/tests/ImageSharp.Benchmarks/General/Vectorization/WidenBytesToUInt32.cs @@ -31,8 +31,8 @@ public void Standard() { const int N = Count / 8; - ref Octet.OfByte sBase = ref Unsafe.As(ref this.source[0]); - ref Octet.OfUInt32 dBase = ref Unsafe.As(ref this.dest[0]); + ref Octet sBase = ref Unsafe.As>(ref this.source[0]); + ref Octet dBase = ref Unsafe.As>(ref this.dest[0]); for (int i = 0; i < N; i++) { From ab4211f54c9633579c9ce4ce1c5ccc4e9df49df6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 27 Feb 2020 02:54:45 +0100 Subject: [PATCH 3/3] Removed previous Octet type --- src/ImageSharp/Common/Tuples/Octet.cs | 112 -------------------------- 1 file changed, 112 deletions(-) delete mode 100644 src/ImageSharp/Common/Tuples/Octet.cs diff --git a/src/ImageSharp/Common/Tuples/Octet.cs b/src/ImageSharp/Common/Tuples/Octet.cs deleted file mode 100644 index 7ece2ed562..0000000000 --- a/src/ImageSharp/Common/Tuples/Octet.cs +++ /dev/null @@ -1,112 +0,0 @@ -// Copyright (c) Six Labors and contributors. -// Licensed under the Apache License, Version 2.0. - -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; - -namespace SixLabors.ImageSharp.Tuples -{ - /// - /// Contains 8 element value tuples of various types. - /// - internal static class Octet - { - /// - /// Value tuple of -s. - /// - [StructLayout(LayoutKind.Explicit, Size = 8 * sizeof(uint))] - public struct OfUInt32 - { - [FieldOffset(0 * sizeof(uint))] - public uint V0; - - [FieldOffset(1 * sizeof(uint))] - public uint V1; - - [FieldOffset(2 * sizeof(uint))] - public uint V2; - - [FieldOffset(3 * sizeof(uint))] - public uint V3; - - [FieldOffset(4 * sizeof(uint))] - public uint V4; - - [FieldOffset(5 * sizeof(uint))] - public uint V5; - - [FieldOffset(6 * sizeof(uint))] - public uint V6; - - [FieldOffset(7 * sizeof(uint))] - public uint V7; - - public override string ToString() - { - return $"{nameof(Octet)}.{nameof(OfUInt32)}({this.V0},{this.V1},{this.V2},{this.V3},{this.V4},{this.V5},{this.V6},{this.V7})"; - } - - [MethodImpl(InliningOptions.ShortMethod)] - public void LoadFrom(ref OfByte src) - { - this.V0 = src.V0; - this.V1 = src.V1; - this.V2 = src.V2; - this.V3 = src.V3; - this.V4 = src.V4; - this.V5 = src.V5; - this.V6 = src.V6; - this.V7 = src.V7; - } - } - - /// - /// Value tuple of -s - /// - [StructLayout(LayoutKind.Explicit, Size = 8)] - public struct OfByte - { - [FieldOffset(0)] - public byte V0; - - [FieldOffset(1)] - public byte V1; - - [FieldOffset(2)] - public byte V2; - - [FieldOffset(3)] - public byte V3; - - [FieldOffset(4)] - public byte V4; - - [FieldOffset(5)] - public byte V5; - - [FieldOffset(6)] - public byte V6; - - [FieldOffset(7)] - public byte V7; - - public override string ToString() - { - return $"{nameof(Octet)}.{nameof(OfByte)}({this.V0},{this.V1},{this.V2},{this.V3},{this.V4},{this.V5},{this.V6},{this.V7})"; - } - - [MethodImpl(InliningOptions.ShortMethod)] - public void LoadFrom(ref OfUInt32 src) - { - this.V0 = (byte)src.V0; - this.V1 = (byte)src.V1; - this.V2 = (byte)src.V2; - this.V3 = (byte)src.V3; - this.V4 = (byte)src.V4; - this.V5 = (byte)src.V5; - this.V6 = (byte)src.V6; - this.V7 = (byte)src.V7; - } - } - } -} \ No newline at end of file