You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

reshape_arm.cpp 24 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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. #include "reshape_arm.h"
  15. #if __ARM_NEON
  16. #include <arm_neon.h>
  17. #endif // __ARM_NEON
  18. namespace ncnn {
  19. Reshape_arm::Reshape_arm()
  20. {
  21. #if __ARM_NEON
  22. support_packing = true;
  23. #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
  24. support_fp16_storage = true;
  25. #endif
  26. #endif // __ARM_NEON
  27. #if NCNN_BF16
  28. support_bf16_storage = true;
  29. #endif
  30. }
  31. int Reshape_arm::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  32. {
  33. int elembits = bottom_blob.elembits();
  34. #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
  35. if (opt.use_fp16_storage && elembits == 16)
  36. return forward_bf16s_fp16s(bottom_blob, top_blob, opt);
  37. #endif
  38. #if NCNN_BF16
  39. if (opt.use_bf16_storage && elembits == 16)
  40. return forward_bf16s_fp16s(bottom_blob, top_blob, opt);
  41. #endif
  42. int elempack = bottom_blob.elempack;
  43. if (permute == 1)
  44. {
  45. // TODO implement permute on-the-fly
  46. Option opt_pack = opt;
  47. opt_pack.blob_allocator = opt.workspace_allocator;
  48. Mat bottom_blob_unpacked;
  49. convert_packing(bottom_blob, bottom_blob_unpacked, 1, opt_pack);
  50. Mat top_blob_unpacked;
  51. int ret = Reshape::forward(bottom_blob_unpacked, top_blob_unpacked, opt_pack);
  52. if (ret != 0)
  53. return ret;
  54. int out_elempack = 1;
  55. if (opt.use_packing_layout)
  56. {
  57. // resolve dst_elempack
  58. int dims = top_blob_unpacked.dims;
  59. if (dims == 1) out_elempack = top_blob_unpacked.w % 4 == 0 ? 4 : 1;
  60. if (dims == 2) out_elempack = top_blob_unpacked.h % 4 == 0 ? 4 : 1;
  61. if (dims == 3) out_elempack = top_blob_unpacked.c % 4 == 0 ? 4 : 1;
  62. }
  63. convert_packing(top_blob_unpacked, top_blob, out_elempack, opt);
  64. return 0;
  65. }
  66. if (ndim == 1)
  67. {
  68. // flatten
  69. flatten(bottom_blob, top_blob, opt);
  70. if (top_blob.empty())
  71. return -100;
  72. return 0;
  73. }
  74. int dims = bottom_blob.dims;
  75. size_t elemsize = bottom_blob.elemsize;
  76. int total = bottom_blob.w * bottom_blob.h * bottom_blob.c * elempack;
  77. if (ndim == 2)
  78. {
  79. int _w = w;
  80. int _h = h;
  81. if (_w == 0)
  82. _w = dims == 1 ? bottom_blob.w * elempack : bottom_blob.w;
  83. if (_h == 0)
  84. _h = dims == 2 ? bottom_blob.h * elempack : bottom_blob.h;
  85. if (_w == -1)
  86. _w = total / _h;
  87. if (_h == -1)
  88. _h = total / _w;
  89. int out_elempack = opt.use_packing_layout && _h % 4 == 0 ? 4 : 1;
  90. size_t out_elemsize = elemsize / elempack * out_elempack;
  91. if (dims == 2 && bottom_blob.h == _h && elempack == out_elempack)
  92. {
  93. top_blob = bottom_blob;
  94. return 0;
  95. }
  96. if (out_elempack == 1)
  97. {
  98. // flatten
  99. flatten(bottom_blob, top_blob, opt);
  100. if (top_blob.empty())
  101. return -100;
  102. top_blob.dims = 2;
  103. top_blob.w = _w;
  104. top_blob.h = _h;
  105. top_blob.cstep = _w * _h;
  106. top_blob.elemsize = out_elemsize;
  107. top_blob.elempack = out_elempack;
  108. return 0;
  109. }
  110. // flatten
  111. Mat bottom_blob_flattened = bottom_blob;
  112. {
  113. Option opt_flatten = opt;
  114. opt_flatten.blob_allocator = opt.workspace_allocator;
  115. flatten(bottom_blob, bottom_blob_flattened, opt_flatten);
  116. if (bottom_blob_flattened.empty())
  117. return -100;
  118. }
  119. top_blob.create(_w, _h / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
  120. if (top_blob.empty())
  121. return -100;
  122. int outw = top_blob.w;
  123. int outh = top_blob.h;
  124. // assert out_elempack == 4
  125. #pragma omp parallel for num_threads(opt.num_threads)
  126. for (int i = 0; i < outh; i++)
  127. {
  128. const float* ptr0 = (const float*)bottom_blob_flattened + outw * i * 4;
  129. const float* ptr1 = (const float*)bottom_blob_flattened + outw * (i * 4 + 1);
  130. const float* ptr2 = (const float*)bottom_blob_flattened + outw * (i * 4 + 2);
  131. const float* ptr3 = (const float*)bottom_blob_flattened + outw * (i * 4 + 3);
  132. float* outptr = (float*)top_blob.row(i);
  133. int j = 0;
  134. #if __ARM_NEON
  135. for (; j + 3 < outw; j += 4)
  136. {
  137. float32x4x4_t _v4;
  138. _v4.val[0] = vld1q_f32(ptr0);
  139. _v4.val[1] = vld1q_f32(ptr1);
  140. _v4.val[2] = vld1q_f32(ptr2);
  141. _v4.val[3] = vld1q_f32(ptr3);
  142. vst4q_f32(outptr, _v4);
  143. ptr0 += 4;
  144. ptr1 += 4;
  145. ptr2 += 4;
  146. ptr3 += 4;
  147. outptr += 16;
  148. }
  149. #endif
  150. for (; j < outw; j++)
  151. {
  152. outptr[0] = *ptr0++;
  153. outptr[1] = *ptr1++;
  154. outptr[2] = *ptr2++;
  155. outptr[3] = *ptr3++;
  156. outptr += 4;
  157. }
  158. }
  159. }
  160. if (ndim == 3)
  161. {
  162. int _w = w;
  163. int _h = h;
  164. int _c = c;
  165. if (_w == 0)
  166. _w = dims == 1 ? bottom_blob.w * elempack : bottom_blob.w;
  167. if (_h == 0)
  168. _h = dims == 2 ? bottom_blob.h * elempack : bottom_blob.h;
  169. if (_c == 0)
  170. _c = dims == 3 ? bottom_blob.c * elempack : bottom_blob.c;
  171. if (_w == -1)
  172. _w = total / _c / _h;
  173. if (_h == -1)
  174. _h = total / _c / _w;
  175. if (_c == -1)
  176. _c = total / _h / _w;
  177. int out_elempack = opt.use_packing_layout && _c % 4 == 0 ? 4 : 1;
  178. size_t out_elemsize = elemsize / elempack * out_elempack;
  179. if (dims == 3 && bottom_blob.c == _c && elempack == out_elempack)
  180. {
  181. top_blob = bottom_blob;
  182. top_blob.w = _w;
  183. top_blob.h = _h;
  184. return 0;
  185. }
  186. // flatten
  187. Mat bottom_blob_flattened = bottom_blob;
  188. {
  189. Option opt_flatten = opt;
  190. opt_flatten.blob_allocator = opt.workspace_allocator;
  191. flatten(bottom_blob, bottom_blob_flattened, opt_flatten);
  192. if (bottom_blob_flattened.empty())
  193. return -100;
  194. }
  195. top_blob.create(_w, _h, _c / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
  196. if (top_blob.empty())
  197. return -100;
  198. int size = top_blob.w * top_blob.h;
  199. if (out_elempack == 4)
  200. {
  201. #pragma omp parallel for num_threads(opt.num_threads)
  202. for (int q = 0; q < top_blob.c; q++)
  203. {
  204. const float* ptr0 = (const float*)bottom_blob_flattened + size * q * 4;
  205. const float* ptr1 = (const float*)bottom_blob_flattened + size * (q * 4 + 1);
  206. const float* ptr2 = (const float*)bottom_blob_flattened + size * (q * 4 + 2);
  207. const float* ptr3 = (const float*)bottom_blob_flattened + size * (q * 4 + 3);
  208. float* outptr = top_blob.channel(q);
  209. int i = 0;
  210. #if __ARM_NEON
  211. for (; i + 3 < size; i += 4)
  212. {
  213. float32x4x4_t _v4;
  214. _v4.val[0] = vld1q_f32(ptr0);
  215. _v4.val[1] = vld1q_f32(ptr1);
  216. _v4.val[2] = vld1q_f32(ptr2);
  217. _v4.val[3] = vld1q_f32(ptr3);
  218. vst4q_f32(outptr, _v4);
  219. ptr0 += 4;
  220. ptr1 += 4;
  221. ptr2 += 4;
  222. ptr3 += 4;
  223. outptr += 16;
  224. }
  225. #endif
  226. for (; i < size; i++)
  227. {
  228. outptr[0] = *ptr0++;
  229. outptr[1] = *ptr1++;
  230. outptr[2] = *ptr2++;
  231. outptr[3] = *ptr3++;
  232. outptr += 4;
  233. }
  234. }
  235. }
  236. if (out_elempack == 1)
  237. {
  238. #pragma omp parallel for num_threads(opt.num_threads)
  239. for (int q = 0; q < top_blob.c; q++)
  240. {
  241. const float* ptr = (const float*)bottom_blob_flattened + size * q;
  242. float* outptr = top_blob.channel(q);
  243. int i = 0;
  244. #if __ARM_NEON
  245. for (; i + 3 < size; i += 4)
  246. {
  247. float32x4_t _v = vld1q_f32(ptr);
  248. vst1q_f32(outptr, _v);
  249. ptr += 4;
  250. outptr += 4;
  251. }
  252. #endif
  253. for (; i < size; i++)
  254. {
  255. *outptr++ = *ptr++;
  256. }
  257. }
  258. }
  259. }
  260. return 0;
  261. }
  262. int Reshape_arm::forward_bf16s_fp16s(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  263. {
  264. int elempack = bottom_blob.elempack;
  265. if (permute == 1)
  266. {
  267. // TODO implement permute on-the-fly
  268. Option opt_pack = opt;
  269. opt_pack.blob_allocator = opt.workspace_allocator;
  270. Mat bottom_blob_unpacked;
  271. convert_packing(bottom_blob, bottom_blob_unpacked, 1, opt_pack);
  272. Mat bottom_blob_unpacked_fp32;
  273. cast_bfloat16_to_float32(bottom_blob_unpacked, bottom_blob_unpacked_fp32, opt_pack);
  274. Mat top_blob_unpacked_fp32;
  275. int ret = Reshape::forward(bottom_blob_unpacked_fp32, top_blob_unpacked_fp32, opt_pack);
  276. if (ret != 0)
  277. return ret;
  278. Mat top_blob_unpacked;
  279. cast_float32_to_bfloat16(top_blob_unpacked_fp32, top_blob_unpacked, opt_pack);
  280. int out_elempack = 1;
  281. if (opt.use_packing_layout)
  282. {
  283. // resolve dst_elempack
  284. int dims = top_blob_unpacked.dims;
  285. if (dims == 1) out_elempack = opt.use_fp16_arithmetic && top_blob_unpacked.w % 8 == 0 ? 8 : top_blob_unpacked.w % 4 == 0 ? 4 : 1;
  286. if (dims == 2) out_elempack = opt.use_fp16_arithmetic && top_blob_unpacked.h % 8 == 0 ? 8 : top_blob_unpacked.h % 4 == 0 ? 4 : 1;
  287. if (dims == 3) out_elempack = opt.use_fp16_arithmetic && top_blob_unpacked.c % 8 == 0 ? 8 : top_blob_unpacked.c % 4 == 0 ? 4 : 1;
  288. }
  289. convert_packing(top_blob_unpacked, top_blob, out_elempack, opt);
  290. return 0;
  291. }
  292. if (ndim == 1)
  293. {
  294. // flatten
  295. flatten(bottom_blob, top_blob, opt);
  296. if (top_blob.empty())
  297. return -100;
  298. return 0;
  299. }
  300. int dims = bottom_blob.dims;
  301. size_t elemsize = bottom_blob.elemsize;
  302. int total = bottom_blob.w * bottom_blob.h * bottom_blob.c * elempack;
  303. if (ndim == 2)
  304. {
  305. int _w = w;
  306. int _h = h;
  307. if (_w == 0)
  308. _w = dims == 1 ? bottom_blob.w * elempack : bottom_blob.w;
  309. if (_h == 0)
  310. _h = dims == 2 ? bottom_blob.h * elempack : bottom_blob.h;
  311. if (_w == -1)
  312. _w = total / _h;
  313. if (_h == -1)
  314. _h = total / _w;
  315. int out_elempack = 1;
  316. if (opt.use_packing_layout)
  317. {
  318. out_elempack = opt.use_fp16_arithmetic && _h % 8 == 0 ? 8 : _h % 4 == 0 ? 4 : 1;
  319. }
  320. size_t out_elemsize = elemsize / elempack * out_elempack;
  321. if (dims == 2 && bottom_blob.h == _h && elempack == out_elempack)
  322. {
  323. top_blob = bottom_blob;
  324. return 0;
  325. }
  326. if (out_elempack == 1)
  327. {
  328. // flatten
  329. flatten(bottom_blob, top_blob, opt);
  330. if (top_blob.empty())
  331. return -100;
  332. top_blob.dims = 2;
  333. top_blob.w = _w;
  334. top_blob.h = _h;
  335. top_blob.cstep = _w * _h;
  336. top_blob.elemsize = out_elemsize;
  337. top_blob.elempack = out_elempack;
  338. return 0;
  339. }
  340. // flatten
  341. Mat bottom_blob_flattened = bottom_blob;
  342. {
  343. Option opt_flatten = opt;
  344. opt_flatten.blob_allocator = opt.workspace_allocator;
  345. flatten(bottom_blob, bottom_blob_flattened, opt_flatten);
  346. if (bottom_blob_flattened.empty())
  347. return -100;
  348. }
  349. top_blob.create(_w, _h / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
  350. if (top_blob.empty())
  351. return -100;
  352. int outw = top_blob.w;
  353. int outh = top_blob.h;
  354. #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
  355. if (out_elempack == 8)
  356. {
  357. #pragma omp parallel for num_threads(opt.num_threads)
  358. for (int i = 0; i < outh; i++)
  359. {
  360. const __fp16* ptr0 = (const __fp16*)bottom_blob_flattened + outw * i * 8;
  361. const __fp16* ptr1 = (const __fp16*)bottom_blob_flattened + outw * (i * 8 + 1);
  362. const __fp16* ptr2 = (const __fp16*)bottom_blob_flattened + outw * (i * 8 + 2);
  363. const __fp16* ptr3 = (const __fp16*)bottom_blob_flattened + outw * (i * 8 + 3);
  364. const __fp16* ptr4 = (const __fp16*)bottom_blob_flattened + outw * (i * 8 + 4);
  365. const __fp16* ptr5 = (const __fp16*)bottom_blob_flattened + outw * (i * 8 + 5);
  366. const __fp16* ptr6 = (const __fp16*)bottom_blob_flattened + outw * (i * 8 + 6);
  367. const __fp16* ptr7 = (const __fp16*)bottom_blob_flattened + outw * (i * 8 + 7);
  368. __fp16* outptr = top_blob.row<__fp16>(i);
  369. int j = 0;
  370. for (; j + 3 < outw; j += 4)
  371. {
  372. float16x8_t _p01 = vcombine_f16(vld1_f16(ptr0), vld1_f16(ptr1));
  373. float16x8_t _p23 = vcombine_f16(vld1_f16(ptr2), vld1_f16(ptr3));
  374. float16x8_t _p45 = vcombine_f16(vld1_f16(ptr4), vld1_f16(ptr5));
  375. float16x8_t _p67 = vcombine_f16(vld1_f16(ptr6), vld1_f16(ptr7));
  376. float16x8x2_t _p0415 = vzipq_f16(_p01, _p45);
  377. float16x8x2_t _p2637 = vzipq_f16(_p23, _p67);
  378. float16x8x4_t _v4;
  379. _v4.val[0] = _p0415.val[0];
  380. _v4.val[1] = _p0415.val[1];
  381. _v4.val[2] = _p2637.val[0];
  382. _v4.val[3] = _p2637.val[1];
  383. vst4q_f16(outptr, _v4);
  384. ptr0 += 4;
  385. ptr1 += 4;
  386. ptr2 += 4;
  387. ptr3 += 4;
  388. ptr4 += 4;
  389. ptr5 += 4;
  390. ptr6 += 4;
  391. ptr7 += 4;
  392. outptr += 32;
  393. }
  394. for (; j < outw; j++)
  395. {
  396. outptr[0] = *ptr0++;
  397. outptr[1] = *ptr1++;
  398. outptr[2] = *ptr2++;
  399. outptr[3] = *ptr3++;
  400. outptr[4] = *ptr4++;
  401. outptr[5] = *ptr5++;
  402. outptr[6] = *ptr6++;
  403. outptr[7] = *ptr7++;
  404. outptr += 8;
  405. }
  406. }
  407. }
  408. #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
  409. if (out_elempack == 4)
  410. {
  411. #pragma omp parallel for num_threads(opt.num_threads)
  412. for (int i = 0; i < outh; i++)
  413. {
  414. const unsigned short* ptr0 = (const unsigned short*)bottom_blob_flattened + outw * i * 4;
  415. const unsigned short* ptr1 = (const unsigned short*)bottom_blob_flattened + outw * (i * 4 + 1);
  416. const unsigned short* ptr2 = (const unsigned short*)bottom_blob_flattened + outw * (i * 4 + 2);
  417. const unsigned short* ptr3 = (const unsigned short*)bottom_blob_flattened + outw * (i * 4 + 3);
  418. unsigned short* outptr = top_blob.row<unsigned short>(i);
  419. int j = 0;
  420. #if __ARM_NEON
  421. for (; j + 3 < outw; j += 4)
  422. {
  423. uint16x4x4_t _v4;
  424. _v4.val[0] = vld1_u16(ptr0);
  425. _v4.val[1] = vld1_u16(ptr1);
  426. _v4.val[2] = vld1_u16(ptr2);
  427. _v4.val[3] = vld1_u16(ptr3);
  428. vst4_u16(outptr, _v4);
  429. ptr0 += 4;
  430. ptr1 += 4;
  431. ptr2 += 4;
  432. ptr3 += 4;
  433. outptr += 16;
  434. }
  435. #endif
  436. for (; j < outw; j++)
  437. {
  438. outptr[0] = *ptr0++;
  439. outptr[1] = *ptr1++;
  440. outptr[2] = *ptr2++;
  441. outptr[3] = *ptr3++;
  442. outptr += 4;
  443. }
  444. }
  445. }
  446. }
  447. if (ndim == 3)
  448. {
  449. int _w = w;
  450. int _h = h;
  451. int _c = c;
  452. if (_w == 0)
  453. _w = dims == 1 ? bottom_blob.w * elempack : bottom_blob.w;
  454. if (_h == 0)
  455. _h = dims == 2 ? bottom_blob.h * elempack : bottom_blob.h;
  456. if (_c == 0)
  457. _c = dims == 3 ? bottom_blob.c * elempack : bottom_blob.c;
  458. if (_w == -1)
  459. _w = total / _c / _h;
  460. if (_h == -1)
  461. _h = total / _c / _w;
  462. if (_c == -1)
  463. _c = total / _h / _w;
  464. int out_elempack = 1;
  465. if (opt.use_packing_layout)
  466. {
  467. out_elempack = opt.use_fp16_arithmetic && _c % 8 == 0 ? 8 : _c % 4 == 0 ? 4 : 1;
  468. }
  469. size_t out_elemsize = elemsize / elempack * out_elempack;
  470. if (dims == 3 && bottom_blob.c == _c && elempack == out_elempack)
  471. {
  472. top_blob = bottom_blob;
  473. top_blob.w = _w;
  474. top_blob.h = _h;
  475. return 0;
  476. }
  477. // flatten
  478. Mat bottom_blob_flattened = bottom_blob;
  479. {
  480. Option opt_flatten = opt;
  481. opt_flatten.blob_allocator = opt.workspace_allocator;
  482. flatten(bottom_blob, bottom_blob_flattened, opt_flatten);
  483. if (bottom_blob_flattened.empty())
  484. return -100;
  485. }
  486. top_blob.create(_w, _h, _c / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
  487. if (top_blob.empty())
  488. return -100;
  489. int size = top_blob.w * top_blob.h;
  490. #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
  491. if (out_elempack == 8)
  492. {
  493. #pragma omp parallel for num_threads(opt.num_threads)
  494. for (int q = 0; q < top_blob.c; q++)
  495. {
  496. const __fp16* ptr0 = (const __fp16*)bottom_blob_flattened + size * q * 8;
  497. const __fp16* ptr1 = (const __fp16*)bottom_blob_flattened + size * (q * 8 + 1);
  498. const __fp16* ptr2 = (const __fp16*)bottom_blob_flattened + size * (q * 8 + 2);
  499. const __fp16* ptr3 = (const __fp16*)bottom_blob_flattened + size * (q * 8 + 3);
  500. const __fp16* ptr4 = (const __fp16*)bottom_blob_flattened + size * (q * 8 + 4);
  501. const __fp16* ptr5 = (const __fp16*)bottom_blob_flattened + size * (q * 8 + 5);
  502. const __fp16* ptr6 = (const __fp16*)bottom_blob_flattened + size * (q * 8 + 6);
  503. const __fp16* ptr7 = (const __fp16*)bottom_blob_flattened + size * (q * 8 + 7);
  504. __fp16* outptr = top_blob.channel(q);
  505. int i = 0;
  506. for (; i + 3 < size; i += 4)
  507. {
  508. float16x8_t _p01 = vcombine_f16(vld1_f16(ptr0), vld1_f16(ptr1));
  509. float16x8_t _p23 = vcombine_f16(vld1_f16(ptr2), vld1_f16(ptr3));
  510. float16x8_t _p45 = vcombine_f16(vld1_f16(ptr4), vld1_f16(ptr5));
  511. float16x8_t _p67 = vcombine_f16(vld1_f16(ptr6), vld1_f16(ptr7));
  512. float16x8x2_t _p0415 = vzipq_f16(_p01, _p45);
  513. float16x8x2_t _p2637 = vzipq_f16(_p23, _p67);
  514. float16x8x4_t _v4;
  515. _v4.val[0] = _p0415.val[0];
  516. _v4.val[1] = _p0415.val[1];
  517. _v4.val[2] = _p2637.val[0];
  518. _v4.val[3] = _p2637.val[1];
  519. vst4q_f16(outptr, _v4);
  520. ptr0 += 4;
  521. ptr1 += 4;
  522. ptr2 += 4;
  523. ptr3 += 4;
  524. ptr4 += 4;
  525. ptr5 += 4;
  526. ptr6 += 4;
  527. ptr7 += 4;
  528. outptr += 32;
  529. }
  530. for (; i < size; i++)
  531. {
  532. outptr[0] = *ptr0++;
  533. outptr[1] = *ptr1++;
  534. outptr[2] = *ptr2++;
  535. outptr[3] = *ptr3++;
  536. outptr[4] = *ptr4++;
  537. outptr[5] = *ptr5++;
  538. outptr[6] = *ptr6++;
  539. outptr[7] = *ptr7++;
  540. outptr += 8;
  541. }
  542. }
  543. }
  544. #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
  545. if (out_elempack == 4)
  546. {
  547. #pragma omp parallel for num_threads(opt.num_threads)
  548. for (int q = 0; q < top_blob.c; q++)
  549. {
  550. const unsigned short* ptr0 = (const unsigned short*)bottom_blob_flattened + size * q * 4;
  551. const unsigned short* ptr1 = (const unsigned short*)bottom_blob_flattened + size * (q * 4 + 1);
  552. const unsigned short* ptr2 = (const unsigned short*)bottom_blob_flattened + size * (q * 4 + 2);
  553. const unsigned short* ptr3 = (const unsigned short*)bottom_blob_flattened + size * (q * 4 + 3);
  554. unsigned short* outptr = top_blob.channel(q);
  555. int i = 0;
  556. #if __ARM_NEON
  557. for (; i + 3 < size; i += 4)
  558. {
  559. uint16x4x4_t _v4;
  560. _v4.val[0] = vld1_u16(ptr0);
  561. _v4.val[1] = vld1_u16(ptr1);
  562. _v4.val[2] = vld1_u16(ptr2);
  563. _v4.val[3] = vld1_u16(ptr3);
  564. vst4_u16(outptr, _v4);
  565. ptr0 += 4;
  566. ptr1 += 4;
  567. ptr2 += 4;
  568. ptr3 += 4;
  569. outptr += 16;
  570. }
  571. #endif
  572. for (; i < size; i++)
  573. {
  574. outptr[0] = *ptr0++;
  575. outptr[1] = *ptr1++;
  576. outptr[2] = *ptr2++;
  577. outptr[3] = *ptr3++;
  578. outptr += 4;
  579. }
  580. }
  581. }
  582. if (out_elempack == 1)
  583. {
  584. #pragma omp parallel for num_threads(opt.num_threads)
  585. for (int q = 0; q < top_blob.c; q++)
  586. {
  587. const unsigned short* ptr = (const unsigned short*)bottom_blob_flattened + size * q;
  588. unsigned short* outptr = top_blob.channel(q);
  589. int i = 0;
  590. #if __ARM_NEON
  591. for (; i + 3 < size; i += 4)
  592. {
  593. uint16x4_t _v = vld1_u16(ptr);
  594. vst1_u16(outptr, _v);
  595. ptr += 4;
  596. outptr += 4;
  597. }
  598. #endif
  599. for (; i < size; i++)
  600. {
  601. *outptr++ = *ptr++;
  602. }
  603. }
  604. }
  605. }
  606. return 0;
  607. }
  608. } // namespace ncnn