Skip to content

Commit

Permalink
refactor: remove target-conditional branches (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverbooth committed Sep 12, 2023
1 parent 89f8ceb commit 8f9fc6e
Show file tree
Hide file tree
Showing 36 changed files with 5 additions and 412 deletions.
2 changes: 0 additions & 2 deletions X10D.Tests/src/Collections/ByteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public void Unpack_ShouldUnpackToSpanCorrectly()
});
}

#if NET5_0_OR_GREATER
[Test]
public void UnpackInternal_Fallback_ShouldUnpackToSpanCorrectly()
{
Expand Down Expand Up @@ -92,7 +91,6 @@ public void UnpackInternal_Ssse3_ShouldUnpackToSpanCorrectly()
Assert.That(bits[7], Is.True);
});
}
#endif

[Test]
public void Unpack_ShouldRepackEqually()
Expand Down
4 changes: 0 additions & 4 deletions X10D.Tests/src/Collections/Int16Tests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#if NET5_0_OR_GREATER
using System.Runtime.Intrinsics.X86;
#endif
using NUnit.Framework;
using X10D.Collections;

Expand Down Expand Up @@ -84,7 +82,6 @@ public void Unpack_ShouldUnpackToSpanCorrectly_GivenFallbackImplementation()
});
}

#if NET5_0_OR_GREATER
[Test]
public void UnpackInternal_Ssse3_ShouldUnpackToSpanCorrectly()
{
Expand Down Expand Up @@ -115,7 +112,6 @@ public void UnpackInternal_Ssse3_ShouldUnpackToSpanCorrectly()
}
});
}
#endif

[Test]
public void Unpack_ShouldRepackEqually()
Expand Down
4 changes: 0 additions & 4 deletions X10D.Tests/src/Collections/Int32Tests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#if NET5_0_OR_GREATER
using System.Runtime.Intrinsics.X86;
#endif
using NUnit.Framework;
using X10D.Collections;

Expand Down Expand Up @@ -85,7 +83,6 @@ public void UnpackInternal_Fallback_ShouldUnpackToSpanCorrectly()
});
}

#if NET5_0_OR_GREATER
[Test]
public void UnpackInternal_Ssse3_ShouldUnpackToSpanCorrectly()
{
Expand Down Expand Up @@ -145,7 +142,6 @@ public void UnpackInternal_Avx2_ShouldUnpackToSpanCorrectly()
}
});
}
#endif

[Test]
public void Unpack_ShouldRepackEqually()
Expand Down
2 changes: 0 additions & 2 deletions X10D.Tests/src/Core/IntrinsicTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#if NET6_0_OR_GREATER
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
using NUnit.Framework;
Expand Down Expand Up @@ -223,4 +222,3 @@ public void ReverseElementsInternal_Sse2_ShouldReturnExpectedVector128Result_Giv
Assert.That(result, Is.EqualTo(expectedResult));
}
}
#endif
8 changes: 0 additions & 8 deletions X10D.Tests/src/Core/SpanTest.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -209,7 +207,6 @@ public void PackByteInternal_Fallback_ShouldReturnCorrectByte_GivenReadOnlySpan_
Assert.That(actual, Is.EqualTo(expected));
}

#if NET5_0_OR_GREATER
[Test]
public void PackByteInternal_Sse2_ShouldReturnCorrectByte_GivenReadOnlySpan_Using()
{
Expand Down Expand Up @@ -241,7 +238,6 @@ public void PackByteInternal_AdvSimd_ShouldReturnCorrectByte_GivenReadOnlySpan_U

Assert.That(actual, Is.EqualTo(expected));
}
#endif

[Test]
public void PackInt16_ShouldReturnSameAsPackByte_WhenSpanHasLength8()
Expand All @@ -268,7 +264,6 @@ public void PackInt16Internal_Fallback_ShouldReturnCorrectInt16_GivenReadOnlySpa
Assert.That(actual, Is.EqualTo(expected));
}

#if NET5_0_OR_GREATER
[Test]
public void PackInt16Internal_Sse2_ShouldReturnCorrectInt16_GivenReadOnlySpan_Using()
{
Expand All @@ -287,7 +282,6 @@ public void PackInt16Internal_Sse2_ShouldReturnCorrectInt16_GivenReadOnlySpan_Us

Assert.That(actual, Is.EqualTo(expected));
}
#endif

[Test]
public void PackInt32Internal_Fallback_ShouldReturnCorrectInt32_GivenReadOnlySpan()
Expand All @@ -304,7 +298,6 @@ public void PackInt32Internal_Fallback_ShouldReturnCorrectInt32_GivenReadOnlySpa
Assert.That(actual, Is.EqualTo(expected));
}

#if NET5_0_OR_GREATER
[Test]
public void PackInt32Internal_Sse2_ShouldReturnCorrectInt32_GivenReadOnlySpan()
{
Expand Down Expand Up @@ -364,7 +357,6 @@ public void PackInt32Internal_AdvSimd_ShouldReturnCorrectInt32_GivenReadOnlySpan

Assert.That(actual, Is.EqualTo(expected));
}
#endif

[Test]
public void PackInt32_ShouldReturnSameAsPackByte_WhenSpanHasLength8_UsingReadOnlySpan()
Expand Down
2 changes: 0 additions & 2 deletions X10D.Tests/src/Drawing/ColorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,7 @@ public void GetClosestConsoleColor_ShouldReturnClosestColor_GivenValidColor()
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));
Expand Down
2 changes: 0 additions & 2 deletions X10D.Tests/src/Text/CoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace X10D.Tests.Text;
[TestFixture]
internal class CoreTests
{
#if NET5_0_OR_GREATER
[Test]
public void ToJsonShouldNotBeEmpty()
{
Expand All @@ -24,5 +23,4 @@ public void ToJsonShouldDeserializeEquivalent()
CollectionAssert.AreEqual(source, target);
CollectionAssert.AreEquivalent(source, target);
}
#endif
}
2 changes: 0 additions & 2 deletions X10D.Tests/src/Text/RuneTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#if NET5_0_OR_GREATER
using System.Text;
using NUnit.Framework;
using X10D.Text;
Expand Down Expand Up @@ -90,4 +89,3 @@ public void RepeatCodepointBeyondU10000ShouldReturnString()
Assert.That(repeated, Is.EqualTo("𐀀𐀀𐀀𐀀𐀀𐀀"));
}
}
#endif
6 changes: 0 additions & 6 deletions X10D.Tests/src/Text/StringTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System.Text;
#if NET5_0_OR_GREATER
using System.Text.Json.Serialization;
#endif
using NUnit.Framework;
using X10D.Text;

Expand Down Expand Up @@ -418,7 +416,6 @@ public void EnumParse_ShouldThrow_GivenEmptyOrWhiteSpaceString()
Assert.Throws<ArgumentException>(() => _ = " ".EnumParse<DayOfWeek>());
}

#if NET5_0_OR_GREATER
[Test]
public void FromJson_ShouldDeserializeCorrectly_GivenJsonString()
{
Expand All @@ -434,7 +431,6 @@ public void FromJson_ShouldDeserializeCorrectly_GivenJsonString()
Assert.That(target.Values[2], Is.EqualTo(3));
});
}
#endif

[Test]
public void GetBytes_ShouldReturnUtf8Bytes_GivenHelloWorld()
Expand Down Expand Up @@ -1006,11 +1002,9 @@ public void WithWhiteSpaceAlternative_ShouldBeCorrect()
});
}

#if NET5_0_OR_GREATER
private struct SampleStructure
{
[JsonPropertyName("values")]
public int[] Values { get; set; }
}
#endif
}
2 changes: 0 additions & 2 deletions X10D.Tests/src/Time/DateOnlyTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#if NET6_0_OR_GREATER
using NUnit.Framework;
using X10D.Time;

Expand Down Expand Up @@ -230,4 +229,3 @@ public void ToUnixTimeSeconds_ShouldBe946684800_Given1Jan2000()
Assert.That(date.ToUnixTimeSeconds(time), Is.EqualTo(946684800));
}
}
#endif
2 changes: 0 additions & 2 deletions X10D.Tests/src/Time/DoubleTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#if NET5_0_OR_GREATER
using NUnit.Framework;
using X10D.Time;

Expand Down Expand Up @@ -52,4 +51,3 @@ public void TicksMillisecondsSecondsMinutesDaysHoursWeeks_ShouldBeNegative_Given
Assert.That((_negativeOne).Weeks() < TimeSpan.Zero);
}
}
#endif
9 changes: 1 addition & 8 deletions X10D/src/Collections/ByteExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -46,13 +43,11 @@ public static void Unpack(this byte value, Span<bool> destination)
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);
}
Expand All @@ -66,7 +61,6 @@ internal static void UnpackInternal_Fallback(this byte value, Span<bool> destina
}
}

#if NETCOREAPP3_0_OR_GREATER
[MethodImpl(CompilerResources.MethodImplOptions)]
internal unsafe static void UnpackInternal_Ssse3(this byte value, Span<bool> destination)
{
Expand All @@ -87,5 +81,4 @@ internal unsafe static void UnpackInternal_Ssse3(this byte value, Span<bool> des
Sse2.StoreScalar((long*)pDestination, correctness.AsInt64());
}
}
#endif
}
45 changes: 0 additions & 45 deletions X10D/src/Collections/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System.Diagnostics.Contracts;
#if NET6_0_OR_GREATER
using System.Runtime.InteropServices;
#endif
using System.Web;

namespace X10D.Collections;
Expand Down Expand Up @@ -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
}

/// <summary>
Expand Down Expand Up @@ -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
}

/// <summary>
Expand Down Expand Up @@ -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
}

/// <summary>
Expand Down
9 changes: 1 addition & 8 deletions X10D/src/Collections/Int16Extensions.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -46,13 +43,11 @@ public static void Unpack(this short value, Span<bool> destination)
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);
}
Expand All @@ -66,7 +61,6 @@ internal static void UnpackInternal_Fallback(this short value, Span<bool> destin
}
}

#if NETCOREAPP3_0_OR_GREATER
[MethodImpl(CompilerResources.MethodImplOptions)]
internal unsafe static void UnpackInternal_Ssse3(this short value, Span<bool> destination)
{
Expand All @@ -89,5 +83,4 @@ internal unsafe static void UnpackInternal_Ssse3(this short value, Span<bool> de
Sse2.Store((byte*)pDestination, correctness);
}
}
#endif
}
Loading

0 comments on commit 8f9fc6e

Please sign in to comment.