Elffuss BraidHub

kikocis/ncnn public

master · 30.8 KiBRawPermalink
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
15template<GridSample::PaddingMode pd, bool align_corner>
16void gridsample_2d_bilinear_compute_blob(const Mat& src, const Mat& grid, Mat& offset_value, int permute_fusion)
17{
18 const int grid_size = grid.w * grid.h;
19
20 float* offset_value_ptr = offset_value.channel(0);
21
22 grid_sample_unormalize<align_corner> unormalize;
23 compute_coord<pd, align_corner> get_coord;
24
25 if (permute_fusion == 0)
26 {
27 for (int y = 0; y < grid.c; y++)
28 {
29 const float* gridptr = grid.channel(y);
30 int x = 0;
31#if __AVX__
32 for (; x + 15 < grid_size; x += 16)
33 {
34 __m256 gx = _mm256_loadu_ps(gridptr);
35 __m256 gy = _mm256_loadu_ps(gridptr + 8);
36
37 transpose2x8_ps(gx, gy);
38
39 gx = unormalize(_mm256_set1_ps(src.w), gx);
40 gx = get_coord(_mm256_set1_ps(src.w), gx);
41
42 gy = unormalize(_mm256_set1_ps(src.h), gy);
43 gy = get_coord(_mm256_set1_ps(src.h), gy);
44
45 __m256 x_w = _mm256_floor_ps(gx);
46 __m256 y_n = _mm256_floor_ps(gy);
47
48 __m256 x1 = _mm256_add_ps(x_w, _mm256_set1_ps(1));
49 __m256 y1 = _mm256_add_ps(y_n, _mm256_set1_ps(1));
50
51 __m256 x0_in_range = _mm256_and_ps(_mm256_cmp_ps(x_w, _mm256_set1_ps(-1), _CMP_GT_OS), _mm256_cmp_ps(_mm256_set1_ps(src.w), x_w, _CMP_GT_OS));
52 __m256 x1_in_range = _mm256_and_ps(_mm256_cmp_ps(x1, _mm256_set1_ps(-1), _CMP_GT_OS), _mm256_cmp_ps(_mm256_set1_ps(src.w), x1, _CMP_GT_OS));
53 __m256 y0_in_range = _mm256_and_ps(_mm256_cmp_ps(y_n, _mm256_set1_ps(-1), _CMP_GT_OS), _mm256_cmp_ps(_mm256_set1_ps(src.h), y_n, _CMP_GT_OS));
54 __m256 y1_in_range = _mm256_and_ps(_mm256_cmp_ps(y1, _mm256_set1_ps(-1), _CMP_GT_OS), _mm256_cmp_ps(_mm256_set1_ps(src.h), y1, _CMP_GT_OS));
55
56 __m256 v00_in_range = _mm256_and_ps(x0_in_range, y0_in_range);
57 __m256 v01_in_range = _mm256_and_ps(x1_in_range, y0_in_range);
58 __m256 v10_in_range = _mm256_and_ps(x0_in_range, y1_in_range);
59 __m256 v11_in_range = _mm256_and_ps(x1_in_range, y1_in_range);
60
61 __m256 nw_offset = _mm256_mul_ps(_mm256_comp_fmadd_ps(y_n, _mm256_set1_ps(src.w), x_w), _mm256_set1_ps(src.elempack));
62 __m256 ne_offset = _mm256_add_ps(nw_offset, _mm256_set1_ps(src.elempack));
63 __m256 sw_offset = _mm256_comp_fmadd_ps(_mm256_set1_ps(src.w), _mm256_set1_ps(src.elempack), nw_offset);
64 __m256 se_offset = _mm256_add_ps(sw_offset, _mm256_set1_ps(src.elempack));
65
66 nw_offset = _mm256_blendv_ps(_mm256_set1_ps(-1.0f), nw_offset, v00_in_range);
67 ne_offset = _mm256_blendv_ps(_mm256_set1_ps(-1.0f), ne_offset, v01_in_range);
68 sw_offset = _mm256_blendv_ps(_mm256_set1_ps(-1.0f), sw_offset, v10_in_range);
69 se_offset = _mm256_blendv_ps(_mm256_set1_ps(-1.0f), se_offset, v11_in_range);
70
71 nw_offset = _mm256_castsi256_ps(_mm256_cvtps_epi32(nw_offset));
72 ne_offset = _mm256_castsi256_ps(_mm256_cvtps_epi32(ne_offset));
73 sw_offset = _mm256_castsi256_ps(_mm256_cvtps_epi32(sw_offset));
74 se_offset = _mm256_castsi256_ps(_mm256_cvtps_epi32(se_offset));
75
76 __m256 alpha = _mm256_sub_ps(gx, x_w);
77 __m256 beta = _mm256_sub_ps(gy, y_n);
78
79 transpose8x6_ps(nw_offset, ne_offset, sw_offset, se_offset, alpha, beta);
80
81 _mm256_storeu_ps(offset_value_ptr, nw_offset);
82 _mm256_storeu_ps(offset_value_ptr + 8, ne_offset);
83 _mm256_storeu_ps(offset_value_ptr + 16, sw_offset);
84 _mm256_storeu_ps(offset_value_ptr + 24, se_offset);
85
86 _mm256_storeu_ps(offset_value_ptr + 32, alpha);
87 _mm256_storeu_ps(offset_value_ptr + 40, beta);
88
89 gridptr += 16;
90 offset_value_ptr += 48;
91 }
92#endif // __AVX__
93
94 for (; x < grid_size; x += 2)
95 {
96 float sample_x = *gridptr;
97 float sample_y = *(gridptr + 1);
98
99 sample_x = unormalize(src.w, sample_x);
100 sample_x = get_coord(src.w, sample_x);
101
102 sample_y = unormalize(src.h, sample_y);
103 sample_y = get_coord(src.h, sample_y);
104
105 int x0 = (int)floorf(sample_x);
106 int y0 = (int)floorf(sample_y);
107 int x1 = x0 + 1;
108 int y1 = y0 + 1;
109
110 bool x0_in_bound = (x0 > -1) & (x0 < src.w);
111 bool x1_in_bound = (x1 > -1) & (x1 < src.w);
112 bool y0_in_bound = (y0 > -1) & (y0 < src.h);
113 bool y1_in_bound = (y1 > -1) & (y1 < src.h);
114
115 bool in_bound_00 = x0_in_bound & y0_in_bound;
116 bool in_bound_01 = x1_in_bound & y0_in_bound;
117 bool in_bound_10 = x0_in_bound & y1_in_bound;
118 bool in_bound_11 = x1_in_bound & y1_in_bound;
119
120 int* offset_ptr = (int*)offset_value_ptr;
121 float* value_ptr = offset_value_ptr + 4;
122
123 offset_ptr[0] = in_bound_00 ? (x0 + y0 * src.w) * src.elempack : -1.0;
124 offset_ptr[1] = in_bound_01 ? (x1 + y0 * src.w) * src.elempack : -1.0;
125 offset_ptr[2] = in_bound_10 ? (x0 + y1 * src.w) * src.elempack : -1.0;
126 offset_ptr[3] = in_bound_11 ? (x1 + y1 * src.w) * src.elempack : -1.0;
127
128 value_ptr[0] = sample_x - x0;
129 value_ptr[1] = sample_y - y0;
130
131 gridptr += 2;
132 offset_value_ptr += 6;
133 }
134 }
135 }
136 else
137 {
138 const float* gridptr_x = grid.channel(0);
139 const float* gridptr_y = grid.channel(1);
140
141 int x = 0;
142#if __AVX__
143 for (; x + 7 < grid_size; x += 8)
144 {
145 __m256 gx = _mm256_loadu_ps(gridptr_x);
146 __m256 gy = _mm256_loadu_ps(gridptr_y);
147
148 gx = unormalize(_mm256_set1_ps(src.w), gx);
149 gx = get_coord(_mm256_set1_ps(src.w), gx);
150
151 gy = unormalize(_mm256_set1_ps(src.h), gy);
152 gy = get_coord(_mm256_set1_ps(src.h), gy);
153
154 __m256 x_w = _mm256_floor_ps(gx);
155 __m256 y_n = _mm256_floor_ps(gy);
156
157 __m256 x1 = _mm256_add_ps(x_w, _mm256_set1_ps(1));
158 __m256 y1 = _mm256_add_ps(y_n, _mm256_set1_ps(1));
159
160 __m256 x0_in_range = _mm256_and_ps(_mm256_cmp_ps(x_w, _mm256_set1_ps(-1), _CMP_GT_OS), _mm256_cmp_ps(_mm256_set1_ps(src.w), x_w, _CMP_GT_OS));
161 __m256 x1_in_range = _mm256_and_ps(_mm256_cmp_ps(x1, _mm256_set1_ps(-1), _CMP_GT_OS), _mm256_cmp_ps(_mm256_set1_ps(src.w), x1, _CMP_GT_OS));
162 __m256 y0_in_range = _mm256_and_ps(_mm256_cmp_ps(y_n, _mm256_set1_ps(-1), _CMP_GT_OS), _mm256_cmp_ps(_mm256_set1_ps(src.h), y_n, _CMP_GT_OS));
163 __m256 y1_in_range = _mm256_and_ps(_mm256_cmp_ps(y1, _mm256_set1_ps(-1), _CMP_GT_OS), _mm256_cmp_ps(_mm256_set1_ps(src.h), y1, _CMP_GT_OS));
164
165 __m256 v00_in_range = _mm256_and_ps(x0_in_range, y0_in_range);
166 __m256 v01_in_range = _mm256_and_ps(x1_in_range, y0_in_range);
167 __m256 v10_in_range = _mm256_and_ps(x0_in_range, y1_in_range);
168 __m256 v11_in_range = _mm256_and_ps(x1_in_range, y1_in_range);
169
170 __m256 nw_offset = _mm256_mul_ps(_mm256_comp_fmadd_ps(y_n, _mm256_set1_ps(src.w), x_w), _mm256_set1_ps(src.elempack));
171 __m256 ne_offset = _mm256_add_ps(nw_offset, _mm256_set1_ps(src.elempack));
172 __m256 sw_offset = _mm256_comp_fmadd_ps(_mm256_set1_ps(src.w), _mm256_set1_ps(src.elempack), nw_offset);
173 __m256 se_offset = _mm256_add_ps(sw_offset, _mm256_set1_ps(src.elempack));
174
175 nw_offset = _mm256_blendv_ps(_mm256_set1_ps(-1.0f), nw_offset, v00_in_range);
176 ne_offset = _mm256_blendv_ps(_mm256_set1_ps(-1.0f), ne_offset, v01_in_range);
177 sw_offset = _mm256_blendv_ps(_mm256_set1_ps(-1.0f), sw_offset, v10_in_range);
178 se_offset = _mm256_blendv_ps(_mm256_set1_ps(-1.0f), se_offset, v11_in_range);
179
180 nw_offset = _mm256_castsi256_ps(_mm256_cvtps_epi32(nw_offset));
181 ne_offset = _mm256_castsi256_ps(_mm256_cvtps_epi32(ne_offset));
182 sw_offset = _mm256_castsi256_ps(_mm256_cvtps_epi32(sw_offset));
183 se_offset = _mm256_castsi256_ps(_mm256_cvtps_epi32(se_offset));
184
185 __m256 alpha = _mm256_sub_ps(gx, x_w);
186 __m256 beta = _mm256_sub_ps(gy, y_n);
187
188 transpose8x6_ps(nw_offset, ne_offset, sw_offset, se_offset, alpha, beta);
189
190 _mm256_storeu_ps(offset_value_ptr, nw_offset);
191 _mm256_storeu_ps(offset_value_ptr + 8, ne_offset);
192 _mm256_storeu_ps(offset_value_ptr + 16, sw_offset);
193 _mm256_storeu_ps(offset_value_ptr + 24, se_offset);
194
195 _mm256_storeu_ps(offset_value_ptr + 32, alpha);
196 _mm256_storeu_ps(offset_value_ptr + 40, beta);
197
198 gridptr_x += 8;
199 gridptr_y += 8;
200 offset_value_ptr += 48;
201 }
202
203#endif // __AVX__
204
205 for (; x < grid_size; x++)
206 {
207 float sample_x = *gridptr_x;
208 float sample_y = *gridptr_y;
209
210 sample_x = unormalize(src.w, sample_x);
211 sample_x = get_coord(src.w, sample_x);
212
213 sample_y = unormalize(src.h, sample_y);
214 sample_y = get_coord(src.h, sample_y);
215
216 int x0 = (int)floorf(sample_x);
217 int y0 = (int)floorf(sample_y);
218 int x1 = x0 + 1;
219 int y1 = y0 + 1;
220
221 bool x0_in_bound = (x0 > -1) & (x0 < src.w);
222 bool x1_in_bound = (x1 > -1) & (x1 < src.w);
223 bool y0_in_bound = (y0 > -1) & (y0 < src.h);
224 bool y1_in_bound = (y1 > -1) & (y1 < src.h);
225
226 bool in_bound_00 = x0_in_bound & y0_in_bound;
227 bool in_bound_01 = x1_in_bound & y0_in_bound;
228 bool in_bound_10 = x0_in_bound & y1_in_bound;
229 bool in_bound_11 = x1_in_bound & y1_in_bound;
230
231 int* offset_ptr = (int*)offset_value_ptr;
232 float* value_ptr = offset_value_ptr + 4;
233
234 offset_ptr[0] = in_bound_00 ? (x0 + y0 * src.w) * src.elempack : -1.0;
235 offset_ptr[1] = in_bound_01 ? (x1 + y0 * src.w) * src.elempack : -1.0;
236 offset_ptr[2] = in_bound_10 ? (x0 + y1 * src.w) * src.elempack : -1.0;
237 offset_ptr[3] = in_bound_11 ? (x1 + y1 * src.w) * src.elempack : -1.0;
238
239 value_ptr[0] = sample_x - x0;
240 value_ptr[1] = sample_y - y0;
241
242 gridptr_x++;
243 gridptr_y++;
244 offset_value_ptr += 6;
245 }
246 }
247}
248
249template<GridSample::PaddingMode pd, bool align_corner>
250void gridsample_3d_bilinear_compute_blob(const Mat& src, const Mat& grid, Mat& offset_value, int permute_fusion)
251{
252 const int grid_size = grid.w * grid.h * grid.d;
253
254 float* offset_value_ptr = offset_value.channel(0);
255
256 grid_sample_unormalize<align_corner> unormalize;
257 compute_coord<pd, align_corner> get_coord;
258
259 if (permute_fusion == 0)
260 {
261 for (int y = 0; y < grid.c; y++)
262 {
263 const float* gridptr = grid.channel(y);
264 int x = 0;
265#if __AVX__
266 for (; x + 23 < grid_size; x += 24)
267 {
268 __m256 gx = _mm256_loadu_ps(gridptr);
269 __m256 gy = _mm256_loadu_ps(gridptr + 8);
270 __m256 gz = _mm256_loadu_ps(gridptr + 16);
271
272 transpose3x8_ps(gx, gy, gz);
273
274 gx = unormalize(_mm256_set1_ps(src.w), gx);
275 gx = get_coord(_mm256_set1_ps(src.w), gx);
276
277 gy = unormalize(_mm256_set1_ps(src.h), gy);
278 gy = get_coord(_mm256_set1_ps(src.h), gy);
279
280 gz = unormalize(_mm256_set1_ps(src.d), gz);
281 gz = get_coord(_mm256_set1_ps(src.d), gz);
282
283 __m256 x_w = _mm256_floor_ps(gx);
284 __m256 y_n = _mm256_floor_ps(gy);
285 __m256 z_t = _mm256_floor_ps(gz);
286
287 __m256 x1 = _mm256_add_ps(x_w, _mm256_set1_ps(1));
288 __m256 y1 = _mm256_add_ps(y_n, _mm256_set1_ps(1));
289 __m256 z1 = _mm256_add_ps(z_t, _mm256_set1_ps(1));
290
291 __m256 x0_in_range = _mm256_and_ps(_mm256_cmp_ps(x_w, _mm256_set1_ps(-1), _CMP_GT_OS), _mm256_cmp_ps(_mm256_set1_ps(src.w), x_w, _CMP_GT_OS));
292 __m256 x1_in_range = _mm256_and_ps(_mm256_cmp_ps(x1, _mm256_set1_ps(-1), _CMP_GT_OS), _mm256_cmp_ps(_mm256_set1_ps(src.w), x1, _CMP_GT_OS));
293 __m256 y0_in_range = _mm256_and_ps(_mm256_cmp_ps(y_n, _mm256_set1_ps(-1), _CMP_GT_OS), _mm256_cmp_ps(_mm256_set1_ps(src.h), y_n, _CMP_GT_OS));
294 __m256 y1_in_range = _mm256_and_ps(_mm256_cmp_ps(y1, _mm256_set1_ps(-1), _CMP_GT_OS), _mm256_cmp_ps(_mm256_set1_ps(src.h), y1, _CMP_GT_OS));
295 __m256 z0_in_range = _mm256_and_ps(_mm256_cmp_ps(z_t, _mm256_set1_ps(-1), _CMP_GT_OS), _mm256_cmp_ps(_mm256_set1_ps(src.d), z_t, _CMP_GT_OS));
296 __m256 z1_in_range = _mm256_and_ps(_mm256_cmp_ps(z1, _mm256_set1_ps(-1), _CMP_GT_OS), _mm256_cmp_ps(_mm256_set1_ps(src.d), z1, _CMP_GT_OS));
297
298 __m256 v000_in_range, v010_in_range, v100_in_range, v110_in_range, v001_in_range, v011_in_range, v101_in_range, v111_in_range;
299 {
300 __m256 v00_in_range = _mm256_and_ps(x0_in_range, y0_in_range);
301 __m256 v01_in_range = _mm256_and_ps(x1_in_range, y0_in_range);
302 __m256 v10_in_range = _mm256_and_ps(x0_in_range, y1_in_range);
303 __m256 v11_in_range = _mm256_and_ps(x1_in_range, y1_in_range);
304
305 v000_in_range = _mm256_and_ps(v00_in_range, z0_in_range);
306 v001_in_range = _mm256_and_ps(v01_in_range, z0_in_range);
307 v010_in_range = _mm256_and_ps(v10_in_range, z0_in_range);
308 v011_in_range = _mm256_and_ps(v11_in_range, z0_in_range);
309
310 v100_in_range = _mm256_and_ps(v00_in_range, z1_in_range);
311 v101_in_range = _mm256_and_ps(v01_in_range, z1_in_range);
312 v110_in_range = _mm256_and_ps(v10_in_range, z1_in_range);
313 v111_in_range = _mm256_and_ps(v11_in_range, z1_in_range);
314 }
315
316 __m256 tnw_offset = _mm256_mul_ps(_mm256_comp_fmadd_ps(_mm256_mul_ps(_mm256_set1_ps(src.w), _mm256_set1_ps(src.h)), z_t,
317 _mm256_comp_fmadd_ps(y_n, _mm256_set1_ps(src.w), x_w)),
318 _mm256_set1_ps(src.elempack));
319 __m256 tne_offset = _mm256_add_ps(tnw_offset, _mm256_set1_ps(src.elempack));
320 __m256 tsw_offset = _mm256_add_ps(tnw_offset, _mm256_mul_ps(_mm256_set1_ps(src.w), _mm256_set1_ps(src.elempack)));
321 __m256 tse_offset = _mm256_add_ps(tsw_offset, _mm256_set1_ps(src.elempack));
322
323 __m256 bnw_offset = _mm256_comp_fmadd_ps(_mm256_mul_ps(_mm256_set1_ps(src.w), _mm256_set1_ps(src.h)), _mm256_set1_ps(src.elempack), tnw_offset);
324 __m256 bne_offset = _mm256_add_ps(bnw_offset, _mm256_set1_ps(src.elempack));
325 __m256 bsw_offset = _mm256_add_ps(bnw_offset, _mm256_mul_ps(_mm256_set1_ps(src.w), _mm256_set1_ps(src.elempack)));
326 __m256 bse_offset = _mm256_add_ps(bsw_offset, _mm256_set1_ps(src.elempack));
327
328 tnw_offset = _mm256_blendv_ps(_mm256_set1_ps(-1.0f), tnw_offset, v000_in_range);
329 tne_offset = _mm256_blendv_ps(_mm256_set1_ps(-1.0f), tne_offset, v001_in_range);
330 tsw_offset = _mm256_blendv_ps(_mm256_set1_ps(-1.0f), tsw_offset, v010_in_range);
331 tse_offset = _mm256_blendv_ps(_mm256_set1_ps(-1.0f), tse_offset, v011_in_range);
332
333 bnw_offset = _mm256_blendv_ps(_mm256_set1_ps(-1.0f), bnw_offset, v100_in_range);
334 bne_offset = _mm256_blendv_ps(_mm256_set1_ps(-1.0f), bne_offset, v101_in_range);
335 bsw_offset = _mm256_blendv_ps(_mm256_set1_ps(-1.0f), bsw_offset, v110_in_range);
336 bse_offset = _mm256_blendv_ps(_mm256_set1_ps(-1.0f), bse_offset, v111_in_range);
337
338 tnw_offset = _mm256_castsi256_ps(_mm256_cvtps_epi32(tnw_offset));
339 tne_offset = _mm256_castsi256_ps(_mm256_cvtps_epi32(tne_offset));
340 tsw_offset = _mm256_castsi256_ps(_mm256_cvtps_epi32(tsw_offset));
341 tse_offset = _mm256_castsi256_ps(_mm256_cvtps_epi32(tse_offset));
342
343 bnw_offset = _mm256_castsi256_ps(_mm256_cvtps_epi32(bnw_offset));
344 bne_offset = _mm256_castsi256_ps(_mm256_cvtps_epi32(bne_offset));
345 bsw_offset = _mm256_castsi256_ps(_mm256_cvtps_epi32(bsw_offset));
346 bse_offset = _mm256_castsi256_ps(_mm256_cvtps_epi32(bse_offset));
347
348 __m256 alpha = _mm256_sub_ps(gx, x_w);
349 __m256 beta = _mm256_sub_ps(gy, y_n);
350 __m256 gamma = _mm256_sub_ps(gz, z_t);
351
352 transpose8x11_ps(tnw_offset, tne_offset, tsw_offset, tse_offset, bnw_offset, bne_offset, bsw_offset, bse_offset, alpha, beta, gamma);
353
354 _mm256_storeu_ps(offset_value_ptr, tnw_offset);
355 _mm256_storeu_ps(offset_value_ptr + 8, tne_offset);
356 _mm256_storeu_ps(offset_value_ptr + 16, tsw_offset);
357 _mm256_storeu_ps(offset_value_ptr + 24, tse_offset);
358
359 _mm256_storeu_ps(offset_value_ptr + 32, bnw_offset);
360 _mm256_storeu_ps(offset_value_ptr + 40, bne_offset);
361 _mm256_storeu_ps(offset_value_ptr + 48, bsw_offset);
362 _mm256_storeu_ps(offset_value_ptr + 56, bse_offset);
363
364 _mm256_storeu_ps(offset_value_ptr + 64, alpha);
365 _mm256_storeu_ps(offset_value_ptr + 72, beta);
366 _mm256_storeu_ps(offset_value_ptr + 80, gamma);
367
368 gridptr += 24;
369
370 offset_value_ptr += 88;
371 }
372#endif // __AVX__
373
374 for (; x < grid_size; x += 3)
375 {
376 float sample_x = *gridptr;
377 float sample_y = *(gridptr + 1);
378 float sample_z = *(gridptr + 2);
379
380 sample_x = unormalize(src.w, sample_x);
381 sample_x = get_coord(src.w, sample_x);
382
383 sample_y = unormalize(src.h, sample_y);
384 sample_y = get_coord(src.h, sample_y);
385
386 sample_z = unormalize(src.d, sample_z);
387 sample_z = get_coord(src.d, sample_z);
388
389 int x0 = (int)floorf(sample_x);
390 int y0 = (int)floorf(sample_y);
391 int z0 = (int)floorf(sample_z);
392 int x1 = x0 + 1;
393 int y1 = y0 + 1;
394 int z1 = z0 + 1;
395
396 bool x0_in_range = (x0 > -1) & (x0 < src.w);
397 bool y0_in_range = (y0 > -1) & (y0 < src.h);
398 bool z0_in_range = (z0 > -1) & (z0 < src.d);
399 bool x1_in_range = (x1 > -1) & (x1 < src.w);
400 bool y1_in_range = (y1 > -1) & (y1 < src.h);
401 bool z1_in_range = (z1 > -1) & (z1 < src.d);
402
403 bool v00_in_range = x0_in_range & y0_in_range;
404 bool v01_in_range = x1_in_range & y0_in_range;
405 bool v10_in_range = x0_in_range & y1_in_range;
406 bool v11_in_range = x1_in_range & y1_in_range;
407
408 bool in_bound_000 = v00_in_range & z0_in_range;
409 bool in_bound_001 = v01_in_range & z0_in_range;
410 bool in_bound_010 = v10_in_range & z0_in_range;
411 bool in_bound_011 = v11_in_range & z0_in_range;
412
413 bool in_bound_100 = v00_in_range & z1_in_range;
414 bool in_bound_101 = v01_in_range & z1_in_range;
415 bool in_bound_110 = v10_in_range & z1_in_range;
416 bool in_bound_111 = v11_in_range & z1_in_range;
417
418 int* offset_ptr = (int*)offset_value_ptr;
419 float* value_ptr = offset_value_ptr + 8;
420
421 offset_ptr[0] = in_bound_000 ? (x0 + y0 * src.w + z0 * src.w * src.h) * src.elempack : -1.0;
422 offset_ptr[1] = in_bound_001 ? (x1 + y0 * src.w + z0 * src.w * src.h) * src.elempack : -1.0;
423 offset_ptr[2] = in_bound_010 ? (x0 + y1 * src.w + z0 * src.w * src.h) * src.elempack : -1.0;
424 offset_ptr[3] = in_bound_011 ? (x1 + y1 * src.w + z0 * src.w * src.h) * src.elempack : -1.0;
425
426 offset_ptr[4] = in_bound_100 ? (x0 + y0 * src.w + z1 * src.w * src.h) * src.elempack : -1.0;
427 offset_ptr[5] = in_bound_101 ? (x1 + y0 * src.w + z1 * src.w * src.h) * src.elempack : -1.0;
428 offset_ptr[6] = in_bound_110 ? (x0 + y1 * src.w + z1 * src.w * src.h) * src.elempack : -1.0;
429 offset_ptr[7] = in_bound_111 ? (x1 + y1 * src.w + z1 * src.w * src.h) * src.elempack : -1.0;
430
431 value_ptr[0] = sample_x - x0;
432 value_ptr[1] = sample_y - y0;
433 value_ptr[2] = sample_z - z0;
434
435 gridptr += 3;
436 offset_value_ptr += 11;
437 }
438 }
439 }
440 else
441 {
442 const float* gridptr_x = grid.channel(0);
443 const float* gridptr_y = grid.channel(1);
444 const float* gridptr_z = grid.channel(2);
445
446 int x = 0;
447#if __AVX__
448 for (; x + 7 < grid_size; x += 8)
449 {
450 __m256 gx = _mm256_loadu_ps(gridptr_x);
451 __m256 gy = _mm256_loadu_ps(gridptr_y);
452 __m256 gz = _mm256_loadu_ps(gridptr_z);
453
454 gx = unormalize(_mm256_set1_ps(src.w), gx);
455 gx = get_coord(_mm256_set1_ps(src.w), gx);
456
457 gy = unormalize(_mm256_set1_ps(src.h), gy);
458 gy = get_coord(_mm256_set1_ps(src.h), gy);
459
460 gz = unormalize(_mm256_set1_ps(src.d), gz);
461 gz = get_coord(_mm256_set1_ps(src.d), gz);
462
463 __m256 x_w = _mm256_floor_ps(gx);
464 __m256 y_n = _mm256_floor_ps(gy);
465 __m256 z_t = _mm256_floor_ps(gz);
466
467 __m256 x1 = _mm256_add_ps(x_w, _mm256_set1_ps(1));
468 __m256 y1 = _mm256_add_ps(y_n, _mm256_set1_ps(1));
469 __m256 z1 = _mm256_add_ps(z_t, _mm256_set1_ps(1));
470
471 __m256 x0_in_range = _mm256_and_ps(_mm256_cmp_ps(x_w, _mm256_set1_ps(-1), _CMP_GT_OS), _mm256_cmp_ps(_mm256_set1_ps(src.w), x_w, _CMP_GT_OS));
472 __m256 x1_in_range = _mm256_and_ps(_mm256_cmp_ps(x1, _mm256_set1_ps(-1), _CMP_GT_OS), _mm256_cmp_ps(_mm256_set1_ps(src.w), x1, _CMP_GT_OS));
473 __m256 y0_in_range = _mm256_and_ps(_mm256_cmp_ps(y_n, _mm256_set1_ps(-1), _CMP_GT_OS), _mm256_cmp_ps(_mm256_set1_ps(src.h), y_n, _CMP_GT_OS));
474 __m256 y1_in_range = _mm256_and_ps(_mm256_cmp_ps(y1, _mm256_set1_ps(-1), _CMP_GT_OS), _mm256_cmp_ps(_mm256_set1_ps(src.h), y1, _CMP_GT_OS));
475 __m256 z0_in_range = _mm256_and_ps(_mm256_cmp_ps(z_t, _mm256_set1_ps(-1), _CMP_GT_OS), _mm256_cmp_ps(_mm256_set1_ps(src.d), z_t, _CMP_GT_OS));
476 __m256 z1_in_range = _mm256_and_ps(_mm256_cmp_ps(z1, _mm256_set1_ps(-1), _CMP_GT_OS), _mm256_cmp_ps(_mm256_set1_ps(src.d), z1, _CMP_GT_OS));
477
478 __m256 v000_in_range, v010_in_range, v100_in_range, v110_in_range, v001_in_range, v011_in_range, v101_in_range, v111_in_range;
479 {
480 __m256 v00_in_range = _mm256_and_ps(x0_in_range, y0_in_range);
481 __m256 v01_in_range = _mm256_and_ps(x1_in_range, y0_in_range);
482 __m256 v10_in_range = _mm256_and_ps(x0_in_range, y1_in_range);
483 __m256 v11_in_range = _mm256_and_ps(x1_in_range, y1_in_range);
484
485 v000_in_range = _mm256_and_ps(v00_in_range, z0_in_range);
486 v001_in_range = _mm256_and_ps(v01_in_range, z0_in_range);
487 v010_in_range = _mm256_and_ps(v10_in_range, z0_in_range);
488 v011_in_range = _mm256_and_ps(v11_in_range, z0_in_range);
489
490 v100_in_range = _mm256_and_ps(v00_in_range, z1_in_range);
491 v101_in_range = _mm256_and_ps(v01_in_range, z1_in_range);
492 v110_in_range = _mm256_and_ps(v10_in_range, z1_in_range);
493 v111_in_range = _mm256_and_ps(v11_in_range, z1_in_range);
494 }
495
496 __m256 tnw_offset = _mm256_mul_ps(_mm256_comp_fmadd_ps(_mm256_mul_ps(_mm256_set1_ps(src.w), _mm256_set1_ps(src.h)), z_t,
497 _mm256_comp_fmadd_ps(y_n, _mm256_set1_ps(src.w), x_w)),
498 _mm256_set1_ps(src.elempack));
499 __m256 tne_offset = _mm256_add_ps(tnw_offset, _mm256_set1_ps(src.elempack));
500 __m256 tsw_offset = _mm256_add_ps(tnw_offset, _mm256_mul_ps(_mm256_set1_ps(src.w), _mm256_set1_ps(src.elempack)));
501 __m256 tse_offset = _mm256_add_ps(tsw_offset, _mm256_set1_ps(src.elempack));
502
503 __m256 bnw_offset = _mm256_comp_fmadd_ps(_mm256_mul_ps(_mm256_set1_ps(src.w), _mm256_set1_ps(src.h)), _mm256_set1_ps(src.elempack), tnw_offset);
504 __m256 bne_offset = _mm256_add_ps(bnw_offset, _mm256_set1_ps(src.elempack));
505 __m256 bsw_offset = _mm256_add_ps(bnw_offset, _mm256_mul_ps(_mm256_set1_ps(src.w), _mm256_set1_ps(src.elempack)));
506 __m256 bse_offset = _mm256_add_ps(bsw_offset, _mm256_set1_ps(src.elempack));
507
508 tnw_offset = _mm256_blendv_ps(_mm256_set1_ps(-1.0f), tnw_offset, v000_in_range);
509 tne_offset = _mm256_blendv_ps(_mm256_set1_ps(-1.0f), tne_offset, v001_in_range);
510 tsw_offset = _mm256_blendv_ps(_mm256_set1_ps(-1.0f), tsw_offset, v010_in_range);
511 tse_offset = _mm256_blendv_ps(_mm256_set1_ps(-1.0f), tse_offset, v011_in_range);
512
513 bnw_offset = _mm256_blendv_ps(_mm256_set1_ps(-1.0f), bnw_offset, v100_in_range);
514 bne_offset = _mm256_blendv_ps(_mm256_set1_ps(-1.0f), bne_offset, v101_in_range);
515 bsw_offset = _mm256_blendv_ps(_mm256_set1_ps(-1.0f), bsw_offset, v110_in_range);
516 bse_offset = _mm256_blendv_ps(_mm256_set1_ps(-1.0f), bse_offset, v111_in_range);
517
518 tnw_offset = _mm256_castsi256_ps(_mm256_cvtps_epi32(tnw_offset));
519 tne_offset = _mm256_castsi256_ps(_mm256_cvtps_epi32(tne_offset));
520 tsw_offset = _mm256_castsi256_ps(_mm256_cvtps_epi32(tsw_offset));
521 tse_offset = _mm256_castsi256_ps(_mm256_cvtps_epi32(tse_offset));
522
523 bnw_offset = _mm256_castsi256_ps(_mm256_cvtps_epi32(bnw_offset));
524 bne_offset = _mm256_castsi256_ps(_mm256_cvtps_epi32(bne_offset));
525 bsw_offset = _mm256_castsi256_ps(_mm256_cvtps_epi32(bsw_offset));
526 bse_offset = _mm256_castsi256_ps(_mm256_cvtps_epi32(bse_offset));
527
528 __m256 alpha = _mm256_sub_ps(gx, x_w);
529 __m256 beta = _mm256_sub_ps(gy, y_n);
530 __m256 gamma = _mm256_sub_ps(gz, z_t);
531
532 transpose8x11_ps(tnw_offset, tne_offset, tsw_offset, tse_offset, bnw_offset, bne_offset, bsw_offset, bse_offset, alpha, beta, gamma);
533
534 _mm256_storeu_ps(offset_value_ptr, tnw_offset);
535 _mm256_storeu_ps(offset_value_ptr + 8, tne_offset);
536 _mm256_storeu_ps(offset_value_ptr + 16, tsw_offset);
537 _mm256_storeu_ps(offset_value_ptr + 24, tse_offset);
538
539 _mm256_storeu_ps(offset_value_ptr + 32, bnw_offset);
540 _mm256_storeu_ps(offset_value_ptr + 40, bne_offset);
541 _mm256_storeu_ps(offset_value_ptr + 48, bsw_offset);
542 _mm256_storeu_ps(offset_value_ptr + 56, bse_offset);
543
544 _mm256_storeu_ps(offset_value_ptr + 64, alpha);
545 _mm256_storeu_ps(offset_value_ptr + 72, beta);
546 _mm256_storeu_ps(offset_value_ptr + 80, gamma);
547
548 gridptr_x += 8;
549 gridptr_y += 8;
550 gridptr_z += 8;
551
552 offset_value_ptr += 88;
553 }
554#endif // __AVX__
555
556 for (; x < grid_size; x++)
557 {
558 float sample_x = *gridptr_x;
559 float sample_y = *gridptr_y;
560 float sample_z = *gridptr_z;
561
562 sample_x = unormalize(src.w, sample_x);
563 sample_x = get_coord(src.w, sample_x);
564
565 sample_y = unormalize(src.h, sample_y);
566 sample_y = get_coord(src.h, sample_y);
567
568 sample_z = unormalize(src.d, sample_z);
569 sample_z = get_coord(src.d, sample_z);
570
571 int x0 = (int)floorf(sample_x);
572 int y0 = (int)floorf(sample_y);
573 int z0 = (int)floorf(sample_z);
574 int x1 = x0 + 1;
575 int y1 = y0 + 1;
576 int z1 = z0 + 1;
577
578 bool x0_in_range = (x0 > -1) & (x0 < src.w);
579 bool y0_in_range = (y0 > -1) & (y0 < src.h);
580 bool z0_in_range = (z0 > -1) & (z0 < src.d);
581 bool x1_in_range = (x1 > -1) & (x1 < src.w);
582 bool y1_in_range = (y1 > -1) & (y1 < src.h);
583 bool z1_in_range = (z1 > -1) & (z1 < src.d);
584
585 bool v00_in_range = x0_in_range & y0_in_range;
586 bool v01_in_range = x1_in_range & y0_in_range;
587 bool v10_in_range = x0_in_range & y1_in_range;
588 bool v11_in_range = x1_in_range & y1_in_range;
589
590 bool in_bound_000 = v00_in_range & z0_in_range;
591 bool in_bound_001 = v01_in_range & z0_in_range;
592 bool in_bound_010 = v10_in_range & z0_in_range;
593 bool in_bound_011 = v11_in_range & z0_in_range;
594
595 bool in_bound_100 = v00_in_range & z1_in_range;
596 bool in_bound_101 = v01_in_range & z1_in_range;
597 bool in_bound_110 = v10_in_range & z1_in_range;
598 bool in_bound_111 = v11_in_range & z1_in_range;
599
600 int* offset_ptr = (int*)offset_value_ptr;
601 float* value_ptr = offset_value_ptr + 8;
602
603 offset_ptr[0] = in_bound_000 ? (x0 + y0 * src.w + z0 * src.w * src.h) * src.elempack : -1.0;
604 offset_ptr[1] = in_bound_001 ? (x1 + y0 * src.w + z0 * src.w * src.h) * src.elempack : -1.0;
605 offset_ptr[2] = in_bound_010 ? (x0 + y1 * src.w + z0 * src.w * src.h) * src.elempack : -1.0;
606 offset_ptr[3] = in_bound_011 ? (x1 + y1 * src.w + z0 * src.w * src.h) * src.elempack : -1.0;
607
608 offset_ptr[4] = in_bound_100 ? (x0 + y0 * src.w + z1 * src.w * src.h) * src.elempack : -1.0;
609 offset_ptr[5] = in_bound_101 ? (x1 + y0 * src.w + z1 * src.w * src.h) * src.elempack : -1.0;
610 offset_ptr[6] = in_bound_110 ? (x0 + y1 * src.w + z1 * src.w * src.h) * src.elempack : -1.0;
611 offset_ptr[7] = in_bound_111 ? (x1 + y1 * src.w + z1 * src.w * src.h) * src.elempack : -1.0;
612
613 value_ptr[0] = sample_x - x0;
614 value_ptr[1] = sample_y - y0;
615 value_ptr[2] = sample_z - z0;
616
617 gridptr_x++;
618 gridptr_y++;
619 gridptr_z++;
620 offset_value_ptr += 11;
621 }
622 }
623}