1 /*
2 * x86/matchfinder_impl.h - x86 implementations of matchfinder functions
3 *
4 * Copyright 2016 Eric Biggers
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following
13 * conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28 #ifdef __AVX2__
29 # if MATCHFINDER_ALIGNMENT < 32
30 # undef MATCHFINDER_ALIGNMENT
31 # define MATCHFINDER_ALIGNMENT 32
32 # endif
33 # include <immintrin.h>
34 static forceinline bool
matchfinder_init_avx2(mf_pos_t * data,size_t size)35 matchfinder_init_avx2(mf_pos_t *data, size_t size)
36 {
37 __m256i v, *p;
38 size_t n;
39
40 if (size % (sizeof(__m256i) * 4) != 0)
41 return false;
42
43 STATIC_ASSERT(sizeof(mf_pos_t) == 2);
44 v = _mm256_set1_epi16(MATCHFINDER_INITVAL);
45 p = (__m256i *)data;
46 n = size / (sizeof(__m256i) * 4);
47 do {
48 p[0] = v;
49 p[1] = v;
50 p[2] = v;
51 p[3] = v;
52 p += 4;
53 } while (--n);
54 return true;
55 }
56
57 static forceinline bool
matchfinder_rebase_avx2(mf_pos_t * data,size_t size)58 matchfinder_rebase_avx2(mf_pos_t *data, size_t size)
59 {
60 __m256i v, *p;
61 size_t n;
62
63 if (size % (sizeof(__m256i) * 4) != 0)
64 return false;
65
66 STATIC_ASSERT(sizeof(mf_pos_t) == 2);
67 v = _mm256_set1_epi16((u16)-MATCHFINDER_WINDOW_SIZE);
68 p = (__m256i *)data;
69 n = size / (sizeof(__m256i) * 4);
70 do {
71 /* PADDSW: Add Packed Signed Integers With Signed Saturation */
72 p[0] = _mm256_adds_epi16(p[0], v);
73 p[1] = _mm256_adds_epi16(p[1], v);
74 p[2] = _mm256_adds_epi16(p[2], v);
75 p[3] = _mm256_adds_epi16(p[3], v);
76 p += 4;
77 } while (--n);
78 return true;
79 }
80 #endif /* __AVX2__ */
81
82 #ifdef __SSE2__
83 # if MATCHFINDER_ALIGNMENT < 16
84 # undef MATCHFINDER_ALIGNMENT
85 # define MATCHFINDER_ALIGNMENT 16
86 # endif
87 # include <emmintrin.h>
88 static forceinline bool
matchfinder_init_sse2(mf_pos_t * data,size_t size)89 matchfinder_init_sse2(mf_pos_t *data, size_t size)
90 {
91 __m128i v, *p;
92 size_t n;
93
94 if (size % (sizeof(__m128i) * 4) != 0)
95 return false;
96
97 STATIC_ASSERT(sizeof(mf_pos_t) == 2);
98 v = _mm_set1_epi16(MATCHFINDER_INITVAL);
99 p = (__m128i *)data;
100 n = size / (sizeof(__m128i) * 4);
101 do {
102 p[0] = v;
103 p[1] = v;
104 p[2] = v;
105 p[3] = v;
106 p += 4;
107 } while (--n);
108 return true;
109 }
110
111 static forceinline bool
matchfinder_rebase_sse2(mf_pos_t * data,size_t size)112 matchfinder_rebase_sse2(mf_pos_t *data, size_t size)
113 {
114 __m128i v, *p;
115 size_t n;
116
117 if (size % (sizeof(__m128i) * 4) != 0)
118 return false;
119
120 STATIC_ASSERT(sizeof(mf_pos_t) == 2);
121 v = _mm_set1_epi16((u16)-MATCHFINDER_WINDOW_SIZE);
122 p = (__m128i *)data;
123 n = size / (sizeof(__m128i) * 4);
124 do {
125 /* PADDSW: Add Packed Signed Integers With Signed Saturation */
126 p[0] = _mm_adds_epi16(p[0], v);
127 p[1] = _mm_adds_epi16(p[1], v);
128 p[2] = _mm_adds_epi16(p[2], v);
129 p[3] = _mm_adds_epi16(p[3], v);
130 p += 4;
131 } while (--n);
132 return true;
133 }
134 #endif /* __SSE2__ */
135
136 #undef arch_matchfinder_init
137 static forceinline bool
arch_matchfinder_init(mf_pos_t * data,size_t size)138 arch_matchfinder_init(__attribute__((unused)) mf_pos_t *data, __attribute__((unused)) size_t size)
139 {
140 #ifdef __AVX2__
141 if (matchfinder_init_avx2(data, size))
142 return true;
143 #endif
144 #ifdef __SSE2__
145 if (matchfinder_init_sse2(data, size))
146 return true;
147 #endif
148 return false;
149 }
150
151 #undef arch_matchfinder_rebase
152 static forceinline bool
arch_matchfinder_rebase(mf_pos_t * data,size_t size)153 arch_matchfinder_rebase(__attribute__((unused)) mf_pos_t *data, __attribute__((unused)) size_t size)
154 {
155 #ifdef __AVX2__
156 if (matchfinder_rebase_avx2(data, size))
157 return true;
158 #endif
159 #ifdef __SSE2__
160 if (matchfinder_rebase_sse2(data, size))
161 return true;
162 #endif
163 return false;
164 }
165