1 // Copyright (c) Facebook, Inc. and its affiliates.
2 // All rights reserved.
3 //
4 // Copyright 2019 Google LLC
5 //
6 // This source code is licensed under the BSD-style license found in the
7 // LICENSE file in the root directory of this source tree.
8
9 #include <assert.h>
10
11 #include <emmintrin.h>
12
13 #include <xnnpack/maxpool.h>
14
15
xnn_u8_maxpool_minmax_ukernel_9p8x__sse2_c16(size_t output_pixels,size_t kernel_elements,size_t channels,const uint8_t ** input,size_t input_offset,uint8_t * output,size_t input_increment,size_t output_increment,const union xnn_u8_minmax_params params[restrict XNN_MIN_ELEMENTS (1)])16 void xnn_u8_maxpool_minmax_ukernel_9p8x__sse2_c16(
17 size_t output_pixels,
18 size_t kernel_elements,
19 size_t channels,
20 const uint8_t** input,
21 size_t input_offset,
22 uint8_t* output,
23 size_t input_increment,
24 size_t output_increment,
25 const union xnn_u8_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) XNN_DISABLE_TSAN
26 {
27 assert(output_pixels != 0);
28 assert(kernel_elements != 0);
29 assert(channels != 0);
30
31 const __m128i voutput_max = _mm_load_si128((const __m128i*) params->sse2.max);
32 const __m128i voutput_min = _mm_load_si128((const __m128i*) params->sse2.min);
33
34 do {
35 uint8_t* o = output;
36 {
37 const uint8_t* i0 = *input++;
38 const uint8_t* i1 = *input++;
39 const uint8_t* i2 = *input++;
40 const uint8_t* i3 = *input++;
41 const uint8_t* i4 = *input++;
42 const uint8_t* i5 = *input++;
43 const uint8_t* i6 = *input++;
44 const uint8_t* i7 = *input++;
45 const uint8_t* i8 = *input++;
46 i0 = (const uint8_t*) ((uintptr_t) i0 + input_offset);
47 i1 = (const uint8_t*) ((uintptr_t) i1 + input_offset);
48 i2 = (const uint8_t*) ((uintptr_t) i2 + input_offset);
49 i3 = (const uint8_t*) ((uintptr_t) i3 + input_offset);
50 i4 = (const uint8_t*) ((uintptr_t) i4 + input_offset);
51 i5 = (const uint8_t*) ((uintptr_t) i5 + input_offset);
52 i6 = (const uint8_t*) ((uintptr_t) i6 + input_offset);
53 i7 = (const uint8_t*) ((uintptr_t) i7 + input_offset);
54 i8 = (const uint8_t*) ((uintptr_t) i8 + input_offset);
55 if (kernel_elements < 2) {
56 i1 = i0;
57 }
58 if (kernel_elements <= 2) {
59 i2 = i0;
60 }
61 if (kernel_elements < 4) {
62 i3 = i0;
63 }
64 if (kernel_elements <= 4) {
65 i4 = i0;
66 }
67 if (kernel_elements < 6) {
68 i5 = i0;
69 }
70 if (kernel_elements <= 6) {
71 i6 = i0;
72 }
73 if (kernel_elements < 8) {
74 i7 = i0;
75 }
76 if (kernel_elements <= 8) {
77 i8 = i0;
78 }
79
80 size_t c = channels;
81 for (; c >= 16; c -= 16) {
82 const __m128i vi0 = _mm_loadu_si128((const __m128i*) i0); i0 += 16;
83 const __m128i vi1 = _mm_loadu_si128((const __m128i*) i1); i1 += 16;
84 const __m128i vi2 = _mm_loadu_si128((const __m128i*) i2); i2 += 16;
85 const __m128i vi3 = _mm_loadu_si128((const __m128i*) i3); i3 += 16;
86 const __m128i vi4 = _mm_loadu_si128((const __m128i*) i4); i4 += 16;
87 const __m128i vi5 = _mm_loadu_si128((const __m128i*) i5); i5 += 16;
88 const __m128i vi6 = _mm_loadu_si128((const __m128i*) i6); i6 += 16;
89 const __m128i vi7 = _mm_loadu_si128((const __m128i*) i7); i7 += 16;
90 const __m128i vi8 = _mm_loadu_si128((const __m128i*) i8); i8 += 16;
91
92 const __m128i vmax018 = _mm_max_epu8(_mm_max_epu8(vi0, vi1), vi8);
93 const __m128i vmax23 = _mm_max_epu8(vi2, vi3);
94 const __m128i vmax45 = _mm_max_epu8(vi4, vi5);
95 const __m128i vmax67 = _mm_max_epu8(vi6, vi7);
96
97 const __m128i vmax2345 = _mm_max_epu8(vmax23, vmax45);
98 const __m128i vmax01678 = _mm_max_epu8(vmax018, vmax67);
99 const __m128i vmax = _mm_max_epu8(vmax2345, vmax01678);
100 const __m128i vout = _mm_max_epu8(_mm_min_epu8(vmax, voutput_max), voutput_min);
101
102 _mm_storeu_si128((__m128i*) o, vout); o += 16;
103 }
104 if (c != 0) {
105 const __m128i vi0 = _mm_loadu_si128((const __m128i*) i0);
106 const __m128i vi1 = _mm_loadu_si128((const __m128i*) i1);
107 const __m128i vi2 = _mm_loadu_si128((const __m128i*) i2);
108 const __m128i vi3 = _mm_loadu_si128((const __m128i*) i3);
109 const __m128i vi4 = _mm_loadu_si128((const __m128i*) i4);
110 const __m128i vi5 = _mm_loadu_si128((const __m128i*) i5);
111 const __m128i vi6 = _mm_loadu_si128((const __m128i*) i6);
112 const __m128i vi7 = _mm_loadu_si128((const __m128i*) i7);
113 const __m128i vi8 = _mm_loadu_si128((const __m128i*) i8);
114
115 const __m128i vmax018 = _mm_max_epu8(_mm_max_epu8(vi0, vi1), vi8);
116 const __m128i vmax23 = _mm_max_epu8(vi2, vi3);
117 const __m128i vmax45 = _mm_max_epu8(vi4, vi5);
118 const __m128i vmax67 = _mm_max_epu8(vi6, vi7);
119
120 const __m128i vmax2345 = _mm_max_epu8(vmax23, vmax45);
121 const __m128i vmax01678 = _mm_max_epu8(vmax018, vmax67);
122 const __m128i vmax = _mm_max_epu8(vmax2345, vmax01678);
123 __m128i vout = _mm_max_epu8(_mm_min_epu8(vmax, voutput_max), voutput_min);
124
125 if (c & 8) {
126 _mm_storel_epi64((__m128i*) o, vout);
127 vout = _mm_unpackhi_epi64(vout, vout);
128 o += 8;
129 }
130 if (c & 4) {
131 *((uint32_t*) o) = (uint32_t) _mm_cvtsi128_si32(vout);
132 vout = _mm_srli_epi64(vout, 32);
133 o += 4;
134 }
135 if (c & 2) {
136 *((uint16_t*) o) = (uint16_t) _mm_extract_epi16(vout, 0);
137 vout = _mm_srli_epi32(vout, 16);
138 o += 2;
139 }
140 if (c & 1) {
141 *((uint8_t*) o) = (uint8_t) _mm_cvtsi128_si32(vout);
142 o += 1;
143 }
144 }
145 }
146
147 for (ptrdiff_t k = (ptrdiff_t) kernel_elements - 9; k > 0; k -= 8) {
148 const uint8_t* i0 = *input++;
149 const uint8_t* i1 = *input++;
150 const uint8_t* i2 = *input++;
151 const uint8_t* i3 = *input++;
152 const uint8_t* i4 = *input++;
153 const uint8_t* i5 = *input++;
154 const uint8_t* i6 = *input++;
155 const uint8_t* i7 = *input++;
156 i0 = (const uint8_t*) ((uintptr_t) i0 + input_offset);
157 i1 = (const uint8_t*) ((uintptr_t) i1 + input_offset);
158 i2 = (const uint8_t*) ((uintptr_t) i2 + input_offset);
159 i3 = (const uint8_t*) ((uintptr_t) i3 + input_offset);
160 i4 = (const uint8_t*) ((uintptr_t) i4 + input_offset);
161 i5 = (const uint8_t*) ((uintptr_t) i5 + input_offset);
162 i6 = (const uint8_t*) ((uintptr_t) i6 + input_offset);
163 i7 = (const uint8_t*) ((uintptr_t) i7 + input_offset);
164 if (k < 2) {
165 i1 = i0;
166 }
167 if (k <= 2) {
168 i2 = i0;
169 }
170 if (k < 4) {
171 i3 = i0;
172 }
173 if (k <= 4) {
174 i4 = i0;
175 }
176 if (k < 6) {
177 i5 = i0;
178 }
179 if (k <= 6) {
180 i6 = i0;
181 }
182 if (k < 8) {
183 i7 = i0;
184 }
185
186 o = output;
187 size_t c = channels;
188 for (; c >= 16; c -= 16) {
189 const __m128i vi0 = _mm_loadu_si128((const __m128i*) i0); i0 += 16;
190 const __m128i vi1 = _mm_loadu_si128((const __m128i*) i1); i1 += 16;
191 const __m128i vi2 = _mm_loadu_si128((const __m128i*) i2); i2 += 16;
192 const __m128i vi3 = _mm_loadu_si128((const __m128i*) i3); i3 += 16;
193 const __m128i vi4 = _mm_loadu_si128((const __m128i*) i4); i4 += 16;
194 const __m128i vi5 = _mm_loadu_si128((const __m128i*) i5); i5 += 16;
195 const __m128i vi6 = _mm_loadu_si128((const __m128i*) i6); i6 += 16;
196 const __m128i vi7 = _mm_loadu_si128((const __m128i*) i7); i7 += 16;
197 const __m128i vo = _mm_loadu_si128((const __m128i*) o);
198
199 const __m128i vmax01 = _mm_max_epu8(_mm_max_epu8(vi0, vi1), vo);
200 const __m128i vmax23 = _mm_max_epu8(vi2, vi3);
201 const __m128i vmax45 = _mm_max_epu8(vi4, vi5);
202 const __m128i vmax67 = _mm_max_epu8(vi6, vi7);
203
204 const __m128i vmax2345 = _mm_max_epu8(vmax23, vmax45);
205 const __m128i vmax0167 = _mm_max_epu8(vmax01, vmax67);
206 const __m128i vmax = _mm_max_epu8(vmax2345, vmax0167);
207 const __m128i vout = _mm_max_epu8(_mm_min_epu8(vmax, voutput_max), voutput_min);
208
209 _mm_storeu_si128((__m128i*) o, vout);
210 o += 16;
211 }
212 if (c != 0) {
213 const __m128i vi0 = _mm_loadu_si128((const __m128i*) i0);
214 const __m128i vi1 = _mm_loadu_si128((const __m128i*) i1);
215 const __m128i vi2 = _mm_loadu_si128((const __m128i*) i2);
216 const __m128i vi3 = _mm_loadu_si128((const __m128i*) i3);
217 const __m128i vi4 = _mm_loadu_si128((const __m128i*) i4);
218 const __m128i vi5 = _mm_loadu_si128((const __m128i*) i5);
219 const __m128i vi6 = _mm_loadu_si128((const __m128i*) i6);
220 const __m128i vi7 = _mm_loadu_si128((const __m128i*) i7);
221 const __m128i vo = _mm_loadu_si128((const __m128i*) o);
222
223 const __m128i vmax01 = _mm_max_epu8(_mm_max_epu8(vi0, vi1), vo);
224 const __m128i vmax23 = _mm_max_epu8(vi2, vi3);
225 const __m128i vmax45 = _mm_max_epu8(vi4, vi5);
226 const __m128i vmax67 = _mm_max_epu8(vi6, vi7);
227
228 const __m128i vmax2345 = _mm_max_epu8(vmax23, vmax45);
229 const __m128i vmax0167 = _mm_max_epu8(vmax01, vmax67);
230 const __m128i vmax = _mm_max_epu8(vmax2345, vmax0167);
231 __m128i vout = _mm_max_epu8(_mm_min_epu8(vmax, voutput_max), voutput_min);
232
233 if (c & 8) {
234 _mm_storel_epi64((__m128i*) o, vout);
235 vout = _mm_unpackhi_epi64(vout, vout);
236 o += 8;
237 }
238 if (c & 4) {
239 *((uint32_t*) o) = (uint32_t) _mm_cvtsi128_si32(vout);
240 vout = _mm_srli_epi64(vout, 32);
241 o += 4;
242 }
243 if (c & 2) {
244 *((uint16_t*) o) = (uint16_t) _mm_extract_epi16(vout, 0);
245 vout = _mm_srli_epi32(vout, 16);
246 o += 2;
247 }
248 if (c & 1) {
249 *((uint8_t*) o) = (uint8_t) _mm_cvtsi128_si32(vout);
250 o += 1;
251 }
252 }
253 }
254 input = (const uint8_t**) ((uintptr_t) input + input_increment);
255 output = (uint8_t*) ((uintptr_t) o + output_increment);
256 } while (--output_pixels != 0);
257 }
258