Coverage Report

Created: 2025-09-04 06:50

/src/zlib-ng/arch/x86/compare256_avx2.c
Line
Count
Source (jump to first uncovered line)
1
/* compare256_avx2.c -- AVX2 version of compare256
2
 * Copyright Mika T. Lindqvist  <postmaster@raasu.org>
3
 * For conditions of distribution and use, see copyright notice in zlib.h
4
 */
5
6
#include "zbuild.h"
7
#include "zmemory.h"
8
#include "deflate.h"
9
#include "fallback_builtins.h"
10
11
#if defined(X86_AVX2) && defined(HAVE_BUILTIN_CTZ)
12
13
#include <immintrin.h>
14
#ifdef _MSC_VER
15
#  include <nmmintrin.h>
16
#endif
17
18
25.7M
static inline uint32_t compare256_avx2_static(const uint8_t *src0, const uint8_t *src1) {
19
25.7M
    uint32_t len = 0;
20
21
28.9M
    do {
22
28.9M
        __m256i ymm_src0, ymm_src1, ymm_cmp;
23
28.9M
        ymm_src0 = _mm256_loadu_si256((__m256i*)src0);
24
28.9M
        ymm_src1 = _mm256_loadu_si256((__m256i*)src1);
25
28.9M
        ymm_cmp = _mm256_cmpeq_epi8(ymm_src0, ymm_src1); /* non-identical bytes = 00, identical bytes = FF */
26
28.9M
        unsigned mask = (unsigned)_mm256_movemask_epi8(ymm_cmp);
27
28.9M
        if (mask != 0xFFFFFFFF) {
28
23.4M
            uint32_t match_byte = (uint32_t)__builtin_ctz(~mask); /* Invert bits so identical = 0 */
29
23.4M
            return len + match_byte;
30
23.4M
        }
31
32
5.51M
        src0 += 32, src1 += 32, len += 32;
33
34
5.51M
        ymm_src0 = _mm256_loadu_si256((__m256i*)src0);
35
5.51M
        ymm_src1 = _mm256_loadu_si256((__m256i*)src1);
36
5.51M
        ymm_cmp = _mm256_cmpeq_epi8(ymm_src0, ymm_src1);
37
5.51M
        mask = (unsigned)_mm256_movemask_epi8(ymm_cmp);
38
5.51M
        if (mask != 0xFFFFFFFF) {
39
1.56M
            uint32_t match_byte = (uint32_t)__builtin_ctz(~mask);
40
1.56M
            return len + match_byte;
41
1.56M
        }
42
43
3.94M
        src0 += 32, src1 += 32, len += 32;
44
3.94M
    } while (len < 256);
45
46
714k
    return 256;
47
25.7M
}
48
49
4.74M
Z_INTERNAL uint32_t compare256_avx2(const uint8_t *src0, const uint8_t *src1) {
50
4.74M
    return compare256_avx2_static(src0, src1);
51
4.74M
}
52
53
#define LONGEST_MATCH       longest_match_avx2
54
20.9M
#define COMPARE256          compare256_avx2_static
55
56
#include "match_tpl.h"
57
58
#define LONGEST_MATCH_SLOW
59
#define LONGEST_MATCH       longest_match_slow_avx2
60
0
#define COMPARE256          compare256_avx2_static
61
62
#include "match_tpl.h"
63
64
#endif