DPDK  23.07.0
rte_thash_x86_gfni.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2021 Intel Corporation
3  */
4 
5 #ifndef _RTE_THASH_X86_GFNI_H_
6 #define _RTE_THASH_X86_GFNI_H_
7 
15 #include <rte_compat.h>
16 #include <rte_vect.h>
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 #if defined(__GFNI__) && defined(__AVX512F__)
23 #define RTE_THASH_GFNI_DEFINED
24 
25 #define RTE_THASH_FIRST_ITER_MSK 0x0f0f0f0f0f0e0c08
26 #define RTE_THASH_PERM_MSK 0x0f0f0f0f0f0f0f0f
27 #define RTE_THASH_FIRST_ITER_MSK_2 0xf0f0f0f0f0e0c080
28 #define RTE_THASH_PERM_MSK_2 0xf0f0f0f0f0f0f0f0
29 #define RTE_THASH_REWIND_MSK 0x0000000000113377
30 
31 __rte_internal
32 static inline void
33 __rte_thash_xor_reduce(__m512i xor_acc, uint32_t *val_1, uint32_t *val_2)
34 {
35  __m256i tmp_256_1, tmp_256_2;
36  __m128i tmp128_1, tmp128_2;
37 
38  tmp_256_1 = _mm512_castsi512_si256(xor_acc);
39  tmp_256_2 = _mm512_extracti32x8_epi32(xor_acc, 1);
40  tmp_256_1 = _mm256_xor_si256(tmp_256_1, tmp_256_2);
41 
42  tmp128_1 = _mm256_castsi256_si128(tmp_256_1);
43  tmp128_2 = _mm256_extracti32x4_epi32(tmp_256_1, 1);
44  tmp128_1 = _mm_xor_si128(tmp128_1, tmp128_2);
45 
46 #ifdef RTE_ARCH_X86_64
47  uint64_t tmp_1, tmp_2;
48  tmp_1 = _mm_extract_epi64(tmp128_1, 0);
49  tmp_2 = _mm_extract_epi64(tmp128_1, 1);
50  tmp_1 ^= tmp_2;
51 
52  *val_1 = (uint32_t)tmp_1;
53  *val_2 = (uint32_t)(tmp_1 >> 32);
54 #else
55  uint32_t tmp_1, tmp_2;
56  tmp_1 = _mm_extract_epi32(tmp128_1, 0);
57  tmp_2 = _mm_extract_epi32(tmp128_1, 1);
58  tmp_1 ^= _mm_extract_epi32(tmp128_1, 2);
59  tmp_2 ^= _mm_extract_epi32(tmp128_1, 3);
60 
61  *val_1 = tmp_1;
62  *val_2 = tmp_2;
63 #endif
64 }
65 
66 __rte_internal
67 static inline __m512i
68 __rte_thash_gfni(const uint64_t *mtrx, const uint8_t *tuple,
69  const uint8_t *secondary_tuple, int len)
70 {
71  __m512i permute_idx = _mm512_set_epi32(0x07060504, 0x07060504,
72  0x06050403, 0x06050403,
73  0x05040302, 0x05040302,
74  0x04030201, 0x04030201,
75  0x03020100, 0x03020100,
76  0x020100FF, 0x020100FF,
77  0x0100FFFE, 0x0100FFFE,
78  0x00FFFEFD, 0x00FFFEFD);
79  const __m512i rewind_idx = _mm512_set_epi32(0x00000000, 0x00000000,
80  0x00000000, 0x00000000,
81  0x00000000, 0x00000000,
82  0x00000000, 0x00000000,
83  0x00000000, 0x00000000,
84  0x0000003B, 0x0000003B,
85  0x00003B3A, 0x00003B3A,
86  0x003B3A39, 0x003B3A39);
87  const __mmask64 rewind_mask = RTE_THASH_REWIND_MSK;
88  const __m512i shift_8 = _mm512_set1_epi8(8);
89  __m512i xor_acc = _mm512_setzero_si512();
90  __m512i perm_bytes = _mm512_setzero_si512();
91  __m512i vals, matrixes, tuple_bytes_2;
92  __m512i tuple_bytes = _mm512_setzero_si512();
93  __mmask64 load_mask, permute_mask_2;
94  __mmask64 permute_mask = 0;
95  int chunk_len = 0, i = 0;
96  uint8_t mtrx_msk;
97  const int prepend = 3;
98 
99  for (; len > 0; len -= 64, tuple += 64) {
100  if (i == 8)
101  perm_bytes = _mm512_maskz_permutexvar_epi8(rewind_mask,
102  rewind_idx, perm_bytes);
103 
104  permute_mask = RTE_THASH_FIRST_ITER_MSK;
105  load_mask = (len >= 64) ? UINT64_MAX : ((1ULL << len) - 1);
106  tuple_bytes = _mm512_maskz_loadu_epi8(load_mask, tuple);
107  if (secondary_tuple) {
108  permute_mask_2 = RTE_THASH_FIRST_ITER_MSK_2;
109  tuple_bytes_2 = _mm512_maskz_loadu_epi8(load_mask,
110  secondary_tuple);
111  }
112 
113  chunk_len = __builtin_popcountll(load_mask);
114  for (i = 0; i < ((chunk_len + prepend) / 8); i++, mtrx += 8) {
115  perm_bytes = _mm512_mask_permutexvar_epi8(perm_bytes,
116  permute_mask, permute_idx, tuple_bytes);
117 
118  if (secondary_tuple)
119  perm_bytes =
120  _mm512_mask_permutexvar_epi8(perm_bytes,
121  permute_mask_2, permute_idx,
122  tuple_bytes_2);
123 
124  matrixes = _mm512_maskz_loadu_epi64(UINT8_MAX, mtrx);
125  vals = _mm512_gf2p8affine_epi64_epi8(perm_bytes,
126  matrixes, 0);
127 
128  xor_acc = _mm512_xor_si512(xor_acc, vals);
129  permute_idx = _mm512_add_epi8(permute_idx, shift_8);
130  permute_mask = RTE_THASH_PERM_MSK;
131  if (secondary_tuple)
132  permute_mask_2 = RTE_THASH_PERM_MSK_2;
133  }
134  }
135 
136  int rest_len = (chunk_len + prepend) % 8;
137  if (rest_len != 0) {
138  mtrx_msk = (1 << (rest_len % 8)) - 1;
139  matrixes = _mm512_maskz_loadu_epi64(mtrx_msk, mtrx);
140  if (i == 8) {
141  perm_bytes = _mm512_maskz_permutexvar_epi8(rewind_mask,
142  rewind_idx, perm_bytes);
143  } else {
144  perm_bytes = _mm512_mask_permutexvar_epi8(perm_bytes,
145  permute_mask, permute_idx, tuple_bytes);
146 
147  if (secondary_tuple)
148  perm_bytes =
149  _mm512_mask_permutexvar_epi8(
150  perm_bytes, permute_mask_2,
151  permute_idx, tuple_bytes_2);
152  }
153 
154  vals = _mm512_gf2p8affine_epi64_epi8(perm_bytes, matrixes, 0);
155  xor_acc = _mm512_xor_si512(xor_acc, vals);
156  }
157 
158  return xor_acc;
159 }
160 
178 __rte_experimental
179 static inline uint32_t
180 rte_thash_gfni(const uint64_t *m, const uint8_t *tuple, int len)
181 {
182  uint32_t val, val_zero;
183 
184  __m512i xor_acc = __rte_thash_gfni(m, tuple, NULL, len);
185  __rte_thash_xor_reduce(xor_acc, &val, &val_zero);
186 
187  return val;
188 }
189 
210 __rte_experimental
211 static inline void
212 rte_thash_gfni_bulk(const uint64_t *mtrx, int len, uint8_t *tuple[],
213  uint32_t val[], uint32_t num)
214 {
215  uint32_t i;
216  uint32_t val_zero;
217  __m512i xor_acc;
218 
219  for (i = 0; i != (num & ~1); i += 2) {
220  xor_acc = __rte_thash_gfni(mtrx, tuple[i], tuple[i + 1], len);
221  __rte_thash_xor_reduce(xor_acc, val + i, val + i + 1);
222  }
223 
224  if (num & 1) {
225  xor_acc = __rte_thash_gfni(mtrx, tuple[i], NULL, len);
226  __rte_thash_xor_reduce(xor_acc, val + i, &val_zero);
227  }
228 }
229 
230 #endif /* __GFNI__ && __AVX512F__ */
231 
232 #ifdef __cplusplus
233 }
234 #endif
235 
236 #endif /* _RTE_THASH_X86_GFNI_H_ */