Elffuss BraidHub

kikocis/ncnn public

master · 17.2 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2022 THL A29 Limited, a Tencent company. All rights reserved.
4//
5// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6// in compliance with the License. You may obtain a copy of the License at
7//
8// https://opensource.org/licenses/BSD-3-Clause
9//
10// Unless required by applicable law or agreed to in writing, software distributed
11// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13// specific language governing permissions and limitations under the License.
14
15static void pooling3x3s2_max_pack4_bf16s_neon(const Mat& bottom_blob, Mat& top_blob, const Option& opt)
16{
17 int w = bottom_blob.w;
18 int inch = bottom_blob.c;
19
20 int outw = top_blob.w;
21 int outh = top_blob.h;
22
23 const int tailstep = (w - 2 * outw + w) * 4;
24
25 #pragma omp parallel for num_threads(opt.num_threads)
26 for (int q = 0; q < inch; q++)
27 {
28 const Mat img0 = bottom_blob.channel(q);
29 unsigned short* outptr = top_blob.channel(q);
30
31 const unsigned short* r0 = img0.row<const unsigned short>(0);
32 const unsigned short* r1 = img0.row<const unsigned short>(1);
33 const unsigned short* r2 = img0.row<const unsigned short>(2);
34
35 for (int i = 0; i < outh; i++)
36 {
37 int j = 0;
38
39 for (; j + 3 < outw; j += 4)
40 {
41#if __aarch64__
42 asm volatile(
43 "prfm pldl1keep, [%1, #256] \n"
44 "ld1 {v0.4h, v1.4h, v2.4h, v3.4h}, [%1], #32 \n"
45
46 "shll v0.4s, v0.4h, #16 \n"
47 "shll v1.4s, v1.4h, #16 \n"
48 "shll v2.4s, v2.4h, #16 \n"
49 "shll v3.4s, v3.4h, #16 \n"
50
51 "fmax v16.4s, v0.4s, v1.4s \n"
52 "fmax v17.4s, v2.4s, v3.4s \n"
53
54 "prfm pldl1keep, [%1, #256] \n"
55 "ld1 {v4.4h, v5.4h, v6.4h, v7.4h}, [%1], #32 \n"
56
57 "shll v4.4s, v4.4h, #16 \n"
58 "shll v5.4s, v5.4h, #16 \n"
59 "shll v6.4s, v6.4h, #16 \n"
60 "shll v7.4s, v7.4h, #16 \n"
61
62 "fmax v18.4s, v4.4s, v5.4s \n"
63 "fmax v19.4s, v6.4s, v7.4s \n"
64
65 "ld1 {v8.4h}, [%1] \n"
66 "shll v8.4s, v8.4h, #16 \n"
67
68 "fmax v20.4s, v16.4s, v2.4s \n"
69 "fmax v21.4s, v17.4s, v4.4s \n"
70
71 "prfm pldl1keep, [%2, #256] \n"
72 "ld1 {v0.4h, v1.4h, v2.4h, v3.4h}, [%2], #32 \n"
73
74 "shll v0.4s, v0.4h, #16 \n"
75 "shll v1.4s, v1.4h, #16 \n"
76 "shll v2.4s, v2.4h, #16 \n"
77 "shll v3.4s, v3.4h, #16 \n"
78
79 "fmax v22.4s, v18.4s, v6.4s \n"
80 "fmax v23.4s, v19.4s, v8.4s \n"
81
82 "prfm pldl1keep, [%2, #256] \n"
83 "ld1 {v4.4h, v5.4h, v6.4h, v7.4h}, [%2], #32 \n"
84
85 "shll v4.4s, v4.4h, #16 \n"
86 "shll v5.4s, v5.4h, #16 \n"
87 "shll v6.4s, v6.4h, #16 \n"
88 "shll v7.4s, v7.4h, #16 \n"
89
90 "fmax v16.4s, v0.4s, v1.4s \n"
91 "fmax v17.4s, v2.4s, v3.4s \n"
92
93 "fmax v18.4s, v4.4s, v5.4s \n"
94 "fmax v19.4s, v6.4s, v7.4s \n"
95
96 "ld1 {v8.4h}, [%2] \n"
97 "shll v8.4s, v8.4h, #16 \n"
98
99 "fmax v24.4s, v16.4s, v2.4s \n"
100 "fmax v25.4s, v17.4s, v4.4s \n"
101
102 "prfm pldl1keep, [%3, #256] \n"
103 "ld1 {v0.4h, v1.4h, v2.4h, v3.4h}, [%3], #32 \n"
104
105 "shll v0.4s, v0.4h, #16 \n"
106 "shll v1.4s, v1.4h, #16 \n"
107 "shll v2.4s, v2.4h, #16 \n"
108 "shll v3.4s, v3.4h, #16 \n"
109
110 "fmax v26.4s, v18.4s, v6.4s \n"
111 "fmax v27.4s, v19.4s, v8.4s \n"
112
113 "prfm pldl1keep, [%3, #256] \n"
114 "ld1 {v4.4h, v5.4h, v6.4h, v7.4h}, [%3], #32 \n"
115
116 "shll v4.4s, v4.4h, #16 \n"
117 "shll v5.4s, v5.4h, #16 \n"
118 "shll v6.4s, v6.4h, #16 \n"
119 "shll v7.4s, v7.4h, #16 \n"
120
121 "fmax v16.4s, v0.4s, v1.4s \n"
122 "fmax v17.4s, v2.4s, v3.4s \n"
123
124 "fmax v18.4s, v4.4s, v5.4s \n"
125 "fmax v19.4s, v6.4s, v7.4s \n"
126
127 "ld1 {v8.4h}, [%3] \n"
128 "shll v8.4s, v8.4h, #16 \n"
129
130 "fmax v28.4s, v16.4s, v2.4s \n"
131 "fmax v29.4s, v17.4s, v4.4s \n"
132 "fmax v30.4s, v18.4s, v6.4s \n"
133 "fmax v31.4s, v19.4s, v8.4s \n"
134
135 "fmax v20.4s, v20.4s, v24.4s \n"
136 "fmax v21.4s, v21.4s, v25.4s \n"
137 "fmax v22.4s, v22.4s, v26.4s \n"
138 "fmax v23.4s, v23.4s, v27.4s \n"
139
140 "fmax v20.4s, v20.4s, v28.4s \n"
141 "fmax v21.4s, v21.4s, v29.4s \n"
142 "fmax v22.4s, v22.4s, v30.4s \n"
143 "fmax v23.4s, v23.4s, v31.4s \n"
144
145 "shrn v20.4h, v20.4s, #16 \n"
146 "shrn v21.4h, v21.4s, #16 \n"
147 "shrn v22.4h, v22.4s, #16 \n"
148 "shrn v23.4h, v23.4s, #16 \n"
149
150 "st1 {v20.4h, v21.4h, v22.4h, v23.4h}, [%0], #32 \n"
151
152 : "=r"(outptr), // %0
153 "=r"(r0), // %1
154 "=r"(r1), // %2
155 "=r"(r2) // %3
156 : "0"(outptr),
157 "1"(r0),
158 "2"(r1),
159 "3"(r2)
160 : "memory", "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23", "v24", "v25", "v26", "v27", "v28", "v29", "v30", "v31");
161#else // __aarch64__
162 asm volatile(
163 "pld [%1, #256] \n"
164 "vld1.u16 {d4-d7}, [%1]! \n"
165
166 "pld [%2, #256] \n"
167 "vld1.u16 {d12-d15}, [%2]! \n"
168
169 "vshll.u16 q0, d4, #16 \n"
170 "vshll.u16 q1, d5, #16 \n"
171 "vshll.u16 q2, d6, #16 \n"
172 "vshll.u16 q3, d7, #16 \n"
173
174 "vshll.u16 q4, d12, #16 \n"
175 "vshll.u16 q5, d13, #16 \n"
176 "vshll.u16 q6, d14, #16 \n"
177 "vshll.u16 q7, d15, #16 \n"
178
179 "vmax.f32 q0, q0, q4 \n"
180 "vmax.f32 q1, q1, q5 \n"
181
182 "pld [%3, #256] \n"
183 "vld1.u16 {d20-d23}, [%3]! \n"
184
185 "vshll.u16 q8, d20, #16 \n"
186 "vshll.u16 q9, d21, #16 \n"
187 "vshll.u16 q10, d22, #16 \n"
188 "vshll.u16 q11, d23, #16 \n"
189
190 "vmax.f32 q2, q2, q6 \n"
191 "vmax.f32 q3, q3, q7 \n"
192
193 "vmax.f32 q0, q0, q8 \n"
194 "vmax.f32 q1, q1, q9 \n"
195
196 "pld [%1, #256] \n"
197 "vld1.u16 {d12-d15}, [%1]! \n"
198
199 "vshll.u16 q4, d12, #16 \n"
200 "vshll.u16 q5, d13, #16 \n"
201 "vshll.u16 q6, d14, #16 \n"
202 "vshll.u16 q7, d15, #16 \n"
203
204 "vmax.f32 q2, q2, q10 \n"
205 "vmax.f32 q3, q3, q11 \n"
206
207 "pld [%2, #256] \n"
208 "vld1.u16 {d20-d23}, [%2]! \n"
209
210 "vshll.u16 q8, d20, #16 \n"
211 "vshll.u16 q9, d21, #16 \n"
212 "vshll.u16 q10, d22, #16 \n"
213 "vshll.u16 q11, d23, #16 \n"
214
215 "vmax.f32 q4, q4, q8 \n"
216 "vmax.f32 q5, q5, q9 \n"
217
218 "pld [%3, #256] \n"
219 "vld1.u16 {d28-d31}, [%3]! \n"
220
221 "vshll.u16 q12, d28, #16 \n"
222 "vshll.u16 q13, d29, #16 \n"
223 "vshll.u16 q14, d30, #16 \n"
224 "vshll.u16 q15, d31, #16 \n"
225
226 "vmax.f32 q6, q6, q10 \n"
227 "vmax.f32 q7, q7, q11 \n"
228
229 "vmax.f32 q4, q4, q12 \n"
230 "vmax.f32 q5, q5, q13 \n"
231
232 "vld1.u16 {d25}, [%1] \n"
233 "vld1.u16 {d27}, [%2] \n"
234 "vshll.u16 q12, d25, #16 \n"
235 "vshll.u16 q13, d27, #16 \n"
236
237 "vmax.f32 q6, q6, q14 \n"
238 "vmax.f32 q7, q7, q15 \n"
239
240 "vld1.u16 {d29}, [%3] \n"
241 "vshll.u16 q14, d29, #16 \n"
242
243 "vmax.f32 q8, q12, q13 \n"
244 "vmax.f32 q8, q8, q14 \n"
245
246 "vmax.f32 q12, q0, q1 \n"
247 "vmax.f32 q13, q2, q3 \n"
248 "vmax.f32 q14, q4, q5 \n"
249 "vmax.f32 q15, q6, q7 \n"
250
251 "vmax.f32 q12, q12, q2 \n"
252 "vmax.f32 q13, q13, q4 \n"
253 "vmax.f32 q14, q14, q6 \n"
254 "vmax.f32 q15, q15, q8 \n"
255
256 "vshrn.u32 d24, q12, #16 \n"
257 "vshrn.u32 d25, q13, #16 \n"
258 "vshrn.u32 d26, q14, #16 \n"
259 "vshrn.u32 d27, q15, #16 \n"
260
261 "vst1.u16 {d24-d27}, [%0]! \n"
262
263 : "=r"(outptr), // %0
264 "=r"(r0), // %1
265 "=r"(r1), // %2
266 "=r"(r2) // %3
267 : "0"(outptr),
268 "1"(r0),
269 "2"(r1),
270 "3"(r2)
271 : "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15");
272#endif // __aarch64__
273 }
274 for (; j + 1 < outw; j += 2)
275 {
276#if __aarch64__
277 asm volatile(
278 "prfm pldl1keep, [%1, #256] \n"
279 "ld1 {v0.4h, v1.4h, v2.4h, v3.4h}, [%1], #32 \n"
280
281 "prfm pldl1keep, [%2, #256] \n"
282 "ld1 {v4.4h, v5.4h, v6.4h, v7.4h}, [%2], #32 \n"
283
284 "shll v0.4s, v0.4h, #16 \n"
285 "shll v1.4s, v1.4h, #16 \n"
286 "shll v2.4s, v2.4h, #16 \n"
287 "shll v3.4s, v3.4h, #16 \n"
288
289 "shll v4.4s, v4.4h, #16 \n"
290 "shll v5.4s, v5.4h, #16 \n"
291 "shll v6.4s, v6.4h, #16 \n"
292 "shll v7.4s, v7.4h, #16 \n"
293
294 "fmax v16.4s, v0.4s, v4.4s \n"
295 "fmax v17.4s, v1.4s, v5.4s \n"
296
297 "prfm pldl1keep, [%3, #256] \n"
298 "ld1 {v20.4h, v21.4h, v22.4h, v23.4h}, [%3], #32 \n"
299
300 "shll v20.4s, v20.4h, #16 \n"
301 "shll v21.4s, v21.4h, #16 \n"
302 "shll v22.4s, v22.4h, #16 \n"
303 "shll v23.4s, v23.4h, #16 \n"
304
305 "fmax v18.4s, v2.4s, v6.4s \n"
306 "fmax v19.4s, v3.4s, v7.4s \n"
307
308 "ld1 {v0.4s}, [%1] \n"
309
310 "fmax v16.4s, v16.4s, v20.4s \n"
311 "fmax v17.4s, v17.4s, v21.4s \n"
312
313 "ld1 {v1.4s}, [%2] \n"
314
315 "fmax v18.4s, v18.4s, v22.4s \n"
316 "fmax v19.4s, v19.4s, v23.4s \n"
317
318 "ld1 {v2.4s}, [%3] \n"
319
320 "fmax v3.4s, v0.4s, v1.4s \n"
321
322 "fmax v20.4s, v16.4s, v17.4s \n"
323 "fmax v21.4s, v18.4s, v19.4s \n"
324
325 "fmax v3.4s, v3.4s, v2.4s \n"
326
327 "fmax v20.4s, v20.4s, v18.4s \n"
328 "fmax v21.4s, v21.4s, v3.4s \n"
329
330 "shrn v20.4h, v20.4s, #16 \n"
331 "shrn v21.4h, v21.4s, #16 \n"
332
333 "st1 {v20.4h, v21.4h}, [%0], #16 \n"
334
335 : "=r"(outptr), // %0
336 "=r"(r0), // %1
337 "=r"(r1), // %2
338 "=r"(r2) // %3
339 : "0"(outptr),
340 "1"(r0),
341 "2"(r1),
342 "3"(r2)
343 : "memory", "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23");
344#else // __aarch64__
345 asm volatile(
346 "pld [%1, #256] \n"
347 "vld1.u16 {d4-d7}, [%1]! \n"
348
349 "pld [%2, #256] \n"
350 "vld1.u16 {d12-d15}, [%2]! \n"
351
352 "vshll.u16 q0, d4, #16 \n"
353 "vshll.u16 q1, d5, #16 \n"
354 "vshll.u16 q2, d6, #16 \n"
355 "vshll.u16 q3, d7, #16 \n"
356
357 "vshll.u16 q4, d12, #16 \n"
358 "vshll.u16 q5, d13, #16 \n"
359 "vshll.u16 q6, d14, #16 \n"
360 "vshll.u16 q7, d15, #16 \n"
361
362 "vmax.f32 q12, q0, q4 \n"
363 "vmax.f32 q13, q1, q5 \n"
364
365 "pld [%3, #256] \n"
366 "vld1.u16 {d20-d23}, [%3]! \n"
367
368 "vshll.u16 q8, d20, #16 \n"
369 "vshll.u16 q9, d21, #16 \n"
370 "vshll.u16 q10, d22, #16 \n"
371 "vshll.u16 q11, d23, #16 \n"
372
373 "vmax.f32 q14, q2, q6 \n"
374 "vmax.f32 q15, q3, q7 \n"
375
376 "vld1.u16 {d1}, [%1] \n"
377 "vshll.u16 q0, d1, #16 \n"
378
379 "vmax.f32 q12, q12, q8 \n"
380 "vmax.f32 q13, q13, q9 \n"
381
382 "vld1.u16 {d3}, [%2] \n"
383 "vshll.u16 q1, d3, #16 \n"
384
385 "vmax.f32 q14, q14, q10 \n"
386 "vmax.f32 q15, q15, q11 \n"
387
388 "vld1.u16 {d5}, [%3] \n"
389 "vshll.u16 q2, d5, #16 \n"
390
391 "vmax.f32 q3, q0, q1 \n"
392
393 "vmax.f32 q4, q12, q13 \n"
394 "vmax.f32 q5, q14, q15 \n"
395
396 "vmax.f32 q3, q3, q2 \n"
397
398 "vmax.f32 q4, q4, q14 \n"
399 "vmax.f32 q5, q5, q3 \n"
400
401 "vshrn.u32 d8, q4, #16 \n"
402 "vshrn.u32 d9, q5, #16 \n"
403
404 "vst1.u16 {d8-d9}, [%0]! \n"
405
406 : "=r"(outptr), // %0
407 "=r"(r0), // %1
408 "=r"(r1), // %2
409 "=r"(r2) // %3
410 : "0"(outptr),
411 "1"(r0),
412 "2"(r1),
413 "3"(r2)
414 : "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15");
415#endif // __aarch64__
416 }
417 for (; j < outw; j++)
418 {
419 float32x4_t _r00 = bfloat2float(vld1_u16(r0));
420 float32x4_t _r01 = bfloat2float(vld1_u16(r0 + 4));
421 float32x4_t _r02 = bfloat2float(vld1_u16(r0 + 8));
422 float32x4_t _r10 = bfloat2float(vld1_u16(r1));
423 float32x4_t _r11 = bfloat2float(vld1_u16(r1 + 4));
424 float32x4_t _r12 = bfloat2float(vld1_u16(r1 + 8));
425 float32x4_t _r20 = bfloat2float(vld1_u16(r2));
426 float32x4_t _r21 = bfloat2float(vld1_u16(r2 + 4));
427 float32x4_t _r22 = bfloat2float(vld1_u16(r2 + 8));
428
429 float32x4_t _max0 = vmaxq_f32(vmaxq_f32(_r00, _r01), _r02);
430 float32x4_t _max1 = vmaxq_f32(vmaxq_f32(_r10, _r11), _r12);
431 float32x4_t _max2 = vmaxq_f32(vmaxq_f32(_r20, _r21), _r22);
432
433 float32x4_t _max = vmaxq_f32(vmaxq_f32(_max0, _max1), _max2);
434
435 vst1_u16(outptr, float2bfloat(_max));
436
437 r0 += 8;
438 r1 += 8;
439 r2 += 8;
440 outptr += 4;
441 }
442
443 r0 += tailstep;
444 r1 += tailstep;
445 r2 += tailstep;
446 }
447 }
448}