Elffuss BraidHub

kikocis/ncnn public

master · 6.4 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 n = w;
132 while (n > 0)
133 {
134 size_t vl = vsetvl_e16m4(n);
135
136 vfloat32m8_t _rows0 = vle32_v_f32m8(rows0p, vl);
137 vfloat32m8_t _rows1 = vle32_v_f32m8(rows1p, vl);
138
139 vfloat32m8_t _Dp = vfmacc_vf_f32m8(vfmul_vf_f32m8(_rows0, b0, vl), b1, _rows1, vl);
140
141 vse16_v_f16m4(Dp, vfncvt_f_f_w_f16m4(_Dp, vl), vl);
142
143 Dp += vl;
144 rows0p += vl;
145 rows1p += vl;
146 n -= vl;
147 }
148
149 beta += 2;
150 }
151}
152
153static void resize_bilinear_image_fp16sa(const Mat& src, Mat& dst, __fp16* alpha, int* xofs, __fp16* beta, int* yofs)
154{
155 int w = dst.w;
156 int h = dst.h;
157
158 // loop body
159 Mat rowsbuf0(w, (size_t)2u);
160 Mat rowsbuf1(w, (size_t)2u);
161 __fp16* rows0 = rowsbuf0;
162 __fp16* rows1 = rowsbuf1;
163
164 int prev_sy1 = -2;
165
166 for (int dy = 0; dy < h; dy++)
167 {
168 int sy = yofs[dy];
169
170 if (sy == prev_sy1)
171 {
172 // reuse all rows
173 }
174 else if (sy == prev_sy1 + 1)
175 {
176 // hresize one row
177 __fp16* rows0_old = rows0;
178 rows0 = rows1;
179 rows1 = rows0_old;
180 const __fp16* S1 = src.row<const __fp16>(sy + 1);
181
182 const __fp16* alphap = alpha;
183 __fp16* rows1p = rows1;
184 int dx = 0;
185 for (; dx < w; dx++)
186 {
187 int sx = xofs[dx];
188 const __fp16* S1p = S1 + sx;
189
190 __fp16 a0 = alphap[0];
191 __fp16 a1 = alphap[1];
192 rows1p[dx] = S1p[0] * a0 + S1p[1] * a1;
193
194 alphap += 2;
195 }
196 }
197 else
198 {
199 // hresize two rows
200 const __fp16* S0 = src.row<const __fp16>(sy);
201 const __fp16* S1 = src.row<const __fp16>(sy + 1);
202
203 const __fp16* alphap = alpha;
204 __fp16* rows0p = rows0;
205 __fp16* rows1p = rows1;
206 int dx = 0;
207 for (; dx < w; dx++)
208 {
209 int sx = xofs[dx];
210 const __fp16* S0p = S0 + sx;
211 const __fp16* S1p = S1 + sx;
212
213 __fp16 a0 = alphap[0];
214 __fp16 a1 = alphap[1];
215 rows0p[dx] = S0p[0] * a0 + S0p[1] * a1;
216 rows1p[dx] = S1p[0] * a0 + S1p[1] * a1;
217
218 alphap += 2;
219 }
220 }
221
222 prev_sy1 = sy;
223
224 // vresize
225 __fp16 b0 = beta[0];
226 __fp16 b1 = beta[1];
227
228 __fp16* rows0p = rows0;
229 __fp16* rows1p = rows1;
230 __fp16* Dp = dst.row<__fp16>(dy);
231
232 int n = w;
233 while (n > 0)
234 {
235 size_t vl = vsetvl_e16m8(n);
236
237 vfloat16m8_t _rows0 = vle16_v_f16m8(rows0p, vl);
238 vfloat16m8_t _rows1 = vle16_v_f16m8(rows1p, vl);
239
240 vfloat16m8_t _Dp = vfmacc_vf_f16m8(vfmul_vf_f16m8(_rows0, b0, vl), b1, _rows1, vl);
241
242 vse16_v_f16m8(Dp, _Dp, vl);
243
244 Dp += vl;
245 rows0p += vl;
246 rows1p += vl;
247 n -= vl;
248 }
249
250 beta += 2;
251 }
252}