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