| 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 | static void resize_bilinear_image_pack4(const Mat& src, Mat& dst, float* alpha, int* xofs, float* beta, int* yofs) |
| 16 | { |
| 17 | int w = dst.w; |
| 18 | int h = dst.h; |
| 19 | |
| 20 | // loop body |
| 21 | Mat rowsbuf0(w, (size_t)4 * 4u, 4); |
| 22 | Mat rowsbuf1(w, (size_t)4 * 4u, 4); |
| 23 | float* rows0 = rowsbuf0; |
| 24 | float* rows1 = rowsbuf1; |
| 25 | |
| 26 | int prev_sy1 = -2; |
| 27 | |
| 28 | for (int dy = 0; dy < h; dy++) |
| 29 | { |
| 30 | int sy = yofs[dy]; |
| 31 | |
| 32 | if (sy == prev_sy1) |
| 33 | { |
| 34 | // reuse all rows |
| 35 | } |
| 36 | else if (sy == prev_sy1 + 1) |
| 37 | { |
| 38 | // hresize one row |
| 39 | float* rows0_old = rows0; |
| 40 | rows0 = rows1; |
| 41 | rows1 = rows0_old; |
| 42 | const float* S1 = src.row(sy + 1); |
| 43 | |
| 44 | const float* alphap = alpha; |
| 45 | float* rows1p = rows1; |
| 46 | int dx = 0; |
| 47 | for (; dx < w; dx++) |
| 48 | { |
| 49 | int sx = xofs[dx] * 4; |
| 50 | const float* S1p = S1 + sx; |
| 51 | |
| 52 | __m128 _a0 = _mm_set1_ps(alphap[0]); |
| 53 | __m128 _a1 = _mm_set1_ps(alphap[1]); |
| 54 | |
| 55 | __m128 _S10 = _mm_load_ps(S1p); |
| 56 | __m128 _S11 = _mm_load_ps(S1p + 4); |
| 57 | __m128 _rows1 = _mm_mul_ps(_S10, _a0); |
| 58 | _rows1 = _mm_comp_fmadd_ps(_S11, _a1, _rows1); |
| 59 | _mm_store_ps(rows1p + dx * 4, _rows1); |
| 60 | |
| 61 | alphap += 2; |
| 62 | } |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | // hresize two rows |
| 67 | const float* S0 = src.row(sy); |
| 68 | const float* S1 = src.row(sy + 1); |
| 69 | |
| 70 | const float* alphap = alpha; |
| 71 | float* rows0p = rows0; |
| 72 | float* rows1p = rows1; |
| 73 | int dx = 0; |
| 74 | for (; dx < w; dx++) |
| 75 | { |
| 76 | int sx = xofs[dx] * 4; |
| 77 | const float* S0p = S0 + sx; |
| 78 | const float* S1p = S1 + sx; |
| 79 | |
| 80 | __m128 _a0 = _mm_set1_ps(alphap[0]); |
| 81 | __m128 _a1 = _mm_set1_ps(alphap[1]); |
| 82 | |
| 83 | __m128 _S00 = _mm_load_ps(S0p); |
| 84 | __m128 _S01 = _mm_load_ps(S0p + 4); |
| 85 | __m128 _S10 = _mm_load_ps(S1p); |
| 86 | __m128 _S11 = _mm_load_ps(S1p + 4); |
| 87 | __m128 _rows0 = _mm_mul_ps(_S00, _a0); |
| 88 | __m128 _rows1 = _mm_mul_ps(_S10, _a0); |
| 89 | _rows0 = _mm_comp_fmadd_ps(_S01, _a1, _rows0); |
| 90 | _rows1 = _mm_comp_fmadd_ps(_S11, _a1, _rows1); |
| 91 | _mm_store_ps(rows0p + dx * 4, _rows0); |
| 92 | _mm_store_ps(rows1p + dx * 4, _rows1); |
| 93 | |
| 94 | alphap += 2; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | prev_sy1 = sy; |
| 99 | |
| 100 | // vresize |
| 101 | __m128 _b0 = _mm_set1_ps(beta[0]); |
| 102 | __m128 _b1 = _mm_set1_ps(beta[1]); |
| 103 | |
| 104 | float* rows0p = rows0; |
| 105 | float* rows1p = rows1; |
| 106 | float* Dp = dst.row(dy); |
| 107 | |
| 108 | for (int dx = 0; dx < w; dx++) |
| 109 | { |
| 110 | __m128 _rows0 = _mm_load_ps(rows0p); |
| 111 | __m128 _rows1 = _mm_load_ps(rows1p); |
| 112 | __m128 _Dp = _mm_mul_ps(_rows0, _b0); |
| 113 | _Dp = _mm_comp_fmadd_ps(_rows1, _b1, _Dp); |
| 114 | _mm_store_ps(Dp, _Dp); |
| 115 | |
| 116 | Dp += 4; |
| 117 | rows0p += 4; |
| 118 | rows1p += 4; |
| 119 | } |
| 120 | |
| 121 | beta += 2; |
| 122 | } |
| 123 | } |