Elffuss BraidHub

kikocis/ncnn public

master · 17.7 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2020 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 padding_constant_pack4_neon(const Mat& src, Mat& dst, int top, int bottom, int left, int right, float32x4_t v)
16{
17 const float* ptr = src;
18 float* outptr = dst;
19
20 int w = src.w;
21 int h = src.h;
22
23 int top_size = top * dst.w;
24 int bottom_size = bottom * dst.w;
25
26#if NCNN_GNU_INLINE_ASM
27#if __aarch64__
28 asm volatile(
29 "mov v0.16b, %10.16b \n"
30 "mov v1.16b, %10.16b \n"
31 "mov v2.16b, %10.16b \n"
32 "mov v3.16b, %10.16b \n"
33
34 // fill top
35 "lsr w4, %w8, #3 \n" // w4 = nn = top_size >> 3
36 "cmp w4, #0 \n"
37 "beq 1f \n"
38
39 "0: \n"
40 "st1 {v0.4s, v1.4s, v2.4s, v3.4s}, [%0], #64 \n"
41 "subs w4, w4, #1 \n"
42 "st1 {v0.4s, v1.4s, v2.4s, v3.4s}, [%0], #64 \n"
43 "bne 0b \n"
44
45 "1: \n"
46
47 // fill top remain
48 "and w4, %w8, #7 \n" // w4 = remain = top_size & 7
49
50 "cmp w4, #4 \n" // w4 >= 4
51 "blt 2f \n"
52 "sub w4, w4, #4 \n"
53 "st1 {v0.4s, v1.4s, v2.4s, v3.4s}, [%0], #64 \n"
54 "2: \n"
55
56 "cmp w4, #2 \n" // w4 >= 2
57 "blt 3f \n"
58 "sub w4, w4, #2 \n"
59 "st1 {v0.4s, v1.4s}, [%0], #32 \n"
60 "3: \n"
61
62 "cmp w4, #0 \n" // w4 > 0
63 "beq 4f \n"
64 "st1 {v0.4s}, [%0], #16 \n"
65 "4: \n"
66
67 // fill center h loop
68 "cmp %w5, #0 \n"
69 "beq 15f \n"
70 "5: \n"
71
72 // fill left
73 "mov w4, %w6 \n" // w4 = left
74 "cmp w4, #0 \n"
75 "beq 7f \n"
76
77 "6: \n"
78 "st1 {v0.4s}, [%0], #16 \n"
79 "subs w4, w4, #1 \n"
80 "bne 6b \n"
81
82 "7: \n"
83
84 // fill middle
85 "lsr w4, %w4, #3 \n" // w4 = nn = w >> 3
86 "cmp w4, #0 \n"
87 "beq 9f \n"
88
89 "8: \n"
90 "prfm pldl1keep, [%1, #512] \n"
91 "ld1 {v16.4s, v17.4s, v18.4s, v19.4s}, [%1], #64 \n"
92 "prfm pldl1keep, [%1, #512] \n"
93 "ld1 {v20.4s, v21.4s, v22.4s, v23.4s}, [%1], #64 \n"
94 "subs w4, w4, #1 \n"
95 "st1 {v16.4s, v17.4s, v18.4s, v19.4s}, [%0], #64 \n"
96 "st1 {v20.4s, v21.4s, v22.4s, v23.4s}, [%0], #64 \n"
97 "bne 8b \n"
98
99 "9: \n"
100
101 "and w4, %w4, #7 \n" // w4 = remain = w & 7
102
103 "cmp w4, #4 \n" // w4 >= 4
104 "blt 10f \n"
105 "prfm pldl1keep, [%1, #512] \n"
106 "ld1 {v16.4s, v17.4s, v18.4s, v19.4s}, [%1], #64 \n"
107 "sub w4, w4, #4 \n"
108 "st1 {v16.4s, v17.4s, v18.4s, v19.4s}, [%0], #64 \n"
109 "10: \n"
110
111 "cmp w4, #2 \n" // w4 >= 2
112 "blt 11f \n"
113 "prfm pldl1keep, [%1, #256] \n"
114 "ld1 {v16.4s, v17.4s}, [%1], #32 \n"
115 "sub w4, w4, #2 \n"
116 "st1 {v16.4s, v17.4s}, [%0], #32 \n"
117 "11: \n"
118
119 "cmp w4, #0 \n" // w4 > 0
120 "beq 12f \n"
121 "prfm pldl1keep, [%1, #128] \n"
122 "ld1 {v16.4s}, [%1], #16 \n"
123 "st1 {v16.4s}, [%0], #16 \n"
124 "12: \n"
125
126 // fill right
127 "mov w4, %w7 \n" // w4 = right
128 "cmp w4, #0 \n"
129 "beq 14f \n"
130
131 "13: \n"
132 "subs w4, w4, #1 \n"
133 "st1 {v0.4s}, [%0], #16 \n"
134 "bne 13b \n"
135 "14: \n"
136
137 "subs %w5, %w5, #1 \n"
138 "bne 5b \n"
139
140 "15: \n"
141
142 // fill bottom
143 "lsr w4, %w9, #3 \n" // w4 = nn = bottom_size >> 3
144 "cmp w4, #0 \n"
145 "beq 17f \n"
146
147 "16: \n"
148 "st1 {v0.4s, v1.4s, v2.4s, v3.4s}, [%0], #64 \n"
149 "subs w4, w4, #1 \n"
150 "st1 {v0.4s, v1.4s, v2.4s, v3.4s}, [%0], #64 \n"
151 "bne 16b \n"
152 "17: \n"
153
154 // fill bottom remain
155 "and w4, %w9, #7 \n" // w4 = remain = bottom_size & 7
156
157 "cmp w4, #4 \n" // w4 >= 4
158 "blt 18f \n"
159 "sub w4, w4, #4 \n"
160 "st1 {v0.4s, v1.4s, v2.4s, v3.4s}, [%0], #64 \n"
161 "18: \n"
162
163 "cmp w4, #2 \n" // w4 >= 2
164 "blt 19f \n"
165 "sub w4, w4, #2 \n"
166 "st1 {v0.4s, v1.4s}, [%0], #32 \n"
167 "19: \n"
168
169 "cmp w4, #0 \n" // w4 > 0
170 "beq 20f \n"
171 "st1 {v0.4s}, [%0], #16 \n"
172 "20: \n"
173
174 : "=r"(outptr), // %0
175 "=r"(ptr) // %1
176 : "0"(outptr),
177 "1"(ptr),
178 "r"(w), // %4
179 "r"(h), // %5
180 "r"(left), // %6
181 "r"(right), // %7
182 "r"(top_size), // %8
183 "r"(bottom_size), // %9
184 "w"(v) // %10
185 : "cc", "memory", "x4", "v0", "v1", "v2", "v3", "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23");
186#else // __aarch64__
187 asm volatile(
188 "vmov q0, %q10 \n"
189 "vmov q1, %q10 \n"
190 "vmov q2, %q10 \n"
191 "vmov q3, %q10 \n"
192
193 // fill top
194 "lsr r4, %8, #3 \n" // r4 = nn = top_size >> 3
195 "cmp r4, #0 \n"
196 "beq 1f \n"
197
198 "0: \n"
199 "vstm %0!, {d0-d7} \n"
200 "subs r4, r4, #1 \n"
201 "vstm %0!, {d0-d7} \n"
202 "bne 0b \n"
203
204 "1: \n"
205
206 // fill top remain
207 "and r4, %8, #7 \n" // r4 = remain = top_size & 7
208
209 "cmp r4, #4 \n" // r4 >= 4
210 "blt 2f \n"
211 "sub r4, r4, #4 \n"
212 "vstm %0!, {d0-d7} \n"
213 "2: \n"
214
215 "cmp r4, #2 \n" // r4 >= 2
216 "blt 3f \n"
217 "sub r4, r4, #2 \n"
218 "vst1.f32 {d0-d3}, [%0 :128]! \n"
219 "3: \n"
220
221 "cmp r4, #0 \n" // r4 > 0
222 "beq 4f \n"
223 "vst1.f32 {d0-d1}, [%0 :128]! \n"
224 "4: \n"
225
226 // fill center h loop
227 "cmp %5, #0 \n"
228 "beq 15f \n"
229 "5: \n"
230
231 // fill left
232 "mov r4, %6 \n" // r4 = left
233 "cmp r4, #0 \n"
234 "beq 7f \n"
235
236 "6: \n"
237 "vst1.f32 {d0-d1}, [%0 :128]! \n"
238 "subs r4, r4, #1 \n"
239 "bne 6b \n"
240
241 "7: \n"
242
243 // fill middle
244 "lsr r4, %4, #3 \n" // r4 = nn = w >> 3
245 "cmp r4, #0 \n"
246 "beq 9f \n"
247
248 "8: \n"
249 "pld [%1, #512] \n"
250 "vldm %1!, {d16-d23} \n"
251 "pld [%1, #512] \n"
252 "vldm %1!, {d24-d31} \n"
253 "subs r4, r4, #1 \n"
254 "vstm %0!, {d16-d23} \n"
255 "vstm %0!, {d24-d31} \n"
256 "bne 8b \n"
257
258 "9: \n"
259
260 "and r4, %4, #7 \n" // r4 = remain = w & 7
261
262 "cmp r4, #4 \n" // r4 >= 4
263 "blt 10f \n"
264 "pld [%1, #512] \n"
265 "vldm %1!, {d16-d23} \n"
266 "sub r4, r4, #4 \n"
267 "vstm %0!, {d16-d23} \n"
268 "10: \n"
269
270 "cmp r4, #2 \n" // r4 >= 2
271 "blt 11f \n"
272 "pld [%1, #256] \n"
273 "vld1.f32 {d16-d19}, [%1 :128]! \n"
274 "sub r4, r4, #2 \n"
275 "vst1.f32 {d16-d19}, [%0 :128]! \n"
276 "11: \n"
277
278 "cmp r4, #0 \n" // r4 > 0
279 "beq 12f \n"
280 "pld [%1, #128] \n"
281 "vld1.f32 {d16-d17}, [%1 :128]! \n"
282 "vst1.f32 {d16-d17}, [%0 :128]! \n"
283 "12: \n"
284
285 // fill right
286 "mov r4, %7 \n" // r4 = right
287 "cmp r4, #0 \n"
288 "beq 14f \n"
289
290 "13: \n"
291 "subs r4, r4, #1 \n"
292 "vst1.f32 {d0-d1}, [%0 :128]! \n"
293 "bne 13b \n"
294 "14: \n"
295
296 "subs %5, %5, #1 \n"
297 "bne 5b \n"
298
299 "15: \n"
300
301 // fill bottom
302 "lsr r4, %9, #3 \n" // r4 = nn = bottom_size >> 3
303 "cmp r4, #0 \n"
304 "beq 17f \n"
305
306 "16: \n"
307 "vstm %0!, {d0-d7} \n"
308 "subs r4, r4, #1 \n"
309 "vstm %0!, {d0-d7} \n"
310 "bne 16b \n"
311 "17: \n"
312
313 // fill bottom remain
314 "and r4, %9, #7 \n" // r4 = remain = bottom_size & 7
315
316 "cmp r4, #4 \n" // r4 >= 4
317 "blt 18f \n"
318 "sub r4, r4, #4 \n"
319 "vstm %0!, {d0-d7} \n"
320 "18: \n"
321
322 "cmp r4, #2 \n" // r4 >= 2
323 "blt 19f \n"
324 "sub r4, r4, #2 \n"
325 "vst1.f32 {d0-d3}, [%0 :128]! \n"
326 "19: \n"
327
328 "cmp r4, #0 \n" // r4 > 0
329 "beq 20f \n"
330 "vst1.f32 {d0-d1}, [%0 :128]! \n"
331 "20: \n"
332
333 : "=r"(outptr), // %0
334 "=r"(ptr) // %1
335 : "0"(outptr),
336 "1"(ptr),
337 "r"(w), // %4
338 "r"(h), // %5
339 "r"(left), // %6
340 "r"(right), // %7
341 "r"(top_size), // %8
342 "r"(bottom_size), // %9
343 "w"(v) // %10
344 : "cc", "memory", "r4", "q0", "q1", "q2", "q3", "q8", "q9", "q10", "q11", "q12", "q13", "q14", "q15");
345#endif // __aarch64__
346#else // NCNN_GNU_INLINE_ASM
347
348 // fill top
349 {
350 int x = 0;
351 for (; x + 3 < top_size; x += 4)
352 {
353 vst1q_f32(outptr, v);
354 vst1q_f32(outptr + 4, v);
355 vst1q_f32(outptr + 8, v);
356 vst1q_f32(outptr + 12, v);
357 outptr += 16;
358 }
359 for (; x < top_size; x++)
360 {
361 vst1q_f32(outptr, v);
362 outptr += 4;
363 }
364 }
365 // fill center
366 for (int y = 0; y < src.h; y++)
367 {
368 for (int x = 0; x < left; x++)
369 {
370 vst1q_f32(outptr, v);
371 outptr += 4;
372 }
373 for (int x = 0; x < src.w; x++)
374 {
375 float32x4_t _p = vld1q_f32(ptr);
376 vst1q_f32(outptr, _p);
377 ptr += 4;
378 outptr += 4;
379 }
380 for (int x = 0; x < right; x++)
381 {
382 vst1q_f32(outptr, v);
383 outptr += 4;
384 }
385 }
386 // fill bottom
387 {
388 int x = 0;
389 for (; x + 3 < bottom_size; x += 4)
390 {
391 vst1q_f32(outptr, v);
392 vst1q_f32(outptr + 4, v);
393 vst1q_f32(outptr + 8, v);
394 vst1q_f32(outptr + 12, v);
395 outptr += 16;
396 }
397 for (; x < bottom_size; x++)
398 {
399 vst1q_f32(outptr, v);
400 outptr += 4;
401 }
402 }
403#endif // NCNN_GNU_INLINE_ASM
404}
405
406static void padding_replicate_pack4_neon(const Mat& src, Mat& dst, int top, int bottom, int left, int right)
407{
408 const float* ptr = src;
409 float* outptr = dst;
410
411 // fill top
412 for (int y = 0; y < top; y++)
413 {
414 const float* ptr0 = ptr;
415 float32x4_t _p = vld1q_f32(ptr0);
416 for (int x = 0; x < left; x++)
417 {
418 vst1q_f32(outptr, _p);
419 outptr += 4;
420 }
421 for (int x = 0; x < src.w; x++)
422 {
423 _p = vld1q_f32(ptr0);
424 vst1q_f32(outptr, _p);
425 ptr0 += 4;
426 outptr += 4;
427 }
428 for (int x = 0; x < right; x++)
429 {
430 vst1q_f32(outptr, _p);
431 outptr += 4;
432 }
433 }
434 // fill center
435 for (int y = 0; y < src.h; y++)
436 {
437 float32x4_t _p = vld1q_f32(ptr);
438 for (int x = 0; x < left; x++)
439 {
440 vst1q_f32(outptr, _p);
441 outptr += 4;
442 }
443 for (int x = 0; x < src.w; x++)
444 {
445 _p = vld1q_f32(ptr);
446 vst1q_f32(outptr, _p);
447 ptr += 4;
448 outptr += 4;
449 }
450 for (int x = 0; x < right; x++)
451 {
452 vst1q_f32(outptr, _p);
453 outptr += 4;
454 }
455 }
456 // fill bottom
457 ptr -= src.w * 4;
458 for (int y = 0; y < bottom; y++)
459 {
460 const float* ptr0 = ptr;
461 float32x4_t _p = vld1q_f32(ptr0);
462 for (int x = 0; x < left; x++)
463 {
464 vst1q_f32(outptr, _p);
465 outptr += 4;
466 }
467 for (int x = 0; x < src.w; x++)
468 {
469 _p = vld1q_f32(ptr0);
470 vst1q_f32(outptr, _p);
471 ptr0 += 4;
472 outptr += 4;
473 }
474 for (int x = 0; x < right; x++)
475 {
476 vst1q_f32(outptr, _p);
477 outptr += 4;
478 }
479 }
480}
481
482static void padding_reflect_pack4_neon(const Mat& src, Mat& dst, int top, int bottom, int left, int right)
483{
484 const float* ptr = src;
485 float* outptr = dst;
486
487 // fill top
488 ptr += top * src.w * 4;
489 for (int y = 0; y < top; y++)
490 {
491 const float* ptr0 = ptr;
492 for (int x = 0; x < left; x++)
493 {
494 float32x4_t _p = vld1q_f32(ptr0 + (left - x) * 4);
495 vst1q_f32(outptr, _p);
496 outptr += 4;
497 }
498 for (int x = 0; x < src.w; x++)
499 {
500 float32x4_t _p = vld1q_f32(ptr0);
501 vst1q_f32(outptr, _p);
502 ptr0 += 4;
503 outptr += 4;
504 }
505 for (int x = 0; x < right; x++)
506 {
507 float32x4_t _p = vld1q_f32(ptr0 - 8 - x * 4);
508 vst1q_f32(outptr, _p);
509 outptr += 4;
510 }
511 ptr -= src.w * 4;
512 }
513 // fill center
514 for (int y = 0; y < src.h; y++)
515 {
516 for (int x = 0; x < left; x++)
517 {
518 float32x4_t _p = vld1q_f32(ptr + (left - x) * 4);
519 vst1q_f32(outptr, _p);
520 outptr += 4;
521 }
522 for (int x = 0; x < src.w; x++)
523 {
524 float32x4_t _p = vld1q_f32(ptr);
525 vst1q_f32(outptr, _p);
526 ptr += 4;
527 outptr += 4;
528 }
529 for (int x = 0; x < right; x++)
530 {
531 float32x4_t _p = vld1q_f32(ptr - 8 - x * 4);
532 vst1q_f32(outptr, _p);
533 outptr += 4;
534 }
535 }
536 // fill bottom
537 ptr -= 2 * src.w * 4;
538 for (int y = 0; y < bottom; y++)
539 {
540 const float* ptr0 = ptr;
541 for (int x = 0; x < left; x++)
542 {
543 float32x4_t _p = vld1q_f32(ptr0 + (left - x) * 4);
544 vst1q_f32(outptr, _p);
545 outptr += 4;
546 }
547 for (int x = 0; x < src.w; x++)
548 {
549 float32x4_t _p = vld1q_f32(ptr0);
550 vst1q_f32(outptr, _p);
551 ptr0 += 4;
552 outptr += 4;
553 }
554 for (int x = 0; x < right; x++)
555 {
556 float32x4_t _p = vld1q_f32(ptr0 - 8 - x * 4);
557 vst1q_f32(outptr, _p);
558 outptr += 4;
559 }
560 ptr -= src.w * 4;
561 }
562}