Elffuss BraidHub

kikocis/ncnn public

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