Elffuss BraidHub

kikocis/ncnn public

master · 6.9 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_bilinear_image_pack4_fp16s(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 float* rows0 = rowsbuf0;
24 float* rows1 = rowsbuf1;
25
26 int prev_sy1 = -2;
27
28 for (int dy = 0; dy < h; dy++)
29 {
30 int sy = yofs[dy];
31
32 if (sy == prev_sy1)
33 {
34 // reuse all rows
35 }
36 else if (sy == prev_sy1 + 1)
37 {
38 // hresize one row
39 float* rows0_old = rows0;
40 rows0 = rows1;
41 rows1 = rows0_old;
42 const __fp16* S1 = src.row<const __fp16>(sy + 1);
43
44 const float* alphap = alpha;
45 float* rows1p = rows1;
46 int dx = 0;
47 for (; dx < w; dx++)
48 {
49 int sx = xofs[dx] * 4;
50 const __fp16* S1p = S1 + sx;
51
52 float32x2_t _a01 = vld1_f32(alphap);
53
54 float32x4_t _S10 = vcvt_f32_f16(vld1_f16(S1p));
55 float32x4_t _S11 = vcvt_f32_f16(vld1_f16(S1p + 4));
56 float32x4_t _rows1 = vmulq_lane_f32(_S10, _a01, 0);
57 _rows1 = vmlaq_lane_f32(_rows1, _S11, _a01, 1);
58 vst1q_f32(rows1p + dx * 4, _rows1);
59
60 alphap += 2;
61 }
62 }
63 else
64 {
65 // hresize two rows
66 const __fp16* S0 = src.row<const __fp16>(sy);
67 const __fp16* S1 = src.row<const __fp16>(sy + 1);
68
69 const float* alphap = alpha;
70 float* rows0p = rows0;
71 float* rows1p = rows1;
72 int dx = 0;
73 for (; dx < w; dx++)
74 {
75 int sx = xofs[dx] * 4;
76 const __fp16* S0p = S0 + sx;
77 const __fp16* S1p = S1 + sx;
78
79 float32x2_t _a01 = vld1_f32(alphap);
80
81 float32x4_t _S00 = vcvt_f32_f16(vld1_f16(S0p));
82 float32x4_t _S01 = vcvt_f32_f16(vld1_f16(S0p + 4));
83 float32x4_t _S10 = vcvt_f32_f16(vld1_f16(S1p));
84 float32x4_t _S11 = vcvt_f32_f16(vld1_f16(S1p + 4));
85 float32x4_t _rows0 = vmulq_lane_f32(_S00, _a01, 0);
86 float32x4_t _rows1 = vmulq_lane_f32(_S10, _a01, 0);
87 _rows0 = vmlaq_lane_f32(_rows0, _S01, _a01, 1);
88 _rows1 = vmlaq_lane_f32(_rows1, _S11, _a01, 1);
89 vst1q_f32(rows0p + dx * 4, _rows0);
90 vst1q_f32(rows1p + dx * 4, _rows1);
91
92 alphap += 2;
93 }
94 }
95
96 prev_sy1 = sy;
97
98 // vresize
99 float32x2_t _b01 = vld1_f32(beta);
100
101 float* rows0p = rows0;
102 float* rows1p = rows1;
103 __fp16* Dp = dst.row<__fp16>(dy);
104
105 for (int dx = 0; dx < w; dx++)
106 {
107 float32x4_t _rows0 = vld1q_f32(rows0p);
108 float32x4_t _rows1 = vld1q_f32(rows1p);
109 float32x4_t _Dp = vmulq_lane_f32(_rows0, _b01, 0);
110 _Dp = vmlaq_lane_f32(_Dp, _rows1, _b01, 1);
111 vst1_f16(Dp, vcvt_f16_f32(_Dp));
112
113 Dp += 4;
114 rows0p += 4;
115 rows1p += 4;
116 }
117
118 beta += 2;
119 }
120}
121
122static void resize_bilinear_image_pack4_fp16sa(const Mat& src, Mat& dst, __fp16* alpha, int* xofs, __fp16* beta, int* yofs)
123{
124 int w = dst.w;
125 int h = dst.h;
126
127 // loop body
128 Mat rowsbuf0(w, (size_t)4 * 2u, 4);
129 Mat rowsbuf1(w, (size_t)4 * 2u, 4);
130 __fp16* rows0 = rowsbuf0;
131 __fp16* rows1 = rowsbuf1;
132
133 int prev_sy1 = -2;
134
135 for (int dy = 0; dy < h; dy++)
136 {
137 int sy = yofs[dy];
138
139 if (sy == prev_sy1)
140 {
141 // reuse all rows
142 }
143 else if (sy == prev_sy1 + 1)
144 {
145 // hresize one row
146 __fp16* rows0_old = rows0;
147 rows0 = rows1;
148 rows1 = rows0_old;
149 const __fp16* S1 = src.row<const __fp16>(sy + 1);
150
151 const __fp16* alphap = alpha;
152 __fp16* rows1p = rows1;
153 int dx = 0;
154 for (; dx < w; dx++)
155 {
156 int sx = xofs[dx] * 4;
157 const __fp16* S1p = S1 + sx;
158
159 float16x4_t _a01 = vld1_f16(alphap);
160
161 float16x4_t _S10 = vld1_f16(S1p);
162 float16x4_t _S11 = vld1_f16(S1p + 4);
163 float16x4_t _rows1 = vmul_lane_f16(_S10, _a01, 0);
164 _rows1 = vfma_lane_f16(_rows1, _S11, _a01, 1);
165 vst1_f16(rows1p + dx * 4, _rows1);
166
167 alphap += 2;
168 }
169 }
170 else
171 {
172 // hresize two rows
173 const __fp16* S0 = src.row<const __fp16>(sy);
174 const __fp16* S1 = src.row<const __fp16>(sy + 1);
175
176 const __fp16* alphap = alpha;
177 __fp16* rows0p = rows0;
178 __fp16* rows1p = rows1;
179 int dx = 0;
180 for (; dx < w; dx++)
181 {
182 int sx = xofs[dx] * 4;
183 const __fp16* S0p = S0 + sx;
184 const __fp16* S1p = S1 + sx;
185
186 float16x4_t _a01 = vld1_f16(alphap);
187
188 float16x4_t _S00 = vld1_f16(S0p);
189 float16x4_t _S01 = vld1_f16(S0p + 4);
190 float16x4_t _S10 = vld1_f16(S1p);
191 float16x4_t _S11 = vld1_f16(S1p + 4);
192 float16x4_t _rows0 = vmul_lane_f16(_S00, _a01, 0);
193 float16x4_t _rows1 = vmul_lane_f16(_S10, _a01, 0);
194 _rows0 = vfma_lane_f16(_rows0, _S01, _a01, 1);
195 _rows1 = vfma_lane_f16(_rows1, _S11, _a01, 1);
196 vst1_f16(rows0p + dx * 4, _rows0);
197 vst1_f16(rows1p + dx * 4, _rows1);
198
199 alphap += 2;
200 }
201 }
202
203 prev_sy1 = sy;
204
205 // vresize
206 float16x4_t _b01 = vld1_f16(beta);
207
208 __fp16* rows0p = rows0;
209 __fp16* rows1p = rows1;
210 __fp16* Dp = dst.row<__fp16>(dy);
211
212 for (int dx = 0; dx < w; dx++)
213 {
214 float16x4_t _rows0 = vld1_f16(rows0p);
215 float16x4_t _rows1 = vld1_f16(rows1p);
216 float16x4_t _Dp = vmul_lane_f16(_rows0, _b01, 0);
217 _Dp = vfma_lane_f16(_Dp, _rows1, _b01, 1);
218 vst1_f16(Dp, _Dp);
219
220 Dp += 4;
221 rows0p += 4;
222 rows1p += 4;
223 }
224
225 beta += 2;
226 }
227}