Elffuss BraidHub

kikocis/ncnn public

master · 11.3 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2020 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_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 v4f32 _a0 = __msa_fill_w_f32(alphap[0]);
58 v4f32 _a1 = __msa_fill_w_f32(alphap[1]);
59 v4f32 _a2 = __msa_fill_w_f32(alphap[2]);
60 v4f32 _a3 = __msa_fill_w_f32(alphap[3]);
61
62 v4f32 _S30 = (v4f32)__msa_ld_w(S3p - 4, 0);
63 v4f32 _S31 = (v4f32)__msa_ld_w(S3p + 0, 0);
64 v4f32 _S32 = (v4f32)__msa_ld_w(S3p + 4, 0);
65 v4f32 _S33 = (v4f32)__msa_ld_w(S3p + 8, 0);
66 v4f32 _rows3 = __msa_fmul_w(_S30, _a0);
67 _rows3 = __msa_fmadd_w(_rows3, _S31, _a1);
68 _rows3 = __msa_fmadd_w(_rows3, _S32, _a2);
69 _rows3 = __msa_fmadd_w(_rows3, _S33, _a3);
70 __msa_st_w((v4i32)_rows3, rows3p + dx * 4, 0);
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 v4f32 _a0 = __msa_fill_w_f32(alphap[0]);
97 v4f32 _a1 = __msa_fill_w_f32(alphap[1]);
98 v4f32 _a2 = __msa_fill_w_f32(alphap[2]);
99 v4f32 _a3 = __msa_fill_w_f32(alphap[3]);
100
101 v4f32 _S20 = (v4f32)__msa_ld_w(S2p - 4, 0);
102 v4f32 _S21 = (v4f32)__msa_ld_w(S2p + 0, 0);
103 v4f32 _S22 = (v4f32)__msa_ld_w(S2p + 4, 0);
104 v4f32 _S23 = (v4f32)__msa_ld_w(S2p + 8, 0);
105 v4f32 _S30 = (v4f32)__msa_ld_w(S3p - 4, 0);
106 v4f32 _S31 = (v4f32)__msa_ld_w(S3p + 0, 0);
107 v4f32 _S32 = (v4f32)__msa_ld_w(S3p + 4, 0);
108 v4f32 _S33 = (v4f32)__msa_ld_w(S3p + 8, 0);
109 v4f32 _rows2 = __msa_fmul_w(_S20, _a0);
110 v4f32 _rows3 = __msa_fmul_w(_S30, _a0);
111 _rows2 = __msa_fmadd_w(_rows2, _S21, _a1);
112 _rows3 = __msa_fmadd_w(_rows3, _S31, _a1);
113 _rows2 = __msa_fmadd_w(_rows2, _S22, _a2);
114 _rows3 = __msa_fmadd_w(_rows3, _S32, _a2);
115 _rows2 = __msa_fmadd_w(_rows2, _S23, _a3);
116 _rows3 = __msa_fmadd_w(_rows3, _S33, _a3);
117 __msa_st_w((v4i32)_rows2, rows2p + dx * 4, 0);
118 __msa_st_w((v4i32)_rows3, rows3p + dx * 4, 0);
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 v4f32 _a0 = __msa_fill_w_f32(alphap[0]);
149 v4f32 _a1 = __msa_fill_w_f32(alphap[1]);
150 v4f32 _a2 = __msa_fill_w_f32(alphap[2]);
151 v4f32 _a3 = __msa_fill_w_f32(alphap[3]);
152
153 v4f32 _S10 = (v4f32)__msa_ld_w(S1p - 4, 0);
154 v4f32 _S11 = (v4f32)__msa_ld_w(S1p + 0, 0);
155 v4f32 _S12 = (v4f32)__msa_ld_w(S1p + 4, 0);
156 v4f32 _S13 = (v4f32)__msa_ld_w(S1p + 8, 0);
157 v4f32 _S20 = (v4f32)__msa_ld_w(S2p - 4, 0);
158 v4f32 _S21 = (v4f32)__msa_ld_w(S2p + 0, 0);
159 v4f32 _S22 = (v4f32)__msa_ld_w(S2p + 4, 0);
160 v4f32 _S23 = (v4f32)__msa_ld_w(S2p + 8, 0);
161 v4f32 _S30 = (v4f32)__msa_ld_w(S3p - 4, 0);
162 v4f32 _S31 = (v4f32)__msa_ld_w(S3p + 0, 0);
163 v4f32 _S32 = (v4f32)__msa_ld_w(S3p + 4, 0);
164 v4f32 _S33 = (v4f32)__msa_ld_w(S3p + 8, 0);
165 v4f32 _rows1 = __msa_fmul_w(_S10, _a0);
166 v4f32 _rows2 = __msa_fmul_w(_S20, _a0);
167 v4f32 _rows3 = __msa_fmul_w(_S30, _a0);
168 _rows1 = __msa_fmadd_w(_rows1, _S11, _a1);
169 _rows2 = __msa_fmadd_w(_rows2, _S21, _a1);
170 _rows3 = __msa_fmadd_w(_rows3, _S31, _a1);
171 _rows1 = __msa_fmadd_w(_rows1, _S12, _a2);
172 _rows2 = __msa_fmadd_w(_rows2, _S22, _a2);
173 _rows3 = __msa_fmadd_w(_rows3, _S32, _a2);
174 _rows1 = __msa_fmadd_w(_rows1, _S13, _a3);
175 _rows2 = __msa_fmadd_w(_rows2, _S23, _a3);
176 _rows3 = __msa_fmadd_w(_rows3, _S33, _a3);
177 __msa_st_w((v4i32)_rows1, rows1p + dx * 4, 0);
178 __msa_st_w((v4i32)_rows2, rows2p + dx * 4, 0);
179 __msa_st_w((v4i32)_rows3, rows3p + dx * 4, 0);
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 v4f32 _a0 = __msa_fill_w_f32(alphap[0]);
206 v4f32 _a1 = __msa_fill_w_f32(alphap[1]);
207 v4f32 _a2 = __msa_fill_w_f32(alphap[2]);
208 v4f32 _a3 = __msa_fill_w_f32(alphap[3]);
209
210 v4f32 _S00 = (v4f32)__msa_ld_w(S0p - 4, 0);
211 v4f32 _S01 = (v4f32)__msa_ld_w(S0p + 0, 0);
212 v4f32 _S02 = (v4f32)__msa_ld_w(S0p + 4, 0);
213 v4f32 _S03 = (v4f32)__msa_ld_w(S0p + 8, 0);
214 v4f32 _S10 = (v4f32)__msa_ld_w(S1p - 4, 0);
215 v4f32 _S11 = (v4f32)__msa_ld_w(S1p + 0, 0);
216 v4f32 _S12 = (v4f32)__msa_ld_w(S1p + 4, 0);
217 v4f32 _S13 = (v4f32)__msa_ld_w(S1p + 8, 0);
218 v4f32 _S20 = (v4f32)__msa_ld_w(S2p - 4, 0);
219 v4f32 _S21 = (v4f32)__msa_ld_w(S2p + 0, 0);
220 v4f32 _S22 = (v4f32)__msa_ld_w(S2p + 4, 0);
221 v4f32 _S23 = (v4f32)__msa_ld_w(S2p + 8, 0);
222 v4f32 _S30 = (v4f32)__msa_ld_w(S3p - 4, 0);
223 v4f32 _S31 = (v4f32)__msa_ld_w(S3p + 0, 0);
224 v4f32 _S32 = (v4f32)__msa_ld_w(S3p + 4, 0);
225 v4f32 _S33 = (v4f32)__msa_ld_w(S3p + 8, 0);
226 v4f32 _rows0 = __msa_fmul_w(_S00, _a0);
227 v4f32 _rows1 = __msa_fmul_w(_S10, _a0);
228 v4f32 _rows2 = __msa_fmul_w(_S20, _a0);
229 v4f32 _rows3 = __msa_fmul_w(_S30, _a0);
230 _rows0 = __msa_fmadd_w(_rows0, _S01, _a1);
231 _rows1 = __msa_fmadd_w(_rows1, _S11, _a1);
232 _rows2 = __msa_fmadd_w(_rows2, _S21, _a1);
233 _rows3 = __msa_fmadd_w(_rows3, _S31, _a1);
234 _rows0 = __msa_fmadd_w(_rows0, _S02, _a2);
235 _rows1 = __msa_fmadd_w(_rows1, _S12, _a2);
236 _rows2 = __msa_fmadd_w(_rows2, _S22, _a2);
237 _rows3 = __msa_fmadd_w(_rows3, _S32, _a2);
238 _rows0 = __msa_fmadd_w(_rows0, _S03, _a3);
239 _rows1 = __msa_fmadd_w(_rows1, _S13, _a3);
240 _rows2 = __msa_fmadd_w(_rows2, _S23, _a3);
241 _rows3 = __msa_fmadd_w(_rows3, _S33, _a3);
242 __msa_st_w((v4i32)_rows0, rows0p + dx * 4, 0);
243 __msa_st_w((v4i32)_rows1, rows1p + dx * 4, 0);
244 __msa_st_w((v4i32)_rows2, rows2p + dx * 4, 0);
245 __msa_st_w((v4i32)_rows3, rows3p + dx * 4, 0);
246
247 alphap += 4;
248 }
249 }
250
251 prev_sy1 = sy;
252
253 // vresize
254 v4f32 _b0 = __msa_fill_w_f32(beta[0]);
255 v4f32 _b1 = __msa_fill_w_f32(beta[1]);
256 v4f32 _b2 = __msa_fill_w_f32(beta[2]);
257 v4f32 _b3 = __msa_fill_w_f32(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 v4f32 _rows0 = (v4f32)__msa_ld_w(rows0p, 0);
268 v4f32 _rows1 = (v4f32)__msa_ld_w(rows1p, 0);
269 v4f32 _rows2 = (v4f32)__msa_ld_w(rows2p, 0);
270 v4f32 _rows3 = (v4f32)__msa_ld_w(rows3p, 0);
271 v4f32 _Dp = __msa_fmul_w(_rows0, _b0);
272 _Dp = __msa_fmadd_w(_Dp, _rows1, _b1);
273 _Dp = __msa_fmadd_w(_Dp, _rows2, _b2);
274 _Dp = __msa_fmadd_w(_Dp, _rows3, _b3);
275 __msa_st_w((v4i32)_Dp, Dp, 0);
276
277 Dp += 4;
278 rows0p += 4;
279 rows1p += 4;
280 rows2p += 4;
281 rows3p += 4;
282 }
283
284 beta += 4;
285 }
286}