| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2022 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 "interp_arm.h" |
| 16 | |
| 17 | #if __ARM_NEON |
| 18 | #include <arm_neon.h> |
| 19 | #include "arm_usability.h" |
| 20 | #endif // __ARM_NEON |
| 21 | |
| 22 | namespace ncnn { |
| 23 | |
| 24 | #include "interp_bicubic.h" |
| 25 | #include "interp_bilinear.h" |
| 26 | |
| 27 | #if __ARM_NEON |
| 28 | #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 29 | #include "interp_bicubic_fp16s.h" |
| 30 | #include "interp_bicubic_pack4_fp16s.h" |
| 31 | #include "interp_bicubic_pack8_fp16s.h" |
| 32 | #include "interp_bilinear_fp16s.h" |
| 33 | #include "interp_bilinear_pack4_fp16s.h" |
| 34 | #include "interp_bilinear_pack8_fp16s.h" |
| 35 | #endif |
| 36 | #endif |
| 37 | |
| 38 | #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 39 | int Interp_arm::forward_fp16s(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 40 | { |
| 41 | const Mat& bottom_blob = bottom_blobs[0]; |
| 42 | const Mat& reference_blob = bottom_blobs[1]; |
| 43 | Mat& top_blob = top_blobs[0]; |
| 44 | |
| 45 | int h = bottom_blob.h; |
| 46 | int w = bottom_blob.w; |
| 47 | int channels = bottom_blob.c; |
| 48 | int dims = bottom_blob.dims; |
| 49 | size_t elemsize = bottom_blob.elemsize; |
| 50 | int elempack = bottom_blob.elempack; |
| 51 | |
| 52 | int outw = reference_blob.w; |
| 53 | int outh = reference_blob.h; |
| 54 | |
| 55 | if (dims == 1) |
| 56 | { |
| 57 | top_blob.create(outw, outh, w, elemsize, elempack, opt.blob_allocator); |
| 58 | if (top_blob.empty()) |
| 59 | return -100; |
| 60 | |
| 61 | if (elempack == 4) |
| 62 | { |
| 63 | #pragma omp parallel for num_threads(opt.num_threads) |
| 64 | for (int q = 0; q < w; q++) |
| 65 | { |
| 66 | Mat top_blob_c = top_blob.channel(q); |
| 67 | float16x4_t _v = vld1_f16((const __fp16*)bottom_blob + q * 4); |
| 68 | top_blob_c.fill(_v); |
| 69 | } |
| 70 | |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | #pragma omp parallel for num_threads(opt.num_threads) |
| 75 | for (int q = 0; q < w; q++) |
| 76 | { |
| 77 | Mat top_blob_c = top_blob.channel(q); |
| 78 | const __fp16* ptr = bottom_blob; |
| 79 | top_blob_c.fill(ptr[q]); |
| 80 | } |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | if (dims == 2) |
| 86 | { |
| 87 | if (outw == w) |
| 88 | { |
| 89 | top_blob = bottom_blob; |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | top_blob.create(outw, h, elemsize, elempack, opt.blob_allocator); |
| 94 | if (top_blob.empty()) |
| 95 | return -100; |
| 96 | |
| 97 | if (elempack == 4) |
| 98 | { |
| 99 | if (resize_type == 1) // nearest |
| 100 | { |
| 101 | const float ws = output_width ? w / (float)outw : 1.f / width_scale; |
| 102 | |
| 103 | #pragma omp parallel for num_threads(opt.num_threads) |
| 104 | for (int y = 0; y < h; y++) |
| 105 | { |
| 106 | const __fp16* ptr = bottom_blob.row<const __fp16>(y); |
| 107 | __fp16* outptr = top_blob.row<__fp16>(y); |
| 108 | for (int x = 0; x < outw; x++) |
| 109 | { |
| 110 | int in_x = std::min((int)(x * ws), (w - 1)); |
| 111 | |
| 112 | float16x4_t _p = vld1_f16(ptr + in_x * 4); |
| 113 | vst1_f16(outptr, _p); |
| 114 | |
| 115 | outptr += 4; |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | if (resize_type == 2) // bilinear |
| 121 | { |
| 122 | int* buf = new int[outw + outw * 2]; |
| 123 | |
| 124 | int* xofs = buf; |
| 125 | float* alpha = (float*)(buf + outw); |
| 126 | |
| 127 | linear_coeffs(w, outw, xofs, alpha, align_corner); |
| 128 | |
| 129 | #pragma omp parallel for num_threads(opt.num_threads) |
| 130 | for (int y = 0; y < h; y++) |
| 131 | { |
| 132 | const __fp16* ptr = bottom_blob.row<const __fp16>(y); |
| 133 | __fp16* outptr = top_blob.row<__fp16>(y); |
| 134 | const float* alphap = alpha; |
| 135 | |
| 136 | for (int x = 0; x < outw; x++) |
| 137 | { |
| 138 | int sx = xofs[x] * 4; |
| 139 | const __fp16* Sp = ptr + sx; |
| 140 | |
| 141 | float32x2_t _a01 = vld1_f32(alphap); |
| 142 | |
| 143 | float32x4_t _S0 = vcvt_f32_f16(vld1_f16(Sp)); |
| 144 | float32x4_t _S1 = vcvt_f32_f16(vld1_f16(Sp + 4)); |
| 145 | float32x4_t _p = vmulq_lane_f32(_S0, _a01, 0); |
| 146 | _p = vmlaq_lane_f32(_p, _S1, _a01, 1); |
| 147 | vst1_f16(outptr, vcvt_f16_f32(_p)); |
| 148 | |
| 149 | alphap += 2; |
| 150 | outptr += 4; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | delete[] buf; |
| 155 | } |
| 156 | |
| 157 | if (resize_type == 3) // bicubic |
| 158 | { |
| 159 | int* buf = new int[outw + outw * 4]; |
| 160 | |
| 161 | int* xofs = buf; |
| 162 | float* alpha = (float*)(buf + outw); |
| 163 | |
| 164 | cubic_coeffs(w, outw, xofs, alpha, align_corner); |
| 165 | |
| 166 | #pragma omp parallel for num_threads(opt.num_threads) |
| 167 | for (int y = 0; y < h; y++) |
| 168 | { |
| 169 | const __fp16* ptr = bottom_blob.row<const __fp16>(y); |
| 170 | __fp16* outptr = top_blob.row<__fp16>(y); |
| 171 | const float* alphap = alpha; |
| 172 | |
| 173 | for (int x = 0; x < outw; x++) |
| 174 | { |
| 175 | int sx = xofs[x] * 4; |
| 176 | const __fp16* Sp = ptr + sx; |
| 177 | |
| 178 | float32x4_t _a0123 = vld1q_f32(alphap); |
| 179 | |
| 180 | float32x4_t _S0 = vcvt_f32_f16(vld1_f16(Sp - 4)); |
| 181 | float32x4_t _S1 = vcvt_f32_f16(vld1_f16(Sp + 0)); |
| 182 | float32x4_t _S2 = vcvt_f32_f16(vld1_f16(Sp + 4)); |
| 183 | float32x4_t _S3 = vcvt_f32_f16(vld1_f16(Sp + 8)); |
| 184 | float32x4_t _p = vmulq_laneq_f32(_S0, _a0123, 0); |
| 185 | _p = vfmaq_laneq_f32(_p, _S1, _a0123, 1); |
| 186 | _p = vfmaq_laneq_f32(_p, _S2, _a0123, 2); |
| 187 | _p = vfmaq_laneq_f32(_p, _S3, _a0123, 3); |
| 188 | vst1_f16(outptr, vcvt_f16_f32(_p)); |
| 189 | |
| 190 | alphap += 4; |
| 191 | outptr += 4; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | delete[] buf; |
| 196 | } |
| 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | if (resize_type == 1) // nearest |
| 202 | { |
| 203 | const float ws = output_width ? w / (float)outw : 1.f / width_scale; |
| 204 | |
| 205 | #pragma omp parallel for num_threads(opt.num_threads) |
| 206 | for (int y = 0; y < h; y++) |
| 207 | { |
| 208 | const __fp16* ptr = bottom_blob.row<const __fp16>(y); |
| 209 | __fp16* outptr = top_blob.row<__fp16>(y); |
| 210 | for (int x = 0; x < outw; x++) |
| 211 | { |
| 212 | int in_x = std::min((int)(x * ws), (w - 1)); |
| 213 | *outptr++ = ptr[in_x]; |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | if (resize_type == 2) // bilinear |
| 219 | { |
| 220 | int* buf = new int[outw + outw * 2]; |
| 221 | |
| 222 | int* xofs = buf; |
| 223 | float* alpha = (float*)(buf + outw); |
| 224 | |
| 225 | linear_coeffs(w, outw, xofs, alpha, align_corner); |
| 226 | |
| 227 | #pragma omp parallel for num_threads(opt.num_threads) |
| 228 | for (int y = 0; y < h; y++) |
| 229 | { |
| 230 | const __fp16* ptr = bottom_blob.row<const __fp16>(y); |
| 231 | __fp16* outptr = top_blob.row<__fp16>(y); |
| 232 | const float* alphap = alpha; |
| 233 | |
| 234 | for (int x = 0; x < outw; x++) |
| 235 | { |
| 236 | int sx = xofs[x]; |
| 237 | const __fp16* Sp = ptr + sx; |
| 238 | float a0 = alphap[0]; |
| 239 | float a1 = alphap[1]; |
| 240 | *outptr++ = (__fp16)((float)Sp[0] * a0 + (float)Sp[1] * a1); |
| 241 | alphap += 2; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | delete[] buf; |
| 246 | } |
| 247 | |
| 248 | if (resize_type == 3) // bicubic |
| 249 | { |
| 250 | int* buf = new int[outw + outw * 4]; |
| 251 | |
| 252 | int* xofs = buf; |
| 253 | float* alpha = (float*)(buf + outw); |
| 254 | |
| 255 | cubic_coeffs(w, outw, xofs, alpha, align_corner); |
| 256 | |
| 257 | #pragma omp parallel for num_threads(opt.num_threads) |
| 258 | for (int y = 0; y < h; y++) |
| 259 | { |
| 260 | const __fp16* ptr = bottom_blob.row<const __fp16>(y); |
| 261 | __fp16* outptr = top_blob.row<__fp16>(y); |
| 262 | const float* alphap = alpha; |
| 263 | |
| 264 | for (int x = 0; x < outw; x++) |
| 265 | { |
| 266 | int sx = xofs[x]; |
| 267 | const __fp16* Sp = ptr + sx; |
| 268 | float a0 = alphap[0]; |
| 269 | float a1 = alphap[1]; |
| 270 | float a2 = alphap[2]; |
| 271 | float a3 = alphap[3]; |
| 272 | *outptr++ = (__fp16)((float)Sp[-1] * a0 + (float)Sp[0] * a1 + (float)Sp[1] * a2 + (float)Sp[2] * a3); |
| 273 | alphap += 4; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | delete[] buf; |
| 278 | } |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | if (outw == w && outh == h) |
| 284 | { |
| 285 | top_blob = bottom_blob; |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | top_blob.create(outw, outh, channels, elemsize, elempack, opt.blob_allocator); |
| 290 | if (top_blob.empty()) |
| 291 | return -100; |
| 292 | |
| 293 | if (elempack == 4) |
| 294 | { |
| 295 | if (resize_type == 1) // nearest |
| 296 | { |
| 297 | const float hs = output_height ? h / (float)outh : 1.f / height_scale; |
| 298 | const float ws = output_width ? w / (float)outw : 1.f / width_scale; |
| 299 | |
| 300 | #pragma omp parallel for num_threads(opt.num_threads) |
| 301 | for (int q = 0; q < channels; q++) |
| 302 | { |
| 303 | const Mat src = bottom_blob.channel(q); |
| 304 | Mat dst = top_blob.channel(q); |
| 305 | |
| 306 | for (int y = 0; y < outh; y++) |
| 307 | { |
| 308 | int in_y = std::min((int)(y * hs), (h - 1)); |
| 309 | |
| 310 | const __fp16* ptr = src.row<const __fp16>(in_y); |
| 311 | __fp16* outptr = dst.row<__fp16>(y); |
| 312 | for (int x = 0; x < outw; x++) |
| 313 | { |
| 314 | int in_x = std::min((int)(x * ws), (w - 1)); |
| 315 | |
| 316 | float16x4_t _p = vld1_f16(ptr + in_x * 4); |
| 317 | vst1_f16(outptr, _p); |
| 318 | |
| 319 | outptr += 4; |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | if (resize_type == 2) // bilinear |
| 326 | { |
| 327 | int* buf = new int[outw + outh + outw * 2 + outh * 2]; |
| 328 | |
| 329 | int* xofs = buf; //new int[outw]; |
| 330 | int* yofs = buf + outw; //new int[outh]; |
| 331 | |
| 332 | float* alpha = (float*)(buf + outw + outh); //new float[outw * 2]; |
| 333 | float* beta = (float*)(buf + outw + outh + outw * 2); //new float[outh * 2]; |
| 334 | |
| 335 | linear_coeffs(w, outw, xofs, alpha, align_corner); |
| 336 | linear_coeffs(h, outh, yofs, beta, align_corner); |
| 337 | |
| 338 | #pragma omp parallel for num_threads(opt.num_threads) |
| 339 | for (int q = 0; q < channels; q++) |
| 340 | { |
| 341 | const Mat src = bottom_blob.channel(q); |
| 342 | Mat dst = top_blob.channel(q); |
| 343 | |
| 344 | resize_bilinear_image_pack4_fp16s(src, dst, alpha, xofs, beta, yofs); |
| 345 | } |
| 346 | |
| 347 | delete[] buf; |
| 348 | } |
| 349 | |
| 350 | if (resize_type == 3) // bicubic |
| 351 | { |
| 352 | int* buf = new int[outw + outh + outw * 4 + outh * 4]; |
| 353 | |
| 354 | int* xofs = buf; //new int[outw]; |
| 355 | int* yofs = buf + outw; //new int[outh]; |
| 356 | |
| 357 | float* alpha = (float*)(buf + outw + outh); //new float[outw * 4]; |
| 358 | float* beta = (float*)(buf + outw + outh + outw * 4); //new float[outh * 4]; |
| 359 | |
| 360 | cubic_coeffs(w, outw, xofs, alpha, align_corner); |
| 361 | cubic_coeffs(h, outh, yofs, beta, align_corner); |
| 362 | |
| 363 | #pragma omp parallel for num_threads(opt.num_threads) |
| 364 | for (int q = 0; q < channels; q++) |
| 365 | { |
| 366 | const Mat src = bottom_blob.channel(q); |
| 367 | Mat dst = top_blob.channel(q); |
| 368 | |
| 369 | resize_bicubic_image_pack4_fp16s(src, dst, alpha, xofs, beta, yofs); |
| 370 | } |
| 371 | |
| 372 | delete[] buf; |
| 373 | } |
| 374 | |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | if (resize_type == 1) // nearest |
| 379 | { |
| 380 | const float hs = output_height ? h / (float)outh : 1.f / height_scale; |
| 381 | const float ws = output_width ? w / (float)outw : 1.f / width_scale; |
| 382 | |
| 383 | #pragma omp parallel for num_threads(opt.num_threads) |
| 384 | for (int q = 0; q < channels; q++) |
| 385 | { |
| 386 | const Mat src = bottom_blob.channel(q); |
| 387 | Mat dst = top_blob.channel(q); |
| 388 | |
| 389 | for (int y = 0; y < outh; y++) |
| 390 | { |
| 391 | int in_y = std::min((int)(y * hs), (h - 1)); |
| 392 | |
| 393 | const __fp16* ptr = src.row<const __fp16>(in_y); |
| 394 | __fp16* outptr = dst.row<__fp16>(y); |
| 395 | for (int x = 0; x < outw; x++) |
| 396 | { |
| 397 | int in_x = std::min((int)(x * ws), (w - 1)); |
| 398 | *outptr++ = ptr[in_x]; |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | if (resize_type == 2) // bilinear |
| 405 | { |
| 406 | int* buf = new int[outw + outh + outw * 2 + outh * 2]; |
| 407 | |
| 408 | int* xofs = buf; //new int[outw]; |
| 409 | int* yofs = buf + outw; //new int[outh]; |
| 410 | |
| 411 | float* alpha = (float*)(buf + outw + outh); //new float[outw * 2]; |
| 412 | float* beta = (float*)(buf + outw + outh + outw * 2); //new float[outh * 2]; |
| 413 | |
| 414 | linear_coeffs(w, outw, xofs, alpha, align_corner); |
| 415 | linear_coeffs(h, outh, yofs, beta, align_corner); |
| 416 | |
| 417 | #pragma omp parallel for num_threads(opt.num_threads) |
| 418 | for (int q = 0; q < channels; q++) |
| 419 | { |
| 420 | const Mat src = bottom_blob.channel(q); |
| 421 | Mat dst = top_blob.channel(q); |
| 422 | |
| 423 | resize_bilinear_image_fp16s(src, dst, alpha, xofs, beta, yofs); |
| 424 | } |
| 425 | |
| 426 | delete[] buf; |
| 427 | } |
| 428 | |
| 429 | if (resize_type == 3) // bicubic |
| 430 | { |
| 431 | int* buf = new int[outw + outh + outw * 4 + outh * 4]; |
| 432 | |
| 433 | int* xofs = buf; //new int[outw]; |
| 434 | int* yofs = buf + outw; //new int[outh]; |
| 435 | |
| 436 | float* alpha = (float*)(buf + outw + outh); //new float[outw * 4]; |
| 437 | float* beta = (float*)(buf + outw + outh + outw * 4); //new float[outh * 4]; |
| 438 | |
| 439 | cubic_coeffs(w, outw, xofs, alpha, align_corner); |
| 440 | cubic_coeffs(h, outh, yofs, beta, align_corner); |
| 441 | |
| 442 | #pragma omp parallel for num_threads(opt.num_threads) |
| 443 | for (int q = 0; q < channels; q++) |
| 444 | { |
| 445 | const Mat src = bottom_blob.channel(q); |
| 446 | Mat dst = top_blob.channel(q); |
| 447 | |
| 448 | resize_bicubic_image_fp16s(src, dst, alpha, xofs, beta, yofs); |
| 449 | } |
| 450 | |
| 451 | delete[] buf; |
| 452 | } |
| 453 | |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | int Interp_arm::forward_fp16sa(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 458 | { |
| 459 | const Mat& bottom_blob = bottom_blobs[0]; |
| 460 | const Mat& reference_blob = bottom_blobs[1]; |
| 461 | Mat& top_blob = top_blobs[0]; |
| 462 | |
| 463 | int h = bottom_blob.h; |
| 464 | int w = bottom_blob.w; |
| 465 | int channels = bottom_blob.c; |
| 466 | int dims = bottom_blob.dims; |
| 467 | size_t elemsize = bottom_blob.elemsize; |
| 468 | int elempack = bottom_blob.elempack; |
| 469 | |
| 470 | int outw = reference_blob.w; |
| 471 | int outh = reference_blob.h; |
| 472 | |
| 473 | if ((elempack == 1 || elempack == 4) && (dims == 1 || resize_type == 1)) // nearest |
| 474 | { |
| 475 | return forward_fp16s(bottom_blobs, top_blobs, opt); |
| 476 | } |
| 477 | |
| 478 | if (dims == 1) |
| 479 | { |
| 480 | top_blob.create(outw, outh, w, elemsize, elempack, opt.blob_allocator); |
| 481 | if (top_blob.empty()) |
| 482 | return -100; |
| 483 | |
| 484 | if (elempack == 8) |
| 485 | { |
| 486 | #pragma omp parallel for num_threads(opt.num_threads) |
| 487 | for (int q = 0; q < w; q++) |
| 488 | { |
| 489 | Mat top_blob_c = top_blob.channel(q); |
| 490 | float16x8_t _v = vld1q_f16((const __fp16*)bottom_blob + q * 8); |
| 491 | top_blob_c.fill(_v); |
| 492 | } |
| 493 | |
| 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | if (dims == 2) |
| 501 | { |
| 502 | if (outw == w) |
| 503 | { |
| 504 | top_blob = bottom_blob; |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | top_blob.create(outw, h, elemsize, elempack, opt.blob_allocator); |
| 509 | if (top_blob.empty()) |
| 510 | return -100; |
| 511 | |
| 512 | if (elempack == 8) |
| 513 | { |
| 514 | if (resize_type == 1) // nearest |
| 515 | { |
| 516 | const float ws = output_width ? w / (float)outw : 1.f / width_scale; |
| 517 | |
| 518 | #pragma omp parallel for num_threads(opt.num_threads) |
| 519 | for (int y = 0; y < h; y++) |
| 520 | { |
| 521 | const __fp16* ptr = bottom_blob.row<const __fp16>(y); |
| 522 | __fp16* outptr = top_blob.row<__fp16>(y); |
| 523 | for (int x = 0; x < outw; x++) |
| 524 | { |
| 525 | int in_x = std::min((int)(x * ws), (w - 1)); |
| 526 | |
| 527 | float16x8_t _p = vld1q_f16(ptr + in_x * 8); |
| 528 | vst1q_f16(outptr, _p); |
| 529 | |
| 530 | outptr += 8; |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | if (resize_type == 2) // bilinear |
| 536 | { |
| 537 | int* buf = new int[outw + outw * 2]; |
| 538 | |
| 539 | int* xofs = buf; |
| 540 | __fp16* alpha = (__fp16*)(buf + outw); |
| 541 | |
| 542 | linear_coeffs_fp16sa(w, outw, xofs, alpha, align_corner); |
| 543 | |
| 544 | #pragma omp parallel for num_threads(opt.num_threads) |
| 545 | for (int y = 0; y < h; y++) |
| 546 | { |
| 547 | const __fp16* ptr = bottom_blob.row<const __fp16>(y); |
| 548 | __fp16* outptr = top_blob.row<__fp16>(y); |
| 549 | const __fp16* alphap = alpha; |
| 550 | |
| 551 | for (int x = 0; x < outw; x++) |
| 552 | { |
| 553 | int sx = xofs[x] * 8; |
| 554 | const __fp16* Sp = ptr + sx; |
| 555 | |
| 556 | float16x4_t _a01 = vld1_f16(alphap); |
| 557 | |
| 558 | float16x8_t _S0 = vld1q_f16(Sp); |
| 559 | float16x8_t _S1 = vld1q_f16(Sp + 8); |
| 560 | float16x8_t _p = vmulq_lane_f16(_S0, _a01, 0); |
| 561 | _p = vfmaq_lane_f16(_p, _S1, _a01, 1); |
| 562 | vst1q_f16(outptr, _p); |
| 563 | |
| 564 | alphap += 2; |
| 565 | outptr += 8; |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | delete[] buf; |
| 570 | } |
| 571 | |
| 572 | if (resize_type == 3) // bicubic |
| 573 | { |
| 574 | int* buf = new int[outw + outw * 4]; |
| 575 | |
| 576 | int* xofs = buf; |
| 577 | __fp16* alpha = (__fp16*)(buf + outw); |
| 578 | |
| 579 | cubic_coeffs_fp16sa(w, outw, xofs, alpha, align_corner); |
| 580 | |
| 581 | #pragma omp parallel for num_threads(opt.num_threads) |
| 582 | for (int y = 0; y < h; y++) |
| 583 | { |
| 584 | const __fp16* ptr = bottom_blob.row<const __fp16>(y); |
| 585 | __fp16* outptr = top_blob.row<__fp16>(y); |
| 586 | const __fp16* alphap = alpha; |
| 587 | |
| 588 | for (int x = 0; x < outw; x++) |
| 589 | { |
| 590 | int sx = xofs[x] * 8; |
| 591 | const __fp16* Sp = ptr + sx; |
| 592 | |
| 593 | float16x4_t _a0123 = vld1_f16(alphap); |
| 594 | |
| 595 | float16x8_t _S0 = vld1q_f16(Sp - 8); |
| 596 | float16x8_t _S1 = vld1q_f16(Sp + 0); |
| 597 | float16x8_t _S2 = vld1q_f16(Sp + 8); |
| 598 | float16x8_t _S3 = vld1q_f16(Sp + 16); |
| 599 | float16x8_t _p = vmulq_lane_f16(_S0, _a0123, 0); |
| 600 | _p = vfmaq_lane_f16(_p, _S1, _a0123, 1); |
| 601 | _p = vfmaq_lane_f16(_p, _S2, _a0123, 2); |
| 602 | _p = vfmaq_lane_f16(_p, _S3, _a0123, 3); |
| 603 | vst1q_f16(outptr, _p); |
| 604 | |
| 605 | alphap += 4; |
| 606 | outptr += 8; |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | delete[] buf; |
| 611 | } |
| 612 | |
| 613 | return 0; |
| 614 | } |
| 615 | |
| 616 | if (elempack == 4) |
| 617 | { |
| 618 | if (resize_type == 2) // bilinear |
| 619 | { |
| 620 | int* buf = new int[outw + outw * 2]; |
| 621 | |
| 622 | int* xofs = buf; |
| 623 | __fp16* alpha = (__fp16*)(buf + outw); |
| 624 | |
| 625 | linear_coeffs_fp16sa(w, outw, xofs, alpha, align_corner); |
| 626 | |
| 627 | #pragma omp parallel for num_threads(opt.num_threads) |
| 628 | for (int y = 0; y < h; y++) |
| 629 | { |
| 630 | const __fp16* ptr = bottom_blob.row<const __fp16>(y); |
| 631 | __fp16* outptr = top_blob.row<__fp16>(y); |
| 632 | const __fp16* alphap = alpha; |
| 633 | |
| 634 | for (int x = 0; x < outw; x++) |
| 635 | { |
| 636 | int sx = xofs[x] * 4; |
| 637 | const __fp16* Sp = ptr + sx; |
| 638 | |
| 639 | float16x4_t _a01 = vld1_f16(alphap); |
| 640 | |
| 641 | float16x4_t _S0 = vld1_f16(Sp); |
| 642 | float16x4_t _S1 = vld1_f16(Sp + 4); |
| 643 | float16x4_t _p = vmul_lane_f16(_S0, _a01, 0); |
| 644 | _p = vfma_lane_f16(_p, _S1, _a01, 1); |
| 645 | vst1_f16(outptr, _p); |
| 646 | |
| 647 | alphap += 2; |
| 648 | outptr += 4; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | delete[] buf; |
| 653 | } |
| 654 | |
| 655 | if (resize_type == 3) // bicubic |
| 656 | { |
| 657 | int* buf = new int[outw + outw * 4]; |
| 658 | |
| 659 | int* xofs = buf; |
| 660 | __fp16* alpha = (__fp16*)(buf + outw); |
| 661 | |
| 662 | cubic_coeffs_fp16sa(w, outw, xofs, alpha, align_corner); |
| 663 | |
| 664 | #pragma omp parallel for num_threads(opt.num_threads) |
| 665 | for (int y = 0; y < h; y++) |
| 666 | { |
| 667 | const __fp16* ptr = bottom_blob.row<const __fp16>(y); |
| 668 | __fp16* outptr = top_blob.row<__fp16>(y); |
| 669 | const __fp16* alphap = alpha; |
| 670 | |
| 671 | for (int x = 0; x < outw; x++) |
| 672 | { |
| 673 | int sx = xofs[x] * 4; |
| 674 | const __fp16* Sp = ptr + sx; |
| 675 | |
| 676 | float16x4_t _a0123 = vld1_f16(alphap); |
| 677 | |
| 678 | float16x4_t _S0 = vld1_f16(Sp - 4); |
| 679 | float16x4_t _S1 = vld1_f16(Sp + 0); |
| 680 | float16x4_t _S2 = vld1_f16(Sp + 4); |
| 681 | float16x4_t _S3 = vld1_f16(Sp + 8); |
| 682 | float16x4_t _p = vmul_lane_f16(_S0, _a0123, 0); |
| 683 | _p = vfma_lane_f16(_p, _S1, _a0123, 1); |
| 684 | _p = vfma_lane_f16(_p, _S2, _a0123, 2); |
| 685 | _p = vfma_lane_f16(_p, _S3, _a0123, 3); |
| 686 | vst1_f16(outptr, _p); |
| 687 | |
| 688 | alphap += 4; |
| 689 | outptr += 4; |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | delete[] buf; |
| 694 | } |
| 695 | |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | if (resize_type == 2) // bilinear |
| 700 | { |
| 701 | int* buf = new int[outw + outw * 2]; |
| 702 | |
| 703 | int* xofs = buf; |
| 704 | __fp16* alpha = (__fp16*)(buf + outw); |
| 705 | |
| 706 | linear_coeffs_fp16sa(w, outw, xofs, alpha, align_corner); |
| 707 | |
| 708 | #pragma omp parallel for num_threads(opt.num_threads) |
| 709 | for (int y = 0; y < h; y++) |
| 710 | { |
| 711 | const __fp16* ptr = bottom_blob.row<const __fp16>(y); |
| 712 | __fp16* outptr = top_blob.row<__fp16>(y); |
| 713 | const __fp16* alphap = alpha; |
| 714 | |
| 715 | for (int x = 0; x < outw; x++) |
| 716 | { |
| 717 | int sx = xofs[x]; |
| 718 | const __fp16* Sp = ptr + sx; |
| 719 | __fp16 a0 = alphap[0]; |
| 720 | __fp16 a1 = alphap[1]; |
| 721 | *outptr++ = Sp[0] * a0 + Sp[1] * a1; |
| 722 | alphap += 2; |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | delete[] buf; |
| 727 | } |
| 728 | |
| 729 | if (resize_type == 3) // bicubic |
| 730 | { |
| 731 | int* buf = new int[outw + outw * 4]; |
| 732 | |
| 733 | int* xofs = buf; |
| 734 | __fp16* alpha = (__fp16*)(buf + outw); |
| 735 | |
| 736 | cubic_coeffs_fp16sa(w, outw, xofs, alpha, align_corner); |
| 737 | |
| 738 | #pragma omp parallel for num_threads(opt.num_threads) |
| 739 | for (int y = 0; y < h; y++) |
| 740 | { |
| 741 | const __fp16* ptr = bottom_blob.row<const __fp16>(y); |
| 742 | __fp16* outptr = top_blob.row<__fp16>(y); |
| 743 | const __fp16* alphap = alpha; |
| 744 | |
| 745 | for (int x = 0; x < outw; x++) |
| 746 | { |
| 747 | int sx = xofs[x]; |
| 748 | const __fp16* Sp = ptr + sx; |
| 749 | __fp16 a0 = alphap[0]; |
| 750 | __fp16 a1 = alphap[1]; |
| 751 | __fp16 a2 = alphap[2]; |
| 752 | __fp16 a3 = alphap[3]; |
| 753 | *outptr++ = Sp[-1] * a0 + Sp[0] * a1 + Sp[1] * a2 + Sp[2] * a3; |
| 754 | alphap += 4; |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | delete[] buf; |
| 759 | } |
| 760 | |
| 761 | return 0; |
| 762 | } |
| 763 | |
| 764 | if (outw == w && outh == h) |
| 765 | { |
| 766 | top_blob = bottom_blob; |
| 767 | return 0; |
| 768 | } |
| 769 | |
| 770 | top_blob.create(outw, outh, channels, elemsize, elempack, opt.blob_allocator); |
| 771 | if (top_blob.empty()) |
| 772 | return -100; |
| 773 | |
| 774 | if (elempack == 8) |
| 775 | { |
| 776 | if (resize_type == 1) // nearest |
| 777 | { |
| 778 | const float hs = output_height ? h / (float)outh : 1.f / height_scale; |
| 779 | const float ws = output_width ? w / (float)outw : 1.f / width_scale; |
| 780 | |
| 781 | #pragma omp parallel for num_threads(opt.num_threads) |
| 782 | for (int q = 0; q < channels; q++) |
| 783 | { |
| 784 | const Mat src = bottom_blob.channel(q); |
| 785 | Mat dst = top_blob.channel(q); |
| 786 | |
| 787 | for (int y = 0; y < outh; y++) |
| 788 | { |
| 789 | int in_y = std::min((int)(y * hs), (h - 1)); |
| 790 | |
| 791 | const __fp16* ptr = src.row<const __fp16>(in_y); |
| 792 | __fp16* outptr = dst.row<__fp16>(y); |
| 793 | for (int x = 0; x < outw; x++) |
| 794 | { |
| 795 | int in_x = std::min((int)(x * ws), (w - 1)); |
| 796 | |
| 797 | float16x8_t _p = vld1q_f16(ptr + in_x * 8); |
| 798 | vst1q_f16(outptr, _p); |
| 799 | |
| 800 | outptr += 8; |
| 801 | } |
| 802 | } |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | if (resize_type == 2) // bilinear |
| 807 | { |
| 808 | int* buf = new int[outw + outh + outw * 2 + outh * 2]; |
| 809 | |
| 810 | int* xofs = buf; //new int[outw]; |
| 811 | int* yofs = buf + outw; //new int[outh]; |
| 812 | |
| 813 | __fp16* alpha = (__fp16*)(buf + outw + outh); //new __fp16[outw * 2]; |
| 814 | __fp16* beta = (__fp16*)(buf + outw + outh + outw * 2); //new __fp16[outh * 2]; |
| 815 | |
| 816 | linear_coeffs_fp16sa(w, outw, xofs, alpha, align_corner); |
| 817 | linear_coeffs_fp16sa(h, outh, yofs, beta, align_corner); |
| 818 | |
| 819 | #pragma omp parallel for num_threads(opt.num_threads) |
| 820 | for (int q = 0; q < channels; q++) |
| 821 | { |
| 822 | const Mat src = bottom_blob.channel(q); |
| 823 | Mat dst = top_blob.channel(q); |
| 824 | |
| 825 | resize_bilinear_image_pack8_fp16sa(src, dst, alpha, xofs, beta, yofs); |
| 826 | } |
| 827 | |
| 828 | delete[] buf; |
| 829 | } |
| 830 | |
| 831 | if (resize_type == 3) // bicubic |
| 832 | { |
| 833 | int* buf = new int[outw + outh + outw * 4 + outh * 4]; |
| 834 | |
| 835 | int* xofs = buf; //new int[outw]; |
| 836 | int* yofs = buf + outw; //new int[outh]; |
| 837 | |
| 838 | __fp16* alpha = (__fp16*)(buf + outw + outh); //new __fp16[outw * 4]; |
| 839 | __fp16* beta = (__fp16*)(buf + outw + outh + outw * 4); //new __fp16[outh * 4]; |
| 840 | |
| 841 | cubic_coeffs_fp16sa(w, outw, xofs, alpha, align_corner); |
| 842 | cubic_coeffs_fp16sa(h, outh, yofs, beta, align_corner); |
| 843 | |
| 844 | #pragma omp parallel for num_threads(opt.num_threads) |
| 845 | for (int q = 0; q < channels; q++) |
| 846 | { |
| 847 | const Mat src = bottom_blob.channel(q); |
| 848 | Mat dst = top_blob.channel(q); |
| 849 | |
| 850 | resize_bicubic_image_pack8_fp16sa(src, dst, alpha, xofs, beta, yofs); |
| 851 | } |
| 852 | |
| 853 | delete[] buf; |
| 854 | } |
| 855 | |
| 856 | return 0; |
| 857 | } |
| 858 | |
| 859 | if (elempack == 4) |
| 860 | { |
| 861 | if (resize_type == 2) // bilinear |
| 862 | { |
| 863 | int* buf = new int[outw + outh + outw * 2 + outh * 2]; |
| 864 | |
| 865 | int* xofs = buf; //new int[outw]; |
| 866 | int* yofs = buf + outw; //new int[outh]; |
| 867 | |
| 868 | __fp16* alpha = (__fp16*)(buf + outw + outh); //new __fp16[outw * 2]; |
| 869 | __fp16* beta = (__fp16*)(buf + outw + outh + outw * 2); //new __fp16[outh * 2]; |
| 870 | |
| 871 | linear_coeffs_fp16sa(w, outw, xofs, alpha, align_corner); |
| 872 | linear_coeffs_fp16sa(h, outh, yofs, beta, align_corner); |
| 873 | |
| 874 | #pragma omp parallel for num_threads(opt.num_threads) |
| 875 | for (int q = 0; q < channels; q++) |
| 876 | { |
| 877 | const Mat src = bottom_blob.channel(q); |
| 878 | Mat dst = top_blob.channel(q); |
| 879 | |
| 880 | resize_bilinear_image_pack4_fp16sa(src, dst, alpha, xofs, beta, yofs); |
| 881 | } |
| 882 | |
| 883 | delete[] buf; |
| 884 | } |
| 885 | |
| 886 | if (resize_type == 3) // bicubic |
| 887 | { |
| 888 | int* buf = new int[outw + outh + outw * 4 + outh * 4]; |
| 889 | |
| 890 | int* xofs = buf; //new int[outw]; |
| 891 | int* yofs = buf + outw; //new int[outh]; |
| 892 | |
| 893 | __fp16* alpha = (__fp16*)(buf + outw + outh); //new __fp16[outw * 4]; |
| 894 | __fp16* beta = (__fp16*)(buf + outw + outh + outw * 4); //new __fp16[outh * 4]; |
| 895 | |
| 896 | cubic_coeffs_fp16sa(w, outw, xofs, alpha, align_corner); |
| 897 | cubic_coeffs_fp16sa(h, outh, yofs, beta, align_corner); |
| 898 | |
| 899 | #pragma omp parallel for num_threads(opt.num_threads) |
| 900 | for (int q = 0; q < channels; q++) |
| 901 | { |
| 902 | const Mat src = bottom_blob.channel(q); |
| 903 | Mat dst = top_blob.channel(q); |
| 904 | |
| 905 | resize_bicubic_image_pack4_fp16sa(src, dst, alpha, xofs, beta, yofs); |
| 906 | } |
| 907 | |
| 908 | delete[] buf; |
| 909 | } |
| 910 | |
| 911 | return 0; |
| 912 | } |
| 913 | |
| 914 | if (resize_type == 2) // bilinear |
| 915 | { |
| 916 | int* buf = new int[outw + outh + outw * 2 + outh * 2]; |
| 917 | |
| 918 | int* xofs = buf; //new int[outw]; |
| 919 | int* yofs = buf + outw; //new int[outh]; |
| 920 | |
| 921 | __fp16* alpha = (__fp16*)(buf + outw + outh); //new __fp16[outw * 2]; |
| 922 | __fp16* beta = (__fp16*)(buf + outw + outh + outw * 2); //new __fp16[outh * 2]; |
| 923 | |
| 924 | linear_coeffs_fp16sa(w, outw, xofs, alpha, align_corner); |
| 925 | linear_coeffs_fp16sa(h, outh, yofs, beta, align_corner); |
| 926 | |
| 927 | #pragma omp parallel for num_threads(opt.num_threads) |
| 928 | for (int q = 0; q < channels; q++) |
| 929 | { |
| 930 | const Mat src = bottom_blob.channel(q); |
| 931 | Mat dst = top_blob.channel(q); |
| 932 | |
| 933 | resize_bilinear_image_fp16sa(src, dst, alpha, xofs, beta, yofs); |
| 934 | } |
| 935 | |
| 936 | delete[] buf; |
| 937 | } |
| 938 | |
| 939 | if (resize_type == 3) // bicubic |
| 940 | { |
| 941 | int* buf = new int[outw + outh + outw * 4 + outh * 4]; |
| 942 | |
| 943 | int* xofs = buf; //new int[outw]; |
| 944 | int* yofs = buf + outw; //new int[outh]; |
| 945 | |
| 946 | __fp16* alpha = (__fp16*)(buf + outw + outh); //new __fp16[outw * 4]; |
| 947 | __fp16* beta = (__fp16*)(buf + outw + outh + outw * 4); //new __fp16[outh * 4]; |
| 948 | |
| 949 | cubic_coeffs_fp16sa(w, outw, xofs, alpha, align_corner); |
| 950 | cubic_coeffs_fp16sa(h, outh, yofs, beta, align_corner); |
| 951 | |
| 952 | #pragma omp parallel for num_threads(opt.num_threads) |
| 953 | for (int q = 0; q < channels; q++) |
| 954 | { |
| 955 | const Mat src = bottom_blob.channel(q); |
| 956 | Mat dst = top_blob.channel(q); |
| 957 | |
| 958 | resize_bicubic_image_fp16sa(src, dst, alpha, xofs, beta, yofs); |
| 959 | } |
| 960 | |
| 961 | delete[] buf; |
| 962 | } |
| 963 | |
| 964 | return 0; |
| 965 | } |
| 966 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 967 | |
| 968 | } // namespace ncnn |