Elffuss BraidHub

kikocis/ncnn public

master · 26.3 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2019 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
15#include "reshape_arm.h"
16
17#if __ARM_NEON
18#include <arm_neon.h>
19#endif // __ARM_NEON
20
21#include "cpu.h"
22
23namespace ncnn {
24
25Reshape_arm::Reshape_arm()
26{
27#if __ARM_NEON
28 support_packing = true;
29#if NCNN_ARM82
30 support_fp16_storage = cpu_support_arm_asimdhp();
31#endif
32#endif // __ARM_NEON
33
34#if NCNN_BF16
35 support_bf16_storage = true;
36#endif
37}
38
39int Reshape_arm::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
40{
41 int elembits = bottom_blob.elembits();
42
43#if NCNN_ARM82
44 if (support_fp16_storage && opt.use_fp16_storage && elembits == 16)
45 return forward_bf16s_fp16s(bottom_blob, top_blob, opt);
46#endif
47
48#if NCNN_BF16
49 if (opt.use_bf16_storage && elembits == 16)
50 return forward_bf16s_fp16s(bottom_blob, top_blob, opt);
51#endif
52
53 int elempack = bottom_blob.elempack;
54
55 if (permute == 1)
56 {
57 // TODO implement permute on-the-fly
58 Option opt_pack = opt;
59 opt_pack.blob_allocator = opt.workspace_allocator;
60
61 Mat bottom_blob_unpacked;
62 convert_packing(bottom_blob, bottom_blob_unpacked, 1, opt_pack);
63
64 Mat top_blob_unpacked;
65 int ret = Reshape::forward(bottom_blob_unpacked, top_blob_unpacked, opt_pack);
66 if (ret != 0)
67 return ret;
68
69 int out_elempack = 1;
70 if (opt.use_packing_layout)
71 {
72 // resolve dst_elempack
73 int dims = top_blob_unpacked.dims;
74 if (dims == 1) out_elempack = top_blob_unpacked.w % 4 == 0 ? 4 : 1;
75 if (dims == 2) out_elempack = top_blob_unpacked.h % 4 == 0 ? 4 : 1;
76 if (dims == 3 || dims == 4) out_elempack = top_blob_unpacked.c % 4 == 0 ? 4 : 1;
77 }
78 convert_packing(top_blob_unpacked, top_blob, out_elempack, opt);
79
80 return 0;
81 }
82
83 if (ndim == 1)
84 {
85 // flatten
86 flatten(bottom_blob, top_blob, opt);
87 if (top_blob.empty())
88 return -100;
89
90 return 0;
91 }
92
93 int dims = bottom_blob.dims;
94 size_t elemsize = bottom_blob.elemsize;
95
96 int total = bottom_blob.w * bottom_blob.h * bottom_blob.d * bottom_blob.c * elempack;
97
98 if (ndim == 2)
99 {
100 int _w = w;
101 int _h = h;
102
103 if (_w == 0)
104 _w = dims == 1 ? bottom_blob.w * elempack : bottom_blob.w;
105 if (_h == 0)
106 _h = dims == 2 ? bottom_blob.h * elempack : bottom_blob.h;
107
108 if (_w == -1)
109 _w = total / _h;
110 if (_h == -1)
111 _h = total / _w;
112
113 int out_elempack = opt.use_packing_layout && _h % 4 == 0 ? 4 : 1;
114 size_t out_elemsize = elemsize / elempack * out_elempack;
115
116 if (dims == 2 && bottom_blob.h * elempack == _h && elempack == out_elempack)
117 {
118 top_blob = bottom_blob;
119 return 0;
120 }
121
122 if (out_elempack == 1)
123 {
124 // flatten
125 flatten(bottom_blob, top_blob, opt);
126 if (top_blob.empty())
127 return -100;
128
129 top_blob.dims = 2;
130 top_blob.w = _w;
131 top_blob.h = _h;
132 top_blob.cstep = _w * _h;
133 top_blob.elemsize = out_elemsize;
134 top_blob.elempack = out_elempack;
135
136 return 0;
137 }
138
139 // flatten
140 Mat bottom_blob_flattened = bottom_blob;
141 {
142 Option opt_flatten = opt;
143 opt_flatten.blob_allocator = opt.workspace_allocator;
144
145 flatten(bottom_blob, bottom_blob_flattened, opt_flatten);
146 if (bottom_blob_flattened.empty())
147 return -100;
148 }
149
150 top_blob.create(_w, _h / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
151 if (top_blob.empty())
152 return -100;
153
154 int outw = top_blob.w;
155 int outh = top_blob.h;
156
157 // assert out_elempack == 4
158
159 #pragma omp parallel for num_threads(opt.num_threads)
160 for (int i = 0; i < outh; i++)
161 {
162 const float* ptr0 = (const float*)bottom_blob_flattened + outw * i * 4;
163 const float* ptr1 = (const float*)bottom_blob_flattened + outw * (i * 4 + 1);
164 const float* ptr2 = (const float*)bottom_blob_flattened + outw * (i * 4 + 2);
165 const float* ptr3 = (const float*)bottom_blob_flattened + outw * (i * 4 + 3);
166 float* outptr = (float*)top_blob.row(i);
167
168 int j = 0;
169#if __ARM_NEON
170 for (; j + 3 < outw; j += 4)
171 {
172 float32x4x4_t _v4;
173 _v4.val[0] = vld1q_f32(ptr0);
174 _v4.val[1] = vld1q_f32(ptr1);
175 _v4.val[2] = vld1q_f32(ptr2);
176 _v4.val[3] = vld1q_f32(ptr3);
177
178 vst4q_f32(outptr, _v4);
179
180 ptr0 += 4;
181 ptr1 += 4;
182 ptr2 += 4;
183 ptr3 += 4;
184 outptr += 16;
185 }
186#endif
187 for (; j < outw; j++)
188 {
189 outptr[0] = *ptr0++;
190 outptr[1] = *ptr1++;
191 outptr[2] = *ptr2++;
192 outptr[3] = *ptr3++;
193
194 outptr += 4;
195 }
196 }
197 }
198
199 if (ndim == 3 || ndim == 4)
200 {
201 int _w = w;
202 int _h = h;
203 int _d = d;
204 int _c = c;
205
206 if (ndim == 3)
207 {
208 if (_w == 0)
209 _w = dims == 1 ? bottom_blob.w * elempack : bottom_blob.w;
210 if (_h == 0)
211 _h = dims == 2 ? bottom_blob.h * elempack : bottom_blob.h;
212 if (_c == 0)
213 _c = dims == 3 ? bottom_blob.c * elempack : bottom_blob.c;
214
215 if (_w == -1)
216 _w = total / _c / _h;
217 if (_h == -1)
218 _h = total / _c / _w;
219 if (_c == -1)
220 _c = total / _h / _w;
221
222 _d = 1;
223 }
224 else // if (ndim == 4)
225 {
226 if (_w == 0)
227 _w = dims == 1 ? bottom_blob.w * elempack : bottom_blob.w;
228 if (_h == 0)
229 _h = dims == 2 ? bottom_blob.h * elempack : bottom_blob.h;
230 if (_d == 0)
231 _d = bottom_blob.d;
232 if (_c == 0)
233 _c = (dims == 3 || dims == 4) ? bottom_blob.c * elempack : bottom_blob.c;
234
235 if (_w == -1)
236 _w = total / _c / _d / _h;
237 if (_h == -1)
238 _h = total / _c / _d / _w;
239 if (_d == -1)
240 _d = total / _c / _h / _w;
241 if (_c == -1)
242 _c = total / _d / _h / _w;
243 }
244
245 int out_elempack = opt.use_packing_layout && _c % 4 == 0 ? 4 : 1;
246 size_t out_elemsize = elemsize / elempack * out_elempack;
247
248 if ((dims == 3 || dims == 4) && bottom_blob.c * elempack == _c && elempack == out_elempack)
249 {
250 top_blob = bottom_blob;
251 top_blob.dims = ndim;
252 top_blob.w = _w;
253 top_blob.h = _h;
254 top_blob.d = _d;
255 return 0;
256 }
257
258 // flatten
259 Mat bottom_blob_flattened = bottom_blob;
260 {
261 Option opt_flatten = opt;
262 opt_flatten.blob_allocator = opt.workspace_allocator;
263
264 flatten(bottom_blob, bottom_blob_flattened, opt_flatten);
265 if (bottom_blob_flattened.empty())
266 return -100;
267 }
268
269 if (ndim == 3)
270 {
271 top_blob.create(_w, _h, _c / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
272 }
273 else // if (ndim == 4)
274 {
275 top_blob.create(_w, _h, _d, _c / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
276 }
277 if (top_blob.empty())
278 return -100;
279
280 int size = top_blob.w * top_blob.h * top_blob.d;
281
282 if (out_elempack == 4)
283 {
284 #pragma omp parallel for num_threads(opt.num_threads)
285 for (int q = 0; q < top_blob.c; q++)
286 {
287 const float* ptr0 = (const float*)bottom_blob_flattened + size * q * 4;
288 const float* ptr1 = (const float*)bottom_blob_flattened + size * (q * 4 + 1);
289 const float* ptr2 = (const float*)bottom_blob_flattened + size * (q * 4 + 2);
290 const float* ptr3 = (const float*)bottom_blob_flattened + size * (q * 4 + 3);
291 float* outptr = top_blob.channel(q);
292
293 int i = 0;
294#if __ARM_NEON
295 for (; i + 3 < size; i += 4)
296 {
297 float32x4x4_t _v4;
298 _v4.val[0] = vld1q_f32(ptr0);
299 _v4.val[1] = vld1q_f32(ptr1);
300 _v4.val[2] = vld1q_f32(ptr2);
301 _v4.val[3] = vld1q_f32(ptr3);
302
303 vst4q_f32(outptr, _v4);
304
305 ptr0 += 4;
306 ptr1 += 4;
307 ptr2 += 4;
308 ptr3 += 4;
309 outptr += 16;
310 }
311#endif
312 for (; i < size; i++)
313 {
314 outptr[0] = *ptr0++;
315 outptr[1] = *ptr1++;
316 outptr[2] = *ptr2++;
317 outptr[3] = *ptr3++;
318
319 outptr += 4;
320 }
321 }
322 }
323
324 if (out_elempack == 1)
325 {
326 #pragma omp parallel for num_threads(opt.num_threads)
327 for (int q = 0; q < top_blob.c; q++)
328 {
329 const float* ptr = (const float*)bottom_blob_flattened + size * q;
330 float* outptr = top_blob.channel(q);
331
332 int i = 0;
333#if __ARM_NEON
334 for (; i + 3 < size; i += 4)
335 {
336 float32x4_t _v = vld1q_f32(ptr);
337 vst1q_f32(outptr, _v);
338 ptr += 4;
339 outptr += 4;
340 }
341#endif
342 for (; i < size; i++)
343 {
344 *outptr++ = *ptr++;
345 }
346 }
347 }
348 }
349
350 return 0;
351}
352
353int Reshape_arm::forward_bf16s_fp16s(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
354{
355 int elempack = bottom_blob.elempack;
356
357 if (permute == 1)
358 {
359 // TODO implement permute on-the-fly
360 Option opt_pack = opt;
361 opt_pack.blob_allocator = opt.workspace_allocator;
362
363 Mat bottom_blob_unpacked;
364 convert_packing(bottom_blob, bottom_blob_unpacked, 1, opt_pack);
365
366 Mat bottom_blob_unpacked_fp32;
367 cast_bfloat16_to_float32(bottom_blob_unpacked, bottom_blob_unpacked_fp32, opt_pack);
368
369 Mat top_blob_unpacked_fp32;
370 int ret = Reshape::forward(bottom_blob_unpacked_fp32, top_blob_unpacked_fp32, opt_pack);
371 if (ret != 0)
372 return ret;
373
374 Mat top_blob_unpacked;
375 cast_float32_to_bfloat16(top_blob_unpacked_fp32, top_blob_unpacked, opt_pack);
376
377 int out_elempack = 1;
378 if (opt.use_packing_layout)
379 {
380 // resolve dst_elempack
381 int dims = top_blob_unpacked.dims;
382#if NCNN_ARM82
383 if (dims == 1) out_elempack = support_fp16_storage && opt.use_fp16_arithmetic && top_blob_unpacked.w % 8 == 0 ? 8 : top_blob_unpacked.w % 4 == 0 ? 4 : 1;
384 if (dims == 2) out_elempack = support_fp16_storage && opt.use_fp16_arithmetic && top_blob_unpacked.h % 8 == 0 ? 8 : top_blob_unpacked.h % 4 == 0 ? 4 : 1;
385 if (dims == 3 || dims == 4) out_elempack = support_fp16_storage && opt.use_fp16_arithmetic && top_blob_unpacked.c % 8 == 0 ? 8 : top_blob_unpacked.c % 4 == 0 ? 4 : 1;
386#else
387 if (dims == 1) out_elempack = top_blob_unpacked.w % 4 == 0 ? 4 : 1;
388 if (dims == 2) out_elempack = top_blob_unpacked.h % 4 == 0 ? 4 : 1;
389 if (dims == 3 || dims == 4) out_elempack = top_blob_unpacked.c % 4 == 0 ? 4 : 1;
390#endif
391 }
392 convert_packing(top_blob_unpacked, top_blob, out_elempack, opt);
393
394 return 0;
395 }
396
397 if (ndim == 1)
398 {
399 // flatten
400 flatten(bottom_blob, top_blob, opt);
401 if (top_blob.empty())
402 return -100;
403
404 return 0;
405 }
406
407 int dims = bottom_blob.dims;
408 size_t elemsize = bottom_blob.elemsize;
409
410 int total = bottom_blob.w * bottom_blob.h * bottom_blob.d * bottom_blob.c * elempack;
411
412 if (ndim == 2)
413 {
414 int _w = w;
415 int _h = h;
416
417 if (_w == 0)
418 _w = dims == 1 ? bottom_blob.w * elempack : bottom_blob.w;
419 if (_h == 0)
420 _h = dims == 2 ? bottom_blob.h * elempack : bottom_blob.h;
421
422 if (_w == -1)
423 _w = total / _h;
424 if (_h == -1)
425 _h = total / _w;
426
427 int out_elempack = 1;
428 if (opt.use_packing_layout)
429 {
430#if NCNN_ARM82
431 out_elempack = support_fp16_storage && opt.use_fp16_arithmetic && _h % 8 == 0 ? 8 : _h % 4 == 0 ? 4 : 1;
432#else
433 out_elempack = _h % 4 == 0 ? 4 : 1;
434#endif
435 }
436 size_t out_elemsize = elemsize / elempack * out_elempack;
437
438 if (dims == 2 && bottom_blob.h * elempack == _h && elempack == out_elempack)
439 {
440 top_blob = bottom_blob;
441 return 0;
442 }
443
444 if (out_elempack == 1)
445 {
446 // flatten
447 flatten(bottom_blob, top_blob, opt);
448 if (top_blob.empty())
449 return -100;
450
451 top_blob.dims = 2;
452 top_blob.w = _w;
453 top_blob.h = _h;
454 top_blob.cstep = _w * _h;
455 top_blob.elemsize = out_elemsize;
456 top_blob.elempack = out_elempack;
457
458 return 0;
459 }
460
461 // flatten
462 Mat bottom_blob_flattened = bottom_blob;
463 {
464 Option opt_flatten = opt;
465 opt_flatten.blob_allocator = opt.workspace_allocator;
466
467 flatten(bottom_blob, bottom_blob_flattened, opt_flatten);
468 if (bottom_blob_flattened.empty())
469 return -100;
470 }
471
472 top_blob.create(_w, _h / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
473 if (top_blob.empty())
474 return -100;
475
476 int outw = top_blob.w;
477 int outh = top_blob.h;
478
479#if NCNN_ARM82
480 if (out_elempack == 8)
481 {
482 #pragma omp parallel for num_threads(opt.num_threads)
483 for (int i = 0; i < outh; i++)
484 {
485 const unsigned short* ptr0 = (const unsigned short*)bottom_blob_flattened + outw * i * 8;
486 const unsigned short* ptr1 = (const unsigned short*)bottom_blob_flattened + outw * (i * 8 + 1);
487 const unsigned short* ptr2 = (const unsigned short*)bottom_blob_flattened + outw * (i * 8 + 2);
488 const unsigned short* ptr3 = (const unsigned short*)bottom_blob_flattened + outw * (i * 8 + 3);
489 const unsigned short* ptr4 = (const unsigned short*)bottom_blob_flattened + outw * (i * 8 + 4);
490 const unsigned short* ptr5 = (const unsigned short*)bottom_blob_flattened + outw * (i * 8 + 5);
491 const unsigned short* ptr6 = (const unsigned short*)bottom_blob_flattened + outw * (i * 8 + 6);
492 const unsigned short* ptr7 = (const unsigned short*)bottom_blob_flattened + outw * (i * 8 + 7);
493 unsigned short* outptr = top_blob.row<unsigned short>(i);
494
495 int j = 0;
496 for (; j + 3 < outw; j += 4)
497 {
498 uint16x8_t _p01 = vcombine_u16(vld1_u16(ptr0), vld1_u16(ptr1));
499 uint16x8_t _p23 = vcombine_u16(vld1_u16(ptr2), vld1_u16(ptr3));
500 uint16x8_t _p45 = vcombine_u16(vld1_u16(ptr4), vld1_u16(ptr5));
501 uint16x8_t _p67 = vcombine_u16(vld1_u16(ptr6), vld1_u16(ptr7));
502
503 uint16x8x2_t _p0415 = vzipq_u16(_p01, _p45);
504 uint16x8x2_t _p2637 = vzipq_u16(_p23, _p67);
505
506 uint16x8x4_t _v4;
507 _v4.val[0] = _p0415.val[0];
508 _v4.val[1] = _p0415.val[1];
509 _v4.val[2] = _p2637.val[0];
510 _v4.val[3] = _p2637.val[1];
511
512 vst4q_u16(outptr, _v4);
513
514 ptr0 += 4;
515 ptr1 += 4;
516 ptr2 += 4;
517 ptr3 += 4;
518 ptr4 += 4;
519 ptr5 += 4;
520 ptr6 += 4;
521 ptr7 += 4;
522 outptr += 32;
523 }
524 for (; j < outw; j++)
525 {
526 outptr[0] = *ptr0++;
527 outptr[1] = *ptr1++;
528 outptr[2] = *ptr2++;
529 outptr[3] = *ptr3++;
530 outptr[4] = *ptr4++;
531 outptr[5] = *ptr5++;
532 outptr[6] = *ptr6++;
533 outptr[7] = *ptr7++;
534
535 outptr += 8;
536 }
537 }
538 }
539#endif // NCNN_ARM82
540
541 if (out_elempack == 4)
542 {
543 #pragma omp parallel for num_threads(opt.num_threads)
544 for (int i = 0; i < outh; i++)
545 {
546 const unsigned short* ptr0 = (const unsigned short*)bottom_blob_flattened + outw * i * 4;
547 const unsigned short* ptr1 = (const unsigned short*)bottom_blob_flattened + outw * (i * 4 + 1);
548 const unsigned short* ptr2 = (const unsigned short*)bottom_blob_flattened + outw * (i * 4 + 2);
549 const unsigned short* ptr3 = (const unsigned short*)bottom_blob_flattened + outw * (i * 4 + 3);
550 unsigned short* outptr = top_blob.row<unsigned short>(i);
551
552 int j = 0;
553#if __ARM_NEON
554 for (; j + 3 < outw; j += 4)
555 {
556 uint16x4x4_t _v4;
557 _v4.val[0] = vld1_u16(ptr0);
558 _v4.val[1] = vld1_u16(ptr1);
559 _v4.val[2] = vld1_u16(ptr2);
560 _v4.val[3] = vld1_u16(ptr3);
561
562 vst4_u16(outptr, _v4);
563
564 ptr0 += 4;
565 ptr1 += 4;
566 ptr2 += 4;
567 ptr3 += 4;
568 outptr += 16;
569 }
570#endif
571 for (; j < outw; j++)
572 {
573 outptr[0] = *ptr0++;
574 outptr[1] = *ptr1++;
575 outptr[2] = *ptr2++;
576 outptr[3] = *ptr3++;
577
578 outptr += 4;
579 }
580 }
581 }
582 }
583
584 if (ndim == 3 || ndim == 4)
585 {
586 int _w = w;
587 int _h = h;
588 int _d = d;
589 int _c = c;
590
591 if (ndim == 3)
592 {
593 if (_w == 0)
594 _w = dims == 1 ? bottom_blob.w * elempack : bottom_blob.w;
595 if (_h == 0)
596 _h = dims == 2 ? bottom_blob.h * elempack : bottom_blob.h;
597 if (_c == 0)
598 _c = dims == 3 ? bottom_blob.c * elempack : bottom_blob.c;
599
600 if (_w == -1)
601 _w = total / _c / _h;
602 if (_h == -1)
603 _h = total / _c / _w;
604 if (_c == -1)
605 _c = total / _h / _w;
606
607 _d = 1;
608 }
609 else // if (ndim == 4)
610 {
611 if (_w == 0)
612 _w = dims == 1 ? bottom_blob.w * elempack : bottom_blob.w;
613 if (_h == 0)
614 _h = dims == 2 ? bottom_blob.h * elempack : bottom_blob.h;
615 if (_d == 0)
616 _d = bottom_blob.d;
617 if (_c == 0)
618 _c = (dims == 3 || dims == 4) ? bottom_blob.c * elempack : bottom_blob.c;
619
620 if (_w == -1)
621 _w = total / _c / _d / _h;
622 if (_h == -1)
623 _h = total / _c / _d / _w;
624 if (_d == -1)
625 _d = total / _c / _h / _w;
626 if (_c == -1)
627 _c = total / _d / _h / _w;
628 }
629
630 int out_elempack = 1;
631 if (opt.use_packing_layout)
632 {
633#if NCNN_ARM82
634 out_elempack = support_fp16_storage && opt.use_fp16_arithmetic && _c % 8 == 0 ? 8 : _c % 4 == 0 ? 4 : 1;
635#else
636 out_elempack = _c % 4 == 0 ? 4 : 1;
637#endif
638 }
639 size_t out_elemsize = elemsize / elempack * out_elempack;
640
641 if ((dims == 3 || dims == 4) && bottom_blob.c * elempack == _c && elempack == out_elempack)
642 {
643 top_blob = bottom_blob;
644 top_blob.dims = ndim;
645 top_blob.w = _w;
646 top_blob.h = _h;
647 top_blob.d = _d;
648 return 0;
649 }
650
651 // flatten
652 Mat bottom_blob_flattened = bottom_blob;
653 {
654 Option opt_flatten = opt;
655 opt_flatten.blob_allocator = opt.workspace_allocator;
656
657 flatten(bottom_blob, bottom_blob_flattened, opt_flatten);
658 if (bottom_blob_flattened.empty())
659 return -100;
660 }
661
662 if (ndim == 3)
663 {
664 top_blob.create(_w, _h, _c / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
665 }
666 else // if (ndim == 4)
667 {
668 top_blob.create(_w, _h, _d, _c / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
669 }
670 if (top_blob.empty())
671 return -100;
672
673 int size = top_blob.w * top_blob.h * top_blob.d;
674
675#if NCNN_ARM82
676 if (out_elempack == 8)
677 {
678 #pragma omp parallel for num_threads(opt.num_threads)
679 for (int q = 0; q < top_blob.c; q++)
680 {
681 const unsigned short* ptr0 = (const unsigned short*)bottom_blob_flattened + size * q * 8;
682 const unsigned short* ptr1 = (const unsigned short*)bottom_blob_flattened + size * (q * 8 + 1);
683 const unsigned short* ptr2 = (const unsigned short*)bottom_blob_flattened + size * (q * 8 + 2);
684 const unsigned short* ptr3 = (const unsigned short*)bottom_blob_flattened + size * (q * 8 + 3);
685 const unsigned short* ptr4 = (const unsigned short*)bottom_blob_flattened + size * (q * 8 + 4);
686 const unsigned short* ptr5 = (const unsigned short*)bottom_blob_flattened + size * (q * 8 + 5);
687 const unsigned short* ptr6 = (const unsigned short*)bottom_blob_flattened + size * (q * 8 + 6);
688 const unsigned short* ptr7 = (const unsigned short*)bottom_blob_flattened + size * (q * 8 + 7);
689 unsigned short* outptr = top_blob.channel(q);
690
691 int i = 0;
692 for (; i + 3 < size; i += 4)
693 {
694 uint16x8_t _p01 = vcombine_u16(vld1_u16(ptr0), vld1_u16(ptr1));
695 uint16x8_t _p23 = vcombine_u16(vld1_u16(ptr2), vld1_u16(ptr3));
696 uint16x8_t _p45 = vcombine_u16(vld1_u16(ptr4), vld1_u16(ptr5));
697 uint16x8_t _p67 = vcombine_u16(vld1_u16(ptr6), vld1_u16(ptr7));
698
699 uint16x8x2_t _p0415 = vzipq_u16(_p01, _p45);
700 uint16x8x2_t _p2637 = vzipq_u16(_p23, _p67);
701
702 uint16x8x4_t _v4;
703 _v4.val[0] = _p0415.val[0];
704 _v4.val[1] = _p0415.val[1];
705 _v4.val[2] = _p2637.val[0];
706 _v4.val[3] = _p2637.val[1];
707
708 vst4q_u16(outptr, _v4);
709
710 ptr0 += 4;
711 ptr1 += 4;
712 ptr2 += 4;
713 ptr3 += 4;
714 ptr4 += 4;
715 ptr5 += 4;
716 ptr6 += 4;
717 ptr7 += 4;
718 outptr += 32;
719 }
720 for (; i < size; i++)
721 {
722 outptr[0] = *ptr0++;
723 outptr[1] = *ptr1++;
724 outptr[2] = *ptr2++;
725 outptr[3] = *ptr3++;
726 outptr[4] = *ptr4++;
727 outptr[5] = *ptr5++;
728 outptr[6] = *ptr6++;
729 outptr[7] = *ptr7++;
730
731 outptr += 8;
732 }
733 }
734 }
735#endif // NCNN_ARM82
736
737 if (out_elempack == 4)
738 {
739 #pragma omp parallel for num_threads(opt.num_threads)
740 for (int q = 0; q < top_blob.c; q++)
741 {
742 const unsigned short* ptr0 = (const unsigned short*)bottom_blob_flattened + size * q * 4;
743 const unsigned short* ptr1 = (const unsigned short*)bottom_blob_flattened + size * (q * 4 + 1);
744 const unsigned short* ptr2 = (const unsigned short*)bottom_blob_flattened + size * (q * 4 + 2);
745 const unsigned short* ptr3 = (const unsigned short*)bottom_blob_flattened + size * (q * 4 + 3);
746 unsigned short* outptr = top_blob.channel(q);
747
748 int i = 0;
749#if __ARM_NEON
750 for (; i + 3 < size; i += 4)
751 {
752 uint16x4x4_t _v4;
753 _v4.val[0] = vld1_u16(ptr0);
754 _v4.val[1] = vld1_u16(ptr1);
755 _v4.val[2] = vld1_u16(ptr2);
756 _v4.val[3] = vld1_u16(ptr3);
757
758 vst4_u16(outptr, _v4);
759
760 ptr0 += 4;
761 ptr1 += 4;
762 ptr2 += 4;
763 ptr3 += 4;
764 outptr += 16;
765 }
766#endif
767 for (; i < size; i++)
768 {
769 outptr[0] = *ptr0++;
770 outptr[1] = *ptr1++;
771 outptr[2] = *ptr2++;
772 outptr[3] = *ptr3++;
773
774 outptr += 4;
775 }
776 }
777 }
778
779 if (out_elempack == 1)
780 {
781 #pragma omp parallel for num_threads(opt.num_threads)
782 for (int q = 0; q < top_blob.c; q++)
783 {
784 const unsigned short* ptr = (const unsigned short*)bottom_blob_flattened + size * q;
785 unsigned short* outptr = top_blob.channel(q);
786
787 int i = 0;
788#if __ARM_NEON
789 for (; i + 3 < size; i += 4)
790 {
791 uint16x4_t _v = vld1_u16(ptr);
792 vst1_u16(outptr, _v);
793 ptr += 4;
794 outptr += 4;
795 }
796#endif
797 for (; i < size; i++)
798 {
799 *outptr++ = *ptr++;
800 }
801 }
802 }
803 }
804
805 return 0;
806}
807
808} // namespace ncnn