Elffuss BraidHub

kikocis/ncnn public

master · 10.1 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2021 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
15#include "eltwise_mips.h"
16
17#if __mips_msa
18#include <msa.h>
19#endif // __mips_msa
20
21#include "mips_usability.h"
22
23namespace ncnn {
24
25Eltwise_mips::Eltwise_mips()
26{
27#if __mips_msa
28 support_packing = true;
29#endif // __mips_msa
30}
31
32int Eltwise_mips::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
33{
34 const Mat& bottom_blob = bottom_blobs[0];
35 int w = bottom_blob.w;
36 int h = bottom_blob.h;
37 int d = bottom_blob.d;
38 int channels = bottom_blob.c;
39 int elempack = bottom_blob.elempack;
40 int size = w * h * d * elempack;
41
42 Mat& top_blob = top_blobs[0];
43 top_blob.create_like(bottom_blob, opt.blob_allocator);
44 if (top_blob.empty())
45 return -100;
46
47 if (op_type == Operation_PROD)
48 {
49 // first blob
50 const Mat& bottom_blob1 = bottom_blobs[1];
51 #pragma omp parallel for num_threads(opt.num_threads)
52 for (int q = 0; q < channels; q++)
53 {
54 const float* ptr = bottom_blob.channel(q);
55 const float* ptr1 = bottom_blob1.channel(q);
56 float* outptr = top_blob.channel(q);
57
58 int i = 0;
59#if __mips_msa
60 for (; i + 3 < size; i += 4)
61 {
62 v4f32 _p = (v4f32)__msa_ld_w(ptr, 0);
63 v4f32 _p1 = (v4f32)__msa_ld_w(ptr1, 0);
64 _p = __msa_fmul_w(_p, _p1);
65 __msa_st_w((v4i32)_p, outptr, 0);
66
67 ptr += 4;
68 ptr1 += 4;
69 outptr += 4;
70 }
71#endif // __mips_msa
72 for (; i < size; i++)
73 {
74 *outptr = *ptr * *ptr1;
75
76 ptr++;
77 ptr1++;
78 outptr++;
79 }
80 }
81
82 for (size_t b = 2; b < bottom_blobs.size(); b++)
83 {
84 const Mat& bottom_blob1 = bottom_blobs[b];
85 #pragma omp parallel for num_threads(opt.num_threads)
86 for (int q = 0; q < channels; q++)
87 {
88 const float* ptr = bottom_blob1.channel(q);
89 float* outptr = top_blob.channel(q);
90
91 int i = 0;
92#if __mips_msa
93 for (; i + 3 < size; i += 4)
94 {
95 v4f32 _p = (v4f32)__msa_ld_w(outptr, 0);
96 v4f32 _p1 = (v4f32)__msa_ld_w(ptr, 0);
97 _p = __msa_fmul_w(_p, _p1);
98 __msa_st_w((v4i32)_p, outptr, 0);
99
100 ptr += 4;
101 outptr += 4;
102 }
103#endif // __mips_msa
104 for (; i < size; i++)
105 {
106 *outptr *= *ptr;
107
108 ptr++;
109 outptr++;
110 }
111 }
112 }
113 }
114 if (op_type == Operation_SUM)
115 {
116 if (coeffs.w == 0)
117 {
118 // first blob
119 const Mat& bottom_blob1 = bottom_blobs[1];
120 #pragma omp parallel for num_threads(opt.num_threads)
121 for (int q = 0; q < channels; q++)
122 {
123 const float* ptr = bottom_blob.channel(q);
124 const float* ptr1 = bottom_blob1.channel(q);
125 float* outptr = top_blob.channel(q);
126
127 int i = 0;
128#if __mips_msa
129 for (; i + 3 < size; i += 4)
130 {
131 v4f32 _p = (v4f32)__msa_ld_w(ptr, 0);
132 v4f32 _p1 = (v4f32)__msa_ld_w(ptr1, 0);
133 _p = __msa_fadd_w(_p, _p1);
134 __msa_st_w((v4i32)_p, outptr, 0);
135
136 ptr += 4;
137 ptr1 += 4;
138 outptr += 4;
139 }
140#endif // __mips_msa
141 for (; i < size; i++)
142 {
143 *outptr = *ptr + *ptr1;
144
145 ptr++;
146 ptr1++;
147 outptr++;
148 }
149 }
150
151 for (size_t b = 2; b < bottom_blobs.size(); b++)
152 {
153 const Mat& bottom_blob1 = bottom_blobs[b];
154 #pragma omp parallel for num_threads(opt.num_threads)
155 for (int q = 0; q < channels; q++)
156 {
157 const float* ptr = bottom_blob1.channel(q);
158 float* outptr = top_blob.channel(q);
159
160 int i = 0;
161#if __mips_msa
162 for (; i + 3 < size; i += 4)
163 {
164 v4f32 _p = (v4f32)__msa_ld_w(outptr, 0);
165 v4f32 _p1 = (v4f32)__msa_ld_w(ptr, 0);
166 _p = __msa_fadd_w(_p, _p1);
167 __msa_st_w((v4i32)_p, outptr, 0);
168
169 ptr += 4;
170 outptr += 4;
171 }
172#endif // __mips_msa
173 for (; i < size; i++)
174 {
175 *outptr += *ptr;
176
177 ptr++;
178 outptr++;
179 }
180 }
181 }
182 }
183 else
184 {
185 // first blob
186 const Mat& bottom_blob1 = bottom_blobs[1];
187 float coeff0 = coeffs[0];
188 float coeff1 = coeffs[1];
189#if __mips_msa
190 v4f32 _coeff0 = (v4f32)__msa_fill_w_f32(coeff0);
191 v4f32 _coeff1 = (v4f32)__msa_fill_w_f32(coeff1);
192#endif // __mips_msa
193 #pragma omp parallel for num_threads(opt.num_threads)
194 for (int q = 0; q < channels; q++)
195 {
196 const float* ptr = bottom_blob.channel(q);
197 const float* ptr1 = bottom_blob1.channel(q);
198 float* outptr = top_blob.channel(q);
199
200 int i = 0;
201#if __mips_msa
202 for (; i + 3 < size; i += 4)
203 {
204 v4f32 _p = (v4f32)__msa_ld_w(ptr, 0);
205 v4f32 _p1 = (v4f32)__msa_ld_w(ptr1, 0);
206 _p = __msa_fmul_w(_p, _coeff0);
207 _p = __msa_fmadd_w(_p, _p1, _coeff1);
208 __msa_st_w((v4i32)_p, outptr, 0);
209
210 ptr += 4;
211 ptr1 += 4;
212 outptr += 4;
213 }
214#endif // __mips_msa
215 for (; i < size; i++)
216 {
217 *outptr = *ptr * coeff0 + *ptr1 * coeff1;
218
219 ptr++;
220 ptr1++;
221 outptr++;
222 }
223 }
224
225 for (size_t b = 2; b < bottom_blobs.size(); b++)
226 {
227 const Mat& bottom_blob1 = bottom_blobs[b];
228 float coeff = coeffs[b];
229#if __mips_msa
230 v4f32 _coeff = (v4f32)__msa_fill_w_f32(coeff);
231#endif // __mips_msa
232 #pragma omp parallel for num_threads(opt.num_threads)
233 for (int q = 0; q < channels; q++)
234 {
235 const float* ptr = bottom_blob1.channel(q);
236 float* outptr = top_blob.channel(q);
237
238 int i = 0;
239#if __mips_msa
240 for (; i + 3 < size; i += 4)
241 {
242 v4f32 _p = (v4f32)__msa_ld_w(outptr, 0);
243 v4f32 _p1 = (v4f32)__msa_ld_w(ptr, 0);
244 _p = __msa_fmadd_w(_p, _p1, _coeff);
245 __msa_st_w((v4i32)_p, outptr, 0);
246
247 ptr += 4;
248 outptr += 4;
249 }
250#endif // __mips_msa
251 for (; i < size; i++)
252 {
253 *outptr += *ptr * coeff;
254
255 ptr++;
256 outptr++;
257 }
258 }
259 }
260 }
261 }
262 if (op_type == Operation_MAX)
263 {
264 // first blob
265 const Mat& bottom_blob1 = bottom_blobs[1];
266 #pragma omp parallel for num_threads(opt.num_threads)
267 for (int q = 0; q < channels; q++)
268 {
269 const float* ptr = bottom_blob.channel(q);
270 const float* ptr1 = bottom_blob1.channel(q);
271 float* outptr = top_blob.channel(q);
272
273 int i = 0;
274#if __mips_msa
275 for (; i + 3 < size; i += 4)
276 {
277 v4f32 _p = (v4f32)__msa_ld_w(ptr, 0);
278 v4f32 _p1 = (v4f32)__msa_ld_w(ptr1, 0);
279 _p = __msa_fmax_w(_p, _p1);
280 __msa_st_w((v4i32)_p, outptr, 0);
281
282 ptr += 4;
283 ptr1 += 4;
284 outptr += 4;
285 }
286#endif // __mips_msa
287 for (; i < size; i++)
288 {
289 *outptr = std::max(*ptr, *ptr1);
290
291 ptr++;
292 ptr1++;
293 outptr++;
294 }
295 }
296
297 for (size_t b = 2; b < bottom_blobs.size(); b++)
298 {
299 const Mat& bottom_blob1 = bottom_blobs[b];
300 #pragma omp parallel for num_threads(opt.num_threads)
301 for (int q = 0; q < channels; q++)
302 {
303 const float* ptr = bottom_blob1.channel(q);
304 float* outptr = top_blob.channel(q);
305
306 int i = 0;
307#if __mips_msa
308 for (; i + 3 < size; i += 4)
309 {
310 v4f32 _p = (v4f32)__msa_ld_w(outptr, 0);
311 v4f32 _p1 = (v4f32)__msa_ld_w(ptr, 0);
312 _p = __msa_fmax_w(_p, _p1);
313 __msa_st_w((v4i32)_p, outptr, 0);
314
315 ptr += 4;
316 outptr += 4;
317 }
318#endif // __mips_msa
319 for (; i < size; i++)
320 {
321 *outptr = std::max(*ptr, *outptr);
322
323 ptr++;
324 outptr++;
325 }
326 }
327 }
328 }
329
330 return 0;
331}
332
333} // namespace ncnn