| 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_bicubic_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 | Mat rowsbuf2(w, (size_t)4 * 4u, 4); |
| 24 | Mat rowsbuf3(w, (size_t)4 * 4u, 4); |
| 25 | float* rows0 = rowsbuf0; |
| 26 | float* rows1 = rowsbuf1; |
| 27 | float* rows2 = rowsbuf2; |
| 28 | float* rows3 = rowsbuf3; |
| 29 | |
| 30 | int prev_sy1 = -3; |
| 31 | |
| 32 | for (int dy = 0; dy < h; dy++) |
| 33 | { |
| 34 | int sy = yofs[dy]; |
| 35 | |
| 36 | if (sy == prev_sy1) |
| 37 | { |
| 38 | // reuse all rows |
| 39 | } |
| 40 | else if (sy == prev_sy1 + 1) |
| 41 | { |
| 42 | // hresize one row |
| 43 | float* rows0_old = rows0; |
| 44 | rows0 = rows1; |
| 45 | rows1 = rows2; |
| 46 | rows2 = rows3; |
| 47 | rows3 = rows0_old; |
| 48 | const float* S3 = src.row(sy + 2); |
| 49 | |
| 50 | const float* alphap = alpha; |
| 51 | float* rows3p = rows3; |
| 52 | for (int dx = 0; dx < w; dx++) |
| 53 | { |
| 54 | int sx = xofs[dx] * 4; |
| 55 | const float* S3p = S3 + sx; |
| 56 | |
| 57 | __m128 _a0 = _mm_set1_ps(alphap[0]); |
| 58 | __m128 _a1 = _mm_set1_ps(alphap[1]); |
| 59 | __m128 _a2 = _mm_set1_ps(alphap[2]); |
| 60 | __m128 _a3 = _mm_set1_ps(alphap[3]); |
| 61 | |
| 62 | __m128 _S30 = _mm_load_ps(S3p - 4); |
| 63 | __m128 _S31 = _mm_load_ps(S3p + 0); |
| 64 | __m128 _S32 = _mm_load_ps(S3p + 4); |
| 65 | __m128 _S33 = _mm_load_ps(S3p + 8); |
| 66 | __m128 _rows3 = _mm_mul_ps(_S30, _a0); |
| 67 | _rows3 = _mm_comp_fmadd_ps(_S31, _a1, _rows3); |
| 68 | _rows3 = _mm_comp_fmadd_ps(_S32, _a2, _rows3); |
| 69 | _rows3 = _mm_comp_fmadd_ps(_S33, _a3, _rows3); |
| 70 | _mm_store_ps(rows3p + dx * 4, _rows3); |
| 71 | |
| 72 | alphap += 4; |
| 73 | } |
| 74 | } |
| 75 | else if (sy == prev_sy1 + 2) |
| 76 | { |
| 77 | // hresize two rows |
| 78 | float* rows0_old = rows0; |
| 79 | float* rows1_old = rows1; |
| 80 | rows0 = rows2; |
| 81 | rows1 = rows3; |
| 82 | rows2 = rows0_old; |
| 83 | rows3 = rows1_old; |
| 84 | const float* S2 = src.row(sy + 1); |
| 85 | const float* S3 = src.row(sy + 2); |
| 86 | |
| 87 | const float* alphap = alpha; |
| 88 | float* rows2p = rows2; |
| 89 | float* rows3p = rows3; |
| 90 | for (int dx = 0; dx < w; dx++) |
| 91 | { |
| 92 | int sx = xofs[dx] * 4; |
| 93 | const float* S2p = S2 + sx; |
| 94 | const float* S3p = S3 + sx; |
| 95 | |
| 96 | __m128 _a0 = _mm_set1_ps(alphap[0]); |
| 97 | __m128 _a1 = _mm_set1_ps(alphap[1]); |
| 98 | __m128 _a2 = _mm_set1_ps(alphap[2]); |
| 99 | __m128 _a3 = _mm_set1_ps(alphap[3]); |
| 100 | |
| 101 | __m128 _S20 = _mm_load_ps(S2p - 4); |
| 102 | __m128 _S21 = _mm_load_ps(S2p + 0); |
| 103 | __m128 _S22 = _mm_load_ps(S2p + 4); |
| 104 | __m128 _S23 = _mm_load_ps(S2p + 8); |
| 105 | __m128 _S30 = _mm_load_ps(S3p - 4); |
| 106 | __m128 _S31 = _mm_load_ps(S3p + 0); |
| 107 | __m128 _S32 = _mm_load_ps(S3p + 4); |
| 108 | __m128 _S33 = _mm_load_ps(S3p + 8); |
| 109 | __m128 _rows2 = _mm_mul_ps(_S20, _a0); |
| 110 | __m128 _rows3 = _mm_mul_ps(_S30, _a0); |
| 111 | _rows2 = _mm_comp_fmadd_ps(_S21, _a1, _rows2); |
| 112 | _rows3 = _mm_comp_fmadd_ps(_S31, _a1, _rows3); |
| 113 | _rows2 = _mm_comp_fmadd_ps(_S22, _a2, _rows2); |
| 114 | _rows3 = _mm_comp_fmadd_ps(_S32, _a2, _rows3); |
| 115 | _rows2 = _mm_comp_fmadd_ps(_S23, _a3, _rows2); |
| 116 | _rows3 = _mm_comp_fmadd_ps(_S33, _a3, _rows3); |
| 117 | _mm_store_ps(rows2p + dx * 4, _rows2); |
| 118 | _mm_store_ps(rows3p + dx * 4, _rows3); |
| 119 | |
| 120 | alphap += 4; |
| 121 | } |
| 122 | } |
| 123 | else if (sy == prev_sy1 + 3) |
| 124 | { |
| 125 | // hresize three rows |
| 126 | float* rows0_old = rows0; |
| 127 | float* rows1_old = rows1; |
| 128 | float* rows2_old = rows2; |
| 129 | rows0 = rows3; |
| 130 | rows1 = rows0_old; |
| 131 | rows2 = rows1_old; |
| 132 | rows3 = rows2_old; |
| 133 | const float* S1 = src.row(sy); |
| 134 | const float* S2 = src.row(sy + 1); |
| 135 | const float* S3 = src.row(sy + 2); |
| 136 | |
| 137 | const float* alphap = alpha; |
| 138 | float* rows1p = rows1; |
| 139 | float* rows2p = rows2; |
| 140 | float* rows3p = rows3; |
| 141 | for (int dx = 0; dx < w; dx++) |
| 142 | { |
| 143 | int sx = xofs[dx] * 4; |
| 144 | const float* S1p = S1 + sx; |
| 145 | const float* S2p = S2 + sx; |
| 146 | const float* S3p = S3 + sx; |
| 147 | |
| 148 | __m128 _a0 = _mm_set1_ps(alphap[0]); |
| 149 | __m128 _a1 = _mm_set1_ps(alphap[1]); |
| 150 | __m128 _a2 = _mm_set1_ps(alphap[2]); |
| 151 | __m128 _a3 = _mm_set1_ps(alphap[3]); |
| 152 | |
| 153 | __m128 _S10 = _mm_load_ps(S1p - 4); |
| 154 | __m128 _S11 = _mm_load_ps(S1p + 0); |
| 155 | __m128 _S12 = _mm_load_ps(S1p + 4); |
| 156 | __m128 _S13 = _mm_load_ps(S1p + 8); |
| 157 | __m128 _S20 = _mm_load_ps(S2p - 4); |
| 158 | __m128 _S21 = _mm_load_ps(S2p + 0); |
| 159 | __m128 _S22 = _mm_load_ps(S2p + 4); |
| 160 | __m128 _S23 = _mm_load_ps(S2p + 8); |
| 161 | __m128 _S30 = _mm_load_ps(S3p - 4); |
| 162 | __m128 _S31 = _mm_load_ps(S3p + 0); |
| 163 | __m128 _S32 = _mm_load_ps(S3p + 4); |
| 164 | __m128 _S33 = _mm_load_ps(S3p + 8); |
| 165 | __m128 _rows1 = _mm_mul_ps(_S10, _a0); |
| 166 | __m128 _rows2 = _mm_mul_ps(_S20, _a0); |
| 167 | __m128 _rows3 = _mm_mul_ps(_S30, _a0); |
| 168 | _rows1 = _mm_comp_fmadd_ps(_S11, _a1, _rows1); |
| 169 | _rows2 = _mm_comp_fmadd_ps(_S21, _a1, _rows2); |
| 170 | _rows3 = _mm_comp_fmadd_ps(_S31, _a1, _rows3); |
| 171 | _rows1 = _mm_comp_fmadd_ps(_S12, _a2, _rows1); |
| 172 | _rows2 = _mm_comp_fmadd_ps(_S22, _a2, _rows2); |
| 173 | _rows3 = _mm_comp_fmadd_ps(_S32, _a2, _rows3); |
| 174 | _rows1 = _mm_comp_fmadd_ps(_S13, _a3, _rows1); |
| 175 | _rows2 = _mm_comp_fmadd_ps(_S23, _a3, _rows2); |
| 176 | _rows3 = _mm_comp_fmadd_ps(_S33, _a3, _rows3); |
| 177 | _mm_store_ps(rows1p + dx * 4, _rows1); |
| 178 | _mm_store_ps(rows2p + dx * 4, _rows2); |
| 179 | _mm_store_ps(rows3p + dx * 4, _rows3); |
| 180 | |
| 181 | alphap += 4; |
| 182 | } |
| 183 | } |
| 184 | else |
| 185 | { |
| 186 | // hresize four rows |
| 187 | const float* S0 = src.row(sy - 1); |
| 188 | const float* S1 = src.row(sy); |
| 189 | const float* S2 = src.row(sy + 1); |
| 190 | const float* S3 = src.row(sy + 2); |
| 191 | |
| 192 | const float* alphap = alpha; |
| 193 | float* rows0p = rows0; |
| 194 | float* rows1p = rows1; |
| 195 | float* rows2p = rows2; |
| 196 | float* rows3p = rows3; |
| 197 | for (int dx = 0; dx < w; dx++) |
| 198 | { |
| 199 | int sx = xofs[dx] * 4; |
| 200 | const float* S0p = S0 + sx; |
| 201 | const float* S1p = S1 + sx; |
| 202 | const float* S2p = S2 + sx; |
| 203 | const float* S3p = S3 + sx; |
| 204 | |
| 205 | __m128 _a0 = _mm_set1_ps(alphap[0]); |
| 206 | __m128 _a1 = _mm_set1_ps(alphap[1]); |
| 207 | __m128 _a2 = _mm_set1_ps(alphap[2]); |
| 208 | __m128 _a3 = _mm_set1_ps(alphap[3]); |
| 209 | |
| 210 | __m128 _S00 = _mm_load_ps(S0p - 4); |
| 211 | __m128 _S01 = _mm_load_ps(S0p + 0); |
| 212 | __m128 _S02 = _mm_load_ps(S0p + 4); |
| 213 | __m128 _S03 = _mm_load_ps(S0p + 8); |
| 214 | __m128 _S10 = _mm_load_ps(S1p - 4); |
| 215 | __m128 _S11 = _mm_load_ps(S1p + 0); |
| 216 | __m128 _S12 = _mm_load_ps(S1p + 4); |
| 217 | __m128 _S13 = _mm_load_ps(S1p + 8); |
| 218 | __m128 _S20 = _mm_load_ps(S2p - 4); |
| 219 | __m128 _S21 = _mm_load_ps(S2p + 0); |
| 220 | __m128 _S22 = _mm_load_ps(S2p + 4); |
| 221 | __m128 _S23 = _mm_load_ps(S2p + 8); |
| 222 | __m128 _S30 = _mm_load_ps(S3p - 4); |
| 223 | __m128 _S31 = _mm_load_ps(S3p + 0); |
| 224 | __m128 _S32 = _mm_load_ps(S3p + 4); |
| 225 | __m128 _S33 = _mm_load_ps(S3p + 8); |
| 226 | __m128 _rows0 = _mm_mul_ps(_S00, _a0); |
| 227 | __m128 _rows1 = _mm_mul_ps(_S10, _a0); |
| 228 | __m128 _rows2 = _mm_mul_ps(_S20, _a0); |
| 229 | __m128 _rows3 = _mm_mul_ps(_S30, _a0); |
| 230 | _rows0 = _mm_comp_fmadd_ps(_S01, _a1, _rows0); |
| 231 | _rows1 = _mm_comp_fmadd_ps(_S11, _a1, _rows1); |
| 232 | _rows2 = _mm_comp_fmadd_ps(_S21, _a1, _rows2); |
| 233 | _rows3 = _mm_comp_fmadd_ps(_S31, _a1, _rows3); |
| 234 | _rows0 = _mm_comp_fmadd_ps(_S02, _a2, _rows0); |
| 235 | _rows1 = _mm_comp_fmadd_ps(_S12, _a2, _rows1); |
| 236 | _rows2 = _mm_comp_fmadd_ps(_S22, _a2, _rows2); |
| 237 | _rows3 = _mm_comp_fmadd_ps(_S32, _a2, _rows3); |
| 238 | _rows0 = _mm_comp_fmadd_ps(_S03, _a3, _rows0); |
| 239 | _rows1 = _mm_comp_fmadd_ps(_S13, _a3, _rows1); |
| 240 | _rows2 = _mm_comp_fmadd_ps(_S23, _a3, _rows2); |
| 241 | _rows3 = _mm_comp_fmadd_ps(_S33, _a3, _rows3); |
| 242 | _mm_store_ps(rows0p + dx * 4, _rows0); |
| 243 | _mm_store_ps(rows1p + dx * 4, _rows1); |
| 244 | _mm_store_ps(rows2p + dx * 4, _rows2); |
| 245 | _mm_store_ps(rows3p + dx * 4, _rows3); |
| 246 | |
| 247 | alphap += 4; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | prev_sy1 = sy; |
| 252 | |
| 253 | // vresize |
| 254 | __m128 _b0 = _mm_set1_ps(beta[0]); |
| 255 | __m128 _b1 = _mm_set1_ps(beta[1]); |
| 256 | __m128 _b2 = _mm_set1_ps(beta[2]); |
| 257 | __m128 _b3 = _mm_set1_ps(beta[3]); |
| 258 | |
| 259 | float* rows0p = rows0; |
| 260 | float* rows1p = rows1; |
| 261 | float* rows2p = rows2; |
| 262 | float* rows3p = rows3; |
| 263 | float* Dp = dst.row(dy); |
| 264 | |
| 265 | for (int dx = 0; dx < w; dx++) |
| 266 | { |
| 267 | __m128 _rows0 = _mm_load_ps(rows0p); |
| 268 | __m128 _rows1 = _mm_load_ps(rows1p); |
| 269 | __m128 _rows2 = _mm_load_ps(rows2p); |
| 270 | __m128 _rows3 = _mm_load_ps(rows3p); |
| 271 | __m128 _Dp = _mm_mul_ps(_rows0, _b0); |
| 272 | _Dp = _mm_comp_fmadd_ps(_rows1, _b1, _Dp); |
| 273 | _Dp = _mm_comp_fmadd_ps(_rows2, _b2, _Dp); |
| 274 | _Dp = _mm_comp_fmadd_ps(_rows3, _b3, _Dp); |
| 275 | _mm_store_ps(Dp, _Dp); |
| 276 | |
| 277 | Dp += 4; |
| 278 | rows0p += 4; |
| 279 | rows1p += 4; |
| 280 | rows2p += 4; |
| 281 | rows3p += 4; |
| 282 | } |
| 283 | |
| 284 | beta += 4; |
| 285 | } |
| 286 | } |