crc32_fast.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 = bswap32(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 = bswap32(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 three dispatch methods in order of priority:
  79. //
  80. // 1. Indirect function (ifunc). This method is slightly more efficient
  81. // than the constructor method because it will change the entry in the
  82. // Procedure Linkage Table (PLT) for the function either at load time or
  83. // at the first call. This avoids having to call the function through a
  84. // function pointer and will treat the function call like a regular call
  85. // through the PLT. ifuncs are created by using
  86. // __attribute__((__ifunc__("resolver"))) on a function which has no
  87. // body. The "resolver" is the name of the function that chooses at
  88. // runtime which implementation to use.
  89. //
  90. // 2. Constructor. This method uses __attribute__((__constructor__)) to
  91. // set crc32_func at load time. This avoids extra computation (and any
  92. // unlikely threading bugs) on the first call to lzma_crc32() to decide
  93. // which implementation should be used.
  94. //
  95. // 3. First Call Resolution. On the very first call to lzma_crc32(), the
  96. // call will be directed to crc32_dispatch() instead. This will set the
  97. // appropriate implementation function and will not be called again.
  98. // This method does not use any kind of locking but is safe because if
  99. // multiple threads run the dispatcher simultaneously then they will all
  100. // set crc32_func to the same value.
  101. typedef uint32_t (*crc32_func_type)(
  102. const uint8_t *buf, size_t size, uint32_t crc);
  103. // Clang 16.0.0 and older has a bug where it marks the ifunc resolver
  104. // function as unused since it is static and never used outside of
  105. // __attribute__((__ifunc__())).
  106. #if defined(CRC_USE_IFUNC) && defined(__clang__)
  107. # pragma GCC diagnostic push
  108. # pragma GCC diagnostic ignored "-Wunused-function"
  109. #endif
  110. // This resolver is shared between all three dispatch methods. It serves as
  111. // the ifunc resolver if ifunc is supported, otherwise it is called as a
  112. // regular function by the constructor or first call resolution methods.
  113. // The function attributes are needed for safe IFUNC resolver usage with GCC.
  114. lzma_resolver_attributes
  115. static crc32_func_type
  116. crc32_resolve(void)
  117. {
  118. return is_arch_extension_supported()
  119. ? &crc32_arch_optimized : &crc32_generic;
  120. }
  121. #if defined(CRC_USE_IFUNC) && defined(__clang__)
  122. # pragma GCC diagnostic pop
  123. #endif
  124. #ifndef CRC_USE_IFUNC
  125. #ifdef HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR
  126. // Constructor method.
  127. # define CRC32_SET_FUNC_ATTR __attribute__((__constructor__))
  128. static crc32_func_type crc32_func;
  129. #else
  130. // First Call Resolution method.
  131. # define CRC32_SET_FUNC_ATTR
  132. static uint32_t crc32_dispatch(const uint8_t *buf, size_t size, uint32_t crc);
  133. static crc32_func_type crc32_func = &crc32_dispatch;
  134. #endif
  135. CRC32_SET_FUNC_ATTR
  136. static void
  137. crc32_set_func(void)
  138. {
  139. crc32_func = crc32_resolve();
  140. return;
  141. }
  142. #ifndef HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR
  143. static uint32_t
  144. crc32_dispatch(const uint8_t *buf, size_t size, uint32_t crc)
  145. {
  146. // When __attribute__((__ifunc__(...))) and
  147. // __attribute__((__constructor__)) isn't supported, set the
  148. // function pointer without any locking. If multiple threads run
  149. // the detection code in parallel, they will all end up setting
  150. // the pointer to the same value. This avoids the use of
  151. // mythread_once() on every call to lzma_crc32() but this likely
  152. // isn't strictly standards compliant. Let's change it if it breaks.
  153. crc32_set_func();
  154. return crc32_func(buf, size, crc);
  155. }
  156. #endif
  157. #endif
  158. #endif
  159. #ifdef CRC_USE_IFUNC
  160. extern LZMA_API(uint32_t)
  161. lzma_crc32(const uint8_t *buf, size_t size, uint32_t crc)
  162. __attribute__((__ifunc__("crc32_resolve")));
  163. #else
  164. extern LZMA_API(uint32_t)
  165. lzma_crc32(const uint8_t *buf, size_t size, uint32_t crc)
  166. {
  167. #if defined(CRC32_GENERIC) && defined(CRC32_ARCH_OPTIMIZED)
  168. // On x86-64, if CLMUL is available, it is the best for non-tiny
  169. // inputs, being over twice as fast as the generic slice-by-four
  170. // version. However, for size <= 16 it's different. In the extreme
  171. // case of size == 1 the generic version can be five times faster.
  172. // At size >= 8 the CLMUL starts to become reasonable. It
  173. // varies depending on the alignment of buf too.
  174. //
  175. // The above doesn't include the overhead of mythread_once().
  176. // At least on x86-64 GNU/Linux, pthread_once() is very fast but
  177. // it still makes lzma_crc32(buf, 1, crc) 50-100 % slower. When
  178. // size reaches 12-16 bytes the overhead becomes negligible.
  179. //
  180. // So using the generic version for size <= 16 may give better
  181. // performance with tiny inputs but if such inputs happen rarely
  182. // it's not so obvious because then the lookup table of the
  183. // generic version may not be in the processor cache.
  184. #ifdef CRC_USE_GENERIC_FOR_SMALL_INPUTS
  185. if (size <= 16)
  186. return crc32_generic(buf, size, crc);
  187. #endif
  188. /*
  189. #ifndef HAVE_FUNC_ATTRIBUTE_CONSTRUCTOR
  190. // See crc32_dispatch(). This would be the alternative which uses
  191. // locking and doesn't use crc32_dispatch(). Note that on Windows
  192. // this method needs Vista threads.
  193. mythread_once(crc64_set_func);
  194. #endif
  195. */
  196. return crc32_func(buf, size, crc);
  197. #elif defined(CRC32_ARCH_OPTIMIZED)
  198. return crc32_arch_optimized(buf, size, crc);
  199. #else
  200. return crc32_generic(buf, size, crc);
  201. #endif
  202. }
  203. #endif