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