Skip to content

Commit

Permalink
Add netstandard2.0 support (#9)
Browse files Browse the repository at this point in the history
* Add netstandard2.0 support.

* CI: only run tests for full framework on windows

* CI: Need to remove --no-build for full framework tests
  • Loading branch information
caesay authored Dec 21, 2023
1 parent 3bb82df commit f425c6a
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ jobs:
- name: Tests
run: dotnet test --no-build --configuration ${{ matrix.configuration }} src
continue-on-error: ${{ matrix.os == 'macos-latest' }}
- name: Tests (Full Framework)
run: dotnet test --configuration ${{ matrix.configuration }} src/VCDiff.Tests/VCDiff.Tests.NetFx.csproj
if: ${{ matrix.os == 'windows-latest' }}
- name: Upload code coverage
uses: codecov/[email protected]
with:
Expand Down
9 changes: 9 additions & 0 deletions src/VCDiff.Tests/VCDiff.Tests.NetFx.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="VCDiff.Tests.csproj" />

<PropertyGroup>
<TargetFrameworks>net462;net48</TargetFrameworks>
</PropertyGroup>

</Project>
1 change: 1 addition & 0 deletions src/VCDiff.Tests/VCDiff.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFrameworks>netcoreapp3.0;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
9 changes: 7 additions & 2 deletions src/VCDiff/Encoders/BlockHash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ private unsafe bool BlockContentsMatch(long block1, long tOffset, byte *sourcePt
}
}
}
#else
#elif NETSTANDARD2_1
int vectorSize = Vector<byte>.Count;
if (lengthToExamine >= vectorSize)
{
Expand Down Expand Up @@ -476,7 +476,7 @@ private unsafe long MatchingBytesToLeftSse2(long start, long tstart, byte* sourc
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private unsafe long MatchingBytesToLeft(long start, long tstart, byte* sourcePtr, byte* targetPtr, ByteBuffer target, long maxBytes)
{
#if NETCOREAPP3_1 || NET5_0
#if NETCOREAPP3_1 || NET5_0 || NET5_0_OR_GREATER
if (Avx2.IsSupported) return MatchingBytesToLeftAvx2(start, tstart, sourcePtr, targetPtr, maxBytes);
if (Sse2.IsSupported) return MatchingBytesToLeftSse2(start, tstart, sourcePtr, targetPtr, maxBytes);
#endif
Expand All @@ -490,6 +490,7 @@ private unsafe long MatchingBytesToLeft(long start, long tstart, byte* sourcePtr
var tBuf = target.AsSpan();
var sBuf = source.AsSpan();

#if NETCOREAPP3_1 || NET5_0 || NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
while (sindex >= vectorSize && tindex >= vectorSize && bytesFound <= maxBytes - vectorSize)
{
tindex -= vectorSize;
Expand All @@ -505,6 +506,7 @@ private unsafe long MatchingBytesToLeft(long start, long tstart, byte* sourcePtr

bytesFound += vectorSize;
}
#endif

while (bytesFound < maxBytes)
{
Expand Down Expand Up @@ -624,6 +626,8 @@ private unsafe long MatchingBytesToRight(long end, long tstart, byte* sourcePtr,
long trgLength = target.Length;
byte* tPtr = targetPtr;
byte* sPtr = sourcePtr;

#if NETCOREAPP3_1 || NET5_0 || NET5_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
int vectorSize = Vector<byte>.Count;
var tBuf = target.AsSpan();
var sBuf = source.AsSpan();
Expand All @@ -641,6 +645,7 @@ private unsafe long MatchingBytesToRight(long end, long tstart, byte* sourcePtr,
tindex += vectorSize;
sindex += vectorSize;
}
#endif

while (bytesFound < maxBytes)
{
Expand Down
5 changes: 2 additions & 3 deletions src/VCDiff/Encoders/InstructionMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void Add(byte first, byte inst, byte size, byte mode, byte opcode)
private int[] NewSizeOpcodeArray(int size)
{
int[] nn = new int[size];
Array.Fill(nn, CodeTable.kNoOpcode);
new Span<int>(nn).Fill(CodeTable.kNoOpcode);
return nn;
}

Expand Down Expand Up @@ -147,8 +147,7 @@ public OpcodeMap(int numInstAndModes, int maxSize)
this.maxSize = maxSize + 1;
this.numInstAndModes = numInstAndModes;
opcodes = new int[numInstAndModes * this.maxSize];

Array.Fill(opcodes, CodeTable.kNoOpcode);
new Span<int>(opcodes).Fill(CodeTable.kNoOpcode);
}

public void Add(byte inst, byte size, byte mode, byte opcode)
Expand Down
18 changes: 14 additions & 4 deletions src/VCDiff/Shared/ByteBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace VCDiff.Shared
public class ByteBuffer : IByteBuffer, IDisposable
{
private MemoryHandle? byteHandle;
private unsafe byte* bytePtr;
private unsafe byte* bytePtr;
private int length;
private int offset;

Expand All @@ -31,7 +31,7 @@ private ByteBuffer()
public unsafe ByteBuffer(byte[] bytes)
{
offset = 0;
var memory = bytes != null ? new Memory<byte>(bytes) : Memory<byte>.Empty;
var memory = bytes != null ? new Memory<byte>(bytes) : Memory<byte>.Empty;
this.byteHandle = memory.Pin();
CreateFromPointer((byte*)this.byteHandle.Value.Pointer, memory.Length);
}
Expand Down Expand Up @@ -65,9 +65,14 @@ private unsafe void CreateFromPointer(byte* pointer, int length)
this.bytePtr = pointer;
this.length = length;
}


#if NET5_0 || NET5_0_OR_GREATER || NETCOREAPP3_1 || NETSTANDARD2_1_OR_GREATER
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe Span<byte> AsSpan() => MemoryMarshal.CreateSpan(ref Unsafe.AsRef<byte>(bytePtr), length);
#else
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe Span<byte> AsSpan() => new Span<byte>(bytePtr, length);
#endif

/// <summary>
/// Dangerously gets the byte pointer.
Expand Down Expand Up @@ -104,7 +109,8 @@ public int Position
set => offset = value;
}

public int Length {
public int Length
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => length;
}
Expand All @@ -119,7 +125,11 @@ public int Length {
public unsafe Span<byte> PeekBytes(int len)
{
int sliceLen = offset + len > this.length ? this.length - offset : len;
#if NET5_0 || NET5_0_OR_GREATER || NETCOREAPP3_1 || NETSTANDARD2_1_OR_GREATER
return MemoryMarshal.CreateSpan(ref Unsafe.AsRef<byte>(bytePtr + offset), sliceLen);
#else
return new Span<byte>(bytePtr + offset, sliceLen);
#endif
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
2 changes: 1 addition & 1 deletion src/VCDiff/Shared/Intrinsics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static unsafe void FillArrayVectorized(long* first, int numValues, long v
}
#else
// Accelerate via loop unrolled solution.
MemoryMarshal.CreateSpan(ref Unsafe.AsRef<long>((void*) first), numValues).Fill(value);
new Span<long>((void*)first, numValues).Fill(value);
#endif
}

Expand Down
11 changes: 9 additions & 2 deletions src/VCDiff/VCDiff.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@

<PropertyGroup>
<RootNamespace>VCDiff</RootNamespace>
<TargetFrameworks>netstandard2.1;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<Nullable>enable</Nullable>
<Version>4.0.1</Version>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);CS1591</NoWarn>
</PropertyGroup>

<PropertyGroup>
Expand All @@ -20,8 +21,14 @@
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="2.2.0" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="5.0.0" Condition="'$(TargetFramework)' == 'netstandard2.1'" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="5.0.0" Condition="$(TargetFramework.StartsWith('netstandard'))" />
<PackageReference Include="PolyShim" Version="1.8.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>

0 comments on commit f425c6a

Please sign in to comment.