2014-07-25 17:57:20 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file memcmplen.h
|
|
|
|
/// \brief Optimized comparison of two buffers
|
|
|
|
//
|
|
|
|
// Author: Lasse Collin
|
|
|
|
//
|
|
|
|
// This file has been put into the public domain.
|
|
|
|
// You can do whatever you want with this file.
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef LZMA_MEMCMPLEN_H
|
|
|
|
#define LZMA_MEMCMPLEN_H
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_IMMINTRIN_H
|
|
|
|
# include <immintrin.h>
|
|
|
|
#endif
|
|
|
|
|
2023-04-19 14:22:16 +00:00
|
|
|
// Only include <intrin.h> if it is needed. The header is only needed
|
|
|
|
// on Windows when using an MSVC compatible compiler. The Intel compiler
|
|
|
|
// can use the intrinsics without the header file.
|
|
|
|
#if defined(TUKLIB_FAST_UNALIGNED_ACCESS) \
|
2023-07-18 10:49:43 +00:00
|
|
|
&& defined(_MSC_VER) \
|
2023-12-20 19:15:16 +00:00
|
|
|
&& (defined(_M_X64) \
|
|
|
|
|| defined(_M_ARM64) || defined(_M_ARM64EC)) \
|
2023-07-18 10:49:43 +00:00
|
|
|
&& !defined(__INTEL_COMPILER)
|
2023-04-19 14:22:16 +00:00
|
|
|
# include <intrin.h>
|
|
|
|
#endif
|
|
|
|
|
2014-07-25 17:57:20 +00:00
|
|
|
|
|
|
|
/// Find out how many equal bytes the two buffers have.
|
|
|
|
///
|
|
|
|
/// \param buf1 First buffer
|
|
|
|
/// \param buf2 Second buffer
|
|
|
|
/// \param len How many bytes have already been compared and will
|
|
|
|
/// be assumed to match
|
|
|
|
/// \param limit How many bytes to compare at most, including the
|
|
|
|
/// already-compared bytes. This must be significantly
|
|
|
|
/// smaller than UINT32_MAX to avoid integer overflows.
|
|
|
|
/// Up to LZMA_MEMCMPLEN_EXTRA bytes may be read past
|
|
|
|
/// the specified limit from both buf1 and buf2.
|
|
|
|
///
|
|
|
|
/// \return Number of equal bytes in the buffers is returned.
|
|
|
|
/// This is always at least len and at most limit.
|
2015-01-26 19:24:39 +00:00
|
|
|
///
|
|
|
|
/// \note LZMA_MEMCMPLEN_EXTRA defines how many extra bytes may be read.
|
|
|
|
/// It's rounded up to 2^n. This extra amount needs to be
|
|
|
|
/// allocated in the buffers being used. It needs to be
|
|
|
|
/// initialized too to keep Valgrind quiet.
|
2023-10-22 14:59:11 +00:00
|
|
|
static lzma_always_inline uint32_t
|
2014-07-25 17:57:20 +00:00
|
|
|
lzma_memcmplen(const uint8_t *buf1, const uint8_t *buf2,
|
|
|
|
uint32_t len, uint32_t limit)
|
|
|
|
{
|
|
|
|
assert(len <= limit);
|
|
|
|
assert(limit <= UINT32_MAX / 2);
|
|
|
|
|
|
|
|
#if defined(TUKLIB_FAST_UNALIGNED_ACCESS) \
|
2023-12-20 19:01:06 +00:00
|
|
|
&& (((TUKLIB_GNUC_REQ(3, 4) || defined(__clang__)) \
|
2023-12-20 19:15:16 +00:00
|
|
|
&& (defined(__x86_64__) \
|
|
|
|
|| defined(__aarch64__))) \
|
2014-07-25 17:57:20 +00:00
|
|
|
|| (defined(__INTEL_COMPILER) && defined(__x86_64__)) \
|
|
|
|
|| (defined(__INTEL_COMPILER) && defined(_M_X64)) \
|
2023-12-20 19:15:16 +00:00
|
|
|
|| (defined(_MSC_VER) && (defined(_M_X64) \
|
|
|
|
|| defined(_M_ARM64) || defined(_M_ARM64EC))))
|
|
|
|
// This is only for x86-64 and ARM64 for now. This might be fine on
|
|
|
|
// other 64-bit processors too. On big endian one should use xor
|
|
|
|
// instead of subtraction and switch to __builtin_clzll().
|
2015-01-26 19:24:39 +00:00
|
|
|
#define LZMA_MEMCMPLEN_EXTRA 8
|
2014-07-25 17:57:20 +00:00
|
|
|
while (len < limit) {
|
2019-12-30 22:41:28 +00:00
|
|
|
const uint64_t x = read64ne(buf1 + len) - read64ne(buf2 + len);
|
2014-07-25 17:57:20 +00:00
|
|
|
if (x != 0) {
|
2023-06-28 12:22:38 +00:00
|
|
|
// MSVC or Intel C compiler on Windows
|
2023-12-20 19:15:16 +00:00
|
|
|
# if defined(_MSC_VER) || defined(__INTEL_COMPILER)
|
2014-07-25 17:57:20 +00:00
|
|
|
unsigned long tmp;
|
|
|
|
_BitScanForward64(&tmp, x);
|
|
|
|
len += (uint32_t)tmp >> 3;
|
2023-07-18 10:57:54 +00:00
|
|
|
// GCC, Clang, or Intel C compiler
|
2023-06-28 12:22:38 +00:00
|
|
|
# else
|
2014-07-25 17:57:20 +00:00
|
|
|
len += (uint32_t)__builtin_ctzll(x) >> 3;
|
|
|
|
# endif
|
|
|
|
return my_min(len, limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
len += 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
return limit;
|
|
|
|
|
|
|
|
#elif defined(TUKLIB_FAST_UNALIGNED_ACCESS) \
|
|
|
|
&& defined(HAVE__MM_MOVEMASK_EPI8) \
|
2022-11-11 12:35:58 +00:00
|
|
|
&& (defined(__SSE2__) \
|
2014-07-25 17:57:20 +00:00
|
|
|
|| (defined(_MSC_VER) && defined(_M_IX86_FP) \
|
|
|
|
&& _M_IX86_FP >= 2))
|
2022-10-05 11:26:00 +00:00
|
|
|
// NOTE: This will use 128-bit unaligned access which
|
|
|
|
// TUKLIB_FAST_UNALIGNED_ACCESS wasn't meant to permit,
|
|
|
|
// but it's convenient here since this is x86-only.
|
2014-07-25 17:57:20 +00:00
|
|
|
//
|
|
|
|
// SSE2 version for 32-bit and 64-bit x86. On x86-64 the above
|
|
|
|
// version is sometimes significantly faster and sometimes
|
|
|
|
// slightly slower than this SSE2 version, so this SSE2
|
|
|
|
// version isn't used on x86-64.
|
2015-01-26 19:24:39 +00:00
|
|
|
# define LZMA_MEMCMPLEN_EXTRA 16
|
2014-07-25 17:57:20 +00:00
|
|
|
while (len < limit) {
|
2023-03-19 20:45:59 +00:00
|
|
|
const uint32_t x = 0xFFFF ^ (uint32_t)_mm_movemask_epi8(
|
|
|
|
_mm_cmpeq_epi8(
|
2014-07-25 17:57:20 +00:00
|
|
|
_mm_loadu_si128((const __m128i *)(buf1 + len)),
|
|
|
|
_mm_loadu_si128((const __m128i *)(buf2 + len))));
|
|
|
|
|
|
|
|
if (x != 0) {
|
2019-06-01 18:36:13 +00:00
|
|
|
len += ctz32(x);
|
2014-07-25 17:57:20 +00:00
|
|
|
return my_min(len, limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
len += 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
return limit;
|
|
|
|
|
|
|
|
#elif defined(TUKLIB_FAST_UNALIGNED_ACCESS) && !defined(WORDS_BIGENDIAN)
|
|
|
|
// Generic 32-bit little endian method
|
2015-01-26 19:24:39 +00:00
|
|
|
# define LZMA_MEMCMPLEN_EXTRA 4
|
2014-07-25 17:57:20 +00:00
|
|
|
while (len < limit) {
|
2019-12-30 22:41:28 +00:00
|
|
|
uint32_t x = read32ne(buf1 + len) - read32ne(buf2 + len);
|
2014-07-25 17:57:20 +00:00
|
|
|
if (x != 0) {
|
|
|
|
if ((x & 0xFFFF) == 0) {
|
|
|
|
len += 2;
|
|
|
|
x >>= 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((x & 0xFF) == 0)
|
|
|
|
++len;
|
|
|
|
|
|
|
|
return my_min(len, limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
len += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
return limit;
|
|
|
|
|
|
|
|
#elif defined(TUKLIB_FAST_UNALIGNED_ACCESS) && defined(WORDS_BIGENDIAN)
|
|
|
|
// Generic 32-bit big endian method
|
2015-01-26 19:24:39 +00:00
|
|
|
# define LZMA_MEMCMPLEN_EXTRA 4
|
2014-07-25 17:57:20 +00:00
|
|
|
while (len < limit) {
|
2019-12-30 22:41:28 +00:00
|
|
|
uint32_t x = read32ne(buf1 + len) ^ read32ne(buf2 + len);
|
2014-07-25 17:57:20 +00:00
|
|
|
if (x != 0) {
|
|
|
|
if ((x & 0xFFFF0000) == 0) {
|
|
|
|
len += 2;
|
|
|
|
x <<= 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((x & 0xFF000000) == 0)
|
|
|
|
++len;
|
|
|
|
|
|
|
|
return my_min(len, limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
len += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
return limit;
|
|
|
|
|
|
|
|
#else
|
|
|
|
// Simple portable version that doesn't use unaligned access.
|
2015-01-26 19:24:39 +00:00
|
|
|
# define LZMA_MEMCMPLEN_EXTRA 0
|
2014-07-25 17:57:20 +00:00
|
|
|
while (len < limit && buf1[len] == buf2[len])
|
|
|
|
++len;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|