Elffuss BraidHub

kikocis/ncnn public

master · 11.0 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_pack8_fp16s_neon(const Mat& src, Mat& dst, int top, int bottom, int left, int right, uint16x8_t v)
16{
17 const unsigned short* ptr = src;
18 unsigned short* 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 asm volatile(
28 "mov v0.16b, %10.16b \n"
29 "mov v1.16b, %10.16b \n"
30 "mov v2.16b, %10.16b \n"
31 "mov v3.16b, %10.16b \n"
32
33 // fill top
34 "lsr w4, %w8, #2 \n" // w4 = nn = top_size >> 2
35 "cmp w4, #0 \n"
36 "beq 1f \n"
37
38 "0: \n"
39 "st1 {v0.8h, v1.8h, v2.8h, v3.8h}, [%0], #64 \n"
40 "subs w4, w4, #1 \n"
41 "bne 0b \n"
42
43 "1: \n"
44
45 // fill top remain
46 "and w4, %w8, #3 \n" // w4 = remain = top_size & 3
47
48 "cmp w4, #2 \n" // w4 >= 2
49 "blt 2f \n"
50 "sub w4, w4, #2 \n"
51 "st1 {v0.8h, v1.8h}, [%0], #32 \n"
52 "2: \n"
53
54 "cmp w4, #0 \n" // w4 > 0
55 "beq 3f \n"
56 "st1 {v0.8h}, [%0], #16 \n"
57 "3: \n"
58
59 // fill center h loop
60 "cmp %w5, #0 \n"
61 "beq 13f \n"
62 "4: \n"
63
64 // fill left
65 "mov w4, %w6 \n" // w4 = left
66 "cmp w4, #0 \n"
67 "beq 6f \n"
68
69 "5: \n"
70 "st1 {v0.8h}, [%0], #16 \n"
71 "subs w4, w4, #1 \n"
72 "bne 5b \n"
73
74 "6: \n"
75
76 // fill middle
77 "lsr w4, %w4, #2 \n" // w4 = nn = w >> 2
78 "cmp w4, #0 \n"
79 "beq 8f \n"
80
81 "7: \n"
82 "prfm pldl1keep, [%1, #512] \n"
83 "ld1 {v16.8h, v17.8h, v18.8h, v19.8h}, [%1], #64 \n"
84 "subs w4, w4, #1 \n"
85 "st1 {v16.8h, v17.8h, v18.8h, v19.8h}, [%0], #64 \n"
86 "bne 7b \n"
87
88 "8: \n"
89
90 "and w4, %w4, #3 \n" // w4 = remain = w & 3
91
92 "cmp w4, #2 \n" // w4 >= 2
93 "blt 9f \n"
94 "prfm pldl1keep, [%1, #256] \n"
95 "ld1 {v16.8h, v17.8h}, [%1], #32 \n"
96 "sub w4, w4, #2 \n"
97 "st1 {v16.8h, v17.8h}, [%0], #32 \n"
98 "9: \n"
99
100 "cmp w4, #0 \n" // w4 > 0
101 "beq 10f \n"
102 "prfm pldl1keep, [%1, #128] \n"
103 "ld1 {v16.8h}, [%1], #16 \n"
104 "st1 {v16.8h}, [%0], #16 \n"
105 "10: \n"
106
107 // fill right
108 "mov w4, %w7 \n" // w4 = right
109 "cmp w4, #0 \n"
110 "beq 12f \n"
111
112 "11: \n"
113 "subs w4, w4, #1 \n"
114 "st1 {v0.8h}, [%0], #16 \n"
115 "bne 11b \n"
116 "12: \n"
117
118 "subs %w5, %w5, #1 \n"
119 "bne 4b \n"
120
121 "13: \n"
122
123 // fill bottom
124 "lsr w4, %w9, #2 \n" // w4 = nn = bottom_size >> 2
125 "cmp w4, #0 \n"
126 "beq 15f \n"
127
128 "14: \n"
129 "st1 {v0.8h, v1.8h, v2.8h, v3.8h}, [%0], #64 \n"
130 "subs w4, w4, #1 \n"
131 "bne 14b \n"
132 "15: \n"
133
134 // fill bottom remain
135 "and w4, %w9, #3 \n" // w4 = remain = bottom_size & 3
136
137 "cmp w4, #2 \n" // w4 >= 2
138 "blt 16f \n"
139 "sub w4, w4, #2 \n"
140 "st1 {v0.8h, v1.8h}, [%0], #32 \n"
141 "16: \n"
142
143 "cmp w4, #0 \n" // w4 > 0
144 "beq 17f \n"
145 "st1 {v0.8h}, [%0], #16 \n"
146 "17: \n"
147
148 : "=r"(outptr), // %0
149 "=r"(ptr) // %1
150 : "0"(outptr),
151 "1"(ptr),
152 "r"(w), // %4
153 "r"(h), // %5
154 "r"(left), // %6
155 "r"(right), // %7
156 "r"(top_size), // %8
157 "r"(bottom_size), // %9
158 "w"(v) // %10
159 : "cc", "memory", "x4", "v0", "v1", "v2", "v3", "v16", "v17", "v18", "v19");
160#else // NCNN_GNU_INLINE_ASM
161
162 // fill top
163 {
164 int x = 0;
165 for (; x + 3 < top_size; x += 4)
166 {
167 vst1q_u16(outptr, v);
168 vst1q_u16(outptr + 8, v);
169 vst1q_u16(outptr + 16, v);
170 vst1q_u16(outptr + 24, v);
171 outptr += 32;
172 }
173 for (; x < top_size; x++)
174 {
175 vst1q_u16(outptr, v);
176 outptr += 8;
177 }
178 }
179 // fill center
180 for (int y = 0; y < src.h; y++)
181 {
182 for (int x = 0; x < left; x++)
183 {
184 vst1q_u16(outptr, v);
185 outptr += 8;
186 }
187 for (int x = 0; x < src.w; x++)
188 {
189 uint16x8_t _p = vld1q_u16(ptr);
190 vst1q_u16(outptr, _p);
191 ptr += 8;
192 outptr += 8;
193 }
194 for (int x = 0; x < right; x++)
195 {
196 vst1q_u16(outptr, v);
197 outptr += 8;
198 }
199 }
200 // fill bottom
201 {
202 int x = 0;
203 for (; x + 3 < bottom_size; x += 4)
204 {
205 vst1q_u16(outptr, v);
206 vst1q_u16(outptr + 8, v);
207 vst1q_u16(outptr + 16, v);
208 vst1q_u16(outptr + 24, v);
209 outptr += 32;
210 }
211 for (; x < bottom_size; x++)
212 {
213 vst1q_u16(outptr, v);
214 outptr += 8;
215 }
216 }
217#endif // NCNN_GNU_INLINE_ASM
218}
219
220static void padding_replicate_pack8_fp16s_neon(const Mat& src, Mat& dst, int top, int bottom, int left, int right)
221{
222 const unsigned short* ptr = src;
223 unsigned short* outptr = dst;
224
225 // fill top
226 for (int y = 0; y < top; y++)
227 {
228 const unsigned short* ptr0 = ptr;
229 uint16x8_t _p = vld1q_u16(ptr0);
230 for (int x = 0; x < left; x++)
231 {
232 vst1q_u16(outptr, _p);
233 outptr += 8;
234 }
235 for (int x = 0; x < src.w; x++)
236 {
237 _p = vld1q_u16(ptr0);
238 vst1q_u16(outptr, _p);
239 ptr0 += 8;
240 outptr += 8;
241 }
242 for (int x = 0; x < right; x++)
243 {
244 vst1q_u16(outptr, _p);
245 outptr += 8;
246 }
247 }
248 // fill center
249 for (int y = 0; y < src.h; y++)
250 {
251 uint16x8_t _p = vld1q_u16(ptr);
252 for (int x = 0; x < left; x++)
253 {
254 vst1q_u16(outptr, _p);
255 outptr += 8;
256 }
257 for (int x = 0; x < src.w; x++)
258 {
259 _p = vld1q_u16(ptr);
260 vst1q_u16(outptr, _p);
261 ptr += 8;
262 outptr += 8;
263 }
264 for (int x = 0; x < right; x++)
265 {
266 vst1q_u16(outptr, _p);
267 outptr += 8;
268 }
269 }
270 // fill bottom
271 ptr -= src.w * 8;
272 for (int y = 0; y < bottom; y++)
273 {
274 const unsigned short* ptr0 = ptr;
275 uint16x8_t _p = vld1q_u16(ptr0);
276 for (int x = 0; x < left; x++)
277 {
278 vst1q_u16(outptr, _p);
279 outptr += 8;
280 }
281 for (int x = 0; x < src.w; x++)
282 {
283 _p = vld1q_u16(ptr0);
284 vst1q_u16(outptr, _p);
285 ptr0 += 8;
286 outptr += 8;
287 }
288 for (int x = 0; x < right; x++)
289 {
290 vst1q_u16(outptr, _p);
291 outptr += 8;
292 }
293 }
294}
295
296static void padding_reflect_pack8_fp16s_neon(const Mat& src, Mat& dst, int top, int bottom, int left, int right)
297{
298 const unsigned short* ptr = src;
299 unsigned short* outptr = dst;
300
301 // fill top
302 ptr += top * src.w * 8;
303 for (int y = 0; y < top; y++)
304 {
305 const unsigned short* ptr0 = ptr;
306 for (int x = 0; x < left; x++)
307 {
308 uint16x8_t _p = vld1q_u16(ptr0 + (left - x) * 8);
309 vst1q_u16(outptr, _p);
310 outptr += 8;
311 }
312 for (int x = 0; x < src.w; x++)
313 {
314 uint16x8_t _p = vld1q_u16(ptr0);
315 vst1q_u16(outptr, _p);
316 ptr0 += 8;
317 outptr += 8;
318 }
319 for (int x = 0; x < right; x++)
320 {
321 uint16x8_t _p = vld1q_u16(ptr0 - 16 - x * 8);
322 vst1q_u16(outptr, _p);
323 outptr += 8;
324 }
325 ptr -= src.w * 8;
326 }
327 // fill center
328 for (int y = 0; y < src.h; y++)
329 {
330 for (int x = 0; x < left; x++)
331 {
332 uint16x8_t _p = vld1q_u16(ptr + (left - x) * 8);
333 vst1q_u16(outptr, _p);
334 outptr += 8;
335 }
336 for (int x = 0; x < src.w; x++)
337 {
338 uint16x8_t _p = vld1q_u16(ptr);
339 vst1q_u16(outptr, _p);
340 ptr += 8;
341 outptr += 8;
342 }
343 for (int x = 0; x < right; x++)
344 {
345 uint16x8_t _p = vld1q_u16(ptr - 16 - x * 8);
346 vst1q_u16(outptr, _p);
347 outptr += 8;
348 }
349 }
350 // fill bottom
351 ptr -= 2 * src.w * 8;
352 for (int y = 0; y < bottom; y++)
353 {
354 const unsigned short* ptr0 = ptr;
355 for (int x = 0; x < left; x++)
356 {
357 uint16x8_t _p = vld1q_u16(ptr0 + (left - x) * 8);
358 vst1q_u16(outptr, _p);
359 outptr += 8;
360 }
361 for (int x = 0; x < src.w; x++)
362 {
363 uint16x8_t _p = vld1q_u16(ptr0);
364 vst1q_u16(outptr, _p);
365 ptr0 += 8;
366 outptr += 8;
367 }
368 for (int x = 0; x < right; x++)
369 {
370 uint16x8_t _p = vld1q_u16(ptr0 - 16 - x * 8);
371 vst1q_u16(outptr, _p);
372 outptr += 8;
373 }
374 ptr -= src.w * 8;
375 }
376}