xref: /freebsd/contrib/xz/src/liblzma/common/memcmplen.h (revision 3b35e7ee8de9b0260149a2b77e87a2b9c7a36244)
1*3b35e7eeSXin LI // SPDX-License-Identifier: 0BSD
2*3b35e7eeSXin LI 
353200025SRui Paulo ///////////////////////////////////////////////////////////////////////////////
453200025SRui Paulo //
553200025SRui Paulo /// \file       memcmplen.h
653200025SRui Paulo /// \brief      Optimized comparison of two buffers
753200025SRui Paulo //
853200025SRui Paulo //  Author:     Lasse Collin
953200025SRui Paulo //
1053200025SRui Paulo ///////////////////////////////////////////////////////////////////////////////
1153200025SRui Paulo 
1253200025SRui Paulo #ifndef LZMA_MEMCMPLEN_H
1353200025SRui Paulo #define LZMA_MEMCMPLEN_H
1453200025SRui Paulo 
1553200025SRui Paulo #include "common.h"
1653200025SRui Paulo 
1753200025SRui Paulo #ifdef HAVE_IMMINTRIN_H
1853200025SRui Paulo #	include <immintrin.h>
1953200025SRui Paulo #endif
2053200025SRui Paulo 
21b333cd44SXin LI // Only include <intrin.h> if it is needed. The header is only needed
22b333cd44SXin LI // on Windows when using an MSVC compatible compiler. The Intel compiler
23b333cd44SXin LI // can use the intrinsics without the header file.
24b333cd44SXin LI #if defined(TUKLIB_FAST_UNALIGNED_ACCESS) \
251f3ced26SXin LI 		&& defined(_MSC_VER) \
26*3b35e7eeSXin LI 		&& (defined(_M_X64) \
27*3b35e7eeSXin LI 			|| defined(_M_ARM64) || defined(_M_ARM64EC)) \
281f3ced26SXin LI 		&& !defined(__INTEL_COMPILER)
29b333cd44SXin LI #	include <intrin.h>
30b333cd44SXin LI #endif
31b333cd44SXin LI 
3253200025SRui Paulo 
3353200025SRui Paulo /// Find out how many equal bytes the two buffers have.
3453200025SRui Paulo ///
3553200025SRui Paulo /// \param      buf1    First buffer
3653200025SRui Paulo /// \param      buf2    Second buffer
3753200025SRui Paulo /// \param      len     How many bytes have already been compared and will
3853200025SRui Paulo ///                     be assumed to match
3953200025SRui Paulo /// \param      limit   How many bytes to compare at most, including the
4053200025SRui Paulo ///                     already-compared bytes. This must be significantly
4153200025SRui Paulo ///                     smaller than UINT32_MAX to avoid integer overflows.
4253200025SRui Paulo ///                     Up to LZMA_MEMCMPLEN_EXTRA bytes may be read past
4353200025SRui Paulo ///                     the specified limit from both buf1 and buf2.
4453200025SRui Paulo ///
4553200025SRui Paulo /// \return     Number of equal bytes in the buffers is returned.
4653200025SRui Paulo ///             This is always at least len and at most limit.
47342bcb12SXin LI ///
48342bcb12SXin LI /// \note       LZMA_MEMCMPLEN_EXTRA defines how many extra bytes may be read.
49342bcb12SXin LI ///             It's rounded up to 2^n. This extra amount needs to be
50342bcb12SXin LI ///             allocated in the buffers being used. It needs to be
51342bcb12SXin LI ///             initialized too to keep Valgrind quiet.
52ca6a6373SXin LI static lzma_always_inline uint32_t
5353200025SRui Paulo lzma_memcmplen(const uint8_t *buf1, const uint8_t *buf2,
5453200025SRui Paulo 		uint32_t len, uint32_t limit)
5553200025SRui Paulo {
5653200025SRui Paulo 	assert(len <= limit);
5753200025SRui Paulo 	assert(limit <= UINT32_MAX / 2);
5853200025SRui Paulo 
5953200025SRui Paulo #if defined(TUKLIB_FAST_UNALIGNED_ACCESS) \
60*3b35e7eeSXin LI 		&& (((TUKLIB_GNUC_REQ(3, 4) || defined(__clang__)) \
61*3b35e7eeSXin LI 				&& (defined(__x86_64__) \
62*3b35e7eeSXin LI 					|| defined(__aarch64__))) \
6353200025SRui Paulo 			|| (defined(__INTEL_COMPILER) && defined(__x86_64__)) \
6453200025SRui Paulo 			|| (defined(__INTEL_COMPILER) && defined(_M_X64)) \
65*3b35e7eeSXin LI 			|| (defined(_MSC_VER) && (defined(_M_X64) \
66*3b35e7eeSXin LI 				|| defined(_M_ARM64) || defined(_M_ARM64EC))))
67*3b35e7eeSXin LI 	// This is only for x86-64 and ARM64 for now. This might be fine on
68*3b35e7eeSXin LI 	// other 64-bit processors too. On big endian one should use xor
69*3b35e7eeSXin LI 	// instead of subtraction and switch to __builtin_clzll().
70*3b35e7eeSXin LI 	//
71*3b35e7eeSXin LI 	// Reasons to use subtraction instead of xor:
72*3b35e7eeSXin LI 	//
73*3b35e7eeSXin LI 	//   - On some x86-64 processors (Intel Sandy Bridge to Tiger Lake),
74*3b35e7eeSXin LI 	//     sub+jz and sub+jnz can be fused but xor+jz or xor+jnz cannot.
75*3b35e7eeSXin LI 	//     Thus using subtraction has potential to be a tiny amount faster
76*3b35e7eeSXin LI 	//     since the code checks if the quotient is non-zero.
77*3b35e7eeSXin LI 	//
78*3b35e7eeSXin LI 	//   - Some processors (Intel Pentium 4) used to have more ALU
79*3b35e7eeSXin LI 	//     resources for add/sub instructions than and/or/xor.
80*3b35e7eeSXin LI 	//
81*3b35e7eeSXin LI 	// The processor info is based on Agner Fog's microarchitecture.pdf
82*3b35e7eeSXin LI 	// version 2023-05-26. https://www.agner.org/optimize/
83342bcb12SXin LI #define LZMA_MEMCMPLEN_EXTRA 8
8453200025SRui Paulo 	while (len < limit) {
85a8675d92SXin LI 		const uint64_t x = read64ne(buf1 + len) - read64ne(buf2 + len);
8653200025SRui Paulo 		if (x != 0) {
871f3ced26SXin LI 	// MSVC or Intel C compiler on Windows
88*3b35e7eeSXin LI #	if defined(_MSC_VER) || defined(__INTEL_COMPILER)
8953200025SRui Paulo 			unsigned long tmp;
9053200025SRui Paulo 			_BitScanForward64(&tmp, x);
9153200025SRui Paulo 			len += (uint32_t)tmp >> 3;
921f3ced26SXin LI 	// GCC, Clang, or Intel C compiler
931f3ced26SXin LI #	else
9453200025SRui Paulo 			len += (uint32_t)__builtin_ctzll(x) >> 3;
9553200025SRui Paulo #	endif
9653200025SRui Paulo 			return my_min(len, limit);
9753200025SRui Paulo 		}
9853200025SRui Paulo 
9953200025SRui Paulo 		len += 8;
10053200025SRui Paulo 	}
10153200025SRui Paulo 
10253200025SRui Paulo 	return limit;
10353200025SRui Paulo 
10453200025SRui Paulo #elif defined(TUKLIB_FAST_UNALIGNED_ACCESS) \
10553200025SRui Paulo 		&& defined(HAVE__MM_MOVEMASK_EPI8) \
1069e6bbe47SXin LI 		&& (defined(__SSE2__) \
10753200025SRui Paulo 			|| (defined(_MSC_VER) && defined(_M_IX86_FP) \
10853200025SRui Paulo 				&& _M_IX86_FP >= 2))
10973ed8e77SXin LI 	// NOTE: This will use 128-bit unaligned access which
11073ed8e77SXin LI 	// TUKLIB_FAST_UNALIGNED_ACCESS wasn't meant to permit,
11173ed8e77SXin LI 	// but it's convenient here since this is x86-only.
11253200025SRui Paulo 	//
11353200025SRui Paulo 	// SSE2 version for 32-bit and 64-bit x86. On x86-64 the above
11453200025SRui Paulo 	// version is sometimes significantly faster and sometimes
11553200025SRui Paulo 	// slightly slower than this SSE2 version, so this SSE2
11653200025SRui Paulo 	// version isn't used on x86-64.
117342bcb12SXin LI #	define LZMA_MEMCMPLEN_EXTRA 16
11853200025SRui Paulo 	while (len < limit) {
119b333cd44SXin LI 		const uint32_t x = 0xFFFF ^ (uint32_t)_mm_movemask_epi8(
120b333cd44SXin LI 			_mm_cmpeq_epi8(
12153200025SRui Paulo 			_mm_loadu_si128((const __m128i *)(buf1 + len)),
12253200025SRui Paulo 			_mm_loadu_si128((const __m128i *)(buf2 + len))));
12353200025SRui Paulo 
12453200025SRui Paulo 		if (x != 0) {
125a8675d92SXin LI 			len += ctz32(x);
12653200025SRui Paulo 			return my_min(len, limit);
12753200025SRui Paulo 		}
12853200025SRui Paulo 
12953200025SRui Paulo 		len += 16;
13053200025SRui Paulo 	}
13153200025SRui Paulo 
13253200025SRui Paulo 	return limit;
13353200025SRui Paulo 
13453200025SRui Paulo #elif defined(TUKLIB_FAST_UNALIGNED_ACCESS) && !defined(WORDS_BIGENDIAN)
13553200025SRui Paulo 	// Generic 32-bit little endian method
136342bcb12SXin LI #	define LZMA_MEMCMPLEN_EXTRA 4
13753200025SRui Paulo 	while (len < limit) {
138a8675d92SXin LI 		uint32_t x = read32ne(buf1 + len) - read32ne(buf2 + len);
13953200025SRui Paulo 		if (x != 0) {
14053200025SRui Paulo 			if ((x & 0xFFFF) == 0) {
14153200025SRui Paulo 				len += 2;
14253200025SRui Paulo 				x >>= 16;
14353200025SRui Paulo 			}
14453200025SRui Paulo 
14553200025SRui Paulo 			if ((x & 0xFF) == 0)
14653200025SRui Paulo 				++len;
14753200025SRui Paulo 
14853200025SRui Paulo 			return my_min(len, limit);
14953200025SRui Paulo 		}
15053200025SRui Paulo 
15153200025SRui Paulo 		len += 4;
15253200025SRui Paulo 	}
15353200025SRui Paulo 
15453200025SRui Paulo 	return limit;
15553200025SRui Paulo 
15653200025SRui Paulo #elif defined(TUKLIB_FAST_UNALIGNED_ACCESS) && defined(WORDS_BIGENDIAN)
15753200025SRui Paulo 	// Generic 32-bit big endian method
158342bcb12SXin LI #	define LZMA_MEMCMPLEN_EXTRA 4
15953200025SRui Paulo 	while (len < limit) {
160a8675d92SXin LI 		uint32_t x = read32ne(buf1 + len) ^ read32ne(buf2 + len);
16153200025SRui Paulo 		if (x != 0) {
16253200025SRui Paulo 			if ((x & 0xFFFF0000) == 0) {
16353200025SRui Paulo 				len += 2;
16453200025SRui Paulo 				x <<= 16;
16553200025SRui Paulo 			}
16653200025SRui Paulo 
16753200025SRui Paulo 			if ((x & 0xFF000000) == 0)
16853200025SRui Paulo 				++len;
16953200025SRui Paulo 
17053200025SRui Paulo 			return my_min(len, limit);
17153200025SRui Paulo 		}
17253200025SRui Paulo 
17353200025SRui Paulo 		len += 4;
17453200025SRui Paulo 	}
17553200025SRui Paulo 
17653200025SRui Paulo 	return limit;
17753200025SRui Paulo 
17853200025SRui Paulo #else
17953200025SRui Paulo 	// Simple portable version that doesn't use unaligned access.
180342bcb12SXin LI #	define LZMA_MEMCMPLEN_EXTRA 0
18153200025SRui Paulo 	while (len < limit && buf1[len] == buf2[len])
18253200025SRui Paulo 		++len;
18353200025SRui Paulo 
18453200025SRui Paulo 	return len;
18553200025SRui Paulo #endif
18653200025SRui Paulo }
18753200025SRui Paulo 
18853200025SRui Paulo #endif
189