Elffuss BraidHub

kikocis/ncnn public

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