memcmplen.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #define LZMA_MEMCMPLEN_EXTRA 8
  63. while (len < limit) {
  64. const uint64_t x = read64ne(buf1 + len) - read64ne(buf2 + len);
  65. if (x != 0) {
  66. // MSVC or Intel C compiler on Windows
  67. # if defined(_MSC_VER) || defined(__INTEL_COMPILER)
  68. unsigned long tmp;
  69. _BitScanForward64(&tmp, x);
  70. len += (uint32_t)tmp >> 3;
  71. // GCC, Clang, or Intel C compiler
  72. # else
  73. len += (uint32_t)__builtin_ctzll(x) >> 3;
  74. # endif
  75. return my_min(len, limit);
  76. }
  77. len += 8;
  78. }
  79. return limit;
  80. #elif defined(TUKLIB_FAST_UNALIGNED_ACCESS) \
  81. && defined(HAVE__MM_MOVEMASK_EPI8) \
  82. && (defined(__SSE2__) \
  83. || (defined(_MSC_VER) && defined(_M_IX86_FP) \
  84. && _M_IX86_FP >= 2))
  85. // NOTE: This will use 128-bit unaligned access which
  86. // TUKLIB_FAST_UNALIGNED_ACCESS wasn't meant to permit,
  87. // but it's convenient here since this is x86-only.
  88. //
  89. // SSE2 version for 32-bit and 64-bit x86. On x86-64 the above
  90. // version is sometimes significantly faster and sometimes
  91. // slightly slower than this SSE2 version, so this SSE2
  92. // version isn't used on x86-64.
  93. # define LZMA_MEMCMPLEN_EXTRA 16
  94. while (len < limit) {
  95. const uint32_t x = 0xFFFF ^ (uint32_t)_mm_movemask_epi8(
  96. _mm_cmpeq_epi8(
  97. _mm_loadu_si128((const __m128i *)(buf1 + len)),
  98. _mm_loadu_si128((const __m128i *)(buf2 + len))));
  99. if (x != 0) {
  100. len += ctz32(x);
  101. return my_min(len, limit);
  102. }
  103. len += 16;
  104. }
  105. return limit;
  106. #elif defined(TUKLIB_FAST_UNALIGNED_ACCESS) && !defined(WORDS_BIGENDIAN)
  107. // Generic 32-bit little endian method
  108. # define LZMA_MEMCMPLEN_EXTRA 4
  109. while (len < limit) {
  110. uint32_t x = read32ne(buf1 + len) - read32ne(buf2 + len);
  111. if (x != 0) {
  112. if ((x & 0xFFFF) == 0) {
  113. len += 2;
  114. x >>= 16;
  115. }
  116. if ((x & 0xFF) == 0)
  117. ++len;
  118. return my_min(len, limit);
  119. }
  120. len += 4;
  121. }
  122. return limit;
  123. #elif defined(TUKLIB_FAST_UNALIGNED_ACCESS) && defined(WORDS_BIGENDIAN)
  124. // Generic 32-bit big endian method
  125. # define LZMA_MEMCMPLEN_EXTRA 4
  126. while (len < limit) {
  127. uint32_t x = read32ne(buf1 + len) ^ read32ne(buf2 + len);
  128. if (x != 0) {
  129. if ((x & 0xFFFF0000) == 0) {
  130. len += 2;
  131. x <<= 16;
  132. }
  133. if ((x & 0xFF000000) == 0)
  134. ++len;
  135. return my_min(len, limit);
  136. }
  137. len += 4;
  138. }
  139. return limit;
  140. #else
  141. // Simple portable version that doesn't use unaligned access.
  142. # define LZMA_MEMCMPLEN_EXTRA 0
  143. while (len < limit && buf1[len] == buf2[len])
  144. ++len;
  145. return len;
  146. #endif
  147. }
  148. #endif