Skip to content

Commit 5f14d6a

Browse files
authored
Merge pull request #125 from DataDog/viq111/1.5.5
Update vendored zstd to 1.5.5
2 parents 03725e7 + ca4d3c7 commit 5f14d6a

Some content is hidden

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

87 files changed

+5454
-3859
lines changed

ZSTD_LICENSE

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ BSD License
22

33
For Zstandard software
44

5-
Copyright (c) 2016-present, Facebook, Inc. All rights reserved.
5+
Copyright (c) Meta Platforms, Inc. and affiliates. All rights reserved.
66

77
Redistribution and use in source and binary forms, with or without modification,
88
are permitted provided that the following conditions are met:
@@ -14,9 +14,9 @@ are permitted provided that the following conditions are met:
1414
this list of conditions and the following disclaimer in the documentation
1515
and/or other materials provided with the distribution.
1616

17-
* Neither the name Facebook nor the names of its contributors may be used to
18-
endorse or promote products derived from this software without specific
19-
prior written permission.
17+
* Neither the name Facebook, nor Meta, nor the names of its contributors may
18+
be used to endorse or promote products derived from this software without
19+
specific prior written permission.
2020

2121
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
2222
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED

allocations.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#ifndef USE_EXTERNAL_ZSTD
2+
/*
3+
* Copyright (c) Meta Platforms, Inc. and affiliates.
4+
* All rights reserved.
5+
*
6+
* This source code is licensed under both the BSD-style license (found in the
7+
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
8+
* in the COPYING file in the root directory of this source tree).
9+
* You may select, at your option, one of the above-listed licenses.
10+
*/
11+
12+
/* This file provides custom allocation primitives
13+
*/
14+
15+
#define ZSTD_DEPS_NEED_MALLOC
16+
#include "zstd_deps.h" /* ZSTD_malloc, ZSTD_calloc, ZSTD_free, ZSTD_memset */
17+
18+
#include "mem.h" /* MEM_STATIC */
19+
#define ZSTD_STATIC_LINKING_ONLY
20+
#include "zstd.h" /* ZSTD_customMem */
21+
22+
#ifndef ZSTD_ALLOCATIONS_H
23+
#define ZSTD_ALLOCATIONS_H
24+
25+
/* custom memory allocation functions */
26+
27+
MEM_STATIC void* ZSTD_customMalloc(size_t size, ZSTD_customMem customMem)
28+
{
29+
if (customMem.customAlloc)
30+
return customMem.customAlloc(customMem.opaque, size);
31+
return ZSTD_malloc(size);
32+
}
33+
34+
MEM_STATIC void* ZSTD_customCalloc(size_t size, ZSTD_customMem customMem)
35+
{
36+
if (customMem.customAlloc) {
37+
/* calloc implemented as malloc+memset;
38+
* not as efficient as calloc, but next best guess for custom malloc */
39+
void* const ptr = customMem.customAlloc(customMem.opaque, size);
40+
ZSTD_memset(ptr, 0, size);
41+
return ptr;
42+
}
43+
return ZSTD_calloc(1, size);
44+
}
45+
46+
MEM_STATIC void ZSTD_customFree(void* ptr, ZSTD_customMem customMem)
47+
{
48+
if (ptr!=NULL) {
49+
if (customMem.customFree)
50+
customMem.customFree(customMem.opaque, ptr);
51+
else
52+
ZSTD_free(ptr);
53+
}
54+
}
55+
56+
#endif /* ZSTD_ALLOCATIONS_H */
57+
58+
#endif /* USE_EXTERNAL_ZSTD */

bits.h

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

0 commit comments

Comments
 (0)