Because of the need to handle cases where the L4 is not contiguous with
the L3 addresses, we have to build up a tuple locally before calling the
hash calculation. This building the tuple, even using AVX-512, still
leads to an extra store of that tuple before loading it back inside the
calculation loop. We can eliminate this unnecessary store and load back
by making a simplified copy of the core calculation function that only
handles tuples of up to 64-bytes in size and that takes those as input
via an already populated AVX-512 register.

Signed-off-by: Bruce Richardson <[email protected]>
---
 lib/hash/thash_gfni_avx512.c | 94 ++++++++++++++++++++++++++++++------
 1 file changed, 79 insertions(+), 15 deletions(-)

diff --git a/lib/hash/thash_gfni_avx512.c b/lib/hash/thash_gfni_avx512.c
index e738812807..8c8384919a 100644
--- a/lib/hash/thash_gfni_avx512.c
+++ b/lib/hash/thash_gfni_avx512.c
@@ -2,7 +2,6 @@
  * Copyright(c) 2026 Intel Corporation
  */
 
-#include <stdalign.h>
 #include <string.h>
 
 #include <rte_thash_x86_gfni.h>
@@ -13,56 +12,121 @@
 #error "GFNI and required AVX512 extensions must be enabled to build this file"
 #endif
 
+static inline uint32_t
+thash_gfni_avx512_hash(const uint64_t *mtrx, __m512i tuple_bytes, int len)
+{
+       __m512i permute_idx = _mm512_set_epi32(0x07060504, 0x07060504,
+               0x06050403, 0x06050403,
+               0x05040302, 0x05040302,
+               0x04030201, 0x04030201,
+               0x03020100, 0x03020100,
+               0x020100FF, 0x020100FF,
+               0x0100FFFE, 0x0100FFFE,
+               0x00FFFEFD, 0x00FFFEFD);
+       const __m512i rewind_idx = _mm512_set_epi32(0, 0, 0, 0,
+               0, 0, 0, 0,
+               0, 0, 0x0000003B, 0x0000003B,
+               0x00003B3A, 0x00003B3A,
+               0x003B3A39, 0x003B3A39);
+       const __m512i shift_8 = _mm512_set1_epi8(8);
+       __m512i xor_acc = _mm512_setzero_si512();
+       __m512i perm_bytes = _mm512_setzero_si512();
+       __mmask64 permute_mask = RTE_THASH_FIRST_ITER_MSK;
+       const int full_len = (len + 3) / 8;
+       uint32_t val, val_zero;
+
+       for (int i = 0; i < full_len; i++, mtrx += 8) {
+               perm_bytes = _mm512_mask_permutexvar_epi8(perm_bytes,
+                       permute_mask, permute_idx, tuple_bytes);
+               const __m512i matrixes = _mm512_maskz_loadu_epi64(UINT8_MAX, 
mtrx);
+               const __m512i vals = _mm512_gf2p8affine_epi64_epi8(perm_bytes, 
matrixes, 0);
+
+               xor_acc = _mm512_xor_si512(xor_acc, vals);
+               permute_idx = _mm512_add_epi8(permute_idx, shift_8);
+               permute_mask = RTE_THASH_PERM_MSK;
+       }
+
+       const int rest_len = (len + 3) % 8;
+       if (rest_len != 0) {
+               const __mmask8 mtrx_mask = (1U << rest_len) - 1;
+               const __m512i matrixes = _mm512_maskz_loadu_epi64(mtrx_mask, 
mtrx);
+
+               if (full_len == 8)
+                       perm_bytes = 
_mm512_maskz_permutexvar_epi8(RTE_THASH_REWIND_MSK,
+                               rewind_idx, perm_bytes);
+               else
+                       perm_bytes = _mm512_mask_permutexvar_epi8(perm_bytes,
+                               permute_mask, permute_idx, tuple_bytes);
+
+               const __m512i vals = _mm512_gf2p8affine_epi64_epi8(perm_bytes, 
matrixes, 0);
+               xor_acc = _mm512_xor_si512(xor_acc, vals);
+       }
+
+       __rte_thash_xor_reduce(xor_acc, &val, &val_zero);
+       return val;
+}
+
 uint32_t
 thash_gfni_avx512_8B(const struct rte_thash_rss_ctx *ctx, const uint8_t *tuple)
 {
-       return rte_thash_gfni(ctx->data.matrices, tuple, 8);
+       uint64_t value;
+
+       memcpy(&value, tuple, sizeof(value));
+       const __m512i tuple_bytes = _mm512_maskz_set1_epi64(1, value);
+
+       return thash_gfni_avx512_hash(ctx->data.matrices, tuple_bytes, 8);
 }
 
 uint32_t
 thash_gfni_avx512_12B(const struct rte_thash_rss_ctx *ctx, const uint8_t 
*tuple)
 {
-       return rte_thash_gfni(ctx->data.matrices, tuple, 12);
+       const __m512i tuple_bytes = _mm512_maskz_loadu_epi8(UINT64_C(0xfff), 
tuple);
+
+       return thash_gfni_avx512_hash(ctx->data.matrices, tuple_bytes, 12);
 }
 
 uint32_t
 thash_gfni_avx512_32B(const struct rte_thash_rss_ctx *ctx, const uint8_t 
*tuple)
 {
-       return rte_thash_gfni(ctx->data.matrices, tuple, 32);
+       const __m512i tuple_bytes = _mm512_zextsi256_si512(
+               _mm256_loadu_si256((const __m256i *)tuple));
+
+       return thash_gfni_avx512_hash(ctx->data.matrices, tuple_bytes, 32);
 }
 
 uint32_t
 thash_gfni_avx512_36B(const struct rte_thash_rss_ctx *ctx, const uint8_t 
*tuple)
 {
-       return rte_thash_gfni(ctx->data.matrices, tuple, 36);
+       const __m512i tuple_bytes = 
_mm512_maskz_loadu_epi8(UINT64_C(0xfffffffff), tuple);
+
+       return thash_gfni_avx512_hash(ctx->data.matrices, tuple_bytes, 36);
 }
 
 uint32_t
 thash_gfni_avx512_12B_split(const struct rte_thash_rss_ctx *ctx,
        const uint8_t *addresses, const uint8_t *ports)
 {
-       alignas(64) uint8_t tuple[64];
        uint32_t ports_value;
+       uint64_t address_value;
 
        memcpy(&ports_value, ports, sizeof(ports_value));
-       __m512i value = _mm512_maskz_loadu_epi8(UINT64_C(0xff), addresses);
-       value = _mm512_mask_set1_epi32(value, UINT16_C(1) << 2, ports_value);
-       _mm512_store_si512(tuple, value);
+       memcpy(&address_value, addresses, sizeof(address_value));
+       __m512i tuple_bytes = _mm512_maskz_set1_epi64(1, address_value);
+       tuple_bytes = _mm512_mask_set1_epi32(tuple_bytes, UINT16_C(1) << 2, 
ports_value);
 
-       return rte_thash_gfni(ctx->data.matrices, tuple, 12);
+       return thash_gfni_avx512_hash(ctx->data.matrices, tuple_bytes, 12);
 }
 
 uint32_t
 thash_gfni_avx512_36B_split(const struct rte_thash_rss_ctx *ctx,
        const uint8_t *addresses, const uint8_t *ports)
 {
-       alignas(64) uint8_t tuple[64];
        uint32_t ports_value;
 
        memcpy(&ports_value, ports, sizeof(ports_value));
-       __m512i value = _mm512_maskz_loadu_epi8(UINT64_C(0xffffffff), 
addresses);
-       value = _mm512_mask_set1_epi32(value, UINT16_C(1) << 8, ports_value);
-       _mm512_store_si512(tuple, value);
+       __m512i tuple_bytes = _mm512_zextsi256_si512(
+               _mm256_loadu_si256((const __m256i *)addresses));
+       tuple_bytes = _mm512_mask_set1_epi32(tuple_bytes, UINT16_C(1) << 8, 
ports_value);
 
-       return rte_thash_gfni(ctx->data.matrices, tuple, 36);
+       return thash_gfni_avx512_hash(ctx->data.matrices, tuple_bytes, 36);
 }
-- 
2.55.0

Reply via email to