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