Elffuss BraidHub

kikocis/ncnn public

master · 7.1 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 linear_coeffs_fp16sa(int w, int outw, int* xofs, __fp16* alpha, int align_corner)
16{
17 double scale = (double)w / outw;
18 if (align_corner)
19 {
20 scale = (double)(w - 1) / (outw - 1);
21 }
22
23 for (int dx = 0; dx < outw; dx++)
24 {
25 float fx = (float)((dx + 0.5) * scale - 0.5);
26 if (align_corner)
27 {
28 fx = static_cast<float>(dx * scale);
29 }
30
31 int sx = floor(fx);
32 fx -= sx;
33
34 if (sx < 0)
35 {
36 sx = 0;
37 fx = 0.f;
38 }
39 if (sx >= w - 1)
40 {
41 sx = w - 2;
42 fx = 1.f;
43 }
44
45 xofs[dx] = sx;
46
47 alpha[dx * 2] = (__fp16)(1.f - fx);
48 alpha[dx * 2 + 1] = (__fp16)fx;
49 }
50}
51
52static void resize_bilinear_image_fp16s(const Mat& src, Mat& dst, float* alpha, int* xofs, float* beta, int* yofs)
53{
54 int w = dst.w;
55 int h = dst.h;
56
57 // loop body
58 Mat rowsbuf0(w);
59 Mat rowsbuf1(w);
60 float* rows0 = rowsbuf0;
61 float* rows1 = rowsbuf1;
62
63 int prev_sy1 = -2;
64
65 for (int dy = 0; dy < h; dy++)
66 {
67 int sy = yofs[dy];
68
69 if (sy == prev_sy1)
70 {
71 // reuse all rows
72 }
73 else if (sy == prev_sy1 + 1)
74 {
75 // hresize one row
76 float* rows0_old = rows0;
77 rows0 = rows1;
78 rows1 = rows0_old;
79 const __fp16* S1 = src.row<const __fp16>(sy + 1);
80
81 const float* alphap = alpha;
82 float* rows1p = rows1;
83 int dx = 0;
84 for (; dx < w; dx++)
85 {
86 int sx = xofs[dx];
87 const __fp16* S1p = S1 + sx;
88
89 float a0 = alphap[0];
90 float a1 = alphap[1];
91 rows1p[dx] = (float)S1p[0] * a0 + (float)S1p[1] * a1;
92
93 alphap += 2;
94 }
95 }
96 else
97 {
98 // hresize two rows
99 const __fp16* S0 = src.row<const __fp16>(sy);
100 const __fp16* S1 = src.row<const __fp16>(sy + 1);
101
102 const float* alphap = alpha;
103 float* rows0p = rows0;
104 float* rows1p = rows1;
105 int dx = 0;
106 for (; dx < w; dx++)
107 {
108 int sx = xofs[dx];
109 const __fp16* S0p = S0 + sx;
110 const __fp16* S1p = S1 + sx;
111
112 float a0 = alphap[0];
113 float a1 = alphap[1];
114 rows0p[dx] = (float)S0p[0] * a0 + (float)S0p[1] * a1;
115 rows1p[dx] = (float)S1p[0] * a0 + (float)S1p[1] * a1;
116
117 alphap += 2;
118 }
119 }
120
121 prev_sy1 = sy;
122
123 // vresize
124 float b0 = beta[0];
125 float b1 = beta[1];
126
127 float* rows0p = rows0;
128 float* rows1p = rows1;
129 __fp16* Dp = dst.row<__fp16>(dy);
130
131 int nn = w >> 3;
132 int remain = w - (nn << 3);
133
134 float32x4_t _b0 = vdupq_n_f32(b0);
135 float32x4_t _b1 = vdupq_n_f32(b1);
136 for (; nn > 0; nn--)
137 {
138 float32x4_t _rows0 = vld1q_f32(rows0p);
139 float32x4_t _rows1 = vld1q_f32(rows1p);
140
141 float32x4_t _Dp = vmulq_f32(_rows0, _b0);
142 _Dp = vfmaq_f32(_Dp, _rows1, _b1);
143
144 vst1_f16(Dp, vcvt_f16_f32(_Dp));
145
146 float32x4_t _rows0n = vld1q_f32(rows0p + 4);
147 float32x4_t _rows1n = vld1q_f32(rows1p + 4);
148
149 float32x4_t _Dn = vmulq_f32(_rows0n, _b0);
150 _Dn = vfmaq_f32(_Dn, _rows1n, _b1);
151
152 vst1_f16(Dp + 4, vcvt_f16_f32(_Dn));
153
154 Dp += 8;
155 rows0p += 8;
156 rows1p += 8;
157 }
158 for (; remain; --remain)
159 {
160 // D[x] = rows0[x]*b0 + rows1[x]*b1;
161 *Dp++ = (__fp16)(*rows0p++ * b0 + *rows1p++ * b1);
162 }
163
164 beta += 2;
165 }
166}
167
168static void resize_bilinear_image_fp16sa(const Mat& src, Mat& dst, __fp16* alpha, int* xofs, __fp16* beta, int* yofs)
169{
170 int w = dst.w;
171 int h = dst.h;
172
173 // loop body
174 Mat rowsbuf0(w, (size_t)2u);
175 Mat rowsbuf1(w, (size_t)2u);
176 __fp16* rows0 = rowsbuf0;
177 __fp16* rows1 = rowsbuf1;
178
179 int prev_sy1 = -2;
180
181 for (int dy = 0; dy < h; dy++)
182 {
183 int sy = yofs[dy];
184
185 if (sy == prev_sy1)
186 {
187 // reuse all rows
188 }
189 else if (sy == prev_sy1 + 1)
190 {
191 // hresize one row
192 __fp16* rows0_old = rows0;
193 rows0 = rows1;
194 rows1 = rows0_old;
195 const __fp16* S1 = src.row<const __fp16>(sy + 1);
196
197 const __fp16* alphap = alpha;
198 __fp16* rows1p = rows1;
199 int dx = 0;
200 for (; dx < w; dx++)
201 {
202 int sx = xofs[dx];
203 const __fp16* S1p = S1 + sx;
204
205 __fp16 a0 = alphap[0];
206 __fp16 a1 = alphap[1];
207 rows1p[dx] = S1p[0] * a0 + S1p[1] * a1;
208
209 alphap += 2;
210 }
211 }
212 else
213 {
214 // hresize two rows
215 const __fp16* S0 = src.row<const __fp16>(sy);
216 const __fp16* S1 = src.row<const __fp16>(sy + 1);
217
218 const __fp16* alphap = alpha;
219 __fp16* rows0p = rows0;
220 __fp16* rows1p = rows1;
221 int dx = 0;
222 for (; dx < w; dx++)
223 {
224 int sx = xofs[dx];
225 const __fp16* S0p = S0 + sx;
226 const __fp16* S1p = S1 + sx;
227
228 __fp16 a0 = alphap[0];
229 __fp16 a1 = alphap[1];
230 rows0p[dx] = S0p[0] * a0 + S0p[1] * a1;
231 rows1p[dx] = S1p[0] * a0 + S1p[1] * a1;
232
233 alphap += 2;
234 }
235 }
236
237 prev_sy1 = sy;
238
239 // vresize
240 __fp16 b0 = beta[0];
241 __fp16 b1 = beta[1];
242
243 __fp16* rows0p = rows0;
244 __fp16* rows1p = rows1;
245 __fp16* Dp = dst.row<__fp16>(dy);
246
247 int nn = w >> 3;
248 int remain = w - (nn << 3);
249
250 float16x8_t _b0 = vdupq_n_f16(b0);
251 float16x8_t _b1 = vdupq_n_f16(b1);
252 for (; nn > 0; nn--)
253 {
254 float16x8_t _rows0 = vld1q_f16(rows0p);
255 float16x8_t _rows1 = vld1q_f16(rows1p);
256
257 float16x8_t _Dp = vmulq_f16(_rows0, _b0);
258 _Dp = vfmaq_f16(_Dp, _rows1, _b1);
259
260 vst1q_f16(Dp, _Dp);
261
262 Dp += 8;
263 rows0p += 8;
264 rows1p += 8;
265 }
266 for (; remain; --remain)
267 {
268 // D[x] = rows0[x]*b0 + rows1[x]*b1;
269 *Dp++ = (__fp16)(*rows0p++ * b0 + *rows1p++ * b1);
270 }
271
272 beta += 2;
273 }
274}