Skip to content

Commit cde19de

Browse files
nodejs-github-botaduh95
authored andcommitted
deps: update zlib to 1.3.2.1-motley-5eb4d7e
PR-URL: #65494 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Filip Skokan <panva.ip@gmail.com>
1 parent 21f18aa commit cde19de

10 files changed

Lines changed: 152 additions & 19 deletions

File tree

deps/zlib/BUILD.gn

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,6 @@ source_set("zlib_adler32_simd") {
9898
"adler32_simd.c",
9999
"adler32_simd.h",
100100
]
101-
102-
if (!is_win || is_clang) {
103-
cflags = [ "-mssse3" ]
104-
}
105101
}
106102

107103
if (use_arm_neon_optimizations) {
@@ -168,6 +164,15 @@ if (use_arm_neon_optimizations) {
168164
}
169165
}
170166

167+
# Enables 64-bit word-at-a-time compare256() in deflate's longest_match() on
168+
# 64-bit little-endian architectures (x64 and arm64). Follows the same pattern
169+
# as INFLATE_CHUNK_READ_64LE and DEFLATE_CHUNK_WRITE_64LE below.
170+
config("zlib_compare256_config") {
171+
if (current_cpu == "x64" || current_cpu == "arm64") {
172+
defines = [ "DEFLATE_COMPARE256_64LE" ]
173+
}
174+
}
175+
171176
config("zlib_data_chunk_simd_config") {
172177
if (use_x86_x64_optimizations) {
173178
defines = [ "INFLATE_CHUNK_SIMD_SSE2" ]
@@ -234,13 +239,6 @@ source_set("zlib_crc32_simd") {
234239
"crc32_simd.h",
235240
"crc_folding.c",
236241
]
237-
238-
if (!is_win || is_clang) {
239-
cflags = [
240-
"-msse4.2",
241-
"-mpclmul",
242-
]
243-
}
244242
}
245243

246244
configs += [ ":zlib_internal_config" ]
@@ -296,6 +294,7 @@ component("zlib") {
296294
sources = [
297295
"adler32.c",
298296
"chromeconf.h",
297+
"compare256.h",
299298
"compress.c",
300299
"contrib/optimizations/insert_string.h",
301300
"cpu_features.c",
@@ -370,6 +369,7 @@ component("zlib") {
370369
public_configs = [ ":zlib_config" ]
371370

372371
configs += [
372+
":zlib_compare256_config",
373373
":zlib_internal_config",
374374

375375
# Must be after no_chromium_code for warning flags to be ordered correctly.

deps/zlib/CMakeLists.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,14 @@ if (ENABLE_SIMD_OPTIMIZATIONS)
9494
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
9595

9696
if (CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
97+
add_definitions(-DDEFLATE_COMPARE256_64LE)
9798
add_definitions(-DINFLATE_CHUNK_SIMD_SSE2)
9899
add_definitions(-DADLER32_SIMD_SSSE3)
99100
add_definitions(-DINFLATE_CHUNK_READ_64LE)
100101
add_definitions(-DDEFLATE_CHUNK_WRITE_64LE)
101102
add_definitions(-DCRC32_SIMD_SSE42_PCLMUL)
102103
if (ENABLE_SIMD_AVX512)
103104
add_definitions(-DCRC32_SIMD_AVX512_PCLMUL)
104-
add_compile_options(-mvpclmulqdq -msse2 -mavx512f -mpclmul)
105-
else()
106-
add_compile_options(-msse4.2 -mpclmul)
107105
endif()
108106
add_definitions(-DDEFLATE_SLIDE_HASH_SSE2)
109107
# Required by CPU features detection code.
@@ -118,6 +116,7 @@ if (ENABLE_SIMD_OPTIMIZATIONS)
118116
add_definitions(-DDEFLATE_CHUNK_WRITE_64LE)
119117
add_definitions(-DCRC32_ARMV8_CRC32)
120118
add_definitions(-DDEFLATE_SLIDE_HASH_NEON)
119+
add_definitions(-DDEFLATE_COMPARE256_64LE)
121120
# Required by CPU features detection code.
122121
if (APPLE)
123122
add_definitions(-DARMV8_OS_MACOS)

deps/zlib/adler32_simd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353

5454
#include <tmmintrin.h>
5555

56-
#if defined(__GNUC__)
56+
#if defined(__GNUC__) || defined(__clang__)
5757
__attribute__((__target__("ssse3")))
5858
#endif
5959
uint32_t ZLIB_INTERNAL adler32_simd_( /* SSSE3 */

deps/zlib/compare256.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/* compare256.h
2+
*
3+
* This does 256-byte match comparison for deflate's longest_match.
4+
*
5+
* Copyright 2026 The Chromium Authors
6+
* Use of this source code is governed by a BSD-style license that can be
7+
* found in the Chromium source repository LICENSE file.
8+
*/
9+
#ifndef COMPARE256_H
10+
#define COMPARE256_H
11+
12+
#include <stdint.h>
13+
#include <string.h>
14+
#if defined(_MSC_VER) && !defined(__clang__)
15+
#include <intrin.h>
16+
#endif
17+
18+
/* Safe unaligned 16-bit load. Compilers optimize this into a single load
19+
* instruction. */
20+
static inline uint16_t read16(const void* p) {
21+
uint16_t v;
22+
memcpy(&v, p, sizeof(v));
23+
return v;
24+
}
25+
26+
/* Finds the byte offset (0..7) of the first difference in non-zero XOR mask
27+
* `x`:
28+
*/
29+
static inline int compare256_diff(uint64_t x) {
30+
#if defined(_MSC_VER) && !defined(__clang__)
31+
unsigned long i;
32+
_BitScanForward64(&i, x);
33+
return (int)i / 8;
34+
#else
35+
return __builtin_ctzll(x) / 8;
36+
#endif
37+
}
38+
39+
/* Returns the number of matching leading bytes (0 to 256) between src0 and
40+
* src1. Compares 8 bytes per iteration with early exit. */
41+
static inline int compare256(const unsigned char* src0,
42+
const unsigned char* src1) {
43+
int len = 0;
44+
do {
45+
uint64_t a, b, x;
46+
memcpy(&a, src0 + len, sizeof(a));
47+
memcpy(&b, src1 + len, sizeof(b));
48+
x = a ^ b;
49+
if (x)
50+
return len + compare256_diff(x);
51+
len += (int)sizeof(a);
52+
} while (len < 256);
53+
return 256;
54+
}
55+
56+
#endif /* COMPARE256_H */

deps/zlib/contrib/tests/utils_unittest.cc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,17 @@
1919
#include "third_party/zlib/contrib/minizip/zip.h"
2020
#endif
2121

22+
#include "compare256.h"
2223
#include "zlib.h"
2324

25+
// Some tests are for little endian optimizations and should be skipped if
26+
// running on big endian.
27+
#if !defined(__BYTE_ORDER__) || __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
28+
#define ZLIB_TEST_LITTLE_ENDIAN 1
29+
#else
30+
#define ZLIB_TEST_LITTLE_ENDIAN 0
31+
#endif
32+
2433
void TestPayloads(size_t input_size, zlib_internal::WrapperType type,
2534
const int compression_level = Z_DEFAULT_COMPRESSION) {
2635
std::vector<unsigned char> input;
@@ -1475,4 +1484,32 @@ TEST(ZlibTest, Crbug500521311) {
14751484
EXPECT_EQ(unzClose(uzf), UNZ_OK);
14761485
}
14771486

1487+
// compare256() returns the number of equal leading bytes. The buffers are
1488+
// exactly the 256 bytes it may read, so ASan traps any over-read.
1489+
TEST(ZlibTest, Compare256) {
1490+
if (!ZLIB_TEST_LITTLE_ENDIAN) {
1491+
GTEST_SKIP() << "compare256() is little-endian only";
1492+
}
1493+
for (int equal = 0; equal <= 256; ++equal) {
1494+
std::vector<unsigned char> a(256, 'a'), b(256, 'a');
1495+
if (equal < 256)
1496+
b[equal] = 'b';
1497+
EXPECT_EQ(compare256(a.data(), b.data()), equal);
1498+
}
1499+
}
1500+
1501+
// longest_match() computes 2 + compare256(scan + 2, match + 2), the first two
1502+
// bytes being known equal, so a match must still reach MAX_MATCH (258).
1503+
TEST(ZlibTest, Compare256ReachesMaxMatch) {
1504+
if (!ZLIB_TEST_LITTLE_ENDIAN) {
1505+
GTEST_SKIP() << "compare256() is little-endian only";
1506+
}
1507+
for (int match_len = 250; match_len <= 258; ++match_len) {
1508+
std::vector<unsigned char> a(258, 'a'), b(258, 'a');
1509+
if (match_len < 258)
1510+
b[match_len] = 'b';
1511+
EXPECT_EQ(2 + compare256(a.data() + 2, b.data() + 2), match_len);
1512+
}
1513+
}
1514+
14781515
#endif

deps/zlib/cpu_features.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ static void _cpu_check_features(void)
160160
#include <immintrin.h>
161161
#include <xsaveintrin.h>
162162
#endif
163+
/* _xgetbv() below needs the xsave ISA. Annotate it here so the build does not
164+
* need -mxsave. */
165+
#if defined(CRC32_SIMD_AVX512_PCLMUL) && (defined(__GNUC__) || defined(__clang__))
166+
__attribute__((__target__("xsave")))
167+
#endif
163168
static void _cpu_check_features(void)
164169
{
165170
int x86_cpu_has_sse2;

deps/zlib/crc32_simd.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
#include <wmmintrin.h>
2222
#include <immintrin.h>
2323

24+
#if defined(__GNUC__) || defined(__clang__)
25+
__attribute__((__target__("avx512f,avx512vl,vpclmulqdq")))
26+
#endif
2427
uint32_t ZLIB_INTERNAL crc32_avx512_simd_( /* AVX512+PCLMUL */
2528
const unsigned char *buf,
2629
z_size_t len,
@@ -212,6 +215,9 @@ uint32_t ZLIB_INTERNAL crc32_avx512_simd_( /* AVX512+PCLMUL */
212215
#include <smmintrin.h>
213216
#include <wmmintrin.h>
214217

218+
#if defined(__GNUC__) || defined(__clang__)
219+
__attribute__((__target__("sse4.2,pclmul")))
220+
#endif
215221
uint32_t ZLIB_INTERNAL crc32_sse42_simd_( /* SSE4.2+PCLMUL */
216222
const unsigned char *buf,
217223
z_size_t len,

deps/zlib/crc_folding.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
#include <immintrin.h>
2626
#include <wmmintrin.h>
2727

28+
#if defined(__GNUC__) || defined(__clang__)
29+
#define TARGET_SSE42_PCLMUL __attribute__((__target__("sse4.2,pclmul")))
30+
#else
31+
#define TARGET_SSE42_PCLMUL
32+
#endif
33+
2834
#define CRC_LOAD(s) \
2935
do { \
3036
__m128i xmm_crc0 = _mm_loadu_si128((__m128i *)s->crc0 + 0);\
@@ -41,6 +47,7 @@
4147
_mm_storeu_si128((__m128i *)s->crc0 + 4, xmm_crc_part);\
4248
} while (0);
4349

50+
TARGET_SSE42_PCLMUL
4451
ZLIB_INTERNAL void crc_fold_init(deflate_state *const s)
4552
{
4653
CRC_LOAD(s)
@@ -55,6 +62,7 @@ ZLIB_INTERNAL void crc_fold_init(deflate_state *const s)
5562
s->strm->adler = 0;
5663
}
5764

65+
TARGET_SSE42_PCLMUL
5866
local void fold_1(deflate_state *const s,
5967
__m128i *xmm_crc0, __m128i *xmm_crc1,
6068
__m128i *xmm_crc2, __m128i *xmm_crc3)
@@ -81,6 +89,7 @@ local void fold_1(deflate_state *const s,
8189
*xmm_crc3 = _mm_castps_si128(ps_res);
8290
}
8391

92+
TARGET_SSE42_PCLMUL
8493
local void fold_2(deflate_state *const s,
8594
__m128i *xmm_crc0, __m128i *xmm_crc1,
8695
__m128i *xmm_crc2, __m128i *xmm_crc3)
@@ -115,6 +124,7 @@ local void fold_2(deflate_state *const s,
115124
*xmm_crc3 = _mm_castps_si128(ps_res31);
116125
}
117126

127+
TARGET_SSE42_PCLMUL
118128
local void fold_3(deflate_state *const s,
119129
__m128i *xmm_crc0, __m128i *xmm_crc1,
120130
__m128i *xmm_crc2, __m128i *xmm_crc3)
@@ -155,6 +165,7 @@ local void fold_3(deflate_state *const s,
155165
*xmm_crc3 = _mm_castps_si128(ps_res32);
156166
}
157167

168+
TARGET_SSE42_PCLMUL
158169
local void fold_4(deflate_state *const s,
159170
__m128i *xmm_crc0, __m128i *xmm_crc1,
160171
__m128i *xmm_crc2, __m128i *xmm_crc3)
@@ -221,6 +232,7 @@ local const unsigned zalign(32) pshufb_shf_table[60] = {
221232
0x0201008f,0x06050403,0x0a090807,0x0e0d0c0b /* shl 1 (16 -15)/shr15*/
222233
};
223234

235+
TARGET_SSE42_PCLMUL
224236
local void partial_fold(deflate_state *const s, const size_t len,
225237
__m128i *xmm_crc0, __m128i *xmm_crc1,
226238
__m128i *xmm_crc2, __m128i *xmm_crc3,
@@ -271,6 +283,7 @@ local void partial_fold(deflate_state *const s, const size_t len,
271283
*xmm_crc3 = _mm_castps_si128(ps_res);
272284
}
273285

286+
TARGET_SSE42_PCLMUL
274287
ZLIB_INTERNAL void crc_fold_copy(deflate_state *const s,
275288
unsigned char *dst, const unsigned char *src, long len)
276289
{
@@ -427,6 +440,7 @@ local const unsigned zalign(16) crc_mask2[4] = {
427440
0x00000000, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
428441
};
429442

443+
TARGET_SSE42_PCLMUL
430444
unsigned ZLIB_INTERNAL crc_fold_512to32(deflate_state *const s)
431445
{
432446
const __m128i xmm_mask = _mm_load_si128((__m128i *)crc_mask);

deps/zlib/deflate.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
#include "slide_hash_simd.h"
5858
#endif
5959

60+
#include "compare256.h"
61+
6062
#if defined(QAT_COMPRESSION_ENABLED)
6163
#include "contrib/qat/deflate_qat.h"
6264
#endif
@@ -1492,7 +1494,13 @@ local uInt longest_match(deflate_state *s, IPos cur_match) {
14921494
Posf *prev = s->prev;
14931495
uInt wmask = s->w_mask;
14941496

1495-
#ifdef UNALIGNED_OK
1497+
#if defined(DEFLATE_COMPARE256_64LE)
1498+
/* Quickly reject non-matches with the 2-byte prefilter
1499+
* (scan_start/scan_end); candidates that pass are extended 8 bytes at a
1500+
* time with compare256(). */
1501+
uInt scan_start = read16(scan);
1502+
uInt scan_end = read16(scan + best_len - 1);
1503+
#elif defined(UNALIGNED_OK)
14961504
/* Compare two bytes at a time. Note: this is not always beneficial.
14971505
* Try with and without -DUNALIGNED_OK to check.
14981506
*/
@@ -1534,7 +1542,13 @@ local uInt longest_match(deflate_state *s, IPos cur_match) {
15341542
* However the length of the match is limited to the lookahead, so
15351543
* the output of deflate is not affected by the uninitialized values.
15361544
*/
1537-
#if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
1545+
#if defined(DEFLATE_COMPARE256_64LE)
1546+
if (read16(match + best_len - 1) != scan_end ||
1547+
read16(match) != scan_start) continue;
1548+
/* scan_start matched, so bytes 0..1 are equal; compare the remaining
1549+
* up to 256 bytes so the length can still reach MAX_MATCH (258). */
1550+
len = 2 + compare256(scan + 2, match + 2);
1551+
#elif (defined(UNALIGNED_OK) && MAX_MATCH == 258)
15381552
/* This code assumes sizeof(unsigned short) == 2. Do not use
15391553
* UNALIGNED_OK if your compiler uses a different size.
15401554
*/
@@ -1624,7 +1638,9 @@ local uInt longest_match(deflate_state *s, IPos cur_match) {
16241638
s->match_start = cur_match;
16251639
best_len = len;
16261640
if (len >= nice_match) break;
1627-
#ifdef UNALIGNED_OK
1641+
#if defined(DEFLATE_COMPARE256_64LE)
1642+
scan_end = read16(scan + best_len - 1);
1643+
#elif defined(UNALIGNED_OK)
16281644
scan_end = *(ushf*)(scan + best_len - 1);
16291645
#else
16301646
scan_end1 = scan[best_len - 1];

src/zlib_version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
// Refer to tools/dep_updaters/update-zlib.sh
33
#ifndef SRC_ZLIB_VERSION_H_
44
#define SRC_ZLIB_VERSION_H_
5-
#define ZLIB_VERSION "1.3.2.1-motley-8002e91"
5+
#define ZLIB_VERSION "1.3.2.1-motley-5eb4d7e"
66
#endif // SRC_ZLIB_VERSION_H_

0 commit comments

Comments
 (0)