lib/libz/0003-Port-Intel-optimizations-adler32-chunkcopy-to-cloudf.patch
$ cat 0003-Port-Intel-optimizations-adler32-chunkcopy-to-cloudf.patch
From 7b243be132503e86d9be0e18454bb05ca033118f Mon Sep 17 00:00:00 2001 From: Janakarajan Natarajan <[email protected]> Date: Mon, 28 Sep 2020 13:24:49 -0500 Subject: [PATCH 3/4] Port Intel optimizations (adler32, chunkcopy) to cloudflare (#23) * Add SIMD SSSE3 implementation of the adler32 checksum Based on the adler32-simd patch from Noel Gordon for the chromium fork of zlib. 17bbb3d73c84 ("zlib adler_simd.c") Signed-off-by: Janakarajan Natarajan <[email protected]> * Port inflate chunk SIMD SSE2 improvements for cloudflare Based on 2 patches from zlib chromium fork: * Adenilson Cavalcanti ([email protected]) 3060dcb - "zlib: inflate using wider loads and stores" * Noel Gordon ([email protected]) 64ffef0 - "Improve zlib inflate speed by using SSE2 chunk copy The improvement in inflate performance is around 15-35%, based on the workload, when checked with a modified zpipe.c and the Silesia corpus. Signed-off-by: Janakarajan Natarajan <[email protected]> --- Makefile.in | 12 ++ adler32.c | 9 ++ adler32_simd.c | 387 ++++++++++++++++++++++++++++++++++++++++++++++++ adler32_simd.h | 37 +++++ chunkcopy.h | 111 ++++++++++---- configure | 26 ++++ inffast_chunk.c | 24 +-- inffast_chunk.h | 2 +- inflate.c | 10 +- 9 files changed, 570 insertions(+), 48 deletions(-) create mode 100644 adler32_simd.c create mode 100644 adler32_simd.h diff --git a/Makefile.in b/Makefile.in index 3647d90..fde5405 100644 --- a/Makefile.in +++ b/Makefile.in @@ -41,6 +41,18 @@ ifneq ($(findstring -DINFLATE_CHUNK_SIMD_NEON,$(CFLAGS)),) STATIC_OBJS += inffast_chunk.o endif +ifneq ($(findstring -DINFLATE_CHUNK_SIMD_SSE2,$(CFLAGS)),) +STATIC_OBJS += inffast_chunk.o +endif + +ifneq ($(findstring -DADLER32_SIMD_NEON,$(CFLAGS)),) +STATIC_OBJS += adler32_simd.o +endif + +ifneq ($(findstring -DADLER32_SIMD_SSSE3,$(CFLAGS)),) +STATIC_OBJS += adler32_simd.o +endif + # TODO: What extension to use here? SHARED_OBJS=$(STATIC_OBJS:.o=.lo) diff --git a/adler32.c b/adler32.c index 127a9a5..05da2ae 100644 --- a/adler32.c +++ b/adler32.c @@ -10,6 +10,10 @@ #include "zutil.h" +#if defined(ADLER32_SIMD_NEON) || defined(ADLER32_SIMD_SSSE3) +#include "adler32_simd.h" +#endif + #define BASE 65521 /* largest prime smaller than 65536 */ #define BASE_X2 131042 /* twice BASE */ #define NMAX 5552 @@ -20,6 +24,11 @@ unsigned long ZEXPORT adler32(unsigned long adler, const unsigned char *buf, unsigned int len) { +#if defined(ADLER32_SIMD_NEON) || defined(ADLER32_SIMD_SSSE3) + if (buf != NULL && len >= 64) + return adler32_simd_((uint32_t)adler, buf, len); +#endif + /* initial Adler-32 value */ if (buf == NULL) return UINT32_C(1) << 0 | UINT32_C(0) << 16; diff --git a/adler32_simd.c b/adler32_simd.c new file mode 100644 index 0000000..5f8c06d --- /dev/null +++ b/adler32_simd.c @@ -0,0 +1,387 @@ +/* adler32_simd.c + * + * (C) 1995-2013 Jean-loup Gailly and Mark Adler + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + * + * Jean-loup Gailly Mark Adler + * [email protected] [email protected] + * + * Copyright 2017 The Chromium Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the Chromium source repository LICENSE file. + * + * Per http://en.wikipedia.org/wiki/Adler-32 the adler32 A value (aka s1) is + * the sum of N input data bytes D1 ... DN, + * + * A = A0 + D1 + D2 + ... + DN + * + * where A0 is the initial value. + * + * SSE2 _mm_sad_epu8() can be used for byte sums (see http://bit.ly/2wpUOeD, + * for example) and accumulating the byte sums can use SSE shuffle-adds (see + * the "Integer" section of http://bit.ly/2erPT8t for details). Arm NEON has + * similar instructions. + * + * The adler32 B value (aka s2) sums the A values from each step: + * + * B0 + (A0 + D1) + (A0 + D1 + D2) + ... + (A0 + D1 + D2 + ... + DN) or + * + * B0 + N.A0 + N.D1 + (N-1).D2 + (N-2).D3 + ... + (N-(N-1)).DN + * + * B0 being the initial value. For 32 bytes (ideal for garden-variety SIMD): + * + * B = B0 + 32.A0 + [D1 D2 D3 ... D32] x [32 31 30 ... 1]. + * + * Adjacent blocks of 32 input bytes can be iterated with the expressions to + * compute the adler32 s1 s2 of M >> 32 input bytes [1]. + * + * As M grows, the s1 s2 sums grow. If left unchecked, they would eventually + * overflow the precision of their integer representation (bad). However, s1 + * and s2 also need to be computed modulo the adler BASE value (reduced). If + * at most NMAX bytes are processed before a reduce, s1 s2 _cannot_ overflow + * a uint32_t type (the NMAX constraint) [2]. + * + * [1] the iterative equations for s2 contain constant factors; these can be + * hoisted from the n-blocks do loop of the SIMD code. + * + * [2] zlib adler32_z() uses this fact to implement NMAX-block-based updates + * of the adler s1 s2 of uint32_t type (see adler32.c). + */ + +#include "adler32_simd.h" + +/* Definitions from adler32.c: largest prime smaller than 65536 */ +#define BASE 65521U +/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */ +#define NMAX 5552 + +#if defined(ADLER32_SIMD_SSSE3) + +#include <tmmintrin.h> + +uint32_t ZLIB_INTERNAL adler32_simd_( /* SSSE3 */ + uint32_t adler, + const unsigned char *buf, + unsigned long len) +{ + /* + * Split Adler-32 into component sums. + */ + uint32_t s1 = adler & 0xffff; + uint32_t s2 = adler >> 16; + + /* + * Process the data in blocks. + */ + const unsigned BLOCK_SIZE = 1 << 5; + + unsigned long blocks = len / BLOCK_SIZE; + len -= blocks * BLOCK_SIZE; + + while (blocks) + { + unsigned n = NMAX / BLOCK_SIZE; /* The NMAX constraint. */ + if (n > blocks) + n = (unsigned) blocks; + blocks -= n; + + const __m128i tap1 = + _mm_setr_epi8(32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17); + const __m128i tap2 = + _mm_setr_epi8(16,15,14,13,12,11,10, 9, 8, 7, 6, 5, 4, 3, 2, 1); + const __m128i zero = + _mm_setr_epi8( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); + const __m128i ones = + _mm_set_epi16( 1, 1, 1, 1, 1, 1, 1, 1); + + /* + * Process n blocks of data. At most NMAX data bytes can be + * processed before s2 must be reduced modulo BASE. + */ + __m128i v_ps = _mm_set_epi32(0, 0, 0, s1 * n); + __m128i v_s2 = _mm_set_epi32(0, 0, 0, s2); + __m128i v_s1 = _mm_set_epi32(0, 0, 0, 0); + + do { + /* + * Load 32 input bytes. + */ + const __m128i bytes1 = _mm_loadu_si128((__m128i*)(buf)); + const __m128i bytes2 = _mm_loadu_si128((__m128i*)(buf + 16)); + + /* + * Add previous block byte sum to v_ps. + */ + v_ps = _mm_add_epi32(v_ps, v_s1); + + /* + * Horizontally add the bytes for s1, multiply-adds the + * bytes by [ 32, 31, 30, ... ] for s2. + */ + v_s1 = _mm_add_epi32(v_s1, _mm_sad_epu8(bytes1, zero)); + const __m128i mad1 = _mm_maddubs_epi16(bytes1, tap1); + v_s2 = _mm_add_epi32(v_s2, _mm_madd_epi16(mad1, ones)); + + v_s1 = _mm_add_epi32(v_s1, _mm_sad_epu8(bytes2, zero)); + const __m128i mad2 = _mm_maddubs_epi16(bytes2, tap2); + v_s2 = _mm_add_epi32(v_s2, _mm_madd_epi16(mad2, ones)); + + buf += BLOCK_SIZE; + + } while (--n); + + v_s2 = _mm_add_epi32(v_s2, _mm_slli_epi32(v_ps, 5)); + + /* + * Sum epi32 ints v_s1(s2) and accumulate in s1(s2). + */ + +#define S23O1 _MM_SHUFFLE(2,3,0,1) /* A B C D -> B A D C */ +#define S1O32 _MM_SHUFFLE(1,0,3,2) /* A B C D -> C D A B */ + + v_s1 = _mm_add_epi32(v_s1, _mm_shuffle_epi32(v_s1, S23O1)); + v_s1 = _mm_add_epi32(v_s1, _mm_shuffle_epi32(v_s1, S1O32)); + + s1 += _mm_cvtsi128_si32(v_s1); + + v_s2 = _mm_add_epi32(v_s2, _mm_shuffle_epi32(v_s2, S23O1)); + v_s2 = _mm_add_epi32(v_s2, _mm_shuffle_epi32(v_s2, S1O32)); + + s2 = _mm_cvtsi128_si32(v_s2); + +#undef S23O1 +#undef S1O32 + + /* + * Reduce. + */ + s1 %= BASE; + s2 %= BASE; + } + + /* + * Handle leftover data. + */ + if (len) { + if (len >= 16) { + s2 += (s1 += *buf++); + s2 += (s1 += *buf++); + s2 += (s1 += *buf++); + s2 += (s1 += *buf++); + + s2 += (s1 += *buf++); + s2 += (s1 += *buf++); + s2 += (s1 += *buf++); + s2 += (s1 += *buf++); + + s2 += (s1 += *buf++); + s2 += (s1 += *buf++); + s2 += (s1 += *buf++); + s2 += (s1 += *buf++); + + s2 += (s1 += *buf++); + s2 += (s1 += *buf++); + s2 += (s1 += *buf++); + s2 += (s1 += *buf++); + + len -= 16; + } + + while (len--) { + s2 += (s1 += *buf++); + } + + if (s1 >= BASE) + s1 -= BASE; + s2 %= BASE; + } + + /* + * Return the recombined sums. + */ + return s1 | (s2 << 16); +} + +#elif defined(ADLER32_SIMD_NEON) + +#include <arm_neon.h> + +uint32_t ZLIB_INTERNAL adler32_simd_( /* NEON */ + uint32_t adler, + const unsigned char *buf, + unsigned long len) +{ + /* + * Split Adler-32 into component sums. + */ + uint32_t s1 = adler & 0xffff; + uint32_t s2 = adler >> 16; + + /* + * Serially compute s1 & s2, until the data is 16-byte aligned. + */ + if ((uintptr_t)buf & 15) { + while ((uintptr_t)buf & 15) { + s2 += (s1 += *buf++); + --len; + } + + if (s1 >= BASE) + s1 -= BASE; + s2 %= BASE; + } + + /* + * Process the data in blocks. + */ + const unsigned BLOCK_SIZE = 1 << 5; + + unsigned long blocks = len / BLOCK_SIZE; + len -= blocks * BLOCK_SIZE; + + while (blocks) + { + unsigned n = NMAX / BLOCK_SIZE; /* The NMAX constraint. */ + if (n > blocks) + n = blocks; + blocks -= n; + + /* + * Process n blocks of data. At most NMAX data bytes can be + * processed before s2 must be reduced modulo BASE. + */ + uint32x4_t v_s2 = (uint32x4_t) { 0, 0, 0, s1 * n }; + uint32x4_t v_s1 = (uint32x4_t) { 0, 0, 0, 0 }; + + uint16x8_t v_column_sum_1 = vdupq_n_u16(0); + uint16x8_t v_column_sum_2 = vdupq_n_u16(0); + uint16x8_t v_column_sum_3 = vdupq_n_u16(0); + uint16x8_t v_column_sum_4 = vdupq_n_u16(0); + + do { + /* + * Load 32 input bytes. + */ + const uint8x16_t bytes1 = vld1q_u8((uint8_t*)(buf)); + const uint8x16_t bytes2 = vld1q_u8((uint8_t*)(buf + 16)); + + /* + * Add previous block byte sum to v_s2. + */ + v_s2 = vaddq_u32(v_s2, v_s1); + + /* + * Horizontally add the bytes for s1. + */ + v_s1 = vpadalq_u16(v_s1, vpadalq_u8(vpaddlq_u8(bytes1), bytes2)); + + /* + * Vertically add the bytes for s2. + */ + v_column_sum_1 = vaddw_u8(v_column_sum_1, vget_low_u8 (bytes1)); + v_column_sum_2 = vaddw_u8(v_column_sum_2, vget_high_u8(bytes1)); + v_column_sum_3 = vaddw_u8(v_column_sum_3, vget_low_u8 (bytes2)); + v_column_sum_4 = vaddw_u8(v_column_sum_4, vget_high_u8(bytes2)); + + buf += BLOCK_SIZE; + + } while (--n); + + v_s2 = vshlq_n_u32(v_s2, 5); + + /* + * Multiply-add bytes by [ 32, 31, 30, ... ] for s2. + */ + v_s2 = vmlal_u16(v_s2, vget_low_u16 (v_column_sum_1), + (uint16x4_t) { 32, 31, 30, 29 }); + v_s2 = vmlal_u16(v_s2, vget_high_u16(v_column_sum_1), + (uint16x4_t) { 28, 27, 26, 25 }); + v_s2 = vmlal_u16(v_s2, vget_low_u16 (v_column_sum_2), + (uint16x4_t) { 24, 23, 22, 21 }); + v_s2 = vmlal_u16(v_s2, vget_high_u16(v_column_sum_2), + (uint16x4_t) { 20, 19, 18, 17 }); + v_s2 = vmlal_u16(v_s2, vget_low_u16 (v_column_sum_3), + (uint16x4_t) { 16, 15, 14, 13 }); + v_s2 = vmlal_u16(v_s2, vget_high_u16(v_column_sum_3), + (uint16x4_t) { 12, 11, 10, 9 }); + v_s2 = vmlal_u16(v_s2, vget_low_u16 (v_column_sum_4), + (uint16x4_t) { 8, 7, 6, 5 }); + v_s2 = vmlal_u16(v_s2, vget_high_u16(v_column_sum_4), + (uint16x4_t) { 4, 3, 2, 1 }); + + /* + * Sum epi32 ints v_s1(s2) and accumulate in s1(s2). + */ + uint32x2_t sum1 = vpadd_u32(vget_low_u32(v_s1), vget_high_u32(v_s1)); + uint32x2_t sum2 = vpadd_u32(vget_low_u32(v_s2), vget_high_u32(v_s2)); + uint32x2_t s1s2 = vpadd_u32(sum1, sum2); + + s1 += vget_lane_u32(s1s2, 0); + s2 += vget_lane_u32(s1s2, 1); + + /* + * Reduce. + */ + s1 %= BASE; + s2 %= BASE; + } + + /* + * Handle leftover data. + */ + if (len) { + if (len >= 16) { + s2 += (s1 += *buf++); + s2 += (s1 += *buf++); + s2 += (s1 += *buf++); + s2 += (s1 += *buf++); + + s2 += (s1 += *buf++); + s2 += (s1 += *buf++); + s2 += (s1 += *buf++); + s2 += (s1 += *buf++); + + s2 += (s1 += *buf++); + s2 += (s1 += *buf++); + s2 += (s1 += *buf++); + s2 += (s1 += *buf++); + + s2 += (s1 += *buf++); + s2 += (s1 += *buf++); + s2 += (s1 += *buf++); + s2 += (s1 += *buf++); + + len -= 16; + } + + while (len--) { + s2 += (s1 += *buf++); + } + + if (s1 >= BASE) + s1 -= BASE; + s2 %= BASE; + } + + /* + * Return the recombined sums. + */ + return s1 | (s2 << 16); +} + +#endif /* ADLER32_SIMD_SSSE3 */ diff --git a/adler32_simd.h b/adler32_simd.h new file mode 100644 index 0000000..285e392 --- /dev/null +++ b/adler32_simd.h @@ -0,0 +1,37 @@ +/* adler32_simd.h + * + * (C) 1995-2013 Jean-loup Gailly and Mark Adler + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + * + * Jean-loup Gailly Mark Adler + * [email protected] [email protected] + * + * Copyright 2017 The Chromium Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the Chromium source repository LICENSE file. + */ + +#include <stdint.h> + +#include "zconf.h" +#include "zutil.h" + +uint32_t ZLIB_INTERNAL adler32_simd_( + uint32_t adler, + const unsigned char *buf, + unsigned long len); diff --git a/chunkcopy.h b/chunkcopy.h index 217da52..cf2b708 100644 --- a/chunkcopy.h +++ b/chunkcopy.h @@ -50,6 +50,11 @@ #if defined(INFLATE_CHUNK_SIMD_NEON) #include <arm_neon.h> typedef uint8x16_t z_vec128i_t; +#elif defined(INFLATE_CHUNK_SIMD_SSE2) +#include <emmintrin.h> +typedef __m128i z_vec128i_t; +#else +#error chunkcopy.h inflate chunk SIMD is not defined for your build target #endif /* @@ -66,7 +71,7 @@ Z_STATIC_ASSERT(vector_128_bits_wide, * instruction appropriate for the z_vec128i_t type. */ static inline z_vec128i_t loadchunk( - const unsigned char FAR* s) { + const unsigned char * s) { z_vec128i_t v; Z_BUILTIN_MEMCPY(&v, s, sizeof(v)); return v; @@ -77,7 +82,7 @@ static inline z_vec128i_t loadchunk( * instruction appropriate for the z_vec128i_t type. */ static inline void storechunk( - unsigned char FAR* d, + unsigned char * d, const z_vec128i_t v) { Z_BUILTIN_MEMCPY(d, &v, sizeof(v)); } @@ -96,9 +101,9 @@ static inline void storechunk( * without iteration, which will hopefully make the branch prediction more * reliable. */ -static inline unsigned char FAR* chunkcopy_core( - unsigned char FAR* out, - const unsigned char FAR* from, +static inline unsigned char * chunkcopy_core( + unsigned char * out, + const unsigned char * from, unsigned len) { const int bump = (--len % CHUNKCOPY_CHUNK_SIZE) + 1; storechunk(out, loadchunk(from)); @@ -121,14 +126,14 @@ static inline unsigned char FAR* chunkcopy_core( * output buffer is beyond the end of the current copy, and this can still be * exploited. */ -static inline unsigned char FAR* chunkcopy_core_safe( - unsigned char FAR* out, - const unsigned char FAR* from, +static inline unsigned char * chunkcopy_core_safe( + unsigned char * out, + const unsigned char * from, unsigned len, - unsigned char FAR* limit) { + unsigned char * limit) { Assert(out + len <= limit, "chunk copy exceeds safety limit"); if ((limit - out) < (ptrdiff_t)CHUNKCOPY_CHUNK_SIZE) { - const unsigned char FAR* Z_RESTRICT rfrom = from; + const unsigned char * Z_RESTRICT rfrom = from; if (len & 8) { Z_BUILTIN_MEMCPY(out, rfrom, 8); out += 8; @@ -162,11 +167,11 @@ static inline unsigned char FAR* chunkcopy_core_safe( * least 258 bytes of output space available (258 being the maximum length * output from a single token; see inffast.c). */ -static inline unsigned char FAR* chunkunroll_relaxed( - unsigned char FAR* out, - unsigned FAR* dist, - unsigned FAR* len) { - const unsigned char FAR* from = out - *dist; +static inline unsigned char * chunkunroll_relaxed( + unsigned char * out, + unsigned * dist, + unsigned * len) { + const unsigned char * from = out - *dist; while (*dist < *len && *dist < CHUNKCOPY_CHUNK_SIZE) { storechunk(out, loadchunk(from)); out += *dist; @@ -220,6 +225,52 @@ static inline z_vec128i_t v_load8_dup(const void* src) { static inline void v_store_128(void* out, const z_vec128i_t vec) { vst1q_u8(out, vec); } +#elif defined (INFLATE_CHUNK_SIMD_SSE2) +/* + * v_load64_dup(): load *src as an unaligned 64-bit int and duplicate it in + * every 64-bit component of the 128-bit result (64-bit int splat). + */ +static inline z_vec128i_t v_load64_dup(const void* src) { + int64_t i64; + Z_BUILTIN_MEMCPY(&i64, src, sizeof(i64)); + return _mm_set1_epi64x(i64); +} + +/* + * v_load32_dup(): load *src as an unaligned 32-bit int and duplicate it in + * every 32-bit component of the 128-bit result (32-bit int splat). + */ +static inline z_vec128i_t v_load32_dup(const void* src) { + int32_t i32; + Z_BUILTIN_MEMCPY(&i32, src, sizeof(i32)); + return _mm_set1_epi32(i32); +} + +/* + * v_load16_dup(): load *src as an unaligned 16-bit int and duplicate it in + * every 16-bit component of the 128-bit result (16-bit int splat). + */ +static inline z_vec128i_t v_load16_dup(const void* src) { + int16_t i16; + Z_BUILTIN_MEMCPY(&i16, src, sizeof(i16)); + return _mm_set1_epi16(i16); +} + +/* + * v_load8_dup(): load the 8-bit int *src and duplicate it in every 8-bit + * component of the 128-bit result (8-bit int splat). + */ +static inline z_vec128i_t v_load8_dup(const void* src) { + return _mm_set1_epi8(*(const char*)src); +} + +/* + * v_store_128(): store the 128-bit vec in a memory destination (that might + * not be 16-byte aligned) void* out. + */ +static inline void v_store_128(void* out, const z_vec128i_t vec) { + _mm_storeu_si128((__m128i*)out, vec); +} #endif /* @@ -228,8 +279,8 @@ static inline void v_store_128(void* out, const z_vec128i_t vec) { * that it's OK to overwrite at least CHUNKCOPY_CHUNK_SIZE*3 bytes of output * even if the length is shorter than this. */ -static inline unsigned char FAR* chunkset_core( - unsigned char FAR* out, +static inline unsigned char * chunkset_core( + unsigned char * out, unsigned period, unsigned len) { z_vec128i_t v; @@ -304,9 +355,9 @@ static inline unsigned char FAR* chunkset_core( * This is reflected in the `restrict`-qualified pointers, allowing the * compiler to re-order loads and stores. */ -static inline unsigned char FAR* chunkcopy_relaxed( - unsigned char FAR* Z_RESTRICT out, - const unsigned char FAR* Z_RESTRICT from, +static inline unsigned char * chunkcopy_relaxed( + unsigned char * Z_RESTRICT out, + const unsigned char * Z_RESTRICT from, unsigned len) { return chunkcopy_core(out, from, len); } @@ -324,11 +375,11 @@ static inline unsigned char FAR* chunkcopy_relaxed( * output buffer is beyond the end of the current copy, and this can still be * exploited. */ -static inline unsigned char FAR* chunkcopy_safe( - unsigned char FAR* out, - const unsigned char FAR* Z_RESTRICT from, +static inline unsigned char * chunkcopy_safe( + unsigned char * out, + const unsigned char * Z_RESTRICT from, unsigned len, - unsigned char FAR* limit) { + unsigned char * limit) { Assert(out + len <= limit, "chunk copy exceeds safety limit"); return chunkcopy_core_safe(out, from, len, limit); } @@ -340,8 +391,8 @@ static inline unsigned char FAR* chunkcopy_safe( * Assumes that len > 0 on entry, and that it's safe to write at least * CHUNKCOPY_CHUNK_SIZE*3 bytes to the output. */ -static inline unsigned char FAR* chunkcopy_lapped_relaxed( - unsigned char FAR* out, +static inline unsigned char * chunkcopy_lapped_relaxed( + unsigned char * out, unsigned dist, unsigned len) { if (dist < len && dist < CHUNKCOPY_CHUNK_SIZE) { @@ -359,11 +410,11 @@ static inline unsigned char FAR* chunkcopy_lapped_relaxed( * output buffer is beyond the end of the current copy, and this can still be * exploited. */ -static inline unsigned char FAR* chunkcopy_lapped_safe( - unsigned char FAR* out, +static inline unsigned char * chunkcopy_lapped_safe( + unsigned char * out, unsigned dist, unsigned len, - unsigned char FAR* limit) { + unsigned char * limit) { Assert(out + len <= limit, "chunk copy exceeds safety limit"); if ((limit - out) < (ptrdiff_t)(3 * CHUNKCOPY_CHUNK_SIZE)) { /* TODO(cavalcantii): try harder to optimise this */ @@ -394,7 +445,7 @@ typedef uint64_t inflate_holder_t; * Ask the compiler to perform a wide, unaligned load of a uint64_t using a * machine instruction appropriate for the uint64_t type. */ -static inline inflate_holder_t read64le(const unsigned char FAR *in) { +static inline inflate_holder_t read64le(const unsigned char *in) { inflate_holder_t input; Z_BUILTIN_MEMCPY(&input, in, sizeof(input)); return input; diff --git a/configure b/configure index e094818..5f0e9bf 100755 --- a/configure +++ b/configure @@ -570,6 +570,32 @@ fi rm -f conftest.c +# Enable SIMD inflate on architectures where the selected ISA is mandatory. +cat > conftest.c << EOF +#if !defined(__x86_64__) && !defined(__amd64__) +#error "not x86-64" +#endif +#include <emmintrin.h> +int main(void) { return _mm_cvtsi128_si32(_mm_setzero_si128()); } +EOF +if check c_compile "for x86-64 SSE2 inflate chunk copy" \ + "$CC" "$CFLAGS -msse2" "$CPPFLAGS" "$LDFLAGS" "$LIBS"; then + CFLAGS="$CFLAGS -msse2 -DINFLATE_CHUNK_SIMD_SSE2 -DINFLATE_CHUNK_READ_64LE" +fi + +cat > conftest.c << EOF +#if !defined(__aarch64__) +#error "not AArch64" +#endif +#include <arm_neon.h> +int main(void) { return vgetq_lane_u8(vdupq_n_u8(0), 0); } +EOF +if check c_compile "for AArch64 NEON inflate chunk copy" \ + "$CC" "$CFLAGS" "$CPPFLAGS" "$LDFLAGS" "$LIBS"; then + CFLAGS="$CFLAGS -DINFLATE_CHUNK_SIMD_NEON -DINFLATE_CHUNK_READ_64LE -DADLER32_SIMD_NEON" +fi +rm -f conftest.c + # # Determine which feature macros to pass. # diff --git a/inffast_chunk.c b/inffast_chunk.c index d80c9a7..39eff71 100644 --- a/inffast_chunk.c +++ b/inffast_chunk.c @@ -95,24 +95,24 @@ void ZLIB_INTERNAL inflate_fast_chunk_(strm, start) z_streamp strm; unsigned start; /* inflate()'s starting value for strm->avail_out */ { - struct inflate_state FAR *state; - z_const unsigned char FAR *in; /* local strm->next_in */ - z_const unsigned char FAR *last; /* have enough input while in < last */ - unsigned char FAR *out; /* local strm->next_out */ - unsigned char FAR *beg; /* inflate()'s initial strm->next_out */ - unsigned char FAR *end; /* while out < end, enough space available */ - unsigned char FAR *limit; /* safety limit for chunky copies */ + struct inflate_state *state; + z_const unsigned char *in; /* local strm->next_in */ + z_const unsigned char *last; /* have enough input while in < last */ + unsigned char *out; /* local strm->next_out */ + unsigned char *beg; /* inflate()'s initial strm->next_out */ + unsigned char *end; /* while out < end, enough space available */ + unsigned char *limit; /* safety limit for chunky copies */ #ifdef INFLATE_STRICT unsigned dmax; /* maximum distance from zlib header */ #endif unsigned wsize; /* window size or zero if not using window */ unsigned whave; /* valid bytes in the window */ unsigned wnext; /* window write index */ - unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */ + unsigned char *window; /* allocated sliding window, if wsize != 0 */ inflate_holder_t hold; /* local strm->hold */ unsigned bits; /* local strm->bits */ - code const FAR *lcode; /* local strm->lencode */ - code const FAR *dcode; /* local strm->distcode */ + code const *lcode; /* local strm->lencode */ + code const *dcode; /* local strm->distcode */ unsigned lmask; /* mask for first level of length codes */ unsigned dmask; /* mask for first level of distance codes */ code here; /* retrieved table entry */ @@ -120,10 +120,10 @@ unsigned start; /* inflate()'s starting value for strm->avail_out */ /* window position, window bytes to copy */ unsigned len; /* match length, unused bytes */ unsigned dist; /* match distance */ - unsigned char FAR *from; /* where to copy match from */ + unsigned char *from; /* where to copy match from */ /* copy state to local variables */ - state = (struct inflate_state FAR *)strm->state; + state = (struct inflate_state *)strm->state; in = strm->next_in; last = in + (strm->avail_in - (INFLATE_FAST_MIN_INPUT - 1)); out = strm->next_out; diff --git a/inffast_chunk.h b/inffast_chunk.h index de6aa0d..5d6025a 100644 --- a/inffast_chunk.h +++ b/inffast_chunk.h @@ -45,4 +45,4 @@ #define INFLATE_FAST_MIN_INPUT 8 #endif -void ZLIB_INTERNAL inflate_fast_chunk_ OF((z_streamp strm, unsigned start)); +void ZLIB_INTERNAL inflate_fast_chunk_(z_streamp strm, unsigned start); diff --git a/inflate.c b/inflate.c index 626488c..e622259 100644 --- a/inflate.c +++ b/inflate.c @@ -83,7 +83,7 @@ #include "zutil.h" #include "inftrees.h" #include "inflate.h" -#ifdef INFLATE_CHUNK_SIMD_NEON +#if defined(INFLATE_CHUNK_SIMD_NEON) || defined(INFLATE_CHUNK_SIMD_SSE2) #include "inffast_chunk.h" #include "chunkcopy.h" #else @@ -265,7 +265,7 @@ static int updatewindow(z_stream *strm, /* if it hasn't been done already, allocate space for the window */ if (state->window == NULL) { -#ifdef INFLATE_CHUNK_SIMD_NEON +#if defined(INFLATE_CHUNK_SIMD_NEON) || defined(INFLATE_CHUNK_SIMD_SSE2) unsigned int wsize = 1U << state->wbits; state->window = (unsigned char *) z_stream_alloc(strm, wsize + CHUNKCOPY_CHUNK_SIZE); @@ -891,7 +891,7 @@ int ZEXPORT inflate(z_stream *strm, if (have >= INFLATE_FAST_MIN_INPUT && left >= INFLATE_FAST_MIN_OUTPUT) { RESTORE(); -#ifdef INFLATE_CHUNK_SIMD_NEON +#if defined(INFLATE_CHUNK_SIMD_NEON) || defined(INFLATE_CHUNK_SIMD_SSE2) inflate_fast_chunk_(strm, out); #else inflate_fast(strm, out); @@ -1011,7 +1011,7 @@ int ZEXPORT inflate(z_stream *strm, else from = state->window + (state->wnext - copy); if (copy > state->length) copy = state->length; -#ifdef INFLATE_CHUNK_SIMD_NEON +#if defined(INFLATE_CHUNK_SIMD_NEON) || defined(INFLATE_CHUNK_SIMD_SSE2) if (copy > left) copy = left; put = chunkcopy_safe(put, from, copy, put + left); } @@ -1094,7 +1094,7 @@ int ZEXPORT inflate(z_stream *strm, Note: a memory error from inflate() is non-recoverable. */ inf_leave: -#ifdef INFLATE_CHUNK_SIMD_NEON +#if defined(INFLATE_CHUNK_SIMD_NEON) || defined(INFLATE_CHUNK_SIMD_SSE2) if (left >= CHUNKCOPY_CHUNK_SIZE) memset(put, 0x55, CHUNKCOPY_CHUNK_SIZE); else -- 2.55.0
