memcmplen.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. On big endian one should use xor
  61. // instead of subtraction and switch to __builtin_clzll().
  62. //
  63. // Reasons to use subtraction instead of xor:
  64. //
  65. // - On some x86-64 processors (Intel Sandy Bridge to Tiger Lake),
  66. // sub+jz and sub+jnz can be fused but xor+jz or xor+jnz cannot.
  67. // Thus using subtraction has potential to be a tiny amount faster
  68. // since the code checks if the quotient is non-zero.
  69. //
  70. // - Some processors (Intel Pentium 4) used to have more ALU
  71. // resources for add/sub instructions than and/or/xor.
  72. //
  73. // The processor info is based on Agner Fog's microarchitecture.pdf
  74. // version 2023-05-26. https://www.agner.org/optimize/
  75. #define LZMA_MEMCMPLEN_EXTRA 8
  76. while (len < limit) {
  77. const uint64_t x = read64ne(buf1 + len) - read64ne(buf2 + len);
  78. if (x != 0) {
  79. // MSVC or Intel C compiler on Windows
  80. # if defined(_MSC_VER) || defined(__INTEL_COMPILER)
  81. unsigned long tmp;
  82. _BitScanForward64(&tmp, x);
  83. len += (uint32_t)tmp >> 3;
  84. // GCC, Clang, or Intel C compiler
  85. # else
  86. len += (uint32_t)__builtin_ctzll(x) >> 3;
  87. # endif
  88. return my_min(len, limit);
  89. }
  90. len += 8;
  91. }
  92. return limit;
  93. #elif defined(TUKLIB_FAST_UNALIGNED_ACCESS) \
  94. && defined(HAVE__MM_MOVEMASK_EPI8) \
  95. && (defined(__SSE2__) \
  96. || (defined(_MSC_VER) && defined(_M_IX86_FP) \
  97. && _M_IX86_FP >= 2))
  98. // NOTE: This will use 128-bit unaligned access which
  99. // TUKLIB_FAST_UNALIGNED_ACCESS wasn't meant to permit,
  100. // but it's convenient here since this is x86-only.
  101. //
  102. // SSE2 version for 32-bit and 64-bit x86. On x86-64 the above
  103. // version is sometimes significantly faster and sometimes
  104. // slightly slower than this SSE2 version, so this SSE2
  105. // version isn't used on x86-64.
  106. # define LZMA_MEMCMPLEN_EXTRA 16
  107. while (len < limit) {
  108. const uint32_t x = 0xFFFF ^ (uint32_t)_mm_movemask_epi8(
  109. _mm_cmpeq_epi8(
  110. _mm_loadu_si128((const __m128i *)(buf1 + len)),
  111. _mm_loadu_si128((const __m128i *)(buf2 + len))));
  112. if (x != 0) {
  113. len += ctz32(x);
  114. return my_min(len, limit);
  115. }
  116. len += 16;
  117. }
  118. return limit;
  119. #elif defined(TUKLIB_FAST_UNALIGNED_ACCESS) && !defined(WORDS_BIGENDIAN)
  120. // Generic 32-bit little endian method
  121. # define LZMA_MEMCMPLEN_EXTRA 4
  122. while (len < limit) {
  123. uint32_t x = read32ne(buf1 + len) - read32ne(buf2 + len);
  124. if (x != 0) {
  125. if ((x & 0xFFFF) == 0) {
  126. len += 2;
  127. x >>= 16;
  128. }
  129. if ((x & 0xFF) == 0)
  130. ++len;
  131. return my_min(len, limit);
  132. }
  133. len += 4;
  134. }
  135. return limit;
  136. #elif defined(TUKLIB_FAST_UNALIGNED_ACCESS) && defined(WORDS_BIGENDIAN)
  137. // Generic 32-bit big endian method
  138. # define LZMA_MEMCMPLEN_EXTRA 4
  139. while (len < limit) {
  140. uint32_t x = read32ne(buf1 + len) ^ read32ne(buf2 + len);
  141. if (x != 0) {
  142. if ((x & 0xFFFF0000) == 0) {
  143. len += 2;
  144. x <<= 16;
  145. }
  146. if ((x & 0xFF000000) == 0)
  147. ++len;
  148. return my_min(len, limit);
  149. }
  150. len += 4;
  151. }
  152. return limit;
  153. #else
  154. // Simple portable version that doesn't use unaligned access.
  155. # define LZMA_MEMCMPLEN_EXTRA 0
  156. while (len < limit && buf1[len] == buf2[len])
  157. ++len;
  158. return len;
  159. #endif
  160. }
  161. #endif