Skip to content

Commit e6a39e1

Browse files
authored
Merge pull request #54 from SpringMT/update-zstd-for-v1.5.4
Update zstd for v1.5.4
2 parents 198582d + 8bc887e commit e6a39e1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+4542
-3027
lines changed

.github/workflows/ruby.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
runs-on: ubuntu-latest
1616
strategy:
1717
matrix:
18-
ruby-version: ['2.7', '3.0', '3.1']
18+
ruby-version: ['2.7', '3.0', '3.1', '3.2']
1919

2020
steps:
2121
- uses: actions/checkout@v2

ext/zstdruby/libzstd/common/bits.h

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under both the BSD-style license (found in the
6+
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
7+
* in the COPYING file in the root directory of this source tree).
8+
* You may select, at your option, one of the above-listed licenses.
9+
*/
10+
11+
#ifndef ZSTD_BITS_H
12+
#define ZSTD_BITS_H
13+
14+
#include "mem.h"
15+
16+
MEM_STATIC unsigned ZSTD_countTrailingZeros32_fallback(U32 val)
17+
{
18+
assert(val != 0);
19+
{
20+
static const int DeBruijnBytePos[32] = {0, 1, 28, 2, 29, 14, 24, 3,
21+
30, 22, 20, 15, 25, 17, 4, 8,
22+
31, 27, 13, 23, 21, 19, 16, 7,
23+
26, 12, 18, 6, 11, 5, 10, 9};
24+
return DeBruijnBytePos[((U32) ((val & -(S32) val) * 0x077CB531U)) >> 27];
25+
}
26+
}
27+
28+
MEM_STATIC unsigned ZSTD_countTrailingZeros32(U32 val)
29+
{
30+
assert(val != 0);
31+
# if defined(_MSC_VER)
32+
# if STATIC_BMI2 == 1
33+
return _tzcnt_u32(val);
34+
# else
35+
if (val != 0) {
36+
unsigned long r;
37+
_BitScanForward(&r, val);
38+
return (unsigned)r;
39+
} else {
40+
/* Should not reach this code path */
41+
__assume(0);
42+
}
43+
# endif
44+
# elif defined(__GNUC__) && (__GNUC__ >= 4)
45+
return (unsigned)__builtin_ctz(val);
46+
# else
47+
return ZSTD_countTrailingZeros32_fallback(val);
48+
# endif
49+
}
50+
51+
MEM_STATIC unsigned ZSTD_countLeadingZeros32_fallback(U32 val) {
52+
assert(val != 0);
53+
{
54+
static const U32 DeBruijnClz[32] = {0, 9, 1, 10, 13, 21, 2, 29,
55+
11, 14, 16, 18, 22, 25, 3, 30,
56+
8, 12, 20, 28, 15, 17, 24, 7,
57+
19, 27, 23, 6, 26, 5, 4, 31};
58+
val |= val >> 1;
59+
val |= val >> 2;
60+
val |= val >> 4;
61+
val |= val >> 8;
62+
val |= val >> 16;
63+
return 31 - DeBruijnClz[(val * 0x07C4ACDDU) >> 27];
64+
}
65+
}
66+
67+
MEM_STATIC unsigned ZSTD_countLeadingZeros32(U32 val)
68+
{
69+
assert(val != 0);
70+
# if defined(_MSC_VER)
71+
# if STATIC_BMI2 == 1
72+
return _lzcnt_u32(val);
73+
# else
74+
if (val != 0) {
75+
unsigned long r;
76+
_BitScanReverse(&r, val);
77+
return (unsigned)(31 - r);
78+
} else {
79+
/* Should not reach this code path */
80+
__assume(0);
81+
}
82+
# endif
83+
# elif defined(__GNUC__) && (__GNUC__ >= 4)
84+
return (unsigned)__builtin_clz(val);
85+
# else
86+
return ZSTD_countLeadingZeros32_fallback(val);
87+
# endif
88+
}
89+
90+
MEM_STATIC unsigned ZSTD_countTrailingZeros64(U64 val)
91+
{
92+
assert(val != 0);
93+
# if defined(_MSC_VER) && defined(_WIN64)
94+
# if STATIC_BMI2 == 1
95+
return _tzcnt_u64(val);
96+
# else
97+
if (val != 0) {
98+
unsigned long r;
99+
_BitScanForward64(&r, val);
100+
return (unsigned)r;
101+
} else {
102+
/* Should not reach this code path */
103+
__assume(0);
104+
}
105+
# endif
106+
# elif defined(__GNUC__) && (__GNUC__ >= 4) && defined(__LP64__)
107+
return (unsigned)__builtin_ctzll(val);
108+
# else
109+
{
110+
U32 mostSignificantWord = (U32)(val >> 32);
111+
U32 leastSignificantWord = (U32)val;
112+
if (leastSignificantWord == 0) {
113+
return 32 + ZSTD_countTrailingZeros32(mostSignificantWord);
114+
} else {
115+
return ZSTD_countTrailingZeros32(leastSignificantWord);
116+
}
117+
}
118+
# endif
119+
}
120+
121+
MEM_STATIC unsigned ZSTD_countLeadingZeros64(U64 val)
122+
{
123+
assert(val != 0);
124+
# if defined(_MSC_VER) && defined(_WIN64)
125+
# if STATIC_BMI2 == 1
126+
return _lzcnt_u64(val);
127+
# else
128+
if (val != 0) {
129+
unsigned long r;
130+
_BitScanReverse64(&r, val);
131+
return (unsigned)(63 - r);
132+
} else {
133+
/* Should not reach this code path */
134+
__assume(0);
135+
}
136+
# endif
137+
# elif defined(__GNUC__) && (__GNUC__ >= 4)
138+
return (unsigned)(__builtin_clzll(val));
139+
# else
140+
{
141+
U32 mostSignificantWord = (U32)(val >> 32);
142+
U32 leastSignificantWord = (U32)val;
143+
if (mostSignificantWord == 0) {
144+
return 32 + ZSTD_countLeadingZeros32(leastSignificantWord);
145+
} else {
146+
return ZSTD_countLeadingZeros32(mostSignificantWord);
147+
}
148+
}
149+
# endif
150+
}
151+
152+
MEM_STATIC unsigned ZSTD_NbCommonBytes(size_t val)
153+
{
154+
if (MEM_isLittleEndian()) {
155+
if (MEM_64bits()) {
156+
return ZSTD_countTrailingZeros64((U64)val) >> 3;
157+
} else {
158+
return ZSTD_countTrailingZeros32((U32)val) >> 3;
159+
}
160+
} else { /* Big Endian CPU */
161+
if (MEM_64bits()) {
162+
return ZSTD_countLeadingZeros64((U64)val) >> 3;
163+
} else {
164+
return ZSTD_countLeadingZeros32((U32)val) >> 3;
165+
}
166+
}
167+
}
168+
169+
MEM_STATIC unsigned ZSTD_highbit32(U32 val) /* compress, dictBuilder, decodeCorpus */
170+
{
171+
assert(val != 0);
172+
return 31 - ZSTD_countLeadingZeros32(val);
173+
}
174+
175+
#endif /* ZSTD_BITS_H */

ext/zstdruby/libzstd/common/bitstream.h

Lines changed: 18 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* ******************************************************************
22
* bitstream
33
* Part of FSE library
4-
* Copyright (c) Yann Collet, Facebook, Inc.
4+
* Copyright (c) Meta Platforms, Inc. and affiliates.
55
*
66
* You can contact the author at :
77
* - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
@@ -30,14 +30,15 @@ extern "C" {
3030
#include "compiler.h" /* UNLIKELY() */
3131
#include "debug.h" /* assert(), DEBUGLOG(), RAWLOG() */
3232
#include "error_private.h" /* error codes and messages */
33+
#include "bits.h" /* ZSTD_highbit32 */
3334

3435

3536
/*=========================================
3637
* Target specific
3738
=========================================*/
3839
#ifndef ZSTD_NO_INTRINSICS
39-
# if defined(__BMI__) && defined(__GNUC__)
40-
# include <immintrin.h> /* support for bextr (experimental) */
40+
# if (defined(__BMI__) || defined(__BMI2__)) && defined(__GNUC__)
41+
# include <immintrin.h> /* support for bextr (experimental)/bzhi */
4142
# elif defined(__ICCARM__)
4243
# include <intrinsics.h>
4344
# endif
@@ -132,48 +133,6 @@ MEM_STATIC void BIT_flushBitsFast(BIT_CStream_t* bitC);
132133
MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits);
133134
/* faster, but works only if nbBits >= 1 */
134135

135-
136-
137-
/*-**************************************************************
138-
* Internal functions
139-
****************************************************************/
140-
MEM_STATIC unsigned BIT_highbit32 (U32 val)
141-
{
142-
assert(val != 0);
143-
{
144-
# if defined(_MSC_VER) /* Visual */
145-
# if STATIC_BMI2 == 1
146-
return _lzcnt_u32(val) ^ 31;
147-
# else
148-
if (val != 0) {
149-
unsigned long r;
150-
_BitScanReverse(&r, val);
151-
return (unsigned)r;
152-
} else {
153-
/* Should not reach this code path */
154-
__assume(0);
155-
}
156-
# endif
157-
# elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */
158-
return __builtin_clz (val) ^ 31;
159-
# elif defined(__ICCARM__) /* IAR Intrinsic */
160-
return 31 - __CLZ(val);
161-
# else /* Software version */
162-
static const unsigned DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29,
163-
11, 14, 16, 18, 22, 25, 3, 30,
164-
8, 12, 20, 28, 15, 17, 24, 7,
165-
19, 27, 23, 6, 26, 5, 4, 31 };
166-
U32 v = val;
167-
v |= v >> 1;
168-
v |= v >> 2;
169-
v |= v >> 4;
170-
v |= v >> 8;
171-
v |= v >> 16;
172-
return DeBruijnClz[ (U32) (v * 0x07C4ACDDU) >> 27];
173-
# endif
174-
}
175-
}
176-
177136
/*===== Local Constants =====*/
178137
static const unsigned BIT_mask[] = {
179138
0, 1, 3, 7, 0xF, 0x1F,
@@ -203,6 +162,16 @@ MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC,
203162
return 0;
204163
}
205164

165+
MEM_STATIC FORCE_INLINE_ATTR size_t BIT_getLowerBits(size_t bitContainer, U32 const nbBits)
166+
{
167+
#if defined(STATIC_BMI2) && STATIC_BMI2 == 1 && !defined(ZSTD_NO_INTRINSICS)
168+
return _bzhi_u64(bitContainer, nbBits);
169+
#else
170+
assert(nbBits < BIT_MASK_SIZE);
171+
return bitContainer & BIT_mask[nbBits];
172+
#endif
173+
}
174+
206175
/*! BIT_addBits() :
207176
* can add up to 31 bits into `bitC`.
208177
* Note : does not check for register overflow ! */
@@ -212,7 +181,7 @@ MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC,
212181
DEBUG_STATIC_ASSERT(BIT_MASK_SIZE == 32);
213182
assert(nbBits < BIT_MASK_SIZE);
214183
assert(nbBits + bitC->bitPos < sizeof(bitC->bitContainer) * 8);
215-
bitC->bitContainer |= (value & BIT_mask[nbBits]) << bitC->bitPos;
184+
bitC->bitContainer |= BIT_getLowerBits(value, nbBits) << bitC->bitPos;
216185
bitC->bitPos += nbBits;
217186
}
218187

@@ -291,7 +260,7 @@ MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, si
291260
bitD->ptr = (const char*)srcBuffer + srcSize - sizeof(bitD->bitContainer);
292261
bitD->bitContainer = MEM_readLEST(bitD->ptr);
293262
{ BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1];
294-
bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0; /* ensures bitsConsumed is always set */
263+
bitD->bitsConsumed = lastByte ? 8 - ZSTD_highbit32(lastByte) : 0; /* ensures bitsConsumed is always set */
295264
if (lastByte == 0) return ERROR(GENERIC); /* endMark not present */ }
296265
} else {
297266
bitD->ptr = bitD->start;
@@ -319,7 +288,7 @@ MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, si
319288
default: break;
320289
}
321290
{ BYTE const lastByte = ((const BYTE*)srcBuffer)[srcSize-1];
322-
bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0;
291+
bitD->bitsConsumed = lastByte ? 8 - ZSTD_highbit32(lastByte) : 0;
323292
if (lastByte == 0) return ERROR(corruption_detected); /* endMark not present */
324293
}
325294
bitD->bitsConsumed += (U32)(sizeof(bitD->bitContainer) - srcSize)*8;
@@ -350,16 +319,6 @@ MEM_STATIC FORCE_INLINE_ATTR size_t BIT_getMiddleBits(size_t bitContainer, U32 c
350319
#endif
351320
}
352321

353-
MEM_STATIC FORCE_INLINE_ATTR size_t BIT_getLowerBits(size_t bitContainer, U32 const nbBits)
354-
{
355-
#if defined(STATIC_BMI2) && STATIC_BMI2 == 1
356-
return _bzhi_u64(bitContainer, nbBits);
357-
#else
358-
assert(nbBits < BIT_MASK_SIZE);
359-
return bitContainer & BIT_mask[nbBits];
360-
#endif
361-
}
362-
363322
/*! BIT_lookBits() :
364323
* Provides next n bits from local register.
365324
* local register is not modified.
@@ -406,7 +365,7 @@ MEM_STATIC FORCE_INLINE_ATTR size_t BIT_readBits(BIT_DStream_t* bitD, unsigned n
406365
}
407366

408367
/*! BIT_readBitsFast() :
409-
* unsafe version; only works only if nbBits >= 1 */
368+
* unsafe version; only works if nbBits >= 1 */
410369
MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits)
411370
{
412371
size_t const value = BIT_lookBitsFast(bitD, nbBits);

ext/zstdruby/libzstd/common/compiler.h

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) Yann Collet, Facebook, Inc.
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
33
* All rights reserved.
44
*
55
* This source code is licensed under both the BSD-style license (found in the
@@ -165,6 +165,12 @@
165165
#define UNLIKELY(x) (x)
166166
#endif
167167

168+
#if __has_builtin(__builtin_unreachable) || (defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)))
169+
# define ZSTD_UNREACHABLE { assert(0), __builtin_unreachable(); }
170+
#else
171+
# define ZSTD_UNREACHABLE { assert(0); }
172+
#endif
173+
168174
/* disable warnings */
169175
#ifdef _MSC_VER /* Visual Studio */
170176
# include <intrin.h> /* For Visual 2005 */
@@ -181,6 +187,8 @@
181187
# ifdef __AVX2__ //MSVC does not have a BMI2 specific flag, but every CPU that supports AVX2 also supports BMI2
182188
# define STATIC_BMI2 1
183189
# endif
190+
# elif defined(__BMI2__) && defined(__x86_64__) && defined(__GNUC__)
191+
# define STATIC_BMI2 1
184192
# endif
185193
#endif
186194

@@ -273,7 +281,18 @@
273281
* Sanitizer
274282
*****************************************************************/
275283

276-
#if ZSTD_MEMORY_SANITIZER
284+
/* Issue #3240 reports an ASAN failure on an llvm-mingw build. Out of an
285+
* abundance of caution, disable our custom poisoning on mingw. */
286+
#ifdef __MINGW32__
287+
#ifndef ZSTD_ASAN_DONT_POISON_WORKSPACE
288+
#define ZSTD_ASAN_DONT_POISON_WORKSPACE 1
289+
#endif
290+
#ifndef ZSTD_MSAN_DONT_POISON_WORKSPACE
291+
#define ZSTD_MSAN_DONT_POISON_WORKSPACE 1
292+
#endif
293+
#endif
294+
295+
#if ZSTD_MEMORY_SANITIZER && !defined(ZSTD_MSAN_DONT_POISON_WORKSPACE)
277296
/* Not all platforms that support msan provide sanitizers/msan_interface.h.
278297
* We therefore declare the functions we need ourselves, rather than trying to
279298
* include the header file... */
@@ -294,7 +313,7 @@ void __msan_poison(const volatile void *a, size_t size);
294313
intptr_t __msan_test_shadow(const volatile void *x, size_t size);
295314
#endif
296315

297-
#if ZSTD_ADDRESS_SANITIZER
316+
#if ZSTD_ADDRESS_SANITIZER && !defined(ZSTD_ASAN_DONT_POISON_WORKSPACE)
298317
/* Not all platforms that support asan provide sanitizers/asan_interface.h.
299318
* We therefore declare the functions we need ourselves, rather than trying to
300319
* include the header file... */

ext/zstdruby/libzstd/common/cpu.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) Facebook, Inc.
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
33
* All rights reserved.
44
*
55
* This source code is licensed under both the BSD-style license (found in the

0 commit comments

Comments
 (0)