From 8f9fc6ef7a330d1731a7efe02c21015e5b59729b Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Wed, 13 Sep 2023 00:46:12 +0100 Subject: [PATCH] refactor: remove target-conditional branches (#87) --- X10D.Tests/src/Collections/ByteTests.cs | 2 - X10D.Tests/src/Collections/Int16Tests.cs | 4 -- X10D.Tests/src/Collections/Int32Tests.cs | 4 -- X10D.Tests/src/Core/IntrinsicTests.cs | 2 - X10D.Tests/src/Core/SpanTest.cs | 8 --- X10D.Tests/src/Drawing/ColorTests.cs | 2 - X10D.Tests/src/Text/CoreTests.cs | 2 - X10D.Tests/src/Text/RuneTests.cs | 2 - X10D.Tests/src/Text/StringTests.cs | 6 --- X10D.Tests/src/Time/DateOnlyTests.cs | 2 - X10D.Tests/src/Time/DoubleTests.cs | 2 - X10D/src/Collections/ByteExtensions.cs | 9 +--- X10D/src/Collections/DictionaryExtensions.cs | 45 ---------------- X10D/src/Collections/Int16Extensions.cs | 9 +--- X10D/src/Collections/Int32Extensions.cs | 9 +--- .../src/CompilerServices/CompilerResources.cs | 4 -- X10D/src/Core/EnumExtensions.cs | 16 ------ X10D/src/Core/IntrinsicExtensions.cs | 2 - X10D/src/Core/IntrinsicUtility.cs | 16 ------ X10D/src/Core/RandomExtensions.cs | 8 --- X10D/src/Core/SpanExtensions.cs | 29 +--------- X10D/src/Drawing/ColorExtensions.cs | 8 --- X10D/src/IO/DecimalExtensions.cs | 6 --- X10D/src/IO/DoubleExtensions.cs | 20 ------- X10D/src/IO/SingleExtensions.cs | 20 ------- X10D/src/IO/StreamExtensions.Reading.cs | 45 ---------------- X10D/src/Linq/EnumerableExtensions.cs | 4 -- X10D/src/Math/MathUtility.cs | 16 ------ X10D/src/Numerics/UInt32Extensions.cs | 24 --------- X10D/src/Numerics/UInt64Extensions.cs | 25 --------- X10D/src/Text/EnumerableExtensions.cs | 2 - X10D/src/Text/Extensions.cs | 2 - X10D/src/Text/RuneExtensions.cs | 4 +- X10D/src/Text/StringExtensions.cs | 54 ------------------- X10D/src/Time/DateOnlyExtensions.cs | 2 - X10D/src/Time/HalfExtensions.cs | 2 - 36 files changed, 5 insertions(+), 412 deletions(-) diff --git a/X10D.Tests/src/Collections/ByteTests.cs b/X10D.Tests/src/Collections/ByteTests.cs index 4891123..304999a 100644 --- a/X10D.Tests/src/Collections/ByteTests.cs +++ b/X10D.Tests/src/Collections/ByteTests.cs @@ -47,7 +47,6 @@ internal class ByteTests }); } -#if NET5_0_OR_GREATER [Test] public void UnpackInternal_Fallback_ShouldUnpackToSpanCorrectly() { @@ -92,7 +91,6 @@ internal class ByteTests Assert.That(bits[7], Is.True); }); } -#endif [Test] public void Unpack_ShouldRepackEqually() diff --git a/X10D.Tests/src/Collections/Int16Tests.cs b/X10D.Tests/src/Collections/Int16Tests.cs index f7bfab3..c01dbe4 100644 --- a/X10D.Tests/src/Collections/Int16Tests.cs +++ b/X10D.Tests/src/Collections/Int16Tests.cs @@ -1,6 +1,4 @@ -#if NET5_0_OR_GREATER using System.Runtime.Intrinsics.X86; -#endif using NUnit.Framework; using X10D.Collections; @@ -84,7 +82,6 @@ internal class Int16Tests }); } -#if NET5_0_OR_GREATER [Test] public void UnpackInternal_Ssse3_ShouldUnpackToSpanCorrectly() { @@ -115,7 +112,6 @@ internal class Int16Tests } }); } -#endif [Test] public void Unpack_ShouldRepackEqually() diff --git a/X10D.Tests/src/Collections/Int32Tests.cs b/X10D.Tests/src/Collections/Int32Tests.cs index 39b1036..0a8ea19 100644 --- a/X10D.Tests/src/Collections/Int32Tests.cs +++ b/X10D.Tests/src/Collections/Int32Tests.cs @@ -1,6 +1,4 @@ -#if NET5_0_OR_GREATER using System.Runtime.Intrinsics.X86; -#endif using NUnit.Framework; using X10D.Collections; @@ -85,7 +83,6 @@ internal class Int32Tests }); } -#if NET5_0_OR_GREATER [Test] public void UnpackInternal_Ssse3_ShouldUnpackToSpanCorrectly() { @@ -145,7 +142,6 @@ internal class Int32Tests } }); } -#endif [Test] public void Unpack_ShouldRepackEqually() diff --git a/X10D.Tests/src/Core/IntrinsicTests.cs b/X10D.Tests/src/Core/IntrinsicTests.cs index e45f236..a37c99a 100644 --- a/X10D.Tests/src/Core/IntrinsicTests.cs +++ b/X10D.Tests/src/Core/IntrinsicTests.cs @@ -1,4 +1,3 @@ -#if NET6_0_OR_GREATER using System.Runtime.Intrinsics; using System.Runtime.Intrinsics.X86; using NUnit.Framework; @@ -223,4 +222,3 @@ internal class IntrinsicTests Assert.That(result, Is.EqualTo(expectedResult)); } } -#endif diff --git a/X10D.Tests/src/Core/SpanTest.cs b/X10D.Tests/src/Core/SpanTest.cs index 4755ff0..957d1cb 100644 --- a/X10D.Tests/src/Core/SpanTest.cs +++ b/X10D.Tests/src/Core/SpanTest.cs @@ -1,7 +1,5 @@ -#if NET5_0_OR_GREATER using System.Runtime.Intrinsics.Arm; using System.Runtime.Intrinsics.X86; -#endif using NUnit.Framework; using X10D.Core; @@ -209,7 +207,6 @@ internal class SpanTest Assert.That(actual, Is.EqualTo(expected)); } -#if NET5_0_OR_GREATER [Test] public void PackByteInternal_Sse2_ShouldReturnCorrectByte_GivenReadOnlySpan_Using() { @@ -241,7 +238,6 @@ internal class SpanTest Assert.That(actual, Is.EqualTo(expected)); } -#endif [Test] public void PackInt16_ShouldReturnSameAsPackByte_WhenSpanHasLength8() @@ -268,7 +264,6 @@ internal class SpanTest Assert.That(actual, Is.EqualTo(expected)); } -#if NET5_0_OR_GREATER [Test] public void PackInt16Internal_Sse2_ShouldReturnCorrectInt16_GivenReadOnlySpan_Using() { @@ -287,7 +282,6 @@ internal class SpanTest Assert.That(actual, Is.EqualTo(expected)); } -#endif [Test] public void PackInt32Internal_Fallback_ShouldReturnCorrectInt32_GivenReadOnlySpan() @@ -304,7 +298,6 @@ internal class SpanTest Assert.That(actual, Is.EqualTo(expected)); } -#if NET5_0_OR_GREATER [Test] public void PackInt32Internal_Sse2_ShouldReturnCorrectInt32_GivenReadOnlySpan() { @@ -364,7 +357,6 @@ internal class SpanTest Assert.That(actual, Is.EqualTo(expected)); } -#endif [Test] public void PackInt32_ShouldReturnSameAsPackByte_WhenSpanHasLength8_UsingReadOnlySpan() diff --git a/X10D.Tests/src/Drawing/ColorTests.cs b/X10D.Tests/src/Drawing/ColorTests.cs index cb5cc97..de95f14 100644 --- a/X10D.Tests/src/Drawing/ColorTests.cs +++ b/X10D.Tests/src/Drawing/ColorTests.cs @@ -207,9 +207,7 @@ internal class ColorTests Assert.That(Color.Plum.GetClosestConsoleColor(), Is.EqualTo(ConsoleColor.DarkGray)); Assert.That(Color.PowderBlue.GetClosestConsoleColor(), Is.EqualTo(ConsoleColor.DarkGray)); Assert.That(Color.Purple.GetClosestConsoleColor(), Is.EqualTo(ConsoleColor.DarkMagenta)); -#if NET6_0_OR_GREATER Assert.That(Color.RebeccaPurple.GetClosestConsoleColor(), Is.EqualTo(ConsoleColor.DarkMagenta)); -#endif Assert.That(Color.Red.GetClosestConsoleColor(), Is.EqualTo(ConsoleColor.Red)); Assert.That(Color.RosyBrown.GetClosestConsoleColor(), Is.EqualTo(ConsoleColor.DarkGray)); Assert.That(Color.RoyalBlue.GetClosestConsoleColor(), Is.EqualTo(ConsoleColor.DarkCyan)); diff --git a/X10D.Tests/src/Text/CoreTests.cs b/X10D.Tests/src/Text/CoreTests.cs index ca2f1dd..684fa26 100644 --- a/X10D.Tests/src/Text/CoreTests.cs +++ b/X10D.Tests/src/Text/CoreTests.cs @@ -6,7 +6,6 @@ namespace X10D.Tests.Text; [TestFixture] internal class CoreTests { -#if NET5_0_OR_GREATER [Test] public void ToJsonShouldNotBeEmpty() { @@ -24,5 +23,4 @@ internal class CoreTests CollectionAssert.AreEqual(source, target); CollectionAssert.AreEquivalent(source, target); } -#endif } diff --git a/X10D.Tests/src/Text/RuneTests.cs b/X10D.Tests/src/Text/RuneTests.cs index fc196c9..0efb605 100644 --- a/X10D.Tests/src/Text/RuneTests.cs +++ b/X10D.Tests/src/Text/RuneTests.cs @@ -1,4 +1,3 @@ -#if NET5_0_OR_GREATER using System.Text; using NUnit.Framework; using X10D.Text; @@ -90,4 +89,3 @@ internal class RuneTests Assert.That(repeated, Is.EqualTo("𐀀𐀀𐀀𐀀𐀀𐀀")); } } -#endif diff --git a/X10D.Tests/src/Text/StringTests.cs b/X10D.Tests/src/Text/StringTests.cs index 060a6f7..d658b72 100644 --- a/X10D.Tests/src/Text/StringTests.cs +++ b/X10D.Tests/src/Text/StringTests.cs @@ -1,7 +1,5 @@ using System.Text; -#if NET5_0_OR_GREATER using System.Text.Json.Serialization; -#endif using NUnit.Framework; using X10D.Text; @@ -418,7 +416,6 @@ internal class StringTests Assert.Throws(() => _ = " ".EnumParse()); } -#if NET5_0_OR_GREATER [Test] public void FromJson_ShouldDeserializeCorrectly_GivenJsonString() { @@ -434,7 +431,6 @@ internal class StringTests Assert.That(target.Values[2], Is.EqualTo(3)); }); } -#endif [Test] public void GetBytes_ShouldReturnUtf8Bytes_GivenHelloWorld() @@ -1006,11 +1002,9 @@ internal class StringTests }); } -#if NET5_0_OR_GREATER private struct SampleStructure { [JsonPropertyName("values")] public int[] Values { get; set; } } -#endif } diff --git a/X10D.Tests/src/Time/DateOnlyTests.cs b/X10D.Tests/src/Time/DateOnlyTests.cs index bae376b..15b10ce 100644 --- a/X10D.Tests/src/Time/DateOnlyTests.cs +++ b/X10D.Tests/src/Time/DateOnlyTests.cs @@ -1,4 +1,3 @@ -#if NET6_0_OR_GREATER using NUnit.Framework; using X10D.Time; @@ -230,4 +229,3 @@ internal class DateOnlyTests Assert.That(date.ToUnixTimeSeconds(time), Is.EqualTo(946684800)); } } -#endif diff --git a/X10D.Tests/src/Time/DoubleTests.cs b/X10D.Tests/src/Time/DoubleTests.cs index 5247837..18afb8f 100644 --- a/X10D.Tests/src/Time/DoubleTests.cs +++ b/X10D.Tests/src/Time/DoubleTests.cs @@ -1,4 +1,3 @@ -#if NET5_0_OR_GREATER using NUnit.Framework; using X10D.Time; @@ -52,4 +51,3 @@ internal class DoubleTests Assert.That((_negativeOne).Weeks() < TimeSpan.Zero); } } -#endif diff --git a/X10D/src/Collections/ByteExtensions.cs b/X10D/src/Collections/ByteExtensions.cs index 8563ae2..1fa4ca9 100644 --- a/X10D/src/Collections/ByteExtensions.cs +++ b/X10D/src/Collections/ByteExtensions.cs @@ -1,12 +1,9 @@ using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; -using X10D.CompilerServices; - -#if NETCOREAPP3_0_OR_GREATER using System.Runtime.Intrinsics; using System.Runtime.Intrinsics.X86; -#endif +using X10D.CompilerServices; namespace X10D.Collections; @@ -46,13 +43,11 @@ public static class ByteExtensions throw new ArgumentException(ExceptionMessages.DestinationSpanLengthTooShort, nameof(destination)); } -#if NETCOREAPP3_0_OR_GREATER if (Sse3.IsSupported) { UnpackInternal_Ssse3(value, destination); return; } -#endif UnpackInternal_Fallback(value, destination); } @@ -66,7 +61,6 @@ public static class ByteExtensions } } -#if NETCOREAPP3_0_OR_GREATER [MethodImpl(CompilerResources.MethodImplOptions)] internal unsafe static void UnpackInternal_Ssse3(this byte value, Span destination) { @@ -87,5 +81,4 @@ public static class ByteExtensions Sse2.StoreScalar((long*)pDestination, correctness.AsInt64()); } } -#endif } diff --git a/X10D/src/Collections/DictionaryExtensions.cs b/X10D/src/Collections/DictionaryExtensions.cs index 387016e..6138a62 100644 --- a/X10D/src/Collections/DictionaryExtensions.cs +++ b/X10D/src/Collections/DictionaryExtensions.cs @@ -1,7 +1,5 @@ using System.Diagnostics.Contracts; -#if NET6_0_OR_GREATER using System.Runtime.InteropServices; -#endif using System.Web; namespace X10D.Collections; @@ -47,23 +45,10 @@ public static class DictionaryExtensions throw new ArgumentNullException(nameof(updateValueFactory)); } -#if NET6_0_OR_GREATER ref var value = ref CollectionsMarshal.GetValueRefOrAddDefault(dictionary, key, out bool exists); // DO NOT CHANGE. reassigning value is necessary to mutate the dictionary, due to ref return above. // mutation of the dictionary is INTENDED BEHAVIOUR. this is not a mistake. return value = exists ? updateValueFactory(key, value!) : addValue; -#else - if (dictionary.TryGetValue(key, out TValue? old)) - { - TValue updated = updateValueFactory(key, old); - dictionary[key] = updated; - - return updated; - } - - dictionary.Add(key, addValue); - return addValue; -#endif } /// @@ -157,25 +142,10 @@ public static class DictionaryExtensions throw new ArgumentNullException(nameof(updateValueFactory)); } -#if NET6_0_OR_GREATER ref TValue? value = ref CollectionsMarshal.GetValueRefOrAddDefault(dictionary, key, out bool exists); // DO NOT CHANGE. reassigning value is necessary to mutate the dictionary, due to ref return above. // mutation of the dictionary is INTENDED BEHAVIOUR. this is not a mistake. return value = exists ? updateValueFactory(key, value!) : addValueFactory(key); -#else - if (dictionary.TryGetValue(key, out TValue? old)) - { - TValue updated = updateValueFactory(key, old); - dictionary[key] = updated; - - return updated; - } - - TValue add = addValueFactory(key); - dictionary.Add(key, add); - - return add; -#endif } /// @@ -284,25 +254,10 @@ public static class DictionaryExtensions throw new ArgumentNullException(nameof(updateValueFactory)); } -#if NET6_0_OR_GREATER ref TValue? value = ref CollectionsMarshal.GetValueRefOrAddDefault(dictionary, key, out bool exists); // DO NOT CHANGE. reassigning value is necessary to mutate the dictionary, due to ref return above. // mutation of the dictionary is INTENDED BEHAVIOUR. this is not a mistake. return value = exists ? updateValueFactory(key, value!, factoryArgument) : addValueFactory(key, factoryArgument); -#else - if (dictionary.TryGetValue(key, out TValue? old)) - { - TValue updated = updateValueFactory(key, old, factoryArgument); - dictionary[key] = updated; - - return updated; - } - - TValue add = addValueFactory(key, factoryArgument); - dictionary.Add(key, add); - - return add; -#endif } /// diff --git a/X10D/src/Collections/Int16Extensions.cs b/X10D/src/Collections/Int16Extensions.cs index 9e6e16b..63948ab 100644 --- a/X10D/src/Collections/Int16Extensions.cs +++ b/X10D/src/Collections/Int16Extensions.cs @@ -1,12 +1,9 @@ using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; -using X10D.CompilerServices; - -#if NETCOREAPP3_0_OR_GREATER using System.Runtime.Intrinsics; using System.Runtime.Intrinsics.X86; -#endif +using X10D.CompilerServices; namespace X10D.Collections; @@ -46,13 +43,11 @@ public static class Int16Extensions throw new ArgumentException(ExceptionMessages.DestinationSpanLengthTooShort, nameof(destination)); } -#if NETCOREAPP3_0_OR_GREATER if (Sse3.IsSupported) { UnpackInternal_Ssse3(value, destination); return; } -#endif UnpackInternal_Fallback(value, destination); } @@ -66,7 +61,6 @@ public static class Int16Extensions } } -#if NETCOREAPP3_0_OR_GREATER [MethodImpl(CompilerResources.MethodImplOptions)] internal unsafe static void UnpackInternal_Ssse3(this short value, Span destination) { @@ -89,5 +83,4 @@ public static class Int16Extensions Sse2.Store((byte*)pDestination, correctness); } } -#endif } diff --git a/X10D/src/Collections/Int32Extensions.cs b/X10D/src/Collections/Int32Extensions.cs index e5bdd7c..c8ab5a0 100644 --- a/X10D/src/Collections/Int32Extensions.cs +++ b/X10D/src/Collections/Int32Extensions.cs @@ -1,12 +1,9 @@ using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; -using X10D.CompilerServices; - -#if NETCOREAPP3_0_OR_GREATER using System.Runtime.Intrinsics; using System.Runtime.Intrinsics.X86; -#endif +using X10D.CompilerServices; namespace X10D.Collections; @@ -46,7 +43,6 @@ public static class Int32Extensions throw new ArgumentException(ExceptionMessages.DestinationSpanLengthTooShort, nameof(destination)); } -#if NETCOREAPP3_0_OR_GREATER if (Avx2.IsSupported) { UnpackInternal_Avx2(value, destination); @@ -58,7 +54,6 @@ public static class Int32Extensions UnpackInternal_Ssse3(value, destination); return; } -#endif UnpackInternal_Fallback(value, destination); } @@ -72,7 +67,6 @@ public static class Int32Extensions } } -#if NETCOREAPP3_0_OR_GREATER [MethodImpl(CompilerResources.MethodImplOptions)] internal static unsafe void UnpackInternal_Ssse3(this int value, Span destination) { @@ -130,5 +124,4 @@ public static class Int32Extensions Avx.Store((byte*)pDestination, correctness); } } -#endif } diff --git a/X10D/src/CompilerServices/CompilerResources.cs b/X10D/src/CompilerServices/CompilerResources.cs index a02ca99..3c68446 100644 --- a/X10D/src/CompilerServices/CompilerResources.cs +++ b/X10D/src/CompilerServices/CompilerResources.cs @@ -4,10 +4,6 @@ namespace X10D.CompilerServices; internal static class CompilerResources { -#if NETCOREAPP3_0_OR_GREATER public const MethodImplOptions MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining | System.Runtime.CompilerServices.MethodImplOptions.AggressiveOptimization; -#else - public const MethodImplOptions MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining; -#endif } diff --git a/X10D/src/Core/EnumExtensions.cs b/X10D/src/Core/EnumExtensions.cs index 2545f1e..6b813ef 100644 --- a/X10D/src/Core/EnumExtensions.cs +++ b/X10D/src/Core/EnumExtensions.cs @@ -20,11 +20,7 @@ public static class EnumExtensions public static T Next(this T value) where T : struct, Enum { -#if NET5_0_OR_GREATER T[] values = Enum.GetValues(); -#else - T[] values = Enum.GetValues(typeof(T)).Cast().ToArray(); -#endif int index = Array.IndexOf(values, value) + 1; index %= values.Length; return values[index]; @@ -44,11 +40,7 @@ public static class EnumExtensions public static T NextUnchecked(this T value) where T : struct, Enum { -#if NET5_0_OR_GREATER T[] values = Enum.GetValues(); -#else - T[] values = Enum.GetValues(typeof(T)).Cast().ToArray(); -#endif int index = Array.IndexOf(values, value) + 1; return values[index]; } @@ -66,11 +58,7 @@ public static class EnumExtensions public static T Previous(this T value) where T : struct, Enum { -#if NET5_0_OR_GREATER T[] values = Enum.GetValues(); -#else - T[] values = Enum.GetValues(typeof(T)).Cast().ToArray(); -#endif int index = Array.IndexOf(values, value) - 1; int length = values.Length; @@ -94,11 +82,7 @@ public static class EnumExtensions public static T PreviousUnchecked(this T value) where T : struct, Enum { -#if NET5_0_OR_GREATER T[] values = Enum.GetValues(); -#else - T[] values = Enum.GetValues(typeof(T)).Cast().ToArray(); -#endif int index = Array.IndexOf(values, value) - 1; return values[index]; } diff --git a/X10D/src/Core/IntrinsicExtensions.cs b/X10D/src/Core/IntrinsicExtensions.cs index 7085c63..5a95f5f 100644 --- a/X10D/src/Core/IntrinsicExtensions.cs +++ b/X10D/src/Core/IntrinsicExtensions.cs @@ -1,4 +1,3 @@ -#if NETCOREAPP3_0_OR_GREATER using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; @@ -188,4 +187,3 @@ public static class IntrinsicExtensions return Sse2.Shuffle(vector.AsDouble(), vector.AsDouble(), 0b01).AsUInt64(); } } -#endif diff --git a/X10D/src/Core/IntrinsicUtility.cs b/X10D/src/Core/IntrinsicUtility.cs index 44997cc..76fc41b 100644 --- a/X10D/src/Core/IntrinsicUtility.cs +++ b/X10D/src/Core/IntrinsicUtility.cs @@ -1,5 +1,3 @@ -#if NETCOREAPP3_0_OR_GREATER - using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; @@ -184,34 +182,22 @@ public static class IntrinsicUtility [MethodImpl(CompilerResources.MethodImplOptions)] internal static Vector64 GetUninitializedVector64() where T : struct { -#if NET6_0_OR_GREATER Unsafe.SkipInit(out Vector64 output); return output; -#else - return default; -#endif } [MethodImpl(CompilerResources.MethodImplOptions)] internal static Vector128 GetUninitializedVector128() where T : struct { -#if NET6_0_OR_GREATER Unsafe.SkipInit(out Vector128 output); return output; -#else - return default; -#endif } [MethodImpl(CompilerResources.MethodImplOptions)] internal static Vector256 GetUninitializedVector256() where T : struct { -#if NET6_0_OR_GREATER Unsafe.SkipInit(out Vector256 output); return output; -#else - return default; -#endif } [Pure] @@ -315,5 +301,3 @@ public static class IntrinsicUtility return Avx2.Add(high, ac); } } - -#endif diff --git a/X10D/src/Core/RandomExtensions.cs b/X10D/src/Core/RandomExtensions.cs index fa8d412..7cbdcc0 100644 --- a/X10D/src/Core/RandomExtensions.cs +++ b/X10D/src/Core/RandomExtensions.cs @@ -11,10 +11,6 @@ namespace X10D.Core; /// public static class RandomExtensions { -#if !NET6_0_OR_GREATER - private static readonly Random Shared = new(); -#endif - /// /// Returns a random value that defined in a specified enum. /// @@ -505,10 +501,6 @@ public static class RandomExtensions internal static Random GetShared() { -#if NET6_0_OR_GREATER return Random.Shared; -#else - return Shared; -#endif } } diff --git a/X10D/src/Core/SpanExtensions.cs b/X10D/src/Core/SpanExtensions.cs index c75880c..b63e354 100644 --- a/X10D/src/Core/SpanExtensions.cs +++ b/X10D/src/Core/SpanExtensions.cs @@ -2,13 +2,10 @@ using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using X10D.CompilerServices; - -#if NETCOREAPP3_0_OR_GREATER using System.Runtime.Intrinsics; using System.Runtime.Intrinsics.X86; using System.Runtime.Intrinsics.Arm; -#endif +using X10D.CompilerServices; #if NET7_0_OR_GREATER using System.Diagnostics; @@ -21,7 +18,6 @@ namespace X10D.Core; /// public static class SpanExtensions { -#if NETCOREAPP3_0_OR_GREATER private const ulong IntegerPackingMagic = 0x0102040810204080; [ExcludeFromCodeCoverage] @@ -35,7 +31,6 @@ public static class SpanExtensions { get => Vector256.Create(IntegerPackingMagic); } -#endif /// /// Returns a value indicating whether a specific enumeration value is contained with the current span of elements. @@ -71,7 +66,6 @@ public static class SpanExtensions [MethodImpl(CompilerResources.MethodImplOptions)] public static bool Contains(this ReadOnlySpan span, T value) where T : struct, Enum { -#if NET6_0_OR_GREATER unsafe { #pragma warning disable CS8500 @@ -114,17 +108,6 @@ public static class SpanExtensions // dotcover enable } } -#else - foreach (var it in span) - { - if (EqualityComparer.Default.Equals(it, value)) - { - return true; - } - } - - return false; -#endif } /// @@ -161,7 +144,6 @@ public static class SpanExtensions return PackByteInternal_Fallback(source); } -#if NETCOREAPP3_0_OR_GREATER if (!BitConverter.IsLittleEndian) { return PackByteInternal_Fallback(source); @@ -176,7 +158,6 @@ public static class SpanExtensions { return PackByteInternal_AdvSimd(source); } -#endif return PackByteInternal_Fallback(source); } @@ -217,12 +198,10 @@ public static class SpanExtensions goto default; } -#if NETCOREAPP3_0_OR_GREATER if (Sse2.IsSupported) { return PackInt16Internal_Sse2(source); } -#endif goto default; case < 16: @@ -269,7 +248,6 @@ public static class SpanExtensions return PackInt16(source); case 32: -#if NETCOREAPP3_0_OR_GREATER if (!BitConverter.IsLittleEndian) { goto default; @@ -289,7 +267,6 @@ public static class SpanExtensions { return PackInt32Internal_AdvSimd(source); } -#endif goto default; default: @@ -383,7 +360,6 @@ public static class SpanExtensions return result; } -#if NETCOREAPP3_0_OR_GREATER [Pure] [MethodImpl(CompilerResources.MethodImplOptions)] internal static byte PackByteInternal_Sse2(this ReadOnlySpan source) @@ -499,7 +475,6 @@ public static class SpanExtensions } } -#if NET5_0_OR_GREATER // dotcover disable //NOSONAR [Pure] @@ -517,6 +492,4 @@ public static class SpanExtensions } //NOSONAR // dotcover enable -#endif -#endif } diff --git a/X10D/src/Drawing/ColorExtensions.cs b/X10D/src/Drawing/ColorExtensions.cs index 86f7615..7e805ac 100644 --- a/X10D/src/Drawing/ColorExtensions.cs +++ b/X10D/src/Drawing/ColorExtensions.cs @@ -72,17 +72,9 @@ public static class ColorExtensions double blue = color.B; var delta = double.MaxValue; -#if NET5_0_OR_GREATER foreach (ConsoleColor consoleColor in Enum.GetValues()) -#else - foreach (ConsoleColor consoleColor in Enum.GetValues(typeof(ConsoleColor))) -#endif { -#if NET5_0_OR_GREATER string name = Enum.GetName(consoleColor)!; -#else - string name = Enum.GetName(typeof(ConsoleColor), consoleColor)!; -#endif Color currentColor = Color.FromName(name == "DarkYellow" ? "Orange" : name); // bug fix double r = currentColor.R - red; double g = currentColor.G - green; diff --git a/X10D/src/IO/DecimalExtensions.cs b/X10D/src/IO/DecimalExtensions.cs index b72b428..68b9a6e 100644 --- a/X10D/src/IO/DecimalExtensions.cs +++ b/X10D/src/IO/DecimalExtensions.cs @@ -93,13 +93,7 @@ public static class DecimalExtensions private static void GetBits(decimal value, Span destination) { -#if NET5_0_OR_GREATER _ = decimal.GetBits(value, destination); -#else - Span buffer = stackalloc byte[16]; - MemoryMarshal.Write(buffer, ref value); - WriteBits(destination, buffer); -#endif } #if !NET5_0_OR_GREATER diff --git a/X10D/src/IO/DoubleExtensions.cs b/X10D/src/IO/DoubleExtensions.cs index 03807b6..c190e45 100644 --- a/X10D/src/IO/DoubleExtensions.cs +++ b/X10D/src/IO/DoubleExtensions.cs @@ -43,17 +43,7 @@ public static class DoubleExtensions /// if the conversion was successful; otherwise, . public static bool TryWriteBigEndianBytes(this double value, Span destination) { -#if NET5_0_OR_GREATER return BinaryPrimitives.TryWriteDoubleBigEndian(destination, value); -#else - if (BitConverter.IsLittleEndian) - { - long tmp = BinaryPrimitives.ReverseEndianness(BitConverter.DoubleToInt64Bits(value)); - return MemoryMarshal.TryWrite(destination, ref tmp); - } - - return MemoryMarshal.TryWrite(destination, ref value); -#endif } /// @@ -64,16 +54,6 @@ public static class DoubleExtensions /// if the conversion was successful; otherwise, . public static bool TryWriteLittleEndianBytes(this double value, Span destination) { -#if NET5_0_OR_GREATER return BinaryPrimitives.TryWriteDoubleLittleEndian(destination, value); -#else - if (BitConverter.IsLittleEndian) - { - return MemoryMarshal.TryWrite(destination, ref value); - } - - long tmp = BinaryPrimitives.ReverseEndianness(BitConverter.DoubleToInt64Bits(value)); - return MemoryMarshal.TryWrite(destination, ref tmp); -#endif } } diff --git a/X10D/src/IO/SingleExtensions.cs b/X10D/src/IO/SingleExtensions.cs index 630c9a0..efdb17b 100644 --- a/X10D/src/IO/SingleExtensions.cs +++ b/X10D/src/IO/SingleExtensions.cs @@ -45,17 +45,7 @@ public static class SingleExtensions /// if the conversion was successful; otherwise, . public static bool TryWriteBigEndianBytes(this float value, Span destination) { -#if NET5_0_OR_GREATER return BinaryPrimitives.TryWriteSingleBigEndian(destination, value); -#else - if (BitConverter.IsLittleEndian) - { - int tmp = BinaryPrimitives.ReverseEndianness(BitConverter.SingleToInt32Bits(value)); - return MemoryMarshal.TryWrite(destination, ref tmp); - } - - return MemoryMarshal.TryWrite(destination, ref value); -#endif } /// @@ -66,16 +56,6 @@ public static class SingleExtensions /// if the conversion was successful; otherwise, . public static bool TryWriteLittleEndianBytes(this float value, Span destination) { -#if NET5_0_OR_GREATER return BinaryPrimitives.TryWriteSingleLittleEndian(destination, value); -#else - if (BitConverter.IsLittleEndian) - { - return MemoryMarshal.TryWrite(destination, ref value); - } - - int tmp = BinaryPrimitives.ReverseEndianness(BitConverter.SingleToInt32Bits(value)); - return MemoryMarshal.TryWrite(destination, ref tmp); -#endif } } diff --git a/X10D/src/IO/StreamExtensions.Reading.cs b/X10D/src/IO/StreamExtensions.Reading.cs index ea856a1..3a32554 100644 --- a/X10D/src/IO/StreamExtensions.Reading.cs +++ b/X10D/src/IO/StreamExtensions.Reading.cs @@ -1,5 +1,4 @@ using System.Buffers.Binary; -using System.Runtime.InteropServices; namespace X10D.IO; @@ -40,11 +39,7 @@ public static partial class StreamExtensions buffer.Reverse(); } -#if NET5_0_OR_GREATER return new decimal(buffer); -#else - return new decimal(buffer.ToArray()); -#endif } /// @@ -82,11 +77,7 @@ public static partial class StreamExtensions buffer.Reverse(); } -#if NET5_0_OR_GREATER return new decimal(buffer); -#else - return new decimal(buffer.ToArray()); -#endif } /// @@ -110,16 +101,7 @@ public static partial class StreamExtensions Span buffer = stackalloc byte[8]; _ = stream.Read(buffer); -#if NET5_0_OR_GREATER return BinaryPrimitives.ReadDoubleBigEndian(buffer); -#else - if (BitConverter.IsLittleEndian) - { - buffer.Reverse(); - } - - return MemoryMarshal.Read(buffer); -#endif } /// @@ -144,16 +126,7 @@ public static partial class StreamExtensions Span buffer = stackalloc byte[8]; _ = stream.Read(buffer); -#if NET5_0_OR_GREATER return BinaryPrimitives.ReadDoubleLittleEndian(buffer); -#else - if (!BitConverter.IsLittleEndian) - { - buffer.Reverse(); - } - - return MemoryMarshal.Read(buffer); -#endif } /// @@ -323,16 +296,7 @@ public static partial class StreamExtensions Span buffer = stackalloc byte[4]; _ = stream.Read(buffer); -#if NET5_0_OR_GREATER return BinaryPrimitives.ReadSingleBigEndian(buffer); -#else - if (BitConverter.IsLittleEndian) - { - buffer.Reverse(); - } - - return MemoryMarshal.Read(buffer); -#endif } /// @@ -358,16 +322,7 @@ public static partial class StreamExtensions Span buffer = stackalloc byte[4]; _ = stream.Read(buffer); -#if NET5_0_OR_GREATER return BinaryPrimitives.ReadSingleLittleEndian(buffer); -#else - if (!BitConverter.IsLittleEndian) - { - buffer.Reverse(); - } - - return MemoryMarshal.Read(buffer); -#endif } /// diff --git a/X10D/src/Linq/EnumerableExtensions.cs b/X10D/src/Linq/EnumerableExtensions.cs index 74ac802..25db9a3 100644 --- a/X10D/src/Linq/EnumerableExtensions.cs +++ b/X10D/src/Linq/EnumerableExtensions.cs @@ -1,6 +1,4 @@ -#if NET5_0_OR_GREATER using System.Runtime.InteropServices; -#endif namespace X10D.Linq; @@ -389,11 +387,9 @@ public static class EnumerableExtensions span = array; break; -#if NET5_0_OR_GREATER case List list: span = CollectionsMarshal.AsSpan(list); break; -#endif default: span = default; diff --git a/X10D/src/Math/MathUtility.cs b/X10D/src/Math/MathUtility.cs index 5146d59..995f955 100644 --- a/X10D/src/Math/MathUtility.cs +++ b/X10D/src/Math/MathUtility.cs @@ -253,15 +253,7 @@ public static class MathUtility public static float Pulse(float value, float lowerBound, float upperBound) { bool result = lowerBound <= value && value <= upperBound; -#if NET6_0_OR_GREATER return Unsafe.As(ref result); -#else - unsafe - { - var pResult = (int*)&result; - return *pResult; - } -#endif } /// @@ -277,15 +269,7 @@ public static class MathUtility public static double Pulse(double value, double lowerBound, double upperBound) { bool result = lowerBound <= value && value <= upperBound; -#if NET6_0_OR_GREATER return Unsafe.As(ref result); -#else - unsafe - { - var pResult = (int*)&result; - return *pResult; - } -#endif } /// diff --git a/X10D/src/Numerics/UInt32Extensions.cs b/X10D/src/Numerics/UInt32Extensions.cs index 6d80db9..319130a 100644 --- a/X10D/src/Numerics/UInt32Extensions.cs +++ b/X10D/src/Numerics/UInt32Extensions.cs @@ -24,20 +24,7 @@ public static class UInt32Extensions [MethodImpl(CompilerResources.MethodImplOptions)] public static int PopCount(this uint value) { -#if NETCOREAPP3_1_OR_GREATER return BitOperations.PopCount(value); -#else - const uint c1 = 0x_55555555u; - const uint c2 = 0x_33333333u; - const uint c3 = 0x_0F0F0F0Fu; - const uint c4 = 0x_01010101u; - - value -= (value >> 1) & c1; - value = (value & c2) + ((value >> 2) & c2); - value = (((value + (value >> 4)) & c3) * c4) >> 24; - - return (int)value; -#endif } /// @@ -82,17 +69,6 @@ public static class UInt32Extensions [MethodImpl(CompilerResources.MethodImplOptions)] public static uint RoundUpToPowerOf2(this uint value) { -#if NET6_0_OR_GREATER return BitOperations.RoundUpToPowerOf2(value); -#else - // Based on https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 - --value; - value |= value >> 1; - value |= value >> 2; - value |= value >> 4; - value |= value >> 8; - value |= value >> 16; - return value + 1; -#endif } } diff --git a/X10D/src/Numerics/UInt64Extensions.cs b/X10D/src/Numerics/UInt64Extensions.cs index 1bce332..b69b20f 100644 --- a/X10D/src/Numerics/UInt64Extensions.cs +++ b/X10D/src/Numerics/UInt64Extensions.cs @@ -24,20 +24,7 @@ public static class UInt64Extensions [MethodImpl(CompilerResources.MethodImplOptions)] public static int PopCount(this ulong value) { -#if NETCOREAPP3_1_OR_GREATER return BitOperations.PopCount(value); -#else - const ulong c1 = 0x_55555555_55555555ul; - const ulong c2 = 0x_33333333_33333333ul; - const ulong c3 = 0x_0F0F0F0F_0F0F0F0Ful; - const ulong c4 = 0x_01010101_01010101ul; - - value -= (value >> 1) & c1; - value = (value & c2) + ((value >> 2) & c2); - value = (((value + (value >> 4)) & c3) * c4) >> 56; - - return (int)value; -#endif } /// @@ -82,18 +69,6 @@ public static class UInt64Extensions [MethodImpl(CompilerResources.MethodImplOptions)] public static ulong RoundUpToPowerOf2(this ulong value) { -#if NET6_0_OR_GREATER return BitOperations.RoundUpToPowerOf2(value); -#else - // Based on https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 - --value; - value |= value >> 1; - value |= value >> 2; - value |= value >> 4; - value |= value >> 8; - value |= value >> 16; - value |= value >> 32; - return value + 1; -#endif } } diff --git a/X10D/src/Text/EnumerableExtensions.cs b/X10D/src/Text/EnumerableExtensions.cs index 0fba459..691f8bf 100644 --- a/X10D/src/Text/EnumerableExtensions.cs +++ b/X10D/src/Text/EnumerableExtensions.cs @@ -55,12 +55,10 @@ public static class EnumerableExtensions throw new ArgumentNullException(nameof(pattern)); } -#if NET6_0_OR_GREATER if (source.TryGetNonEnumeratedCount(out int count) && count == 0) { yield break; } -#endif var options = RegexOptions.Compiled | (ignoreCase ? RegexOptions.IgnoreCase : RegexOptions.None); var regex = new Regex(pattern, options, Regex.InfiniteMatchTimeout); diff --git a/X10D/src/Text/Extensions.cs b/X10D/src/Text/Extensions.cs index 0e5a9e9..9d9a4b6 100644 --- a/X10D/src/Text/Extensions.cs +++ b/X10D/src/Text/Extensions.cs @@ -1,4 +1,3 @@ -#if NET5_0_OR_GREATER using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using System.Text.Json; @@ -25,4 +24,3 @@ public static class Extensions return JsonSerializer.Serialize(value, options); } } -#endif diff --git a/X10D/src/Text/RuneExtensions.cs b/X10D/src/Text/RuneExtensions.cs index e842fcb..adfaeec 100644 --- a/X10D/src/Text/RuneExtensions.cs +++ b/X10D/src/Text/RuneExtensions.cs @@ -1,4 +1,3 @@ -#if NETCOREAPP3_0_OR_GREATER using System.Diagnostics; using System.Diagnostics.Contracts; using System.Globalization; @@ -106,11 +105,10 @@ public static class RuneExtensions #if NET7_0_OR_GREATER throw new UnreachableException(message); #else - throw new InvalidOperationException(message); + throw new InvalidOperationException(message); #endif //NOSONAR // dotcover enable } } } -#endif diff --git a/X10D/src/Text/StringExtensions.cs b/X10D/src/Text/StringExtensions.cs index c4c68cc..6b33f9d 100644 --- a/X10D/src/Text/StringExtensions.cs +++ b/X10D/src/Text/StringExtensions.cs @@ -2,9 +2,7 @@ using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using System.Text; -#if NET5_0_OR_GREATER using System.Text.Json; -#endif using X10D.Collections; using X10D.CompilerServices; using X10D.Core; @@ -575,7 +573,6 @@ public static class StringExtensions return Enum.Parse(value, ignoreCase); } -#if NET5_0_OR_GREATER /// /// Returns an object from the specified JSON string. /// @@ -590,7 +587,6 @@ public static class StringExtensions { return JsonSerializer.Deserialize(value, options); } -#endif /// /// Gets a [] representing the value the with @@ -688,7 +684,6 @@ public static class StringExtensions for (var index = 0; index < value.Length; index++) { -#if NETCOREAPP3_0_OR_GREATER var rune = new Rune(value[index]); if (!Rune.IsLetter(rune)) @@ -700,19 +695,6 @@ public static class StringExtensions { return false; } -#else - char current = value[index]; - - if (!char.IsLetter(current)) - { - continue; - } - - if (!char.IsLower(current)) - { - return false; - } -#endif } return true; @@ -777,7 +759,6 @@ public static class StringExtensions for (int index = 0, endIndex = value.Length - 1; index < value.Length; index++, endIndex--) { -#if NETCOREAPP3_0_OR_GREATER Rune startRune = new Rune(value[index]); Rune endRune = new Rune(value[endIndex]); @@ -797,27 +778,6 @@ public static class StringExtensions { return false; } -#else - char startChar = value[index]; - char endChar = value[endIndex]; - - if (!char.IsLetter(startChar) && !char.IsNumber(startChar)) - { - endIndex++; - continue; - } - - if (!char.IsLetter(endChar) && !char.IsNumber(endChar)) - { - index--; - continue; - } - - if (char.ToUpperInvariant(startChar) != char.ToUpperInvariant(endChar)) - { - return false; - } -#endif } return true; @@ -841,7 +801,6 @@ public static class StringExtensions for (var index = 0; index < value.Length; index++) { -#if NETCOREAPP3_0_OR_GREATER var rune = new Rune(value[index]); if (!Rune.IsLetter(rune)) @@ -853,19 +812,6 @@ public static class StringExtensions { return false; } -#else - char current = value[index]; - - if (!char.IsLetter(current)) - { - continue; - } - - if (!char.IsUpper(current)) - { - return false; - } -#endif } return true; diff --git a/X10D/src/Time/DateOnlyExtensions.cs b/X10D/src/Time/DateOnlyExtensions.cs index d349068..f2b4e81 100644 --- a/X10D/src/Time/DateOnlyExtensions.cs +++ b/X10D/src/Time/DateOnlyExtensions.cs @@ -1,4 +1,3 @@ -#if NET6_0_OR_GREATER using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; @@ -198,4 +197,3 @@ public static class DateOnlyExtensions return value.ToDateTime(time).ToUnixTimeSeconds(); } } -#endif diff --git a/X10D/src/Time/HalfExtensions.cs b/X10D/src/Time/HalfExtensions.cs index 26cec4f..fa914b5 100644 --- a/X10D/src/Time/HalfExtensions.cs +++ b/X10D/src/Time/HalfExtensions.cs @@ -1,4 +1,3 @@ -#if NET5_0_OR_GREATER using System.Diagnostics.Contracts; using System.Runtime.CompilerServices; using X10D.CompilerServices; @@ -92,4 +91,3 @@ public static class HalfExtensions return TimeSpan.FromDays((float)value * 7); } } -#endif