Skip to content

Commit 25d339e

Browse files
committed
0.6.1: Ported zstd v1.5.2, minor optimizations related to BMI2
1 parent bae5d34 commit 25d339e

38 files changed

+385
-646
lines changed

src/Zstd.Extern/libzstd.dll

-512 Bytes
Binary file not shown.

src/ZstdSharp/Unsafe/Arrays.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace ZstdSharp.Unsafe
55
{
66
public static unsafe partial class Methods
77
{
8-
static uint* rtbTable = GetArrayPointer(new uint[8] {
8+
static readonly uint* rtbTable = GetArrayPointer(new uint[8] {
99
0,
1010
473195,
1111
504333,
@@ -15,7 +15,7 @@ public static unsafe partial class Methods
1515
750000,
1616
830000,
1717
});
18-
static byte* LL_Code = GetArrayPointer(new byte[64]
18+
static readonly byte* LL_Code = GetArrayPointer(new byte[64]
1919
{
2020
0,
2121
1,
@@ -82,7 +82,7 @@ public static unsafe partial class Methods
8282
24,
8383
24,
8484
});
85-
static byte* ML_Code = GetArrayPointer(new byte[128]
85+
static readonly byte* ML_Code = GetArrayPointer(new byte[128]
8686
{
8787
0,
8888
1,
@@ -213,14 +213,14 @@ public static unsafe partial class Methods
213213
42,
214214
42,
215215
});
216-
static ulong* srcSizeTiers = GetArrayPointer(new ulong[4]
216+
static readonly ulong* srcSizeTiers = GetArrayPointer(new ulong[4]
217217
{
218218
16 * (1 << 10),
219219
128 * (1 << 10),
220220
256 * (1 << 10),
221221
(unchecked(0UL - 1)),
222222
});
223-
static ZSTD_blockCompressor[][] blockCompressor = new ZSTD_blockCompressor[4][]
223+
static readonly ZSTD_blockCompressor[][] blockCompressor = new ZSTD_blockCompressor[4][]
224224
{
225225
new ZSTD_blockCompressor[10]
226226
{
@@ -443,7 +443,7 @@ public static unsafe partial class Methods
443443
},
444444
},
445445
};
446-
static uint* baseLLfreqs = GetArrayPointer(new uint[36]
446+
static readonly uint* baseLLfreqs = GetArrayPointer(new uint[36]
447447
{
448448
4,
449449
2,
@@ -482,7 +482,7 @@ public static unsafe partial class Methods
482482
1,
483483
1,
484484
});
485-
static uint* baseOFCfreqs = GetArrayPointer(new uint[32]
485+
static readonly uint* baseOFCfreqs = GetArrayPointer(new uint[32]
486486
{
487487
6,
488488
2,
@@ -546,7 +546,7 @@ public static unsafe partial class Methods
546546
HUF_decompress4X1,
547547
HUF_decompress4X2,
548548
};
549-
static uint* dec32table = GetArrayPointer(new uint[8]
549+
static readonly uint* dec32table = GetArrayPointer(new uint[8]
550550
{
551551
0,
552552
1,
@@ -557,7 +557,7 @@ public static unsafe partial class Methods
557557
4,
558558
4,
559559
});
560-
static int* dec64table = GetArrayPointer(new int[8]
560+
static readonly int* dec64table = GetArrayPointer(new int[8]
561561
{
562562
8,
563563
8,
@@ -569,6 +569,6 @@ public static unsafe partial class Methods
569569
11,
570570
});
571571

572-
private static byte* emptyWindowString = GetArrayPointer(new byte[] {32, 0});
572+
private readonly static byte* emptyWindowString = GetArrayPointer(new byte[] {32, 0});
573573
}
574574
}

src/ZstdSharp/Unsafe/Bitstream.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ private static uint BIT_highbit32(uint val)
2121
}
2222
}
2323

24-
public static uint* BIT_mask = GetArrayPointer(new uint[32]
24+
public static readonly uint* BIT_mask = GetArrayPointer(new uint[32]
2525
{
2626
0,
2727
1,
@@ -283,13 +283,35 @@ private static nuint BIT_getMiddleBits(nuint bitContainer, uint start, uint nbBi
283283
uint regMask = (uint)((nuint)(sizeof(nuint)) * 8 - 1);
284284

285285
assert(nbBits < ((nuint)(sizeof(uint) * 32) / (nuint)(sizeof(uint))));
286-
return (nuint)((bitContainer >> (int)(start & regMask)) & ((((ulong)(1)) << (int)nbBits) - 1));
286+
#if NETCOREAPP3_1_OR_GREATER
287+
if (System.Runtime.Intrinsics.X86.Bmi2.X64.IsSupported)
288+
{
289+
return (nuint)System.Runtime.Intrinsics.X86.Bmi2.X64.ZeroHighBits(bitContainer >> (int)(start & regMask), nbBits);
290+
}
291+
292+
if (System.Runtime.Intrinsics.X86.Bmi2.IsSupported)
293+
{
294+
return System.Runtime.Intrinsics.X86.Bmi2.ZeroHighBits((uint)(bitContainer >> (int)(start & regMask)), nbBits);
295+
}
296+
#endif
297+
return bitContainer >> (int)(start & regMask) & BIT_mask[nbBits];
287298
}
288299

289300
[MethodImpl(MethodImplOptions.AggressiveInlining)]
290301
private static nuint BIT_getLowerBits(nuint bitContainer, uint nbBits)
291302
{
292303
assert(nbBits < ((nuint)(sizeof(uint) * 32) / (nuint)(sizeof(uint))));
304+
#if NETCOREAPP3_1_OR_GREATER
305+
if (System.Runtime.Intrinsics.X86.Bmi2.X64.IsSupported)
306+
{
307+
return (nuint)System.Runtime.Intrinsics.X86.Bmi2.X64.ZeroHighBits(bitContainer, nbBits);
308+
}
309+
310+
if (System.Runtime.Intrinsics.X86.Bmi2.IsSupported)
311+
{
312+
return System.Runtime.Intrinsics.X86.Bmi2.ZeroHighBits((uint)bitContainer, nbBits);
313+
}
314+
#endif
293315
return bitContainer & BIT_mask[nbBits];
294316
}
295317

@@ -308,6 +330,7 @@ private static nuint BIT_lookBits(BIT_DStream_t* bitD, uint nbBits)
308330
/*! BIT_lookBitsFast() :
309331
* unsafe version; only works if nbBits >= 1 */
310332
[MethodImpl(MethodImplOptions.AggressiveInlining)]
333+
[InlineMethod.Inline]
311334
private static nuint BIT_lookBitsFast(BIT_DStream_t* bitD, uint nbBits)
312335
{
313336
uint regMask = (uint)((nuint)(sizeof(nuint)) * 8 - 1);
@@ -317,6 +340,7 @@ private static nuint BIT_lookBitsFast(BIT_DStream_t* bitD, uint nbBits)
317340
}
318341

319342
[MethodImpl(MethodImplOptions.AggressiveInlining)]
343+
[InlineMethod.Inline]
320344
private static void BIT_skipBits(BIT_DStream_t* bitD, uint nbBits)
321345
{
322346
bitD->bitsConsumed += nbBits;
@@ -354,6 +378,7 @@ private static nuint BIT_readBitsFast(BIT_DStream_t* bitD, uint nbBits)
354378
* point you must use BIT_reloadDStream() to reload.
355379
*/
356380
[MethodImpl(MethodImplOptions.AggressiveInlining)]
381+
[InlineMethod.Inline]
357382
private static BIT_DStream_status BIT_reloadDStreamFast(BIT_DStream_t* bitD)
358383
{
359384
if ((bitD->ptr < bitD->limitPtr))

src/ZstdSharp/Unsafe/EntropyCommon.cs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -246,21 +246,11 @@ private static nuint FSE_readNCount_body_default(short* normalizedCounter, uint*
246246
return FSE_readNCount_body(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
247247
}
248248

249-
private static nuint FSE_readNCount_body_bmi2(short* normalizedCounter, uint* maxSVPtr, uint* tableLogPtr, void* headerBuffer, nuint hbSize)
250-
{
251-
return FSE_readNCount_body(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
252-
}
253-
254249
/*! FSE_readNCount_bmi2():
255250
* Same as FSE_readNCount() but pass bmi2=1 when your CPU supports BMI2 and 0 otherwise.
256251
*/
257252
public static nuint FSE_readNCount_bmi2(short* normalizedCounter, uint* maxSVPtr, uint* tableLogPtr, void* headerBuffer, nuint hbSize, int bmi2)
258253
{
259-
if (bmi2 != 0)
260-
{
261-
return FSE_readNCount_body_bmi2(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
262-
}
263-
264254
return FSE_readNCount_body_default(normalizedCounter, maxSVPtr, tableLogPtr, headerBuffer, hbSize);
265255
}
266256

@@ -407,18 +397,8 @@ private static nuint HUF_readStats_body_default(byte* huffWeight, nuint hwSize,
407397
return HUF_readStats_body(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize, 0);
408398
}
409399

410-
private static nuint HUF_readStats_body_bmi2(byte* huffWeight, nuint hwSize, uint* rankStats, uint* nbSymbolsPtr, uint* tableLogPtr, void* src, nuint srcSize, void* workSpace, nuint wkspSize)
411-
{
412-
return HUF_readStats_body(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize, 1);
413-
}
414-
415400
public static nuint HUF_readStats_wksp(byte* huffWeight, nuint hwSize, uint* rankStats, uint* nbSymbolsPtr, uint* tableLogPtr, void* src, nuint srcSize, void* workSpace, nuint wkspSize, int bmi2)
416401
{
417-
if (bmi2 != 0)
418-
{
419-
return HUF_readStats_body_bmi2(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize);
420-
}
421-
422402
return HUF_readStats_body_default(huffWeight, hwSize, rankStats, nbSymbolsPtr, tableLogPtr, src, srcSize, workSpace, wkspSize);
423403
}
424404
}

src/ZstdSharp/Unsafe/Fastcover.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ private static nuint FASTCOVER_hashPtrToIndex(void* p, uint f, uint d)
2121
return ZSTD_hash8Ptr(p, f);
2222
}
2323

24-
public static FASTCOVER_accel_t* FASTCOVER_defaultAccelParameters = GetArrayPointer(new FASTCOVER_accel_t[11]
24+
public static readonly FASTCOVER_accel_t* FASTCOVER_defaultAccelParameters = GetArrayPointer(new FASTCOVER_accel_t[11]
2525
{
2626
new FASTCOVER_accel_t
2727
{

src/ZstdSharp/Unsafe/FseDecompress.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -401,18 +401,8 @@ private static nuint FSE_decompress_wksp_body_default(void* dst, nuint dstCapaci
401401
return FSE_decompress_wksp_body(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize, 0);
402402
}
403403

404-
private static nuint FSE_decompress_wksp_body_bmi2(void* dst, nuint dstCapacity, void* cSrc, nuint cSrcSize, uint maxLog, void* workSpace, nuint wkspSize)
405-
{
406-
return FSE_decompress_wksp_body(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize, 1);
407-
}
408-
409404
public static nuint FSE_decompress_wksp_bmi2(void* dst, nuint dstCapacity, void* cSrc, nuint cSrcSize, uint maxLog, void* workSpace, nuint wkspSize, int bmi2)
410405
{
411-
if (bmi2 != 0)
412-
{
413-
return FSE_decompress_wksp_body_bmi2(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize);
414-
}
415-
416406
return FSE_decompress_wksp_body_default(dst, dstCapacity, cSrc, cSrcSize, maxLog, workSpace, wkspSize);
417407
}
418408

src/ZstdSharp/Unsafe/HUF_CStream_t.cs

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,44 +23,34 @@ public unsafe partial struct _bitContainer_e__FixedBuffer
2323
public nuint e0;
2424
public nuint e1;
2525

26-
public ref nuint this[int index]
27-
{
28-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
29-
[InlineMethod.Inline]
30-
get => ref *(this + (uint)index);
31-
}
32-
33-
public ref nuint this[uint index]
26+
public ref nuint this[nuint index]
3427
{
3528
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3629
[InlineMethod.Inline]
3730
get => ref *(this + index);
3831
}
3932

40-
public ref nuint this[nuint index]
33+
public ref nuint this[nint index]
4134
{
4235
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4336
[InlineMethod.Inline]
44-
get => ref *(this + (uint)index);
37+
get => ref *(this + (nuint)index);
4538
}
4639

4740
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4841
[InlineMethod.Inline]
4942
public static implicit operator nuint*(in _bitContainer_e__FixedBuffer t)
5043
{
5144
Ldarg_0();
52-
Ldflda(new FieldRef(typeof(_bitContainer_e__FixedBuffer), nameof(e0)));
5345
return IL.ReturnPointer<nuint>();
5446
}
5547

5648
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5749
[InlineMethod.Inline]
58-
public static nuint* operator +(in _bitContainer_e__FixedBuffer t, uint index)
50+
public static nuint* operator +(in _bitContainer_e__FixedBuffer t, nuint index)
5951
{
6052
Ldarg_0();
61-
Ldflda(new FieldRef(typeof(_bitContainer_e__FixedBuffer), nameof(e0)));
6253
Ldarg_1();
63-
Conv_I();
6454
Sizeof<nuint>();
6555
Conv_I();
6656
Mul();
@@ -74,44 +64,34 @@ public unsafe partial struct _bitPos_e__FixedBuffer
7464
public nuint e0;
7565
public nuint e1;
7666

77-
public ref nuint this[int index]
78-
{
79-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
80-
[InlineMethod.Inline]
81-
get => ref *(this + (uint)index);
82-
}
83-
84-
public ref nuint this[uint index]
67+
public ref nuint this[nuint index]
8568
{
8669
[MethodImpl(MethodImplOptions.AggressiveInlining)]
8770
[InlineMethod.Inline]
8871
get => ref *(this + index);
8972
}
9073

91-
public ref nuint this[nuint index]
74+
public ref nuint this[nint index]
9275
{
9376
[MethodImpl(MethodImplOptions.AggressiveInlining)]
9477
[InlineMethod.Inline]
95-
get => ref *(this + (uint)index);
78+
get => ref *(this + (nuint)index);
9679
}
9780

9881
[MethodImpl(MethodImplOptions.AggressiveInlining)]
9982
[InlineMethod.Inline]
10083
public static implicit operator nuint*(in _bitPos_e__FixedBuffer t)
10184
{
10285
Ldarg_0();
103-
Ldflda(new FieldRef(typeof(_bitPos_e__FixedBuffer), nameof(e0)));
10486
return IL.ReturnPointer<nuint>();
10587
}
10688

10789
[MethodImpl(MethodImplOptions.AggressiveInlining)]
10890
[InlineMethod.Inline]
109-
public static nuint* operator +(in _bitPos_e__FixedBuffer t, uint index)
91+
public static nuint* operator +(in _bitPos_e__FixedBuffer t, nuint index)
11092
{
11193
Ldarg_0();
112-
Ldflda(new FieldRef(typeof(_bitPos_e__FixedBuffer), nameof(e0)));
11394
Ldarg_1();
114-
Conv_I();
11595
Sizeof<nuint>();
11696
Conv_I();
11797
Mul();

0 commit comments

Comments
 (0)