| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2021 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 "pixelshuffle_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 | PixelShuffle_arm::PixelShuffle_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 PixelShuffle_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 w = bottom_blob.w; |
| 56 | int h = bottom_blob.h; |
| 57 | int channels = bottom_blob.c; |
| 58 | size_t elemsize = bottom_blob.elemsize; |
| 59 | int elempack = bottom_blob.elempack; |
| 60 | |
| 61 | int outw = w * upscale_factor; |
| 62 | int outh = h * upscale_factor; |
| 63 | int outc = channels * elempack / (upscale_factor * upscale_factor); |
| 64 | |
| 65 | int out_elempack = 1; |
| 66 | #if __ARM_NEON |
| 67 | if (opt.use_packing_layout) |
| 68 | { |
| 69 | out_elempack = outc % 4 == 0 ? 4 : 1; |
| 70 | } |
| 71 | #endif |
| 72 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 73 | |
| 74 | if (upscale_factor != 2 || mode != 0) |
| 75 | { |
| 76 | Option opt_pack = opt; |
| 77 | opt_pack.blob_allocator = opt.workspace_allocator; |
| 78 | |
| 79 | Mat bottom_blob_unpacked; |
| 80 | convert_packing(bottom_blob, bottom_blob_unpacked, 1, opt_pack); |
| 81 | |
| 82 | return PixelShuffle::forward(bottom_blob_unpacked, top_blob, opt); |
| 83 | } |
| 84 | |
| 85 | top_blob.create(outw, outh, outc / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 86 | if (top_blob.empty()) |
| 87 | return -100; |
| 88 | |
| 89 | #if __ARM_NEON |
| 90 | if (elempack == 4 && out_elempack == 4) |
| 91 | { |
| 92 | #pragma omp parallel for num_threads(opt.num_threads) |
| 93 | for (int p = 0; p < outc / out_elempack; p++) |
| 94 | { |
| 95 | Mat m = top_blob.channel(p); |
| 96 | |
| 97 | const float* sptr0 = bottom_blob.channel(p * 4); |
| 98 | const float* sptr1 = bottom_blob.channel(p * 4 + 1); |
| 99 | const float* sptr2 = bottom_blob.channel(p * 4 + 2); |
| 100 | const float* sptr3 = bottom_blob.channel(p * 4 + 3); |
| 101 | |
| 102 | for (int i = 0; i < h; i++) |
| 103 | { |
| 104 | float* outptr0 = m.row(i * 2); |
| 105 | float* outptr1 = m.row(i * 2 + 1); |
| 106 | |
| 107 | int j = 0; |
| 108 | for (; j + 1 < w; j += 2) |
| 109 | { |
| 110 | float32x4_t _p00 = vld1q_f32(sptr0); |
| 111 | float32x4_t _p10 = vld1q_f32(sptr1); |
| 112 | float32x4_t _p20 = vld1q_f32(sptr2); |
| 113 | float32x4_t _p30 = vld1q_f32(sptr3); |
| 114 | |
| 115 | float32x4_t _p01 = vld1q_f32(sptr0 + 4); |
| 116 | float32x4_t _p11 = vld1q_f32(sptr1 + 4); |
| 117 | float32x4_t _p21 = vld1q_f32(sptr2 + 4); |
| 118 | float32x4_t _p31 = vld1q_f32(sptr3 + 4); |
| 119 | |
| 120 | float32x4x4_t _s0; |
| 121 | _s0.val[0] = vcombine_f32(vget_low_f32(_p00), vget_low_f32(_p01)); |
| 122 | _s0.val[1] = vcombine_f32(vget_low_f32(_p10), vget_low_f32(_p11)); |
| 123 | _s0.val[2] = vcombine_f32(vget_low_f32(_p20), vget_low_f32(_p21)); |
| 124 | _s0.val[3] = vcombine_f32(vget_low_f32(_p30), vget_low_f32(_p31)); |
| 125 | |
| 126 | float32x4x4_t _s1; |
| 127 | _s1.val[0] = vcombine_f32(vget_high_f32(_p00), vget_high_f32(_p01)); |
| 128 | _s1.val[1] = vcombine_f32(vget_high_f32(_p10), vget_high_f32(_p11)); |
| 129 | _s1.val[2] = vcombine_f32(vget_high_f32(_p20), vget_high_f32(_p21)); |
| 130 | _s1.val[3] = vcombine_f32(vget_high_f32(_p30), vget_high_f32(_p31)); |
| 131 | |
| 132 | vst4q_f32(outptr0, _s0); |
| 133 | vst4q_f32(outptr1, _s1); |
| 134 | |
| 135 | sptr0 += 8; |
| 136 | sptr1 += 8; |
| 137 | sptr2 += 8; |
| 138 | sptr3 += 8; |
| 139 | outptr0 += 16; |
| 140 | outptr1 += 16; |
| 141 | } |
| 142 | for (; j < w; j++) |
| 143 | { |
| 144 | outptr0[0] = sptr0[0]; |
| 145 | outptr0[1] = sptr1[0]; |
| 146 | outptr0[2] = sptr2[0]; |
| 147 | outptr0[3] = sptr3[0]; |
| 148 | |
| 149 | outptr0[4] = sptr0[1]; |
| 150 | outptr0[5] = sptr1[1]; |
| 151 | outptr0[6] = sptr2[1]; |
| 152 | outptr0[7] = sptr3[1]; |
| 153 | |
| 154 | outptr1[0] = sptr0[2]; |
| 155 | outptr1[1] = sptr1[2]; |
| 156 | outptr1[2] = sptr2[2]; |
| 157 | outptr1[3] = sptr3[2]; |
| 158 | |
| 159 | outptr1[4] = sptr0[3]; |
| 160 | outptr1[5] = sptr1[3]; |
| 161 | outptr1[6] = sptr2[3]; |
| 162 | outptr1[7] = sptr3[3]; |
| 163 | |
| 164 | sptr0 += 4; |
| 165 | sptr1 += 4; |
| 166 | sptr2 += 4; |
| 167 | sptr3 += 4; |
| 168 | outptr0 += 8; |
| 169 | outptr1 += 8; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | if (elempack == 4 && out_elempack == 1) |
| 178 | { |
| 179 | #pragma omp parallel for num_threads(opt.num_threads) |
| 180 | for (int p = 0; p < outc / out_elempack; p++) |
| 181 | { |
| 182 | Mat m = top_blob.channel(p); |
| 183 | |
| 184 | const float* sptr = bottom_blob.channel(p); |
| 185 | |
| 186 | for (int i = 0; i < h; i++) |
| 187 | { |
| 188 | float* outptr0 = m.row(i * 2); |
| 189 | float* outptr1 = m.row(i * 2 + 1); |
| 190 | |
| 191 | int j = 0; |
| 192 | for (; j + 1 < w; j += 2) |
| 193 | { |
| 194 | float32x4_t _p0 = vld1q_f32(sptr); |
| 195 | float32x4_t _p1 = vld1q_f32(sptr + 4); |
| 196 | |
| 197 | float32x4_t _s0 = vcombine_f32(vget_low_f32(_p0), vget_low_f32(_p1)); |
| 198 | float32x4_t _s1 = vcombine_f32(vget_high_f32(_p0), vget_high_f32(_p1)); |
| 199 | |
| 200 | vst1q_f32(outptr0, _s0); |
| 201 | vst1q_f32(outptr1, _s1); |
| 202 | |
| 203 | sptr += 8; |
| 204 | outptr0 += 4; |
| 205 | outptr1 += 4; |
| 206 | } |
| 207 | for (; j < w; j++) |
| 208 | { |
| 209 | outptr0[0] = sptr[0]; |
| 210 | outptr0[1] = sptr[1]; |
| 211 | outptr1[0] = sptr[2]; |
| 212 | outptr1[1] = sptr[3]; |
| 213 | |
| 214 | sptr += 4; |
| 215 | outptr0 += 2; |
| 216 | outptr1 += 2; |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | #endif // __ARM_NEON |
| 224 | |
| 225 | return PixelShuffle::forward(bottom_blob, top_blob, opt); |
| 226 | } |
| 227 | |
| 228 | int PixelShuffle_arm::forward_bf16s_fp16s(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 229 | { |
| 230 | int w = bottom_blob.w; |
| 231 | int h = bottom_blob.h; |
| 232 | int channels = bottom_blob.c; |
| 233 | size_t elemsize = bottom_blob.elemsize; |
| 234 | int elempack = bottom_blob.elempack; |
| 235 | |
| 236 | int outw = w * upscale_factor; |
| 237 | int outh = h * upscale_factor; |
| 238 | int outc = channels * elempack / (upscale_factor * upscale_factor); |
| 239 | |
| 240 | int out_elempack = 1; |
| 241 | #if __ARM_NEON |
| 242 | if (opt.use_packing_layout) |
| 243 | { |
| 244 | #if NCNN_ARM82 |
| 245 | out_elempack = support_fp16_storage && opt.use_fp16_arithmetic && outc % 8 == 0 ? 8 : outc % 4 == 0 ? 4 : 1; |
| 246 | #else |
| 247 | out_elempack = outc % 4 == 0 ? 4 : 1; |
| 248 | #endif |
| 249 | } |
| 250 | #endif |
| 251 | size_t out_elemsize = elemsize / elempack * out_elempack; |
| 252 | |
| 253 | if (upscale_factor != 2 || mode != 0) |
| 254 | { |
| 255 | Option opt_pack = opt; |
| 256 | opt_pack.blob_allocator = opt.workspace_allocator; |
| 257 | |
| 258 | Mat bottom_blob_unpacked; |
| 259 | convert_packing(bottom_blob, bottom_blob_unpacked, 1, opt_pack); |
| 260 | |
| 261 | top_blob.create(outw, outh, outc, 2u, opt.blob_allocator); |
| 262 | if (top_blob.empty()) |
| 263 | return -100; |
| 264 | |
| 265 | #pragma omp parallel for num_threads(opt.num_threads) |
| 266 | for (int p = 0; p < outc; p++) |
| 267 | { |
| 268 | Mat m = top_blob.channel(p); |
| 269 | |
| 270 | for (int sh = 0; sh < upscale_factor; sh++) |
| 271 | { |
| 272 | for (int sw = 0; sw < upscale_factor; sw++) |
| 273 | { |
| 274 | int q; |
| 275 | if (mode == 0) |
| 276 | q = p * upscale_factor * upscale_factor + sh * upscale_factor + sw; |
| 277 | else // if (mode == 1) |
| 278 | q = (sh * upscale_factor + sw) * outc + p; |
| 279 | |
| 280 | const unsigned short* sptr = bottom_blob_unpacked.channel(q); |
| 281 | |
| 282 | for (int i = 0; i < h; i++) |
| 283 | { |
| 284 | unsigned short* outptr = m.row<unsigned short>(i * upscale_factor + sh) + sw; |
| 285 | for (int j = 0; j < w; j++) |
| 286 | { |
| 287 | outptr[0] = sptr[0]; |
| 288 | |
| 289 | sptr++; |
| 290 | outptr += upscale_factor; |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | top_blob.create(outw, outh, outc / out_elempack, out_elemsize, out_elempack, opt.blob_allocator); |
| 301 | if (top_blob.empty()) |
| 302 | return -100; |
| 303 | |
| 304 | #if __ARM_NEON |
| 305 | if (elempack == 8 && out_elempack == 8) |
| 306 | { |
| 307 | #pragma omp parallel for num_threads(opt.num_threads) |
| 308 | for (int p = 0; p < outc / out_elempack; p++) |
| 309 | { |
| 310 | Mat m = top_blob.channel(p); |
| 311 | |
| 312 | const unsigned short* sptr0 = bottom_blob.channel(p * 4); |
| 313 | const unsigned short* sptr1 = bottom_blob.channel(p * 4 + 1); |
| 314 | const unsigned short* sptr2 = bottom_blob.channel(p * 4 + 2); |
| 315 | const unsigned short* sptr3 = bottom_blob.channel(p * 4 + 3); |
| 316 | |
| 317 | for (int i = 0; i < h; i++) |
| 318 | { |
| 319 | unsigned short* outptr0 = m.row<unsigned short>(i * 2); |
| 320 | unsigned short* outptr1 = m.row<unsigned short>(i * 2 + 1); |
| 321 | |
| 322 | int j = 0; |
| 323 | for (; j + 3 < w; j += 4) |
| 324 | { |
| 325 | uint16x8x4_t _p0 = vld4q_u16(sptr0); |
| 326 | uint16x8x4_t _p1 = vld4q_u16(sptr1); |
| 327 | uint16x8x4_t _p2 = vld4q_u16(sptr2); |
| 328 | uint16x8x4_t _p3 = vld4q_u16(sptr3); |
| 329 | |
| 330 | uint32x4x2_t _s04_0 = vzipq_u32(vreinterpretq_u32_u16(_p0.val[0]), vreinterpretq_u32_u16(_p1.val[0])); |
| 331 | uint32x4x2_t _s15_0 = vzipq_u32(vreinterpretq_u32_u16(_p0.val[1]), vreinterpretq_u32_u16(_p1.val[1])); |
| 332 | uint32x4x2_t _s26_0 = vzipq_u32(vreinterpretq_u32_u16(_p0.val[2]), vreinterpretq_u32_u16(_p1.val[2])); |
| 333 | uint32x4x2_t _s37_0 = vzipq_u32(vreinterpretq_u32_u16(_p0.val[3]), vreinterpretq_u32_u16(_p1.val[3])); |
| 334 | uint32x4x2_t _s04_1 = vzipq_u32(vreinterpretq_u32_u16(_p2.val[0]), vreinterpretq_u32_u16(_p3.val[0])); |
| 335 | uint32x4x2_t _s15_1 = vzipq_u32(vreinterpretq_u32_u16(_p2.val[1]), vreinterpretq_u32_u16(_p3.val[1])); |
| 336 | uint32x4x2_t _s26_1 = vzipq_u32(vreinterpretq_u32_u16(_p2.val[2]), vreinterpretq_u32_u16(_p3.val[2])); |
| 337 | uint32x4x2_t _s37_1 = vzipq_u32(vreinterpretq_u32_u16(_p2.val[3]), vreinterpretq_u32_u16(_p3.val[3])); |
| 338 | |
| 339 | uint16x8_t _s0_0 = vreinterpretq_u16_u32(vcombine_u32(vget_low_u32(_s04_0.val[0]), vget_low_u32(_s04_1.val[0]))); |
| 340 | uint16x8_t _s0_1 = vreinterpretq_u16_u32(vcombine_u32(vget_low_u32(_s15_0.val[0]), vget_low_u32(_s15_1.val[0]))); |
| 341 | uint16x8_t _s0_2 = vreinterpretq_u16_u32(vcombine_u32(vget_high_u32(_s04_0.val[0]), vget_high_u32(_s04_1.val[0]))); |
| 342 | uint16x8_t _s0_3 = vreinterpretq_u16_u32(vcombine_u32(vget_high_u32(_s15_0.val[0]), vget_high_u32(_s15_1.val[0]))); |
| 343 | uint16x8_t _s0_4 = vreinterpretq_u16_u32(vcombine_u32(vget_low_u32(_s04_0.val[1]), vget_low_u32(_s04_1.val[1]))); |
| 344 | uint16x8_t _s0_5 = vreinterpretq_u16_u32(vcombine_u32(vget_low_u32(_s15_0.val[1]), vget_low_u32(_s15_1.val[1]))); |
| 345 | uint16x8_t _s0_6 = vreinterpretq_u16_u32(vcombine_u32(vget_high_u32(_s04_0.val[1]), vget_high_u32(_s04_1.val[1]))); |
| 346 | uint16x8_t _s0_7 = vreinterpretq_u16_u32(vcombine_u32(vget_high_u32(_s15_0.val[1]), vget_high_u32(_s15_1.val[1]))); |
| 347 | uint16x8_t _s1_0 = vreinterpretq_u16_u32(vcombine_u32(vget_low_u32(_s26_0.val[0]), vget_low_u32(_s26_1.val[0]))); |
| 348 | uint16x8_t _s1_1 = vreinterpretq_u16_u32(vcombine_u32(vget_low_u32(_s37_0.val[0]), vget_low_u32(_s37_1.val[0]))); |
| 349 | uint16x8_t _s1_2 = vreinterpretq_u16_u32(vcombine_u32(vget_high_u32(_s26_0.val[0]), vget_high_u32(_s26_1.val[0]))); |
| 350 | uint16x8_t _s1_3 = vreinterpretq_u16_u32(vcombine_u32(vget_high_u32(_s37_0.val[0]), vget_high_u32(_s37_1.val[0]))); |
| 351 | uint16x8_t _s1_4 = vreinterpretq_u16_u32(vcombine_u32(vget_low_u32(_s26_0.val[1]), vget_low_u32(_s26_1.val[1]))); |
| 352 | uint16x8_t _s1_5 = vreinterpretq_u16_u32(vcombine_u32(vget_low_u32(_s37_0.val[1]), vget_low_u32(_s37_1.val[1]))); |
| 353 | uint16x8_t _s1_6 = vreinterpretq_u16_u32(vcombine_u32(vget_high_u32(_s26_0.val[1]), vget_high_u32(_s26_1.val[1]))); |
| 354 | uint16x8_t _s1_7 = vreinterpretq_u16_u32(vcombine_u32(vget_high_u32(_s37_0.val[1]), vget_high_u32(_s37_1.val[1]))); |
| 355 | |
| 356 | vst1q_u16(outptr0, _s0_0); |
| 357 | vst1q_u16(outptr0 + 8, _s0_1); |
| 358 | vst1q_u16(outptr0 + 16, _s0_2); |
| 359 | vst1q_u16(outptr0 + 24, _s0_3); |
| 360 | vst1q_u16(outptr0 + 32, _s0_4); |
| 361 | vst1q_u16(outptr0 + 40, _s0_5); |
| 362 | vst1q_u16(outptr0 + 48, _s0_6); |
| 363 | vst1q_u16(outptr0 + 56, _s0_7); |
| 364 | vst1q_u16(outptr1, _s1_0); |
| 365 | vst1q_u16(outptr1 + 8, _s1_1); |
| 366 | vst1q_u16(outptr1 + 16, _s1_2); |
| 367 | vst1q_u16(outptr1 + 24, _s1_3); |
| 368 | vst1q_u16(outptr1 + 32, _s1_4); |
| 369 | vst1q_u16(outptr1 + 40, _s1_5); |
| 370 | vst1q_u16(outptr1 + 48, _s1_6); |
| 371 | vst1q_u16(outptr1 + 56, _s1_7); |
| 372 | |
| 373 | sptr0 += 32; |
| 374 | sptr1 += 32; |
| 375 | sptr2 += 32; |
| 376 | sptr3 += 32; |
| 377 | outptr0 += 64; |
| 378 | outptr1 += 64; |
| 379 | } |
| 380 | for (; j < w; j++) |
| 381 | { |
| 382 | outptr0[0] = sptr0[0]; |
| 383 | outptr0[1] = sptr0[4]; |
| 384 | outptr0[2] = sptr1[0]; |
| 385 | outptr0[3] = sptr1[4]; |
| 386 | outptr0[4] = sptr2[0]; |
| 387 | outptr0[5] = sptr2[4]; |
| 388 | outptr0[6] = sptr3[0]; |
| 389 | outptr0[7] = sptr3[4]; |
| 390 | |
| 391 | outptr0[8] = sptr0[1]; |
| 392 | outptr0[9] = sptr0[5]; |
| 393 | outptr0[10] = sptr1[1]; |
| 394 | outptr0[11] = sptr1[5]; |
| 395 | outptr0[12] = sptr2[1]; |
| 396 | outptr0[13] = sptr2[5]; |
| 397 | outptr0[14] = sptr3[1]; |
| 398 | outptr0[15] = sptr3[5]; |
| 399 | |
| 400 | outptr1[0] = sptr0[2]; |
| 401 | outptr1[1] = sptr0[6]; |
| 402 | outptr1[2] = sptr1[2]; |
| 403 | outptr1[3] = sptr1[6]; |
| 404 | outptr1[4] = sptr2[2]; |
| 405 | outptr1[5] = sptr2[6]; |
| 406 | outptr1[6] = sptr3[2]; |
| 407 | outptr1[7] = sptr3[6]; |
| 408 | |
| 409 | outptr1[8] = sptr0[3]; |
| 410 | outptr1[9] = sptr0[7]; |
| 411 | outptr1[10] = sptr1[3]; |
| 412 | outptr1[11] = sptr1[7]; |
| 413 | outptr1[12] = sptr2[3]; |
| 414 | outptr1[13] = sptr2[7]; |
| 415 | outptr1[14] = sptr3[3]; |
| 416 | outptr1[15] = sptr3[7]; |
| 417 | |
| 418 | sptr0 += 8; |
| 419 | sptr1 += 8; |
| 420 | sptr2 += 8; |
| 421 | sptr3 += 8; |
| 422 | outptr0 += 16; |
| 423 | outptr1 += 16; |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | if (elempack == 8 && out_elempack == 4) |
| 432 | { |
| 433 | #pragma omp parallel for num_threads(opt.num_threads) |
| 434 | for (int p = 0; p < outc / out_elempack; p++) |
| 435 | { |
| 436 | Mat m = top_blob.channel(p); |
| 437 | |
| 438 | const unsigned short* sptr0 = bottom_blob.channel(p * 2); |
| 439 | const unsigned short* sptr1 = bottom_blob.channel(p * 2 + 1); |
| 440 | |
| 441 | for (int i = 0; i < h; i++) |
| 442 | { |
| 443 | unsigned short* outptr0 = m.row<unsigned short>(i * 2); |
| 444 | unsigned short* outptr1 = m.row<unsigned short>(i * 2 + 1); |
| 445 | |
| 446 | int j = 0; |
| 447 | for (; j + 3 < w; j += 4) |
| 448 | { |
| 449 | uint16x8x4_t _p0 = vld4q_u16(sptr0); |
| 450 | uint16x8x4_t _p1 = vld4q_u16(sptr1); |
| 451 | |
| 452 | uint32x4x2_t _s04 = vzipq_u32(vreinterpretq_u32_u16(_p0.val[0]), vreinterpretq_u32_u16(_p1.val[0])); |
| 453 | uint32x4x2_t _s15 = vzipq_u32(vreinterpretq_u32_u16(_p0.val[1]), vreinterpretq_u32_u16(_p1.val[1])); |
| 454 | uint32x4x2_t _s26 = vzipq_u32(vreinterpretq_u32_u16(_p0.val[2]), vreinterpretq_u32_u16(_p1.val[2])); |
| 455 | uint32x4x2_t _s37 = vzipq_u32(vreinterpretq_u32_u16(_p0.val[3]), vreinterpretq_u32_u16(_p1.val[3])); |
| 456 | |
| 457 | uint16x8_t _s0_0 = vreinterpretq_u16_u32(vcombine_u32(vget_low_u32(_s04.val[0]), vget_low_u32(_s15.val[0]))); |
| 458 | uint16x8_t _s0_1 = vreinterpretq_u16_u32(vcombine_u32(vget_high_u32(_s04.val[0]), vget_high_u32(_s15.val[0]))); |
| 459 | uint16x8_t _s0_2 = vreinterpretq_u16_u32(vcombine_u32(vget_low_u32(_s04.val[1]), vget_low_u32(_s15.val[1]))); |
| 460 | uint16x8_t _s0_3 = vreinterpretq_u16_u32(vcombine_u32(vget_high_u32(_s04.val[1]), vget_high_u32(_s15.val[1]))); |
| 461 | uint16x8_t _s1_0 = vreinterpretq_u16_u32(vcombine_u32(vget_low_u32(_s26.val[0]), vget_low_u32(_s37.val[0]))); |
| 462 | uint16x8_t _s1_1 = vreinterpretq_u16_u32(vcombine_u32(vget_high_u32(_s26.val[0]), vget_high_u32(_s37.val[0]))); |
| 463 | uint16x8_t _s1_2 = vreinterpretq_u16_u32(vcombine_u32(vget_low_u32(_s26.val[1]), vget_low_u32(_s37.val[1]))); |
| 464 | uint16x8_t _s1_3 = vreinterpretq_u16_u32(vcombine_u32(vget_high_u32(_s26.val[1]), vget_high_u32(_s37.val[1]))); |
| 465 | |
| 466 | vst1q_u16(outptr0, _s0_0); |
| 467 | vst1q_u16(outptr0 + 8, _s0_1); |
| 468 | vst1q_u16(outptr0 + 16, _s0_2); |
| 469 | vst1q_u16(outptr0 + 24, _s0_3); |
| 470 | vst1q_u16(outptr1, _s1_0); |
| 471 | vst1q_u16(outptr1 + 8, _s1_1); |
| 472 | vst1q_u16(outptr1 + 16, _s1_2); |
| 473 | vst1q_u16(outptr1 + 24, _s1_3); |
| 474 | |
| 475 | sptr0 += 32; |
| 476 | sptr1 += 32; |
| 477 | outptr0 += 32; |
| 478 | outptr1 += 32; |
| 479 | } |
| 480 | for (; j < w; j++) |
| 481 | { |
| 482 | outptr0[0] = sptr0[0]; |
| 483 | outptr0[1] = sptr0[4]; |
| 484 | outptr0[2] = sptr1[0]; |
| 485 | outptr0[3] = sptr1[4]; |
| 486 | |
| 487 | outptr0[4] = sptr0[1]; |
| 488 | outptr0[5] = sptr0[5]; |
| 489 | outptr0[6] = sptr1[1]; |
| 490 | outptr0[7] = sptr1[5]; |
| 491 | |
| 492 | outptr1[0] = sptr0[2]; |
| 493 | outptr1[1] = sptr0[6]; |
| 494 | outptr1[2] = sptr1[2]; |
| 495 | outptr1[3] = sptr1[6]; |
| 496 | |
| 497 | outptr1[4] = sptr0[3]; |
| 498 | outptr1[5] = sptr0[7]; |
| 499 | outptr1[6] = sptr1[3]; |
| 500 | outptr1[7] = sptr1[7]; |
| 501 | |
| 502 | sptr0 += 8; |
| 503 | sptr1 += 8; |
| 504 | outptr0 += 8; |
| 505 | outptr1 += 8; |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | if (elempack == 8 && out_elempack == 1) |
| 514 | { |
| 515 | #pragma omp parallel for num_threads(opt.num_threads) |
| 516 | for (int p = 0; p < outc / out_elempack / 2; p++) |
| 517 | { |
| 518 | Mat m0 = top_blob.channel(p * 2); |
| 519 | Mat m1 = top_blob.channel(p * 2 + 1); |
| 520 | |
| 521 | const unsigned short* sptr = bottom_blob.channel(p); |
| 522 | |
| 523 | for (int i = 0; i < h; i++) |
| 524 | { |
| 525 | unsigned short* outptr00 = m0.row<unsigned short>(i * 2); |
| 526 | unsigned short* outptr01 = m0.row<unsigned short>(i * 2 + 1); |
| 527 | unsigned short* outptr10 = m1.row<unsigned short>(i * 2); |
| 528 | unsigned short* outptr11 = m1.row<unsigned short>(i * 2 + 1); |
| 529 | |
| 530 | int j = 0; |
| 531 | for (; j + 3 < w; j += 4) |
| 532 | { |
| 533 | uint32x4x4_t _p = vld4q_u32((unsigned int*)sptr); |
| 534 | |
| 535 | uint16x8_t _s0 = vreinterpretq_u16_u32(_p.val[0]); |
| 536 | uint16x8_t _s1 = vreinterpretq_u16_u32(_p.val[1]); |
| 537 | uint16x8_t _s2 = vreinterpretq_u16_u32(_p.val[2]); |
| 538 | uint16x8_t _s3 = vreinterpretq_u16_u32(_p.val[3]); |
| 539 | |
| 540 | vst1q_u16(outptr00, _s0); |
| 541 | vst1q_u16(outptr01, _s1); |
| 542 | vst1q_u16(outptr10, _s2); |
| 543 | vst1q_u16(outptr11, _s3); |
| 544 | |
| 545 | sptr += 32; |
| 546 | outptr00 += 8; |
| 547 | outptr01 += 8; |
| 548 | outptr10 += 8; |
| 549 | outptr11 += 8; |
| 550 | } |
| 551 | for (; j < w; j++) |
| 552 | { |
| 553 | outptr00[0] = sptr[0]; |
| 554 | outptr00[1] = sptr[1]; |
| 555 | outptr01[0] = sptr[2]; |
| 556 | outptr01[1] = sptr[3]; |
| 557 | |
| 558 | outptr10[0] = sptr[4]; |
| 559 | outptr10[1] = sptr[5]; |
| 560 | outptr11[0] = sptr[6]; |
| 561 | outptr11[1] = sptr[7]; |
| 562 | |
| 563 | sptr += 8; |
| 564 | outptr00 += 2; |
| 565 | outptr01 += 2; |
| 566 | outptr10 += 2; |
| 567 | outptr11 += 2; |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | if (elempack == 4 && out_elempack == 4) |
| 576 | { |
| 577 | #pragma omp parallel for num_threads(opt.num_threads) |
| 578 | for (int p = 0; p < outc / out_elempack; p++) |
| 579 | { |
| 580 | Mat m = top_blob.channel(p); |
| 581 | |
| 582 | const unsigned short* sptr0 = bottom_blob.channel(p * 4); |
| 583 | const unsigned short* sptr1 = bottom_blob.channel(p * 4 + 1); |
| 584 | const unsigned short* sptr2 = bottom_blob.channel(p * 4 + 2); |
| 585 | const unsigned short* sptr3 = bottom_blob.channel(p * 4 + 3); |
| 586 | |
| 587 | for (int i = 0; i < h; i++) |
| 588 | { |
| 589 | unsigned short* outptr0 = m.row<unsigned short>(i * 2); |
| 590 | unsigned short* outptr1 = m.row<unsigned short>(i * 2 + 1); |
| 591 | |
| 592 | int j = 0; |
| 593 | for (; j + 3 < w; j += 4) |
| 594 | { |
| 595 | uint16x8_t _p00 = vld1q_u16(sptr0); |
| 596 | uint16x8_t _p10 = vld1q_u16(sptr1); |
| 597 | uint16x8_t _p20 = vld1q_u16(sptr2); |
| 598 | uint16x8_t _p30 = vld1q_u16(sptr3); |
| 599 | uint16x8_t _p01 = vld1q_u16(sptr0 + 8); |
| 600 | uint16x8_t _p11 = vld1q_u16(sptr1 + 8); |
| 601 | uint16x8_t _p21 = vld1q_u16(sptr2 + 8); |
| 602 | uint16x8_t _p31 = vld1q_u16(sptr3 + 8); |
| 603 | |
| 604 | uint32x4x2_t _p0 = vuzpq_u32(vreinterpretq_u32_u16(_p00), vreinterpretq_u32_u16(_p01)); |
| 605 | uint32x4x2_t _p1 = vuzpq_u32(vreinterpretq_u32_u16(_p10), vreinterpretq_u32_u16(_p11)); |
| 606 | uint32x4x2_t _p2 = vuzpq_u32(vreinterpretq_u32_u16(_p20), vreinterpretq_u32_u16(_p21)); |
| 607 | uint32x4x2_t _p3 = vuzpq_u32(vreinterpretq_u32_u16(_p30), vreinterpretq_u32_u16(_p31)); |
| 608 | |
| 609 | uint16x8x4_t _s0; |
| 610 | _s0.val[0] = vreinterpretq_u16_u32(_p0.val[0]); |
| 611 | _s0.val[1] = vreinterpretq_u16_u32(_p1.val[0]); |
| 612 | _s0.val[2] = vreinterpretq_u16_u32(_p2.val[0]); |
| 613 | _s0.val[3] = vreinterpretq_u16_u32(_p3.val[0]); |
| 614 | |
| 615 | uint16x8x4_t _s1; |
| 616 | _s1.val[0] = vreinterpretq_u16_u32(_p0.val[1]); |
| 617 | _s1.val[1] = vreinterpretq_u16_u32(_p1.val[1]); |
| 618 | _s1.val[2] = vreinterpretq_u16_u32(_p2.val[1]); |
| 619 | _s1.val[3] = vreinterpretq_u16_u32(_p3.val[1]); |
| 620 | |
| 621 | vst4q_u16(outptr0, _s0); |
| 622 | vst4q_u16(outptr1, _s1); |
| 623 | |
| 624 | sptr0 += 16; |
| 625 | sptr1 += 16; |
| 626 | sptr2 += 16; |
| 627 | sptr3 += 16; |
| 628 | outptr0 += 32; |
| 629 | outptr1 += 32; |
| 630 | } |
| 631 | for (; j < w; j++) |
| 632 | { |
| 633 | outptr0[0] = sptr0[0]; |
| 634 | outptr0[1] = sptr1[0]; |
| 635 | outptr0[2] = sptr2[0]; |
| 636 | outptr0[3] = sptr3[0]; |
| 637 | |
| 638 | outptr0[4] = sptr0[1]; |
| 639 | outptr0[5] = sptr1[1]; |
| 640 | outptr0[6] = sptr2[1]; |
| 641 | outptr0[7] = sptr3[1]; |
| 642 | |
| 643 | outptr1[0] = sptr0[2]; |
| 644 | outptr1[1] = sptr1[2]; |
| 645 | outptr1[2] = sptr2[2]; |
| 646 | outptr1[3] = sptr3[2]; |
| 647 | |
| 648 | outptr1[4] = sptr0[3]; |
| 649 | outptr1[5] = sptr1[3]; |
| 650 | outptr1[6] = sptr2[3]; |
| 651 | outptr1[7] = sptr3[3]; |
| 652 | |
| 653 | sptr0 += 4; |
| 654 | sptr1 += 4; |
| 655 | sptr2 += 4; |
| 656 | sptr3 += 4; |
| 657 | outptr0 += 8; |
| 658 | outptr1 += 8; |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | if (elempack == 4 && out_elempack == 1) |
| 667 | { |
| 668 | #pragma omp parallel for num_threads(opt.num_threads) |
| 669 | for (int p = 0; p < outc / out_elempack; p++) |
| 670 | { |
| 671 | Mat m = top_blob.channel(p); |
| 672 | |
| 673 | const unsigned short* sptr = bottom_blob.channel(p); |
| 674 | |
| 675 | for (int i = 0; i < h; i++) |
| 676 | { |
| 677 | unsigned short* outptr0 = m.row<unsigned short>(i * 2); |
| 678 | unsigned short* outptr1 = m.row<unsigned short>(i * 2 + 1); |
| 679 | |
| 680 | int j = 0; |
| 681 | for (; j + 3 < w; j += 4) |
| 682 | { |
| 683 | uint16x8_t _p0 = vld1q_u16(sptr); |
| 684 | uint16x8_t _p1 = vld1q_u16(sptr + 8); |
| 685 | |
| 686 | uint32x4x2_t _s01 = vuzpq_u32(vreinterpretq_u32_u16(_p0), vreinterpretq_u32_u16(_p1)); |
| 687 | |
| 688 | uint16x8_t _s0 = vreinterpretq_u16_u32(_s01.val[0]); |
| 689 | uint16x8_t _s1 = vreinterpretq_u16_u32(_s01.val[1]); |
| 690 | |
| 691 | vst1q_u16(outptr0, _s0); |
| 692 | vst1q_u16(outptr1, _s1); |
| 693 | |
| 694 | sptr += 16; |
| 695 | outptr0 += 8; |
| 696 | outptr1 += 8; |
| 697 | } |
| 698 | for (; j < w; j++) |
| 699 | { |
| 700 | outptr0[0] = sptr[0]; |
| 701 | outptr0[1] = sptr[1]; |
| 702 | outptr1[0] = sptr[2]; |
| 703 | outptr1[1] = sptr[3]; |
| 704 | |
| 705 | sptr += 4; |
| 706 | outptr0 += 2; |
| 707 | outptr1 += 2; |
| 708 | } |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | return 0; |
| 713 | } |
| 714 | #endif // __ARM_NEON |
| 715 | |
| 716 | #pragma omp parallel for num_threads(opt.num_threads) |
| 717 | for (int p = 0; p < outc; p++) |
| 718 | { |
| 719 | Mat m = top_blob.channel(p); |
| 720 | |
| 721 | for (int sh = 0; sh < upscale_factor; sh++) |
| 722 | { |
| 723 | for (int sw = 0; sw < upscale_factor; sw++) |
| 724 | { |
| 725 | int q = p * upscale_factor * upscale_factor + sh * upscale_factor + sw; |
| 726 | |
| 727 | const unsigned short* sptr = bottom_blob.channel(q); |
| 728 | |
| 729 | for (int i = 0; i < h; i++) |
| 730 | { |
| 731 | unsigned short* outptr = m.row<unsigned short>(i * upscale_factor + sh) + sw; |
| 732 | for (int j = 0; j < w; j++) |
| 733 | { |
| 734 | outptr[0] = sptr[0]; |
| 735 | |
| 736 | sptr++; |
| 737 | outptr += upscale_factor; |
| 738 | } |
| 739 | } |
| 740 | } |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | return 0; |
| 745 | } |
| 746 | |
| 747 | } // namespace ncnn |