diff --git a/DTLib.XXHash.PerformanceTest/xxHashBenchmark.cs b/DTLib.XXHash.PerformanceTest/xxHashBenchmark.cs index befa369..1268d32 100644 --- a/DTLib.XXHash.PerformanceTest/xxHashBenchmark.cs +++ b/DTLib.XXHash.PerformanceTest/xxHashBenchmark.cs @@ -36,7 +36,7 @@ { return xxHash32.ComputeHash(data, data.Length); } - +#if NET6_0_OR_GREATER [Benchmark] public uint Hash32_Span() { @@ -50,7 +50,7 @@ ReadOnlySpan span = new ReadOnlySpan(data); return xxHash32.ComputeHash(span, span.Length); } - +#endif [Benchmark] public uint Hash32_Stream() { @@ -70,7 +70,7 @@ { return xxHash64.ComputeHash(data, data.Length); } - +#if NET6_0_OR_GREATER [Benchmark] public ulong Hash64_Span() { @@ -84,6 +84,7 @@ ReadOnlySpan span = new ReadOnlySpan(data); return xxHash64.ComputeHash(span, span.Length); } +#endif [Benchmark] public ulong Hash64_Stream() diff --git a/DTLib.XXHash.Tests/UtilsTest.cs b/DTLib.XXHash.Tests/UtilsTest.cs index d2de679..c624c07 100644 --- a/DTLib.XXHash.Tests/UtilsTest.cs +++ b/DTLib.XXHash.Tests/UtilsTest.cs @@ -35,7 +35,7 @@ } } } - +#if NET6_0_OR_GREATER [Fact] public void Should_convert_to_bytes() { @@ -80,5 +80,6 @@ // Assert Assert.Equal(guid1, guid2); } +#endif } } \ No newline at end of file diff --git a/DTLib.XXHash.Tests/xxHash128Test.cs b/DTLib.XXHash.Tests/xxHash128Test.cs index 14701e7..ce315da 100644 --- a/DTLib.XXHash.Tests/xxHash128Test.cs +++ b/DTLib.XXHash.Tests/xxHash128Test.cs @@ -1,3 +1,4 @@ +#if NET6_0_OR_GREATER using System; using System.Text; using Xunit; @@ -111,4 +112,5 @@ namespace DTLib.XXHash.Tests } } -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/DTLib.XXHash.Tests/xxHash32Test.cs b/DTLib.XXHash.Tests/xxHash32Test.cs index d3a6ce4..c24a90b 100644 --- a/DTLib.XXHash.Tests/xxHash32Test.cs +++ b/DTLib.XXHash.Tests/xxHash32Test.cs @@ -10,23 +10,29 @@ namespace DTLib.XXHash.Tests public class xxHash32Test { + [Fact] public void Compute_hash32_for_the_length_1() { // Arrange byte[] data = {0xde}; +#if NET6_0_OR_GREATER Span span = new Span(data); ReadOnlySpan rspan = new ReadOnlySpan(data); - +#endif // Act uint hash1 = xxHash32.ComputeHash(data, data.Length); +#if NET6_0_OR_GREATER uint hash2 = xxHash32.ComputeHash(span, span.Length); uint hash3 = xxHash32.ComputeHash(rspan, rspan.Length); +#endif // Assert Assert.Equal(hash1, (uint) 0x2330eac0); +#if NET6_0_OR_GREATER Assert.Equal(hash2, (uint) 0x2330eac0); Assert.Equal(hash3, (uint) 0x2330eac0); +#endif } [Fact] @@ -34,18 +40,24 @@ namespace DTLib.XXHash.Tests { // Arrange byte[] data = {0xde, 0x55, 0x47, 0x7f, 0x14}; +#if NET6_0_OR_GREATER Span span = new Span(data); ReadOnlySpan rspan = new ReadOnlySpan(data); - +#endif + // Act uint hash1 = xxHash32.ComputeHash(data, data.Length); +#if NET6_0_OR_GREATER uint hash2 = xxHash32.ComputeHash(span, span.Length); uint hash3 = xxHash32.ComputeHash(rspan, rspan.Length); +#endif // Assert Assert.Equal(hash1, (uint) 0x112348ba); +#if NET6_0_OR_GREATER Assert.Equal(hash2, (uint) 0x112348ba); Assert.Equal(hash3, (uint) 0x112348ba); +#endif } [Fact] @@ -57,18 +69,24 @@ namespace DTLib.XXHash.Tests 0xde, 0x55, 0x47, 0x7f, 0x14, 0x8f, 0xf1, 0x48, 0x22, 0x3a, 0x40, 0x96, 0x56, 0xc5, 0xdc, 0xbb }; +#if NET6_0_OR_GREATER Span span = new Span(data); ReadOnlySpan rspan = new ReadOnlySpan(data); +#endif // Act uint hash1 = xxHash32.ComputeHash(data, data.Length); +#if NET6_0_OR_GREATER uint hash2 = xxHash32.ComputeHash(span, span.Length); uint hash3 = xxHash32.ComputeHash(rspan, rspan.Length); +#endif // Assert Assert.Equal(hash1, (uint) 0xcdf89609); +#if NET6_0_OR_GREATER Assert.Equal(hash2, (uint) 0xcdf89609); Assert.Equal(hash3, (uint) 0xcdf89609); +#endif } [Fact] @@ -81,18 +99,24 @@ namespace DTLib.XXHash.Tests 0x22, 0x3a, 0x40, 0x96, 0x56, 0xc5, 0xdc, 0xbb, 0x0e }; +#if NET6_0_OR_GREATER Span span = new Span(data); ReadOnlySpan rspan = new ReadOnlySpan(data); +#endif // Act uint hash1 = xxHash32.ComputeHash(data, data.Length); +#if NET6_0_OR_GREATER uint hash2 = xxHash32.ComputeHash(span, span.Length); uint hash3 = xxHash32.ComputeHash(rspan, rspan.Length); +#endif // Assert Assert.Equal(hash1, (uint) 0xbca8f924); +#if NET6_0_OR_GREATER Assert.Equal(hash2, (uint) 0xbca8f924); Assert.Equal(hash3, (uint) 0xbca8f924); +#endif } [Fact] @@ -105,18 +129,24 @@ namespace DTLib.XXHash.Tests 0x22, 0x3a, 0x40, 0x96, 0x56, 0xc5, 0xdc, 0xbb, 0x0e, 0x59, 0x4d, 0x42, 0xc5 }; +#if NET6_0_OR_GREATER Span span = new Span(data); ReadOnlySpan rspan = new ReadOnlySpan(data); +#endif // Act uint hash1 = xxHash32.ComputeHash(data, data.Length); +#if NET6_0_OR_GREATER uint hash2 = xxHash32.ComputeHash(span, span.Length); uint hash3 = xxHash32.ComputeHash(rspan, rspan.Length); +#endif // Assert Assert.Equal(hash1, (uint) 0xf4518e14); +#if NET6_0_OR_GREATER Assert.Equal(hash2, (uint) 0xf4518e14); Assert.Equal(hash3, (uint) 0xf4518e14); +#endif } [Fact] @@ -130,18 +160,24 @@ namespace DTLib.XXHash.Tests 0x0e, 0x59, 0x4d, 0x42, 0xc5, 0x07, 0x21, 0x08, 0x1c, 0x2c, 0xc9, 0x38, 0x7d, 0x43, 0x83, 0x11, }; +#if NET6_0_OR_GREATER Span span = new Span(data); ReadOnlySpan rspan = new ReadOnlySpan(data); +#endif // Act uint hash1 = xxHash32.ComputeHash(data, data.Length); +#if NET6_0_OR_GREATER uint hash2 = xxHash32.ComputeHash(span, span.Length); uint hash3 = xxHash32.ComputeHash(rspan, rspan.Length); +#endif // Assert Assert.Equal(hash1, (uint) 0xf8497daa); +#if NET6_0_OR_GREATER Assert.Equal(hash2, (uint) 0xf8497daa); Assert.Equal(hash3, (uint) 0xf8497daa); +#endif } [Fact] diff --git a/DTLib.XXHash.Tests/xxHash3Test.cs b/DTLib.XXHash.Tests/xxHash3Test.cs index 451408a..b0b9154 100644 --- a/DTLib.XXHash.Tests/xxHash3Test.cs +++ b/DTLib.XXHash.Tests/xxHash3Test.cs @@ -1,4 +1,5 @@ -using System; +#if NET6_0_OR_GREATER +using System; using System.Text; using Xunit; @@ -68,3 +69,4 @@ namespace DTLib.XXHash.Tests } } } +#endif \ No newline at end of file diff --git a/DTLib.XXHash.Tests/xxHash64Test.cs b/DTLib.XXHash.Tests/xxHash64Test.cs index 9a4304e..8e03f15 100644 --- a/DTLib.XXHash.Tests/xxHash64Test.cs +++ b/DTLib.XXHash.Tests/xxHash64Test.cs @@ -15,18 +15,24 @@ namespace DTLib.XXHash.Tests { // Arrange byte[] data = {0x60}; +#if NET6_0_OR_GREATER Span span = new Span(data); ReadOnlySpan rspan = new ReadOnlySpan(data); +#endif // Act ulong hash1 = xxHash64.ComputeHash(data, data.Length); +#if NET6_0_OR_GREATER ulong hash2 = xxHash64.ComputeHash(span, span.Length); ulong hash3 = xxHash64.ComputeHash(rspan, rspan.Length); +#endif // Assert Assert.Equal(hash1, (ulong) 0xb3e7ca6ca5ba3445); +#if NET6_0_OR_GREATER Assert.Equal(hash2, (ulong) 0xb3e7ca6ca5ba3445); Assert.Equal(hash3, (ulong) 0xb3e7ca6ca5ba3445); +#endif } [Fact] @@ -34,18 +40,23 @@ namespace DTLib.XXHash.Tests { // Arrange byte[] data = {0x60, 0x82, 0x40, 0x77, 0x8a}; +#if NET6_0_OR_GREATER Span span = new Span(data); ReadOnlySpan rspan = new ReadOnlySpan(data); - +#endif // Act ulong hash1 = xxHash64.ComputeHash(data, data.Length); +#if NET6_0_OR_GREATER ulong hash2 = xxHash64.ComputeHash(span, span.Length); ulong hash3 = xxHash64.ComputeHash(rspan, rspan.Length); +#endif // Assert Assert.Equal(hash1, (ulong) 0x917b11ed024938fc); +#if NET6_0_OR_GREATER Assert.Equal(hash2, (ulong) 0x917b11ed024938fc); Assert.Equal(hash3, (ulong) 0x917b11ed024938fc); +#endif } [Fact] @@ -57,18 +68,24 @@ namespace DTLib.XXHash.Tests 0x60, 0x82, 0x40, 0x77, 0x8a, 0x0e, 0xe4, 0xd5, 0x85, 0x1f, 0xa6, 0x86, 0x34, }; +#if NET6_0_OR_GREATER Span span = new Span(data); ReadOnlySpan rspan = new ReadOnlySpan(data); +#endif // Act ulong hash1 = xxHash64.ComputeHash(data, data.Length); +#if NET6_0_OR_GREATER ulong hash2 = xxHash64.ComputeHash(span, span.Length); ulong hash3 = xxHash64.ComputeHash(rspan, rspan.Length); +#endif // Assert Assert.Equal(hash1, (ulong) 0x9d1cb0d181d58bee); +#if NET6_0_OR_GREATER Assert.Equal(hash2, (ulong) 0x9d1cb0d181d58bee); Assert.Equal(hash3, (ulong) 0x9d1cb0d181d58bee); +#endif } [Fact] @@ -82,18 +99,24 @@ namespace DTLib.XXHash.Tests 0x30, 0x5d, 0x84, 0x54, 0x15, 0xf9, 0xbd, 0x03, 0x4b, 0x0f, 0x90, 0x4e, 0xf5, 0x57, 0x21, 0x21, }; +#if NET6_0_OR_GREATER Span span = new Span(data); ReadOnlySpan rspan = new ReadOnlySpan(data); +#endif // Act ulong hash1 = xxHash64.ComputeHash(data, data.Length); +#if NET6_0_OR_GREATER ulong hash2 = xxHash64.ComputeHash(span, span.Length); ulong hash3 = xxHash64.ComputeHash(rspan, rspan.Length); +#endif // Assert Assert.Equal(hash1, (ulong) 0x9233096b7804e12c); +#if NET6_0_OR_GREATER Assert.Equal(hash2, (ulong) 0x9233096b7804e12c); Assert.Equal(hash3, (ulong) 0x9233096b7804e12c); +#endif } [Fact] @@ -111,18 +134,24 @@ namespace DTLib.XXHash.Tests 0x27, 0x6d, 0xb3, 0x5c, 0xc7, 0xc0, 0xd0, 0xa0, 0x7e, 0x28, 0xce, 0x46, 0x85, 0xb7, 0x2b, 0x16, }; +#if NET6_0_OR_GREATER Span span = new Span(data); ReadOnlySpan rspan = new ReadOnlySpan(data); +#endif // Act ulong hash1 = xxHash64.ComputeHash(data, data.Length); +#if NET6_0_OR_GREATER ulong hash2 = xxHash64.ComputeHash(span, span.Length); ulong hash3 = xxHash64.ComputeHash(rspan, rspan.Length); +#endif // Assert Assert.Equal(hash1, (ulong) 0x4c0a65b1ef9ea060); +#if NET6_0_OR_GREATER Assert.Equal(hash2, (ulong) 0x4c0a65b1ef9ea060); Assert.Equal(hash3, (ulong) 0x4c0a65b1ef9ea060); +#endif } [Fact] diff --git a/DTLib.XXHash/DTLib.XXHash.csproj b/DTLib.XXHash/DTLib.XXHash.csproj index c33a92f..bb0b9cb 100644 --- a/DTLib.XXHash/DTLib.XXHash.csproj +++ b/DTLib.XXHash/DTLib.XXHash.csproj @@ -13,8 +13,8 @@ - - + + diff --git a/DTLib.XXHash/xxHash128.XXH.cs b/DTLib.XXHash/xxHash128.XXH.cs index abe8665..49cf3fe 100644 --- a/DTLib.XXHash/xxHash128.XXH.cs +++ b/DTLib.XXHash/xxHash128.XXH.cs @@ -7,27 +7,27 @@ namespace DTLib.XXHash { public static partial class xxHash128 { - private static readonly ulong XXH_PRIME64_1 = 11400714785074694791UL; - private static readonly ulong XXH_PRIME64_2 = 14029467366897019727UL; - private static readonly ulong XXH_PRIME64_3 = 1609587929392839161UL; - private static readonly ulong XXH_PRIME64_4 = 9650029242287828579UL; - private static readonly ulong XXH_PRIME64_5 = 2870177450012600261UL; + private const ulong XXH_PRIME64_1 = 11400714785074694791UL; + private const ulong XXH_PRIME64_2 = 14029467366897019727UL; + private const ulong XXH_PRIME64_3 = 1609587929392839161UL; + private const ulong XXH_PRIME64_4 = 9650029242287828579UL; + private const ulong XXH_PRIME64_5 = 2870177450012600261UL; - private static readonly uint XXH_PRIME32_1 = 2654435761U; - private static readonly uint XXH_PRIME32_2 = 2246822519U; - private static readonly uint XXH_PRIME32_3 = 3266489917U; - private static readonly uint XXH_PRIME32_4 = 668265263U; - private static readonly uint XXH_PRIME32_5 = 374761393U; + private const uint XXH_PRIME32_1 = 2654435761U; + private const uint XXH_PRIME32_2 = 2246822519U; + private const uint XXH_PRIME32_3 = 3266489917U; + private const uint XXH_PRIME32_4 = 668265263U; + private const uint XXH_PRIME32_5 = 374761393U; - private static readonly int XXH_STRIPE_LEN = 64; - private static readonly int XXH_ACC_NB = 8; - private static readonly int XXH_SECRET_CONSUME_RATE = 8; - private static readonly int XXH_SECRET_MERGEACCS_START = 11; - private static readonly int XXH_SECRET_DEFAULT_SIZE = 192; - private static readonly int XXH_SECRET_LASTACC_START = 7; + private const int XXH_STRIPE_LEN = 64; + private const int XXH_ACC_NB = 8; + private const int XXH_SECRET_CONSUME_RATE = 8; + private const int XXH_SECRET_MERGEACCS_START = 11; + private const int XXH_SECRET_DEFAULT_SIZE = 192; + private const int XXH_SECRET_LASTACC_START = 7; - private static readonly byte MM_SHUFFLE_0_3_0_1 = 0b0011_0001; - private static readonly byte MM_SHUFFLE_1_0_3_2 = 0b0100_1110; + private const byte MM_SHUFFLE_0_3_0_1 = 0b0011_0001; + private const byte MM_SHUFFLE_1_0_3_2 = 0b0100_1110; [FixedAddressValueType] private static readonly Vector256 M256i_XXH_PRIME32_1 = Vector256.Create(XXH_PRIME32_1); diff --git a/DTLib.XXHash/xxHash128.XXH3.cs b/DTLib.XXHash/xxHash128.XXH3.cs index 493cbef..7fce705 100644 --- a/DTLib.XXHash/xxHash128.XXH3.cs +++ b/DTLib.XXHash/xxHash128.XXH3.cs @@ -30,12 +30,12 @@ namespace DTLib.XXHash XXH_PRIME64_4, XXH_PRIME32_2, XXH_PRIME64_5, XXH_PRIME32_1 }; - private static readonly int XXH3_SECRET_SIZE_MIN = 136; - private static readonly int XXH3_SECRET_DEFAULT_SIZE = 192; - private static readonly int XXH3_MIDSIZE_MAX = 240; - private static readonly int XXH3_MIDSIZE_STARTOFFSET = 3; - private static readonly int XXH3_MIDSIZE_LASTOFFSET = 17; - private static readonly int XXH3_ACC_SIZE = 64; + private const int XXH3_SECRET_SIZE_MIN = 136; + private const int XXH3_SECRET_DEFAULT_SIZE = 192; + private const int XXH3_MIDSIZE_MAX = 240; + private const int XXH3_MIDSIZE_STARTOFFSET = 3; + private const int XXH3_MIDSIZE_LASTOFFSET = 17; + private const int XXH3_ACC_SIZE = 64; [MethodImpl(MethodImplOptions.AggressiveInlining)] private static unsafe uint128 XXH3_128bits_internal(byte* input, int len, ulong seed, byte* secret, int secretLen) diff --git a/DTLib.XXHash/xxHash3.XXH.cs b/DTLib.XXHash/xxHash3.XXH.cs index b2fff29..b9581ec 100644 --- a/DTLib.XXHash/xxHash3.XXH.cs +++ b/DTLib.XXHash/xxHash3.XXH.cs @@ -2,16 +2,17 @@ using System.Runtime.Intrinsics; using System.Runtime.Intrinsics.X86; using System.Runtime.CompilerServices; +#pragma warning disable CS0414 // Field is assigned but its value is never used namespace DTLib.XXHash { public static partial class xxHash3 { - private static readonly ulong XXH_PRIME64_1 = 11400714785074694791UL; - private static readonly ulong XXH_PRIME64_2 = 14029467366897019727UL; - private static readonly ulong XXH_PRIME64_3 = 1609587929392839161UL; - private static readonly ulong XXH_PRIME64_4 = 9650029242287828579UL; - private static readonly ulong XXH_PRIME64_5 = 2870177450012600261UL; + private const ulong XXH_PRIME64_1 = 11400714785074694791UL; + private const ulong XXH_PRIME64_2 = 14029467366897019727UL; + private const ulong XXH_PRIME64_3 = 1609587929392839161UL; + private const ulong XXH_PRIME64_4 = 9650029242287828579UL; + private const ulong XXH_PRIME64_5 = 2870177450012600261UL; private static readonly uint XXH_PRIME32_1 = 2654435761U; private static readonly uint XXH_PRIME32_2 = 2246822519U; @@ -19,15 +20,15 @@ namespace DTLib.XXHash private static readonly uint XXH_PRIME32_4 = 668265263U; private static readonly uint XXH_PRIME32_5 = 374761393U; - private static readonly int XXH_STRIPE_LEN = 64; - private static readonly int XXH_ACC_NB = XXH_STRIPE_LEN / 8; - private static readonly int XXH_SECRET_CONSUME_RATE = 8; - private static readonly int XXH_SECRET_DEFAULT_SIZE = 192; - private static readonly int XXH_SECRET_MERGEACCS_START = 11; - private static readonly int XXH_SECRET_LASTACC_START = 7; + private const int XXH_STRIPE_LEN = 64; + private const int XXH_ACC_NB = XXH_STRIPE_LEN / 8; + private const int XXH_SECRET_CONSUME_RATE = 8; + private const int XXH_SECRET_DEFAULT_SIZE = 192; + private const int XXH_SECRET_MERGEACCS_START = 11; + private const int XXH_SECRET_LASTACC_START = 7; - private static readonly byte MM_SHUFFLE_0_3_0_1 = 0b0011_0001; - private static readonly byte MM_SHUFFLE_1_0_3_2 = 0b0100_1110; + private const byte MM_SHUFFLE_0_3_0_1 = 0b0011_0001; + private const byte MM_SHUFFLE_1_0_3_2 = 0b0100_1110; [FixedAddressValueType] private static readonly Vector256 M256i_XXH_PRIME32_1 = Vector256.Create(XXH_PRIME32_1); diff --git a/DTLib.XXHash/xxHash3.XXH3.cs b/DTLib.XXHash/xxHash3.XXH3.cs index b61ec26..798a375 100644 --- a/DTLib.XXHash/xxHash3.XXH3.cs +++ b/DTLib.XXHash/xxHash3.XXH3.cs @@ -29,11 +29,11 @@ namespace DTLib.XXHash XXH_PRIME64_4, XXH_PRIME32_2, XXH_PRIME64_5, XXH_PRIME32_1 }; - private static readonly int XXH3_MIDSIZE_MAX = 240; - private static readonly int XXH3_MIDSIZE_STARTOFFSET = 3; - private static readonly int XXH3_MIDSIZE_LASTOFFSET = 17; - private static readonly int XXH3_SECRET_SIZE_MIN = 136; - private static readonly int XXH3_SECRET_DEFAULT_SIZE = 192; + private const int XXH3_MIDSIZE_MAX = 240; + private const int XXH3_MIDSIZE_STARTOFFSET = 3; + private const int XXH3_MIDSIZE_LASTOFFSET = 17; + private const int XXH3_SECRET_SIZE_MIN = 136; + private const int XXH3_SECRET_DEFAULT_SIZE = 192; [MethodImpl(MethodImplOptions.AggressiveInlining)] private static unsafe ulong XXH3_64bits_internal(byte* input, int len, ulong seed64, byte* secret, diff --git a/DTLib.XXHash/xxHash64.XXH.cs b/DTLib.XXHash/xxHash64.XXH.cs index 08abb01..46ade40 100644 --- a/DTLib.XXHash/xxHash64.XXH.cs +++ b/DTLib.XXHash/xxHash64.XXH.cs @@ -6,11 +6,11 @@ namespace DTLib.XXHash { public static partial class xxHash64 { - private static readonly ulong XXH_PRIME64_1 = 11400714785074694791UL; - private static readonly ulong XXH_PRIME64_2 = 14029467366897019727UL; - private static readonly ulong XXH_PRIME64_3 = 1609587929392839161UL; - private static readonly ulong XXH_PRIME64_4 = 9650029242287828579UL; - private static readonly ulong XXH_PRIME64_5 = 2870177450012600261UL; + private const ulong XXH_PRIME64_1 = 11400714785074694791UL; + private const ulong XXH_PRIME64_2 = 14029467366897019727UL; + private const ulong XXH_PRIME64_3 = 1609587929392839161UL; + private const ulong XXH_PRIME64_4 = 9650029242287828579UL; + private const ulong XXH_PRIME64_5 = 2870177450012600261UL; [MethodImpl(MethodImplOptions.AggressiveInlining)] private static ulong XXH_rotl64(ulong x, int r)