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