| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2023 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 | #if __SSE2__ |
| 16 | #if __AVX__ |
| 17 | #if __AVX512F__ |
| 18 | static void cubic_interp1d_p16(__m512& coeffs0, __m512& coeffs1, __m512& coeffs2, __m512& coeffs3, const __m512& tx) |
| 19 | { |
| 20 | const __m512 A = _mm512_set1_ps(-0.75f); |
| 21 | |
| 22 | const __m512 x0 = _mm512_add_ps(tx, _mm512_set1_ps(1.0f)); |
| 23 | const __m512& x1 = tx; |
| 24 | const __m512 x2 = _mm512_sub_ps(_mm512_set1_ps(1.0f), tx); |
| 25 | |
| 26 | coeffs0 = _mm512_sub_ps(_mm512_mul_ps(_mm512_add_ps(_mm512_mul_ps(_mm512_sub_ps(_mm512_mul_ps(A, x0), _mm512_mul_ps(_mm512_set1_ps(5.0f), A)), x0), _mm512_mul_ps(_mm512_set1_ps(8.0f), A)), x0), _mm512_mul_ps(_mm512_set1_ps(4), A)); |
| 27 | coeffs1 = _mm512_add_ps(_mm512_mul_ps(_mm512_mul_ps(_mm512_sub_ps(_mm512_mul_ps(_mm512_add_ps(A, _mm512_set1_ps(2.0f)), x1), _mm512_add_ps(A, _mm512_set1_ps(3.0f))), x1), x1), _mm512_set1_ps(1.0f)); |
| 28 | coeffs2 = _mm512_add_ps(_mm512_mul_ps(_mm512_mul_ps(_mm512_sub_ps(_mm512_mul_ps(_mm512_add_ps(A, _mm512_set1_ps(2.0f)), x2), _mm512_add_ps(A, _mm512_set1_ps(3.0f))), x2), x2), _mm512_set1_ps(1.0f)); |
| 29 | coeffs3 = _mm512_sub_ps(_mm512_sub_ps(_mm512_sub_ps(_mm512_set1_ps(1.0f), coeffs0), coeffs1), coeffs2); |
| 30 | } |
| 31 | |
| 32 | static void gridsample_2d_bicubic_apply_interpolation_p16(const Mat& src, Mat& dst, Mat& offset_value, const Option& opt) |
| 33 | { |
| 34 | const int channels = dst.c; |
| 35 | const int outw = dst.w; |
| 36 | const int outh = dst.h; |
| 37 | const int grid_size = outw * outh; |
| 38 | |
| 39 | #pragma omp parallel for num_threads(opt.num_threads) |
| 40 | for (int q = 0; q < channels; q++) |
| 41 | { |
| 42 | const float* srcptr = src.channel(q); |
| 43 | float* dstptr = dst.channel(q); |
| 44 | |
| 45 | const float* offset_value_ptr = offset_value.channel(0); |
| 46 | |
| 47 | for (int i = 0; i < grid_size; i++) |
| 48 | { |
| 49 | __m512 x_coeffs0, x_coeffs1, x_coeffs2, x_coeffs3; |
| 50 | __m512 y_coeffs0, y_coeffs1, y_coeffs2, y_coeffs3; |
| 51 | __m512 value_f[4]; |
| 52 | cubic_interp1d_p16(x_coeffs0, x_coeffs1, x_coeffs2, x_coeffs3, _mm512_set1_ps(offset_value_ptr[0])); |
| 53 | cubic_interp1d_p16(y_coeffs0, y_coeffs1, y_coeffs2, y_coeffs3, _mm512_set1_ps(offset_value_ptr[1])); |
| 54 | |
| 55 | const int* offset_ptr = (int*)offset_value_ptr + 2; |
| 56 | |
| 57 | for (int ii = 0; ii < 4; ii++) |
| 58 | { |
| 59 | __m512 x0_val = offset_ptr[0] >= 0 ? _mm512_loadu_ps(srcptr + offset_ptr[0]) : _mm512_set1_ps(0); |
| 60 | __m512 x1_val = offset_ptr[1] >= 0 ? _mm512_loadu_ps(srcptr + offset_ptr[1]) : _mm512_set1_ps(0); |
| 61 | __m512 x2_val = offset_ptr[2] >= 0 ? _mm512_loadu_ps(srcptr + offset_ptr[2]) : _mm512_set1_ps(0); |
| 62 | __m512 x3_val = offset_ptr[3] >= 0 ? _mm512_loadu_ps(srcptr + offset_ptr[3]) : _mm512_set1_ps(0); |
| 63 | |
| 64 | value_f[ii] = _mm512_mul_ps(x_coeffs0, x0_val); |
| 65 | value_f[ii] = _mm512_fmadd_ps(x_coeffs1, x1_val, value_f[ii]); |
| 66 | value_f[ii] = _mm512_fmadd_ps(x_coeffs2, x2_val, value_f[ii]); |
| 67 | value_f[ii] = _mm512_fmadd_ps(x_coeffs3, x3_val, value_f[ii]); |
| 68 | |
| 69 | offset_ptr += 4; |
| 70 | } |
| 71 | |
| 72 | __m512 _v = _mm512_mul_ps(y_coeffs0, value_f[0]); |
| 73 | _v = _mm512_fmadd_ps(y_coeffs1, value_f[1], _v); |
| 74 | _v = _mm512_fmadd_ps(y_coeffs2, value_f[2], _v); |
| 75 | _v = _mm512_fmadd_ps(y_coeffs3, value_f[3], _v); |
| 76 | _mm512_storeu_ps(dstptr, _v); |
| 77 | |
| 78 | dstptr += 16; |
| 79 | offset_value_ptr += 18; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | #endif // __AVX512F__ |
| 85 | static void cubic_interp1d_p8(__m256& coeffs0, __m256& coeffs1, __m256& coeffs2, __m256& coeffs3, const __m256& tx) |
| 86 | { |
| 87 | const __m256 A = _mm256_set1_ps(-0.75f); |
| 88 | |
| 89 | const __m256 x0 = _mm256_add_ps(tx, _mm256_set1_ps(1)); |
| 90 | const __m256& x1 = tx; |
| 91 | const __m256 x2 = _mm256_sub_ps(_mm256_set1_ps(1), tx); |
| 92 | //const __m256 x3 = _mm256_add_ps(x2, _mm256_set1_ps(1)); |
| 93 | |
| 94 | coeffs0 = _mm256_sub_ps(_mm256_mul_ps(_mm256_add_ps(_mm256_mul_ps(_mm256_sub_ps(_mm256_mul_ps(A, x0), _mm256_mul_ps(_mm256_set1_ps(5.0f), A)), x0), _mm256_mul_ps(_mm256_set1_ps(8.0f), A)), x0), _mm256_mul_ps(_mm256_set1_ps(4), A)); |
| 95 | coeffs1 = _mm256_add_ps(_mm256_mul_ps(_mm256_mul_ps(_mm256_sub_ps(_mm256_mul_ps(_mm256_add_ps(A, _mm256_set1_ps(2.0f)), x1), _mm256_add_ps(A, _mm256_set1_ps(3.0f))), x1), x1), _mm256_set1_ps(1)); |
| 96 | coeffs2 = _mm256_add_ps(_mm256_mul_ps(_mm256_mul_ps(_mm256_sub_ps(_mm256_mul_ps(_mm256_add_ps(A, _mm256_set1_ps(2.0f)), x2), _mm256_add_ps(A, _mm256_set1_ps(3.0f))), x2), x2), _mm256_set1_ps(1)); |
| 97 | coeffs3 = _mm256_sub_ps(_mm256_sub_ps(_mm256_sub_ps(_mm256_set1_ps(1), coeffs0), coeffs1), coeffs2); |
| 98 | } |
| 99 | |
| 100 | static void gridsample_2d_bicubic_apply_interpolation_p8(const Mat& src, Mat& dst, Mat& offset_value, const Option& opt) |
| 101 | { |
| 102 | const int channels = dst.c; |
| 103 | const int outw = dst.w; |
| 104 | const int outh = dst.h; |
| 105 | const int grid_size = outw * outh; |
| 106 | |
| 107 | #pragma omp parallel for num_threads(opt.num_threads) |
| 108 | for (int q = 0; q < channels; q++) |
| 109 | { |
| 110 | const float* srcptr = src.channel(q); |
| 111 | float* dstptr = dst.channel(q); |
| 112 | |
| 113 | const float* offset_value_ptr = offset_value.channel(0); |
| 114 | |
| 115 | for (int i = 0; i < grid_size; i++) |
| 116 | { |
| 117 | __m256 x_coeffs0, x_coeffs1, x_coeffs2, x_coeffs3; |
| 118 | __m256 y_coeffs0, y_coeffs1, y_coeffs2, y_coeffs3; |
| 119 | __m256 value_f[4]; |
| 120 | cubic_interp1d_p8(x_coeffs0, x_coeffs1, x_coeffs2, x_coeffs3, _mm256_set1_ps(offset_value_ptr[0])); |
| 121 | cubic_interp1d_p8(y_coeffs0, y_coeffs1, y_coeffs2, y_coeffs3, _mm256_set1_ps(offset_value_ptr[1])); |
| 122 | |
| 123 | const int* offset_ptr = (int*)offset_value_ptr + 2; |
| 124 | |
| 125 | for (int ii = 0; ii < 4; ii++) |
| 126 | { |
| 127 | __m256 x0_val = offset_ptr[0] >= 0 ? _mm256_loadu_ps(srcptr + offset_ptr[0]) : _mm256_set1_ps(0); |
| 128 | __m256 x1_val = offset_ptr[1] >= 0 ? _mm256_loadu_ps(srcptr + offset_ptr[1]) : _mm256_set1_ps(0); |
| 129 | __m256 x2_val = offset_ptr[2] >= 0 ? _mm256_loadu_ps(srcptr + offset_ptr[2]) : _mm256_set1_ps(0); |
| 130 | __m256 x3_val = offset_ptr[3] >= 0 ? _mm256_loadu_ps(srcptr + offset_ptr[3]) : _mm256_set1_ps(0); |
| 131 | |
| 132 | value_f[ii] = _mm256_mul_ps(x_coeffs0, x0_val); |
| 133 | value_f[ii] = _mm256_comp_fmadd_ps(x_coeffs1, x1_val, value_f[ii]); |
| 134 | value_f[ii] = _mm256_comp_fmadd_ps(x_coeffs2, x2_val, value_f[ii]); |
| 135 | value_f[ii] = _mm256_comp_fmadd_ps(x_coeffs3, x3_val, value_f[ii]); |
| 136 | |
| 137 | offset_ptr += 4; |
| 138 | } |
| 139 | |
| 140 | __m256 _v = _mm256_mul_ps(y_coeffs0, value_f[0]); |
| 141 | _v = _mm256_comp_fmadd_ps(y_coeffs1, value_f[1], _v); |
| 142 | _v = _mm256_comp_fmadd_ps(y_coeffs2, value_f[2], _v); |
| 143 | _v = _mm256_comp_fmadd_ps(y_coeffs3, value_f[3], _v); |
| 144 | _mm256_storeu_ps(dstptr, _v); |
| 145 | |
| 146 | dstptr += 8; |
| 147 | offset_value_ptr += 18; |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | #endif // __AVX__ |
| 153 | static void cubic_interp1d_p4(__m128& coeffs0, __m128& coeffs1, __m128& coeffs2, __m128& coeffs3, const __m128& tx) |
| 154 | { |
| 155 | const __m128 A = _mm_set_ps1(-0.75f); |
| 156 | |
| 157 | const __m128 x0 = _mm_add_ps(tx, _mm_set_ps1(1.0f)); |
| 158 | const __m128& x1 = tx; |
| 159 | const __m128 x2 = _mm_sub_ps(_mm_set_ps1(1.0f), tx); |
| 160 | //const __m128 x3 = _mm_add_ps(x2, _mm_set_ps1(1.0f)); |
| 161 | |
| 162 | coeffs0 = _mm_sub_ps(_mm_mul_ps(_mm_add_ps(_mm_mul_ps(_mm_sub_ps(_mm_mul_ps(A, x0), _mm_mul_ps(_mm_set_ps1(5.0f), A)), x0), _mm_mul_ps(_mm_set_ps1(8.0f), A)), x0), _mm_mul_ps(_mm_set_ps1(4), A)); |
| 163 | coeffs1 = _mm_add_ps(_mm_mul_ps(_mm_mul_ps(_mm_sub_ps(_mm_mul_ps(_mm_add_ps(A, _mm_set_ps1(2.0f)), x1), _mm_add_ps(A, _mm_set_ps1(3.0f))), x1), x1), _mm_set_ps1(1.0f)); |
| 164 | coeffs2 = _mm_add_ps(_mm_mul_ps(_mm_mul_ps(_mm_sub_ps(_mm_mul_ps(_mm_add_ps(A, _mm_set_ps1(2.0f)), x2), _mm_add_ps(A, _mm_set_ps1(3.0f))), x2), x2), _mm_set_ps1(1.0f)); |
| 165 | coeffs3 = _mm_sub_ps(_mm_sub_ps(_mm_sub_ps(_mm_set_ps1(1.0f), coeffs0), coeffs1), coeffs2); |
| 166 | } |
| 167 | |
| 168 | static void gridsample_2d_bicubic_apply_interpolation_p4(const Mat& src, Mat& dst, Mat& offset_value, const Option& opt) |
| 169 | { |
| 170 | const int channels = dst.c; |
| 171 | const int outw = dst.w; |
| 172 | const int outh = dst.h; |
| 173 | const int grid_size = outw * outh; |
| 174 | |
| 175 | __m128 x_coeffs0, x_coeffs1, x_coeffs2, x_coeffs3; |
| 176 | __m128 y_coeffs0, y_coeffs1, y_coeffs2, y_coeffs3; |
| 177 | __m128 value_f[4]; |
| 178 | |
| 179 | #pragma omp parallel for num_threads(opt.num_threads) |
| 180 | for (int q = 0; q < channels; q++) |
| 181 | { |
| 182 | const float* srcptr = src.channel(q); |
| 183 | float* dstptr = dst.channel(q); |
| 184 | |
| 185 | const float* offset_value_ptr = offset_value.channel(0); |
| 186 | |
| 187 | for (int i = 0; i < grid_size; i++) |
| 188 | { |
| 189 | cubic_interp1d_p4(x_coeffs0, x_coeffs1, x_coeffs2, x_coeffs3, _mm_set_ps1(offset_value_ptr[0])); |
| 190 | cubic_interp1d_p4(y_coeffs0, y_coeffs1, y_coeffs2, y_coeffs3, _mm_set_ps1(offset_value_ptr[1])); |
| 191 | |
| 192 | const int* offset_ptr = (int*)offset_value_ptr + 2; |
| 193 | |
| 194 | for (int ii = 0; ii < 4; ii++) |
| 195 | { |
| 196 | __m128 x0_val = offset_ptr[0] >= 0 ? _mm_loadu_ps(srcptr + offset_ptr[0]) : _mm_set1_ps(0); |
| 197 | __m128 x1_val = offset_ptr[1] >= 0 ? _mm_loadu_ps(srcptr + offset_ptr[1]) : _mm_set1_ps(0); |
| 198 | __m128 x2_val = offset_ptr[2] >= 0 ? _mm_loadu_ps(srcptr + offset_ptr[2]) : _mm_set1_ps(0); |
| 199 | __m128 x3_val = offset_ptr[3] >= 0 ? _mm_loadu_ps(srcptr + offset_ptr[3]) : _mm_set1_ps(0); |
| 200 | |
| 201 | value_f[ii] = _mm_mul_ps(x_coeffs0, x0_val); |
| 202 | value_f[ii] = _mm_comp_fmadd_ps(x_coeffs1, x1_val, value_f[ii]); |
| 203 | value_f[ii] = _mm_comp_fmadd_ps(x_coeffs2, x2_val, value_f[ii]); |
| 204 | value_f[ii] = _mm_comp_fmadd_ps(x_coeffs3, x3_val, value_f[ii]); |
| 205 | |
| 206 | offset_ptr += 4; |
| 207 | } |
| 208 | |
| 209 | __m128 _v = _mm_mul_ps(y_coeffs0, value_f[0]); |
| 210 | _v = _mm_comp_fmadd_ps(y_coeffs1, value_f[1], _v); |
| 211 | _v = _mm_comp_fmadd_ps(y_coeffs2, value_f[2], _v); |
| 212 | _v = _mm_comp_fmadd_ps(y_coeffs3, value_f[3], _v); |
| 213 | _mm_storeu_ps(dstptr, _v); |
| 214 | |
| 215 | dstptr += 4; |
| 216 | offset_value_ptr += 18; |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | #endif // __SSE2__ |
| 222 | |
| 223 | static inline void cubic_interp1d(float& coeffs0, float& coeffs1, float& coeffs2, float& coeffs3, float fx) |
| 224 | { |
| 225 | const float A = -0.75f; |
| 226 | |
| 227 | float fx0 = fx + 1; |
| 228 | float fx1 = fx; |
| 229 | float fx2 = 1 - fx; |
| 230 | // float fx3 = 2 - fx; |
| 231 | |
| 232 | coeffs0 = A * fx0 * fx0 * fx0 - 5 * A * fx0 * fx0 + 8 * A * fx0 - 4 * A; |
| 233 | coeffs1 = (A + 2) * fx1 * fx1 * fx1 - (A + 3) * fx1 * fx1 + 1; |
| 234 | coeffs2 = (A + 2) * fx2 * fx2 * fx2 - (A + 3) * fx2 * fx2 + 1; |
| 235 | coeffs3 = 1.f - coeffs0 - coeffs1 - coeffs2; |
| 236 | } |
| 237 | |
| 238 | static void gridsample_2d_bicubic_apply_interpolation_p1(const Mat& src, Mat& dst, Mat& offset_value, const Option& opt) |
| 239 | { |
| 240 | const int channels = dst.c; |
| 241 | const int outw = dst.w; |
| 242 | const int outh = dst.h; |
| 243 | const int grid_size = outw * outh; |
| 244 | |
| 245 | #pragma omp parallel for num_threads(opt.num_threads) |
| 246 | for (int q = 0; q < channels; q++) |
| 247 | { |
| 248 | const float* srcptr = src.channel(q); |
| 249 | float* dstptr = dst.channel(q); |
| 250 | |
| 251 | const float* offset_value_ptr = offset_value.channel(0); |
| 252 | |
| 253 | for (int x = 0; x < grid_size; x++) |
| 254 | { |
| 255 | float x_coeffs0, x_coeffs1, x_coeffs2, x_coeffs3; |
| 256 | float y_coeffs0, y_coeffs1, y_coeffs2, y_coeffs3; |
| 257 | float value_f[4]; |
| 258 | cubic_interp1d(x_coeffs0, x_coeffs1, x_coeffs2, x_coeffs3, offset_value_ptr[0]); |
| 259 | cubic_interp1d(y_coeffs0, y_coeffs1, y_coeffs2, y_coeffs3, offset_value_ptr[1]); |
| 260 | |
| 261 | const int* offset_ptr = (int*)offset_value_ptr + 2; |
| 262 | |
| 263 | for (int ii = 0; ii < 4; ii++) |
| 264 | { |
| 265 | float x0_val = offset_ptr[0] >= 0 ? *(srcptr + offset_ptr[0]) : 0; |
| 266 | float x1_val = offset_ptr[1] >= 0 ? *(srcptr + offset_ptr[1]) : 0; |
| 267 | float x2_val = offset_ptr[2] >= 0 ? *(srcptr + offset_ptr[2]) : 0; |
| 268 | float x3_val = offset_ptr[3] >= 0 ? *(srcptr + offset_ptr[3]) : 0; |
| 269 | |
| 270 | value_f[ii] = x_coeffs0 * x0_val; |
| 271 | value_f[ii] = x_coeffs1 * x1_val + value_f[ii]; |
| 272 | value_f[ii] = x_coeffs2 * x2_val + value_f[ii]; |
| 273 | value_f[ii] = x_coeffs3 * x3_val + value_f[ii]; |
| 274 | |
| 275 | offset_ptr += 4; |
| 276 | } |
| 277 | |
| 278 | float _v = y_coeffs0 * value_f[0]; |
| 279 | _v = y_coeffs1 * value_f[1] + _v; |
| 280 | _v = y_coeffs2 * value_f[2] + _v; |
| 281 | _v = y_coeffs3 * value_f[3] + _v; |
| 282 | *dstptr = _v; |
| 283 | |
| 284 | dstptr++; |
| 285 | offset_value_ptr += 18; |
| 286 | } |
| 287 | } |
| 288 | } |