diff options
author | Jia Tan <jiat0218@gmail.com> | 2023-10-14 12:17:57 +0800 |
---|---|---|
committer | Jia Tan <jiat0218@gmail.com> | 2023-10-18 23:54:36 +0800 |
commit | 8c0f9376f58c0696d5d6719705164d35542dd891 (patch) | |
tree | 104f3471d2ecd77b25314e67fdef743fcecbe193 /src/liblzma/check/crc32_fast.c | |
parent | liblzma: Define CRC_USE_IFUNC in crc_common.h. (diff) | |
download | xz-8c0f9376f58c0696d5d6719705164d35542dd891.tar.xz |
liblzma: Create crc_clmul.c.
Both crc32_clmul() and crc64_clmul() are now exported from
crc32_clmul.c as lzma_crc32_clmul() and lzma_crc64_clmul(). This
ensures that is_clmul_supported() (now lzma_is_clmul_supported()) is
not duplicated between crc32_fast.c and crc64_fast.c.
Also, it encapsulates the complexity of the CLMUL implementations into a
single file and reduces the complexity of crc32_fast.c and crc64_fast.c.
Before, CLMUL code was present in crc32_fast.c, crc64_fast.c, and
crc_common.h.
During the conversion, various cleanups were applied to code (thanks to
Lasse Collin) including:
- Require using semicolons with MASK_/L/H/LH macros.
- Variable typing and const handling improvements.
- Improvements to comments.
- Fixes to the pragmas used.
- Removed unneeded variables.
- Whitespace improvements.
- Fixed CRC_USE_GENERIC_FOR_SMALL_INPUTS handling.
- Silenced warnings and removed the need for some #pragmas
Diffstat (limited to 'src/liblzma/check/crc32_fast.c')
-rw-r--r-- | src/liblzma/check/crc32_fast.c | 120 |
1 files changed, 4 insertions, 116 deletions
diff --git a/src/liblzma/check/crc32_fast.c b/src/liblzma/check/crc32_fast.c index 3ac9b6d7..8849a476 100644 --- a/src/liblzma/check/crc32_fast.c +++ b/src/liblzma/check/crc32_fast.c @@ -34,11 +34,11 @@ #include "check.h" #include "crc_common.h" +#ifdef CRC_GENERIC + /////////////////// // Generic CRC32 // /////////////////// -#ifdef CRC_GENERIC - static uint32_t crc32_generic(const uint8_t *buf, size_t size, uint32_t crc) @@ -99,118 +99,6 @@ crc32_generic(const uint8_t *buf, size_t size, uint32_t crc) } #endif - -///////////////////// -// x86 CLMUL CRC32 // -///////////////////// - -#ifdef CRC_CLMUL - -#include <immintrin.h> - - -/* -// These functions were used to generate the constants -// at the top of crc32_clmul(). -static uint64_t -calc_lo(uint64_t p, uint64_t a, int n) -{ - uint64_t b = 0; int i; - for (i = 0; i < n; i++) { - b = b >> 1 | (a & 1) << (n - 1); - a = (a >> 1) ^ ((0 - (a & 1)) & p); - } - return b; -} - -// same as ~crc(&a, sizeof(a), ~0) -static uint64_t -calc_hi(uint64_t p, uint64_t a, int n) -{ - int i; - for (i = 0; i < n; i++) - a = (a >> 1) ^ ((0 - (a & 1)) & p); - return a; -} -*/ - - -// MSVC (VS2015 - VS2022) produces bad 32-bit x86 code from the CLMUL CRC -// code when optimizations are enabled (release build). According to the bug -// report, the ebx register is corrupted and the calculated result is wrong. -// Trying to workaround the problem with "__asm mov ebx, ebx" didn't help. -// The following pragma works and performance is still good. x86-64 builds -// aren't affected by this problem. -// -// NOTE: Another pragma after the function restores the optimizations. -// If the #if condition here is updated, the other one must be updated too. -#if defined(_MSC_VER) && !defined(__INTEL_COMPILER) && !defined(__clang__) \ - && defined(_M_IX86) -# pragma optimize("g", off) -#endif - -// EDG-based compilers (Intel's classic compiler and compiler for E2K) can -// define __GNUC__ but the attribute must not be used with them. -// The new Clang-based ICX needs the attribute. -// -// NOTE: Build systems check for this too, keep them in sync with this. -#if (defined(__GNUC__) || defined(__clang__)) && !defined(__EDG__) -__attribute__((__target__("ssse3,sse4.1,pclmul"))) -#endif -static uint32_t -crc32_clmul(const uint8_t *buf, size_t size, uint32_t crc) -{ - // The prototypes of the intrinsics use signed types while most of - // the values are treated as unsigned here. These warnings in this - // function have been checked and found to be harmless so silence them. -#if TUKLIB_GNUC_REQ(4, 6) || defined(__clang__) -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wsign-conversion" -# pragma GCC diagnostic ignored "-Wconversion" -#endif - -#ifndef CRC_USE_GENERIC_FOR_SMALL_INPUTS - // The code assumes that there is at least one byte of input. - if (size == 0) - return crc; -#endif - - // uint32_t poly = 0xedb88320; - uint64_t p = 0x1db710640; // p << 1 - uint64_t mu = 0x1f7011641; // calc_lo(p, p, 32) << 1 | 1 - uint64_t k5 = 0x163cd6124; // calc_hi(p, p, 32) << 1 - uint64_t k4 = 0x0ccaa009e; // calc_hi(p, p, 64) << 1 - uint64_t k3 = 0x1751997d0; // calc_hi(p, p, 128) << 1 - - __m128i vfold4 = _mm_set_epi64x(mu, p); - __m128i vfold8 = _mm_set_epi64x(0, k5); - __m128i vfold16 = _mm_set_epi64x(k4, k3); - - __m128i v0, v1, v2; - - crc_simd_body(buf, size, &v0, &v1, vfold16, _mm_cvtsi32_si128(~crc)); - - v1 = _mm_xor_si128( - _mm_clmulepi64_si128(v0, vfold16, 0x10), v1); // xxx0 - v2 = _mm_shuffle_epi32(v1, 0xe7); // 0xx0 - v0 = _mm_slli_epi64(v1, 32); // [0] - v0 = _mm_clmulepi64_si128(v0, vfold8, 0x00); - v0 = _mm_xor_si128(v0, v2); // [1] [2] - v2 = _mm_clmulepi64_si128(v0, vfold4, 0x10); - v2 = _mm_clmulepi64_si128(v2, vfold4, 0x00); - v0 = _mm_xor_si128(v0, v2); // [2] - return ~_mm_extract_epi32(v0, 2); - -#if TUKLIB_GNUC_REQ(4, 6) || defined(__clang__) -# pragma GCC diagnostic pop -#endif -} -#if defined(_MSC_VER) && !defined(__INTEL_COMPILER) && !defined(__clang__) \ - && defined(_M_IX86) -# pragma optimize("", on) -#endif -#endif - #if defined(CRC_GENERIC) && defined(CRC_CLMUL) typedef uint32_t (*crc32_func_type)( const uint8_t *buf, size_t size, uint32_t crc); @@ -226,7 +114,7 @@ typedef uint32_t (*crc32_func_type)( static crc32_func_type crc32_resolve(void) { - return is_clmul_supported() ? &crc32_clmul : &crc32_generic; + return lzma_is_clmul_supported() ? &lzma_crc32_clmul : &crc32_generic; } #if defined(HAVE_FUNC_ATTRIBUTE_IFUNC) && defined(__clang__) @@ -305,7 +193,7 @@ lzma_crc32(const uint8_t *buf, size_t size, uint32_t crc) return crc32_func(buf, size, crc); #elif defined(CRC_CLMUL) - return crc32_clmul(buf, size, crc); + return lzma_crc32_clmul(buf, size, crc); #else return crc32_generic(buf, size, crc); |