|
| 1 | +/* compare256_avx512.c -- AVX512 version of compare256 |
| 2 | + * Copyright (C) 2025 Hans Kristian Rosbach |
| 3 | + * Based on AVX2 implementation by Mika T. Lindqvist |
| 4 | + * For conditions of distribution and use, see copyright notice in zlib.h |
| 5 | + */ |
| 6 | + |
| 7 | +#include "zbuild.h" |
| 8 | +#include "zmemory.h" |
| 9 | +#include "deflate.h" |
| 10 | +#include "fallback_builtins.h" |
| 11 | + |
| 12 | +#if defined(X86_AVX512) && defined(HAVE_BUILTIN_CTZLL) |
| 13 | + |
| 14 | +#include <immintrin.h> |
| 15 | +#ifdef _MSC_VER |
| 16 | +# include <nmmintrin.h> |
| 17 | +#endif |
| 18 | + |
| 19 | +static inline uint32_t compare256_avx512_static(const uint8_t *src0, const uint8_t *src1) { |
| 20 | + __m512i zmm_src0_4, zmm_src1_4; |
| 21 | + __m512i zmm_src0_3, zmm_src1_3; |
| 22 | + __m512i zmm_src0_2, zmm_src1_2; |
| 23 | + __m512i zmm_src0_1, zmm_src1_1; |
| 24 | + __m128i xmm_src0_0, xmm_src1_0; |
| 25 | + uint64_t mask_1, mask_2, mask_3, mask_4; |
| 26 | + uint32_t mask_0; |
| 27 | + |
| 28 | + // First do a 16byte round before increasing to 64bytes, this reduces the |
| 29 | + // penalty for the short matches, and those are usually the most common ones. |
| 30 | + // This requires us to overlap on the last round, giving a small penalty |
| 31 | + // on matches of 192+ bytes (Still faster than AVX2 though). |
| 32 | + |
| 33 | + // 16 bytes |
| 34 | + xmm_src0_0 = _mm_loadu_si128((__m128i*)src0); |
| 35 | + xmm_src1_0 = _mm_loadu_si128((__m128i*)src1); |
| 36 | + mask_0 = (uint32_t)_mm_cmpeq_epu8_mask(xmm_src0_0, xmm_src1_0); // zero-extended to use __builtin_ctz |
| 37 | + if (mask_0 != 0x0000FFFF) { |
| 38 | + // There is potential for using __builtin_ctzg/__builtin_ctzs/_tzcnt_u16/__tzcnt_u16 here |
| 39 | + uint32_t match_byte = (uint32_t)__builtin_ctz(~mask_0); /* Invert bits so identical = 0 */ |
| 40 | + return match_byte; |
| 41 | + } |
| 42 | + |
| 43 | + // 64 bytes |
| 44 | + zmm_src0_1 = _mm512_loadu_si512((__m512i*)(src0 + 16)); |
| 45 | + zmm_src1_1 = _mm512_loadu_si512((__m512i*)(src1 + 16)); |
| 46 | + mask_1 = _mm512_cmpeq_epu8_mask(zmm_src0_1, zmm_src1_1); |
| 47 | + if (mask_1 != 0xFFFFFFFFFFFFFFFF) { |
| 48 | + uint32_t match_byte = (uint32_t)__builtin_ctzll(~mask_1); |
| 49 | + return 16 + match_byte; |
| 50 | + } |
| 51 | + |
| 52 | + // 64 bytes |
| 53 | + zmm_src0_2 = _mm512_loadu_si512((__m512i*)(src0 + 80)); |
| 54 | + zmm_src1_2 = _mm512_loadu_si512((__m512i*)(src1 + 80)); |
| 55 | + mask_2 = _mm512_cmpeq_epu8_mask(zmm_src0_2, zmm_src1_2); |
| 56 | + if (mask_2 != 0xFFFFFFFFFFFFFFFF) { |
| 57 | + uint32_t match_byte = (uint32_t)__builtin_ctzll(~mask_2); |
| 58 | + return 80 + match_byte; |
| 59 | + } |
| 60 | + |
| 61 | + // 64 bytes |
| 62 | + zmm_src0_3 = _mm512_loadu_si512((__m512i*)(src0 + 144)); |
| 63 | + zmm_src1_3 = _mm512_loadu_si512((__m512i*)(src1 + 144)); |
| 64 | + mask_3 = _mm512_cmpeq_epu8_mask(zmm_src0_3, zmm_src1_3); |
| 65 | + if (mask_3 != 0xFFFFFFFFFFFFFFFF) { |
| 66 | + uint32_t match_byte = (uint32_t)__builtin_ctzll(~mask_3); |
| 67 | + return 144 + match_byte; |
| 68 | + } |
| 69 | + |
| 70 | + // 64 bytes (overlaps the previous 16 bytes for fast tail processing) |
| 71 | + zmm_src0_4 = _mm512_loadu_si512((__m512i*)(src0 + 192)); |
| 72 | + zmm_src1_4 = _mm512_loadu_si512((__m512i*)(src1 + 192)); |
| 73 | + mask_4 = _mm512_cmpeq_epu8_mask(zmm_src0_4, zmm_src1_4); |
| 74 | + if (mask_4 != 0xFFFFFFFFFFFFFFFF) { |
| 75 | + uint32_t match_byte = (uint32_t)__builtin_ctzll(~mask_4); |
| 76 | + return 192 + match_byte; |
| 77 | + } |
| 78 | + |
| 79 | + return 256; |
| 80 | +} |
| 81 | + |
| 82 | +Z_INTERNAL uint32_t compare256_avx512(const uint8_t *src0, const uint8_t *src1) { |
| 83 | + return compare256_avx512_static(src0, src1); |
| 84 | +} |
| 85 | + |
| 86 | +#define LONGEST_MATCH longest_match_avx512 |
| 87 | +#define COMPARE256 compare256_avx512_static |
| 88 | + |
| 89 | +#include "match_tpl.h" |
| 90 | + |
| 91 | +#define LONGEST_MATCH_SLOW |
| 92 | +#define LONGEST_MATCH longest_match_slow_avx512 |
| 93 | +#define COMPARE256 compare256_avx512_static |
| 94 | + |
| 95 | +#include "match_tpl.h" |
| 96 | + |
| 97 | +#endif |
0 commit comments