1 /* compare258_avx.c -- AVX2 version of compare258
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 "../../zutil.h"
8 
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 /* UNALIGNED_OK, AVX2 intrinsic comparison */
compare256_unaligned_avx2_static(const unsigned char * src0,const unsigned char * src1)19 static inline uint32_t compare256_unaligned_avx2_static(const unsigned char *src0, const unsigned char *src1) {
20     uint32_t len = 0;
21 
22     do {
23         __m256i ymm_src0, ymm_src1, ymm_cmp;
24         ymm_src0 = _mm256_loadu_si256((__m256i*)src0);
25         ymm_src1 = _mm256_loadu_si256((__m256i*)src1);
26         ymm_cmp = _mm256_cmpeq_epi8(ymm_src0, ymm_src1); /* non-identical bytes = 00, identical bytes = FF */
27         unsigned mask = (unsigned)_mm256_movemask_epi8(ymm_cmp);
28         if (mask != 0xFFFFFFFF) {
29             uint32_t match_byte = (uint32_t)__builtin_ctz(~mask); /* Invert bits so identical = 0 */
30             return len + match_byte;
31         }
32 
33         src0 += 32, src1 += 32, len += 32;
34 
35         ymm_src0 = _mm256_loadu_si256((__m256i*)src0);
36         ymm_src1 = _mm256_loadu_si256((__m256i*)src1);
37         ymm_cmp = _mm256_cmpeq_epi8(ymm_src0, ymm_src1);
38         mask = (unsigned)_mm256_movemask_epi8(ymm_cmp);
39         if (mask != 0xFFFFFFFF) {
40             uint32_t match_byte = (uint32_t)__builtin_ctz(~mask);
41             return len + match_byte;
42         }
43 
44         src0 += 32, src1 += 32, len += 32;
45     } while (len < 256);
46 
47     return 256;
48 }
49 
compare258_unaligned_avx2_static(const unsigned char * src0,const unsigned char * src1)50 static inline uint32_t compare258_unaligned_avx2_static(const unsigned char *src0, const unsigned char *src1) {
51     if (*(uint16_t *)src0 != *(uint16_t *)src1)
52         return (*src0 == *src1);
53 
54     return compare256_unaligned_avx2_static(src0+2, src1+2) + 2;
55 }
56 
compare258_unaligned_avx2(const unsigned char * src0,const unsigned char * src1)57 Z_INTERNAL uint32_t compare258_unaligned_avx2(const unsigned char *src0, const unsigned char *src1) {
58     return compare258_unaligned_avx2_static(src0, src1);
59 }
60 
61 #define LONGEST_MATCH   longest_match_unaligned_avx2
62 #define COMPARE256      compare256_unaligned_avx2_static
63 #define COMPARE258      compare258_unaligned_avx2_static
64 
65 #include "match_tpl.h"
66 
67 #endif
68