memcmplen.h 4.9 KB

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