Debian Codegraph @

lib/x86/crc32_impl.h

raw
1 /*
2 * x86/crc32_impl.h - x86 implementations of the gzip CRC-32 algorithm
3 *
4 * Copyright 2016 Eric Biggers
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following
13 * conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28 #ifndef LIB_X86_CRC32_IMPL_H
29 #define LIB_X86_CRC32_IMPL_H
30
31 #include "cpu_features.h"
32
33 /*
34 * pshufb(x, shift_tab[len..len+15]) left shifts x by 16-len bytes.
35 * pshufb(x, shift_tab[len+16..len+31]) right shifts x by len bytes.
36 */
37 static const u8 MAYBE_UNUSED shift_tab[48] = {
38 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
39 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
40 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
41 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
42 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
43 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
44 };
45
46 #if defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER)
47 /*
48 * PCLMULQDQ implementation. This targets PCLMULQDQ+SSE4.1, since in practice
49 * all CPUs that support PCLMULQDQ also support SSE4.1.
50 */
51 # define crc32_x86_pclmulqdq crc32_x86_pclmulqdq
52 # define SUFFIX _pclmulqdq
53 # define ATTRIBUTES _target_attribute("pclmul,sse4.1")
54 # define VL 16
55 # define USE_AVX512 0
56 # include "crc32_pclmul_template.h"
57
58 /*
59 * PCLMULQDQ/AVX implementation. Same as above, but this is compiled with AVX
60 * enabled so that the compiler can generate VEX-coded instructions which can be
61 * slightly more efficient. It still uses 128-bit vectors.
62 */
63 # define crc32_x86_pclmulqdq_avx crc32_x86_pclmulqdq_avx
64 # define SUFFIX _pclmulqdq_avx
65 # define ATTRIBUTES _target_attribute("pclmul,avx")
66 # define VL 16
67 # define USE_AVX512 0
68 # include "crc32_pclmul_template.h"
69 #endif
70
71 /*
72 * VPCLMULQDQ/AVX2 implementation. This is used on CPUs that have AVX2 and
73 * VPCLMULQDQ but don't have AVX-512, for example Intel Alder Lake.
74 *
75 * Currently this can't be enabled with MSVC because MSVC has a bug where it
76 * incorrectly assumes that VPCLMULQDQ implies AVX-512:
77 * https://developercommunity.visualstudio.com/t/Compiler-incorrectly-assumes-VAES-and-VP/10578785
78 *
79 * gcc 8.1 and 8.2 had a similar bug where they assumed that
80 * _mm256_clmulepi64_epi128() always needed AVX512. It's fixed in gcc 8.3.
81 *
82 * _mm256_zextsi128_si256() requires gcc 10.
83 */
84 #if (GCC_PREREQ(10, 1) || CLANG_PREREQ(6, 0, 10000000)) && \
85 !defined(LIBDEFLATE_ASSEMBLER_DOES_NOT_SUPPORT_VPCLMULQDQ)
86 # define crc32_x86_vpclmulqdq_avx2 crc32_x86_vpclmulqdq_avx2
87 # define SUFFIX _vpclmulqdq_avx2
88 # define ATTRIBUTES _target_attribute("vpclmulqdq,pclmul,avx2")
89 # define VL 32
90 # define USE_AVX512 0
91 # include "crc32_pclmul_template.h"
92 #endif
93
94 #if (GCC_PREREQ(10, 1) || CLANG_PREREQ(6, 0, 10000000) || MSVC_PREREQ(1920)) && \
95 !defined(LIBDEFLATE_ASSEMBLER_DOES_NOT_SUPPORT_VPCLMULQDQ)
96 /*
97 * VPCLMULQDQ/AVX512 implementation using 256-bit vectors. This is very similar
98 * to the VPCLMULQDQ/AVX2 implementation but takes advantage of the vpternlog
99 * instruction and more registers. This is used on certain older Intel CPUs,
100 * specifically Ice Lake and Tiger Lake, which support VPCLMULQDQ and AVX512 but
101 * downclock a bit too eagerly when ZMM registers are used.
102 *
103 * _mm256_zextsi128_si256() requires gcc 10.
104 */
105 # define crc32_x86_vpclmulqdq_avx512_vl256 crc32_x86_vpclmulqdq_avx512_vl256
106 # define SUFFIX _vpclmulqdq_avx512_vl256
107 # define ATTRIBUTES _target_attribute("vpclmulqdq,pclmul,avx512bw,avx512vl")
108 # define VL 32
109 # define USE_AVX512 1
110 # include "crc32_pclmul_template.h"
111
112 /*
113 * VPCLMULQDQ/AVX512 implementation using 512-bit vectors. This is used on CPUs
114 * that have a good AVX-512 implementation including VPCLMULQDQ.
115 *
116 * _mm512_zextsi128_si512() requires gcc 10.
117 */
118 # define crc32_x86_vpclmulqdq_avx512_vl512 crc32_x86_vpclmulqdq_avx512_vl512
119 # define SUFFIX _vpclmulqdq_avx512_vl512
120 # define ATTRIBUTES _target_attribute("vpclmulqdq,pclmul,avx512bw,avx512vl")
121 # define VL 64
122 # define USE_AVX512 1
123 # include "crc32_pclmul_template.h"
124 #endif
125
126 static inline crc32_func_t
127 arch_select_crc32_func(void)
128 {
129 const u32 features MAYBE_UNUSED = get_x86_cpu_features();
130
131 #ifdef crc32_x86_vpclmulqdq_avx512_vl512
132 if ((features & X86_CPU_FEATURE_ZMM) &&
133 HAVE_VPCLMULQDQ(features) && HAVE_PCLMULQDQ(features) &&
134 HAVE_AVX512BW(features) && HAVE_AVX512VL(features))
135 return crc32_x86_vpclmulqdq_avx512_vl512;
136 #endif
137 #ifdef crc32_x86_vpclmulqdq_avx512_vl256
138 if (HAVE_VPCLMULQDQ(features) && HAVE_PCLMULQDQ(features) &&
139 HAVE_AVX512BW(features) && HAVE_AVX512VL(features))
140 return crc32_x86_vpclmulqdq_avx512_vl256;
141 #endif
142 #ifdef crc32_x86_vpclmulqdq_avx2
143 if (HAVE_VPCLMULQDQ(features) && HAVE_PCLMULQDQ(features) &&
144 HAVE_AVX2(features))
145 return crc32_x86_vpclmulqdq_avx2;
146 #endif
147 #ifdef crc32_x86_pclmulqdq_avx
148 if (HAVE_PCLMULQDQ(features) && HAVE_AVX(features))
149 return crc32_x86_pclmulqdq_avx;
150 #endif
151 #ifdef crc32_x86_pclmulqdq
152 if (HAVE_PCLMULQDQ(features))
153 return crc32_x86_pclmulqdq;
154 #endif
155 return NULL;
156 }
157 #define arch_select_crc32_func arch_select_crc32_func
158
159 #endif /* LIB_X86_CRC32_IMPL_H */