memcmplen.h 5.6 KB

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