Path: blob/master/Utilities/cmliblzma/liblzma/common/memcmplen.h
3153 views
// SPDX-License-Identifier: 0BSD12///////////////////////////////////////////////////////////////////////////////3//4/// \file memcmplen.h5/// \brief Optimized comparison of two buffers6//7// Author: Lasse Collin8//9///////////////////////////////////////////////////////////////////////////////1011#ifndef LZMA_MEMCMPLEN_H12#define LZMA_MEMCMPLEN_H1314#include "common.h"1516#ifdef HAVE_IMMINTRIN_H17# include <immintrin.h>18#endif1920// Only include <intrin.h> if it is needed. The header is only needed21// on Windows when using an MSVC compatible compiler. The Intel compiler22// can use the intrinsics without the header file.23#if defined(TUKLIB_FAST_UNALIGNED_ACCESS) \24&& defined(_MSC_VER) \25&& (defined(_M_X64) \26|| defined(_M_ARM64) || defined(_M_ARM64EC)) \27&& !defined(__INTEL_COMPILER)28# include <intrin.h>29#endif303132/// Find out how many equal bytes the two buffers have.33///34/// \param buf1 First buffer35/// \param buf2 Second buffer36/// \param len How many bytes have already been compared and will37/// be assumed to match38/// \param limit How many bytes to compare at most, including the39/// already-compared bytes. This must be significantly40/// smaller than UINT32_MAX to avoid integer overflows.41/// Up to LZMA_MEMCMPLEN_EXTRA bytes may be read past42/// the specified limit from both buf1 and buf2.43///44/// \return Number of equal bytes in the buffers is returned.45/// This is always at least len and at most limit.46///47/// \note LZMA_MEMCMPLEN_EXTRA defines how many extra bytes may be read.48/// It's rounded up to 2^n. This extra amount needs to be49/// allocated in the buffers being used. It needs to be50/// initialized too to keep Valgrind quiet.51static lzma_always_inline uint32_t52lzma_memcmplen(const uint8_t *buf1, const uint8_t *buf2,53uint32_t len, uint32_t limit)54{55assert(len <= limit);56assert(limit <= UINT32_MAX / 2);5758#if defined(TUKLIB_FAST_UNALIGNED_ACCESS) \59&& (((TUKLIB_GNUC_REQ(3, 4) || defined(__clang__)) \60&& (defined(__x86_64__) \61|| defined(__aarch64__))) \62|| (defined(__INTEL_COMPILER) && defined(__x86_64__)) \63|| (defined(__INTEL_COMPILER) && defined(_M_X64)) \64|| (defined(_MSC_VER) && (defined(_M_X64) \65|| defined(_M_ARM64) || defined(_M_ARM64EC))))66// This is only for x86-64 and ARM64 for now. This might be fine on67// other 64-bit processors too. On big endian one should use xor68// instead of subtraction and switch to __builtin_clzll().69//70// Reasons to use subtraction instead of xor:71//72// - On some x86-64 processors (Intel Sandy Bridge to Tiger Lake),73// sub+jz and sub+jnz can be fused but xor+jz or xor+jnz cannot.74// Thus using subtraction has potential to be a tiny amount faster75// since the code checks if the quotient is non-zero.76//77// - Some processors (Intel Pentium 4) used to have more ALU78// resources for add/sub instructions than and/or/xor.79//80// The processor info is based on Agner Fog's microarchitecture.pdf81// version 2023-05-26. https://www.agner.org/optimize/82#define LZMA_MEMCMPLEN_EXTRA 883while (len < limit) {84const uint64_t x = read64ne(buf1 + len) - read64ne(buf2 + len);85if (x != 0) {86// MSVC or Intel C compiler on Windows87# if defined(_MSC_VER) || defined(__INTEL_COMPILER)88unsigned long tmp;89_BitScanForward64(&tmp, x);90len += (uint32_t)tmp >> 3;91// GCC, Clang, or Intel C compiler92# else93len += (uint32_t)__builtin_ctzll(x) >> 3;94# endif95return my_min(len, limit);96}9798len += 8;99}100101return limit;102103#elif defined(TUKLIB_FAST_UNALIGNED_ACCESS) \104&& defined(HAVE__MM_MOVEMASK_EPI8) \105&& (defined(__SSE2__) \106|| (defined(_MSC_VER) && defined(_M_IX86_FP) \107&& _M_IX86_FP >= 2))108// NOTE: This will use 128-bit unaligned access which109// TUKLIB_FAST_UNALIGNED_ACCESS wasn't meant to permit,110// but it's convenient here since this is x86-only.111//112// SSE2 version for 32-bit and 64-bit x86. On x86-64 the above113// version is sometimes significantly faster and sometimes114// slightly slower than this SSE2 version, so this SSE2115// version isn't used on x86-64.116# define LZMA_MEMCMPLEN_EXTRA 16117while (len < limit) {118const uint32_t x = 0xFFFF ^ (uint32_t)_mm_movemask_epi8(119_mm_cmpeq_epi8(120_mm_loadu_si128((const __m128i *)(buf1 + len)),121_mm_loadu_si128((const __m128i *)(buf2 + len))));122123if (x != 0) {124len += ctz32(x);125return my_min(len, limit);126}127128len += 16;129}130131return limit;132133#elif defined(TUKLIB_FAST_UNALIGNED_ACCESS) && !defined(WORDS_BIGENDIAN)134// Generic 32-bit little endian method135# define LZMA_MEMCMPLEN_EXTRA 4136while (len < limit) {137uint32_t x = read32ne(buf1 + len) - read32ne(buf2 + len);138if (x != 0) {139if ((x & 0xFFFF) == 0) {140len += 2;141x >>= 16;142}143144if ((x & 0xFF) == 0)145++len;146147return my_min(len, limit);148}149150len += 4;151}152153return limit;154155#elif defined(TUKLIB_FAST_UNALIGNED_ACCESS) && defined(WORDS_BIGENDIAN)156// Generic 32-bit big endian method157# define LZMA_MEMCMPLEN_EXTRA 4158while (len < limit) {159uint32_t x = read32ne(buf1 + len) ^ read32ne(buf2 + len);160if (x != 0) {161if ((x & 0xFFFF0000) == 0) {162len += 2;163x <<= 16;164}165166if ((x & 0xFF000000) == 0)167++len;168169return my_min(len, limit);170}171172len += 4;173}174175return limit;176177#else178// Simple portable version that doesn't use unaligned access.179# define LZMA_MEMCMPLEN_EXTRA 0180while (len < limit && buf1[len] == buf2[len])181++len;182183return len;184#endif185}186187#endif188189190