Skip to content

Commit

Permalink
Reverted MacOS-hack / skip for AVX2 (#53)
Browse files Browse the repository at this point in the history
* Reverted MacOS-hack / skip for AVX2

* Print HW-infos in tests

Cf. #53 (comment)

* Execute explicit AVX2 tests only when it is supported

As of now MacOS doesn't support AVX2, so these tests would fail.
  • Loading branch information
gfoidl authored Dec 26, 2018
1 parent ca8f05b commit b28b923
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private OperationStatus DecodeImpl<T>(
ref byte destBytes = ref MemoryMarshal.GetReference(data);

// s - 45 >= 0 used 'lea' as opposed to s >= 45
if (Avx2.IsSupported && srcLength - 45 >= 0 && !s_isMac)
if (Avx2.IsSupported && srcLength - 45 >= 0)
{
Avx2Decode(ref src, ref destBytes, srcLength, ref sourceIndex, ref destIndex);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private OperationStatus EncodeImpl<T>(
if (srcLength < 16)
goto Scalar;

if (Avx2.IsSupported && srcLength >= 32 && !s_isMac)
if (Avx2.IsSupported && srcLength - 32 >= 0)
{
Avx2Encode(ref srcBytes, ref dest, srcLength, ref sourceIndex, ref destIndex);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;

namespace gfoidl.Base64.Internal
Expand All @@ -14,12 +13,8 @@ partial class Base64EncoderImpl
protected static readonly Vector256<sbyte> s_avx_decodeShuffleVec;
protected static readonly Vector256<int> s_avx_decodePermuteVec;
//---------------------------------------------------------------------
protected static readonly bool s_isMac = false;
//---------------------------------------------------------------------
static Base64EncoderImpl()
{
s_isMac = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);

if (Ssse3.IsSupported)
{
s_sse_encodeShuffleVec = Vector128.Create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ private OperationStatus DecodeImpl<T>(
ref byte destBytes = ref MemoryMarshal.GetReference(data);

// s - 45 >= 0 used 'lea' as opposed to s >= 45
if (Avx2.IsSupported && srcLength - 45 >= 0 && !s_isMac)
if (Avx2.IsSupported && srcLength - 45 >= 0)
{
Avx2Decode(ref src, ref destBytes, srcLength, ref sourceIndex, ref destIndex);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private OperationStatus EncodeImpl<T>(
if (srcLength < 16)
goto Scalar;

if (Avx2.IsSupported && srcLength >= 32 && !s_isMac)
if (Avx2.IsSupported && srcLength - 32 >= 0)
{
Avx2Encode(ref srcBytes, ref dest, srcLength, ref sourceIndex, ref destIndex);

Expand Down
4 changes: 0 additions & 4 deletions tests/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
<NoWarn>$(NoWarn);GF0001</NoWarn>
</PropertyGroup>

<PropertyGroup Condition="'$(OS)' == 'Unix' AND $([System.IO.File]::Exists('/usr/lib/libc.dylib'))">
<DefineConstants>OSX</DefineConstants>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="NUnit" Version="3.11.0" />
Expand Down
22 changes: 22 additions & 0 deletions tests/gfoidl.Base64.Tests/HWInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Runtime.Intrinsics.X86;
using NUnit.Framework;

namespace gfoidl.Base64.Tests
{
[SetUpFixture]
public class HWInfo
{
[OneTimeSetUp]
public void OneTimeSetup()
{
TestContext.Progress.WriteLine(new string('-', 20));
TestContext.Progress.WriteLine("SIMD-Info");
TestContext.Progress.WriteLine($"Sse : {Sse.IsSupported}");
TestContext.Progress.WriteLine($"Sse2 : {Sse2.IsSupported}");
TestContext.Progress.WriteLine($"Ssse3: {Ssse3.IsSupported}");
TestContext.Progress.WriteLine($"Avx : {Avx.IsSupported}");
TestContext.Progress.WriteLine($"Avx2 : {Avx2.IsSupported}");
TestContext.Progress.WriteLine(new string('-', 20));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
using gfoidl.Base64.Internal;
using NUnit.Framework;

#if NETCOREAPP
using System.Runtime.Intrinsics.X86;
#endif

namespace gfoidl.Base64.Tests.Internal.Base64EncoderTests
{
[TestFixture(typeof(byte))]
Expand Down Expand Up @@ -101,10 +105,12 @@ public void Invalid_data_various_length___status_InvalidData()
}
}
//---------------------------------------------------------------------
#if NETCOREAPP3_0 && DEBUG && !OSX
#if NETCOREAPP3_0 && DEBUG
[Test]
public void Large_data___avx2_event_fired()
{
Assume.That(Avx2.IsSupported);

var sut = new Base64Encoder();
var data = new byte[50];
var rnd = new Random(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
using gfoidl.Base64.Internal;
using NUnit.Framework;

#if NETCOREAPP
using System.Runtime.Intrinsics.X86;
#endif

namespace gfoidl.Base64.Tests.Internal.Base64UrlEncoderTests
{
[TestFixture(typeof(byte))]
Expand Down Expand Up @@ -105,10 +109,12 @@ public void Invalid_data_various_length___status_InvalidData()
}
}
//---------------------------------------------------------------------
#if NETCOREAPP3_0 && DEBUG && !OSX
#if NETCOREAPP3_0 && DEBUG
[Test]
public void Large_data___avx2_event_fired()
{
Assume.That(Avx2.IsSupported);

var sut = new Base64UrlEncoder();
var data = new byte[50];
var rnd = new Random(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,25 @@ namespace gfoidl.Base64.Tests.Internal.Vector256HelperTests
{
public class LessThan
{
private static readonly Vector256<sbyte> s_zero = Vector256<sbyte>.Zero;
private static readonly Vector256<sbyte> s_one = Vector256.Create((sbyte)1);
private static readonly Vector256<sbyte> s_allOnes = Vector256.Create((sbyte)-1);
private static readonly Vector256<sbyte> s_zero;
private static readonly Vector256<sbyte> s_one;
private static readonly Vector256<sbyte> s_allOnes;
//---------------------------------------------------------------------
static LessThan()
{
if (Avx2.IsSupported)
{
s_zero = Vector256<sbyte>.Zero;
s_one = Vector256.Create((sbyte)1);
s_allOnes = Vector256.Create((sbyte)-1);
}
}
//---------------------------------------------------------------------
[Test]
public void Zero_and_zero___false()
{
Assume.That(Avx2.IsSupported);

Vector256<sbyte> left = s_zero;
Vector256<sbyte> right = s_zero;

Expand All @@ -28,6 +40,8 @@ public void Zero_and_zero___false()
[Test]
public void One_and_zero___false()
{
Assume.That(Avx2.IsSupported);

Vector256<sbyte> left = s_one;
Vector256<sbyte> right = s_zero;

Expand All @@ -41,6 +55,8 @@ public void One_and_zero___false()
[Test]
public void Zero_and_one___true()
{
Assume.That(Avx2.IsSupported);

Vector256<sbyte> left = s_zero;
Vector256<sbyte> right = s_one;

Expand All @@ -54,6 +70,8 @@ public void Zero_and_one___true()
[Test]
public void Only_one_zero_and_one___true()
{
Assume.That(Avx2.IsSupported);

Vector256<sbyte> left = s_one;
Vector256<sbyte> right = s_one;

Expand All @@ -73,6 +91,8 @@ public void Only_one_zero_and_one___true()
[Test]
public void One_4_in_5s_and_5s___true()
{
Assume.That(Avx2.IsSupported);

Vector256<sbyte> left = Vector256.Create((sbyte)5);
Vector256<sbyte> right = Vector256.Create((sbyte)5);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
using gfoidl.Base64.Internal;
using NUnit.Framework;

Expand All @@ -11,6 +12,8 @@ public class ReadVector256
[Test]
public void Byte___correct_combined_sbyte_vector()
{
Assume.That(Avx2.IsSupported);

byte[] chars = new byte[32];
sbyte[] expected = new sbyte[32];

Expand All @@ -31,6 +34,8 @@ public void Byte___correct_combined_sbyte_vector()
[Test]
public void Char___correct_combined_sbyte_vector()
{
Assume.That(Avx2.IsSupported);

char[] chars = new char[32];
sbyte[] expected = new sbyte[32];

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
using gfoidl.Base64.Internal;
using NUnit.Framework;

Expand All @@ -11,6 +12,8 @@ public class WriteVector256
[Test]
public void Byte___correct_data_written()
{
Assume.That(Avx2.IsSupported);

sbyte[] data = new sbyte[32];
byte[] expected = new byte[32];

Expand All @@ -31,6 +34,8 @@ public void Byte___correct_data_written()
[Test]
public void Char___correct_data_written()
{
Assume.That(Avx2.IsSupported);

sbyte[] data = new sbyte[32];
char[] expected = new char[32];

Expand Down
18 changes: 8 additions & 10 deletions tests/gfoidl.Base64.Tests/gfoidl.Base64.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,28 @@
<TargetFrameworks>$(StandardTestTfms)</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\source\gfoidl.Base64\gfoidl.Base64.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Remove="Internal\Vector*HelperTests\*.cs" />
<Compile Remove="HWInfo.cs" />
</ItemGroup>

<ItemGroup>
<None Include="Internal\Vector*HelperTests\*.cs" />
<None Include="HWInfo.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp3.0'">
<Compile Include="Internal\Vector*HelperTests\*.cs" />
<Compile Include="HWInfo.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp2.1'">
<Compile Include="Internal\Vector128HelperTests\*.cs" />
</ItemGroup>

<!-- https://github.com/gfoidl/Base64/issues/2 -->
<!-- https://github.com/Microsoft/msbuild/issues/2468#issuecomment-324719212 -->
<ItemGroup Condition="'$(TargetFramework)' != 'netcoreapp3.0' OR ( '$(OS)' == 'Unix' AND $([System.IO.File]::Exists('/usr/lib/libc.dylib')) )">
<Compile Remove="Internal\Vector256HelperTests\*.cs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\source\gfoidl.Base64\gfoidl.Base64.csproj" />
<Compile Include="HWInfo.cs" />
</ItemGroup>

</Project>

0 comments on commit b28b923

Please sign in to comment.