xref: /freebsd/contrib/xz/src/liblzma/common/memcmplen.h (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
1 // SPDX-License-Identifier: 0BSD
2 
3 ///////////////////////////////////////////////////////////////////////////////
4 //
5 /// \file       memcmplen.h
6 /// \brief      Optimized comparison of two buffers
7 //
8 //  Author:     Lasse Collin
9 //
10 ///////////////////////////////////////////////////////////////////////////////
11 
12 #ifndef LZMA_MEMCMPLEN_H
13 #define LZMA_MEMCMPLEN_H
14 
15 #include "common.h"
16 
17 #ifdef HAVE_IMMINTRIN_H
18 #	include <immintrin.h>
19 #endif
20 
21 // Only include <intrin.h> if it is needed. The header is only needed
22 // on Windows when using an MSVC compatible compiler. The Intel compiler
23 // can use the intrinsics without the header file.
24 #if defined(TUKLIB_FAST_UNALIGNED_ACCESS) \
25 		&& defined(_MSC_VER) \
26 		&& (defined(_M_X64) \
27 			|| defined(_M_ARM64) || defined(_M_ARM64EC)) \
28 		&& !defined(__INTEL_COMPILER)
29 #	include <intrin.h>
30 #endif
31 
32 
33 /// Find out how many equal bytes the two buffers have.
34 ///
35 /// \param      buf1    First buffer
36 /// \param      buf2    Second buffer
37 /// \param      len     How many bytes have already been compared and will
38 ///                     be assumed to match
39 /// \param      limit   How many bytes to compare at most, including the
40 ///                     already-compared bytes. This must be significantly
41 ///                     smaller than UINT32_MAX to avoid integer overflows.
42 ///                     Up to LZMA_MEMCMPLEN_EXTRA bytes may be read past
43 ///                     the specified limit from both buf1 and buf2.
44 ///
45 /// \return     Number of equal bytes in the buffers is returned.
46 ///             This is always at least len and at most limit.
47 ///
48 /// \note       LZMA_MEMCMPLEN_EXTRA defines how many extra bytes may be read.
49 ///             It's rounded up to 2^n. This extra amount needs to be
50 ///             allocated in the buffers being used. It needs to be
51 ///             initialized too to keep Valgrind quiet.
52 static lzma_always_inline uint32_t
53 lzma_memcmplen(const uint8_t *buf1, const uint8_t *buf2,
54 		uint32_t len, uint32_t limit)
55 {
56 	assert(len <= limit);
57 	assert(limit <= UINT32_MAX / 2);
58 
59 #if defined(TUKLIB_FAST_UNALIGNED_ACCESS) \
60 		&& (((TUKLIB_GNUC_REQ(3, 4) || defined(__clang__)) \
61 				&& (defined(__x86_64__) \
62 					|| defined(__aarch64__))) \
63 			|| (defined(__INTEL_COMPILER) && defined(__x86_64__)) \
64 			|| (defined(__INTEL_COMPILER) && defined(_M_X64)) \
65 			|| (defined(_MSC_VER) && (defined(_M_X64) \
66 				|| defined(_M_ARM64) || defined(_M_ARM64EC))))
67 	// This is only for x86-64 and ARM64 for now. This might be fine on
68 	// other 64-bit processors too. On big endian one should use xor
69 	// instead of subtraction and switch to __builtin_clzll().
70 #define LZMA_MEMCMPLEN_EXTRA 8
71 	while (len < limit) {
72 		const uint64_t x = read64ne(buf1 + len) - read64ne(buf2 + len);
73 		if (x != 0) {
74 	// MSVC or Intel C compiler on Windows
75 #	if defined(_MSC_VER) || defined(__INTEL_COMPILER)
76 			unsigned long tmp;
77 			_BitScanForward64(&tmp, x);
78 			len += (uint32_t)tmp >> 3;
79 	// GCC, Clang, or Intel C compiler
80 #	else
81 			len += (uint32_t)__builtin_ctzll(x) >> 3;
82 #	endif
83 			return my_min(len, limit);
84 		}
85 
86 		len += 8;
87 	}
88 
89 	return limit;
90 
91 #elif defined(TUKLIB_FAST_UNALIGNED_ACCESS) \
92 		&& defined(HAVE__MM_MOVEMASK_EPI8) \
93 		&& (defined(__SSE2__) \
94 			|| (defined(_MSC_VER) && defined(_M_IX86_FP) \
95 				&& _M_IX86_FP >= 2))
96 	// NOTE: This will use 128-bit unaligned access which
97 	// TUKLIB_FAST_UNALIGNED_ACCESS wasn't meant to permit,
98 	// but it's convenient here since this is x86-only.
99 	//
100 	// SSE2 version for 32-bit and 64-bit x86. On x86-64 the above
101 	// version is sometimes significantly faster and sometimes
102 	// slightly slower than this SSE2 version, so this SSE2
103 	// version isn't used on x86-64.
104 #	define LZMA_MEMCMPLEN_EXTRA 16
105 	while (len < limit) {
106 		const uint32_t x = 0xFFFF ^ (uint32_t)_mm_movemask_epi8(
107 			_mm_cmpeq_epi8(
108 			_mm_loadu_si128((const __m128i *)(buf1 + len)),
109 			_mm_loadu_si128((const __m128i *)(buf2 + len))));
110 
111 		if (x != 0) {
112 			len += ctz32(x);
113 			return my_min(len, limit);
114 		}
115 
116 		len += 16;
117 	}
118 
119 	return limit;
120 
121 #elif defined(TUKLIB_FAST_UNALIGNED_ACCESS) && !defined(WORDS_BIGENDIAN)
122 	// Generic 32-bit little endian method
123 #	define LZMA_MEMCMPLEN_EXTRA 4
124 	while (len < limit) {
125 		uint32_t x = read32ne(buf1 + len) - read32ne(buf2 + len);
126 		if (x != 0) {
127 			if ((x & 0xFFFF) == 0) {
128 				len += 2;
129 				x >>= 16;
130 			}
131 
132 			if ((x & 0xFF) == 0)
133 				++len;
134 
135 			return my_min(len, limit);
136 		}
137 
138 		len += 4;
139 	}
140 
141 	return limit;
142 
143 #elif defined(TUKLIB_FAST_UNALIGNED_ACCESS) && defined(WORDS_BIGENDIAN)
144 	// Generic 32-bit big endian method
145 #	define LZMA_MEMCMPLEN_EXTRA 4
146 	while (len < limit) {
147 		uint32_t x = read32ne(buf1 + len) ^ read32ne(buf2 + len);
148 		if (x != 0) {
149 			if ((x & 0xFFFF0000) == 0) {
150 				len += 2;
151 				x <<= 16;
152 			}
153 
154 			if ((x & 0xFF000000) == 0)
155 				++len;
156 
157 			return my_min(len, limit);
158 		}
159 
160 		len += 4;
161 	}
162 
163 	return limit;
164 
165 #else
166 	// Simple portable version that doesn't use unaligned access.
167 #	define LZMA_MEMCMPLEN_EXTRA 0
168 	while (len < limit && buf1[len] == buf2[len])
169 		++len;
170 
171 	return len;
172 #endif
173 }
174 
175 #endif
176