Elffuss BraidHub

kikocis/ncnn public

master · 11.2 KiBRawPermalink
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
15static void resize_bicubic_image_pack16(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)16 * 4u, 16);
22 Mat rowsbuf1(w, (size_t)16 * 4u, 16);
23 Mat rowsbuf2(w, (size_t)16 * 4u, 16);
24 Mat rowsbuf3(w, (size_t)16 * 4u, 16);
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] * 16;
55 const float* S3p = S3 + sx;
56
57 __m512 _a0 = _mm512_set1_ps(alphap[0]);
58 __m512 _a1 = _mm512_set1_ps(alphap[1]);
59 __m512 _a2 = _mm512_set1_ps(alphap[2]);
60 __m512 _a3 = _mm512_set1_ps(alphap[3]);
61
62 __m512 _S30 = _mm512_load_ps(S3p - 16);
63 __m512 _S31 = _mm512_load_ps(S3p + 0);
64 __m512 _S32 = _mm512_load_ps(S3p + 16);
65 __m512 _S33 = _mm512_load_ps(S3p + 32);
66 __m512 _rows3 = _mm512_mul_ps(_S30, _a0);
67 _rows3 = _mm512_fmadd_ps(_S31, _a1, _rows3);
68 _rows3 = _mm512_fmadd_ps(_S32, _a2, _rows3);
69 _rows3 = _mm512_fmadd_ps(_S33, _a3, _rows3);
70 _mm512_store_ps(rows3p + dx * 16, _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] * 16;
93 const float* S2p = S2 + sx;
94 const float* S3p = S3 + sx;
95
96 __m512 _a0 = _mm512_set1_ps(alphap[0]);
97 __m512 _a1 = _mm512_set1_ps(alphap[1]);
98 __m512 _a2 = _mm512_set1_ps(alphap[2]);
99 __m512 _a3 = _mm512_set1_ps(alphap[3]);
100
101 __m512 _S20 = _mm512_load_ps(S2p - 16);
102 __m512 _S21 = _mm512_load_ps(S2p + 0);
103 __m512 _S22 = _mm512_load_ps(S2p + 16);
104 __m512 _S23 = _mm512_load_ps(S2p + 32);
105 __m512 _S30 = _mm512_load_ps(S3p - 16);
106 __m512 _S31 = _mm512_load_ps(S3p + 0);
107 __m512 _S32 = _mm512_load_ps(S3p + 16);
108 __m512 _S33 = _mm512_load_ps(S3p + 32);
109 __m512 _rows2 = _mm512_mul_ps(_S20, _a0);
110 __m512 _rows3 = _mm512_mul_ps(_S30, _a0);
111 _rows2 = _mm512_fmadd_ps(_S21, _a1, _rows2);
112 _rows3 = _mm512_fmadd_ps(_S31, _a1, _rows3);
113 _rows2 = _mm512_fmadd_ps(_S22, _a2, _rows2);
114 _rows3 = _mm512_fmadd_ps(_S32, _a2, _rows3);
115 _rows2 = _mm512_fmadd_ps(_S23, _a3, _rows2);
116 _rows3 = _mm512_fmadd_ps(_S33, _a3, _rows3);
117 _mm512_store_ps(rows2p + dx * 16, _rows2);
118 _mm512_store_ps(rows3p + dx * 16, _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] * 16;
144 const float* S1p = S1 + sx;
145 const float* S2p = S2 + sx;
146 const float* S3p = S3 + sx;
147
148 __m512 _a0 = _mm512_set1_ps(alphap[0]);
149 __m512 _a1 = _mm512_set1_ps(alphap[1]);
150 __m512 _a2 = _mm512_set1_ps(alphap[2]);
151 __m512 _a3 = _mm512_set1_ps(alphap[3]);
152
153 __m512 _S10 = _mm512_load_ps(S1p - 16);
154 __m512 _S11 = _mm512_load_ps(S1p + 0);
155 __m512 _S12 = _mm512_load_ps(S1p + 16);
156 __m512 _S13 = _mm512_load_ps(S1p + 32);
157 __m512 _S20 = _mm512_load_ps(S2p - 16);
158 __m512 _S21 = _mm512_load_ps(S2p + 0);
159 __m512 _S22 = _mm512_load_ps(S2p + 16);
160 __m512 _S23 = _mm512_load_ps(S2p + 32);
161 __m512 _S30 = _mm512_load_ps(S3p - 16);
162 __m512 _S31 = _mm512_load_ps(S3p + 0);
163 __m512 _S32 = _mm512_load_ps(S3p + 16);
164 __m512 _S33 = _mm512_load_ps(S3p + 32);
165 __m512 _rows1 = _mm512_mul_ps(_S10, _a0);
166 __m512 _rows2 = _mm512_mul_ps(_S20, _a0);
167 __m512 _rows3 = _mm512_mul_ps(_S30, _a0);
168 _rows1 = _mm512_fmadd_ps(_S11, _a1, _rows1);
169 _rows2 = _mm512_fmadd_ps(_S21, _a1, _rows2);
170 _rows3 = _mm512_fmadd_ps(_S31, _a1, _rows3);
171 _rows1 = _mm512_fmadd_ps(_S12, _a2, _rows1);
172 _rows2 = _mm512_fmadd_ps(_S22, _a2, _rows2);
173 _rows3 = _mm512_fmadd_ps(_S32, _a2, _rows3);
174 _rows1 = _mm512_fmadd_ps(_S13, _a3, _rows1);
175 _rows2 = _mm512_fmadd_ps(_S23, _a3, _rows2);
176 _rows3 = _mm512_fmadd_ps(_S33, _a3, _rows3);
177 _mm512_store_ps(rows1p + dx * 16, _rows1);
178 _mm512_store_ps(rows2p + dx * 16, _rows2);
179 _mm512_store_ps(rows3p + dx * 16, _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] * 16;
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 __m512 _a0 = _mm512_set1_ps(alphap[0]);
206 __m512 _a1 = _mm512_set1_ps(alphap[1]);
207 __m512 _a2 = _mm512_set1_ps(alphap[2]);
208 __m512 _a3 = _mm512_set1_ps(alphap[3]);
209
210 __m512 _S00 = _mm512_load_ps(S0p - 16);
211 __m512 _S01 = _mm512_load_ps(S0p + 0);
212 __m512 _S02 = _mm512_load_ps(S0p + 16);
213 __m512 _S03 = _mm512_load_ps(S0p + 32);
214 __m512 _S10 = _mm512_load_ps(S1p - 16);
215 __m512 _S11 = _mm512_load_ps(S1p + 0);
216 __m512 _S12 = _mm512_load_ps(S1p + 16);
217 __m512 _S13 = _mm512_load_ps(S1p + 32);
218 __m512 _S20 = _mm512_load_ps(S2p - 16);
219 __m512 _S21 = _mm512_load_ps(S2p + 0);
220 __m512 _S22 = _mm512_load_ps(S2p + 16);
221 __m512 _S23 = _mm512_load_ps(S2p + 32);
222 __m512 _S30 = _mm512_load_ps(S3p - 16);
223 __m512 _S31 = _mm512_load_ps(S3p + 0);
224 __m512 _S32 = _mm512_load_ps(S3p + 16);
225 __m512 _S33 = _mm512_load_ps(S3p + 32);
226 __m512 _rows0 = _mm512_mul_ps(_S00, _a0);
227 __m512 _rows1 = _mm512_mul_ps(_S10, _a0);
228 __m512 _rows2 = _mm512_mul_ps(_S20, _a0);
229 __m512 _rows3 = _mm512_mul_ps(_S30, _a0);
230 _rows0 = _mm512_fmadd_ps(_S01, _a1, _rows0);
231 _rows1 = _mm512_fmadd_ps(_S11, _a1, _rows1);
232 _rows2 = _mm512_fmadd_ps(_S21, _a1, _rows2);
233 _rows3 = _mm512_fmadd_ps(_S31, _a1, _rows3);
234 _rows0 = _mm512_fmadd_ps(_S02, _a2, _rows0);
235 _rows1 = _mm512_fmadd_ps(_S12, _a2, _rows1);
236 _rows2 = _mm512_fmadd_ps(_S22, _a2, _rows2);
237 _rows3 = _mm512_fmadd_ps(_S32, _a2, _rows3);
238 _rows0 = _mm512_fmadd_ps(_S03, _a3, _rows0);
239 _rows1 = _mm512_fmadd_ps(_S13, _a3, _rows1);
240 _rows2 = _mm512_fmadd_ps(_S23, _a3, _rows2);
241 _rows3 = _mm512_fmadd_ps(_S33, _a3, _rows3);
242 _mm512_store_ps(rows0p + dx * 16, _rows0);
243 _mm512_store_ps(rows1p + dx * 16, _rows1);
244 _mm512_store_ps(rows2p + dx * 16, _rows2);
245 _mm512_store_ps(rows3p + dx * 16, _rows3);
246
247 alphap += 4;
248 }
249 }
250
251 prev_sy1 = sy;
252
253 // vresize
254 __m512 _b0 = _mm512_set1_ps(beta[0]);
255 __m512 _b1 = _mm512_set1_ps(beta[1]);
256 __m512 _b2 = _mm512_set1_ps(beta[2]);
257 __m512 _b3 = _mm512_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 __m512 _rows0 = _mm512_load_ps(rows0p);
268 __m512 _rows1 = _mm512_load_ps(rows1p);
269 __m512 _rows2 = _mm512_load_ps(rows2p);
270 __m512 _rows3 = _mm512_load_ps(rows3p);
271 __m512 _Dp = _mm512_mul_ps(_rows0, _b0);
272 _Dp = _mm512_fmadd_ps(_rows1, _b1, _Dp);
273 _Dp = _mm512_fmadd_ps(_rows2, _b2, _Dp);
274 _Dp = _mm512_fmadd_ps(_rows3, _b3, _Dp);
275 _mm512_store_ps(Dp, _Dp);
276
277 Dp += 16;
278 rows0p += 16;
279 rows1p += 16;
280 rows2p += 16;
281 rows3p += 16;
282 }
283
284 beta += 4;
285 }
286}