crc32_fast.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // SPDX-License-Identifier: 0BSD
  2. ///////////////////////////////////////////////////////////////////////////////
  3. //
  4. /// \file crc32.c
  5. /// \brief CRC32 calculation
  6. //
  7. // Authors: Lasse Collin
  8. // Ilya Kurdyukov
  9. // Hans Jansen
  10. //
  11. ///////////////////////////////////////////////////////////////////////////////
  12. #include "check.h"
  13. #include "crc_common.h"
  14. #if defined(CRC_X86_CLMUL)
  15. # define BUILDING_CRC32_CLMUL
  16. # include "crc_x86_clmul.h"
  17. #elif defined(CRC32_ARM64)
  18. # error #include "crc32_arm64.h"
  19. #endif
  20. #ifdef CRC32_GENERIC
  21. ///////////////////
  22. // Generic CRC32 //
  23. ///////////////////
  24. static uint32_t
  25. crc32_generic(const uint8_t *buf, size_t size, uint32_t crc)
  26. {
  27. crc = ~crc;
  28. #ifdef WORDS_BIGENDIAN
  29. crc = byteswap32(crc);
  30. #endif
  31. if (size > 8) {
  32. // Fix the alignment, if needed. The if statement above
  33. // ensures that this won't read past the end of buf[].
  34. while ((uintptr_t)(buf) & 7) {
  35. crc = lzma_crc32_table[0][*buf++ ^ A(crc)] ^ S8(crc);
  36. --size;
  37. }
  38. // Calculate the position where to stop.
  39. const uint8_t *const limit = buf + (size & ~(size_t)(7));
  40. // Calculate how many bytes must be calculated separately
  41. // before returning the result.
  42. size &= (size_t)(7);
  43. // Calculate the CRC32 using the slice-by-eight algorithm.
  44. while (buf < limit) {
  45. crc ^= aligned_read32ne(buf);
  46. buf += 4;
  47. crc = lzma_crc32_table[7][A(crc)]
  48. ^ lzma_crc32_table[6][B(crc)]
  49. ^ lzma_crc32_table[5][C(crc)]
  50. ^ lzma_crc32_table[4][D(crc)];
  51. const uint32_t tmp = aligned_read32ne(buf);
  52. buf += 4;
  53. // At least with some compilers, it is critical for
  54. // performance, that the crc variable is XORed
  55. // between the two table-lookup pairs.
  56. crc = lzma_crc32_table[3][A(tmp)]
  57. ^ lzma_crc32_table[2][B(tmp)]
  58. ^ crc
  59. ^ lzma_crc32_table[1][C(tmp)]
  60. ^ lzma_crc32_table[0][D(tmp)];
  61. }
  62. }
  63. while (size-- != 0)
  64. crc = lzma_crc32_table[0][*buf++ ^ A(crc)] ^ S8(crc);
  65. #ifdef WORDS_BIGENDIAN
  66. crc = byteswap32(crc);
  67. #endif
  68. return ~crc;
  69. }
  70. #endif
  71. #if defined(CRC32_GENERIC) && defined(CRC32_ARCH_OPTIMIZED)
  72. //////////////////////////
  73. // Function dispatching //
  74. //////////////////////////
  75. // If both the generic and arch-optimized implementations are built, then
  76. // the function to use is selected at runtime because the system running
  77. // the binary might not have the arch-specific instruction set extension(s)
  78. // available. The dispatch methods in order of priority:
  79. //
  80. // 1. Constructor. This method uses __attribute__((__constructor__)) to
  81. // set crc32_func at load time. This avoids extra computation (and any
  82. // unlikely threading bugs) on the first call to lzma_crc32() to decide
  83. // which implementation should be used.
  84. //
  85. // 2. First Call Resolution. On the very first call to lzma_crc32(), the
  86. // call will be directed to crc32_dispatch() instead. This will set the
  87. // appropriate implementation function and will not be called again.
  88. // This method does not use any kind of locking but is safe because if
  89. // multiple threads run the dispatcher simultaneously then they will all
  90. // set crc32_func to the same value.
  91. typedef uint32_t (*crc32_func_type)(
  92. const uint8_t *buf, size_t size, uint32_t crc);
  93. // This resolver is shared between all dispatch methods.
  94. static crc32_func_type
  95. crc32_resolve(void)
  96. {
  97. return is_arch_extension_supported()
  98. ? &crc32_arch_optimized : &crc32_generic;
  99. }
  100. #ifdef HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR
  101. // Constructor method.
  102. # define CRC32_SET_FUNC_ATTR __attribute__((__constructor__))
  103. static crc32_func_type crc32_func;
  104. #else
  105. // First Call Resolution method.
  106. # define CRC32_SET_FUNC_ATTR
  107. static uint32_t crc32_dispatch(const uint8_t *buf, size_t size, uint32_t crc);
  108. static crc32_func_type crc32_func = &crc32_dispatch;
  109. #endif
  110. CRC32_SET_FUNC_ATTR
  111. static void
  112. crc32_set_func(void)
  113. {
  114. crc32_func = crc32_resolve();
  115. return;
  116. }
  117. #ifndef HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR
  118. static uint32_t
  119. crc32_dispatch(const uint8_t *buf, size_t size, uint32_t crc)
  120. {
  121. // When __attribute__((__constructor__)) isn't supported, set the
  122. // function pointer without any locking. If multiple threads run
  123. // the detection code in parallel, they will all end up setting
  124. // the pointer to the same value. This avoids the use of
  125. // mythread_once() on every call to lzma_crc32() but this likely
  126. // isn't strictly standards compliant. Let's change it if it breaks.
  127. crc32_set_func();
  128. return crc32_func(buf, size, crc);
  129. }
  130. #endif
  131. #endif
  132. extern LZMA_API(uint32_t)
  133. lzma_crc32(const uint8_t *buf, size_t size, uint32_t crc)
  134. {
  135. #if defined(CRC32_GENERIC) && defined(CRC32_ARCH_OPTIMIZED)
  136. // On x86-64, if CLMUL is available, it is the best for non-tiny
  137. // inputs, being over twice as fast as the generic slice-by-four
  138. // version. However, for size <= 16 it's different. In the extreme
  139. // case of size == 1 the generic version can be five times faster.
  140. // At size >= 8 the CLMUL starts to become reasonable. It
  141. // varies depending on the alignment of buf too.
  142. //
  143. // The above doesn't include the overhead of mythread_once().
  144. // At least on x86-64 GNU/Linux, pthread_once() is very fast but
  145. // it still makes lzma_crc32(buf, 1, crc) 50-100 % slower. When
  146. // size reaches 12-16 bytes the overhead becomes negligible.
  147. //
  148. // So using the generic version for size <= 16 may give better
  149. // performance with tiny inputs but if such inputs happen rarely
  150. // it's not so obvious because then the lookup table of the
  151. // generic version may not be in the processor cache.
  152. #ifdef CRC_USE_GENERIC_FOR_SMALL_INPUTS
  153. if (size <= 16)
  154. return crc32_generic(buf, size, crc);
  155. #endif
  156. /*
  157. #ifndef HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR
  158. // See crc32_dispatch(). This would be the alternative which uses
  159. // locking and doesn't use crc32_dispatch(). Note that on Windows
  160. // this method needs Vista threads.
  161. mythread_once(crc64_set_func);
  162. #endif
  163. */
  164. return crc32_func(buf, size, crc);
  165. #elif defined(CRC32_ARCH_OPTIMIZED)
  166. return crc32_arch_optimized(buf, size, crc);
  167. #else
  168. return crc32_generic(buf, size, crc);
  169. #endif
  170. }