Elffuss BraidHub

kikocis/ncnn public

master · 10.7 KiBRawPermalink
1// yala is pleased to support the open source community by making ncnn available.
2//
3//
4// Copyright (C) 2022 yala <zhaojunchao@loongson.cn>;<junchao82@qq.com>. All rights reserved.
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 "unaryop_loongarch.h"
16
17// #include <fenv.h>
18#include <float.h>
19
20#if __loongarch_sx
21#include <lsxintrin.h>
22#include "lsx_mathfun.h"
23#endif // __loongarch_sx
24
25namespace ncnn {
26
27UnaryOp_loongarch::UnaryOp_loongarch()
28{
29#if __loongarch_sx
30 support_packing = true;
31#endif // __loongarch_sx
32}
33
34template<typename Op>
35static int unary_op_inplace(Mat& a, const Option& opt)
36{
37 Op op;
38
39 int w = a.w;
40 int h = a.h;
41 int d = a.d;
42 int channels = a.c;
43 int elempack = a.elempack;
44 int size = w * h * d * elempack;
45
46 #pragma omp parallel for num_threads(opt.num_threads)
47 for (int q = 0; q < channels; q++)
48 {
49 float* ptr = a.channel(q);
50
51 int i = 0;
52#if __loongarch_sx
53 for (; i + 3 < size; i += 4)
54 {
55 __builtin_prefetch(ptr + 16);
56 __m128 _p = (__m128)__lsx_vld(ptr, 0);
57 _p = op.func_pack4(_p);
58 __lsx_vst(_p, ptr, 0);
59 ptr += 4;
60 }
61#endif // __loongarch_sx
62 for (; i < size; i++)
63 {
64 *ptr = op.func(*ptr);
65 ptr++;
66 }
67 }
68
69 return 0;
70}
71
72namespace UnaryOp_loongarch_functor {
73
74struct unary_op_abs
75{
76 float func(const float& x) const
77 {
78 return (float)fabs(x);
79 }
80#if __loongarch_sx
81 __m128 func_pack4(const __m128& x) const
82 {
83 return (__m128)__lsx_vbitclri_w((__m128i)x, 31);
84 }
85#endif // __loongarch_sx
86};
87
88struct unary_op_neg
89{
90 float func(const float& x) const
91 {
92 return -x;
93 }
94#if __loongarch_sx
95 __m128 func_pack4(const __m128& x) const
96 {
97 return (__m128)__lsx_vbitrevi_w((__m128i)x, 31);
98 }
99#endif // __loongarch_sx
100};
101
102struct unary_op_floor
103{
104 float func(const float& x) const
105 {
106 return (float)floor(x);
107 }
108#if __loongarch_sx
109 __m128 func_pack4(const __m128& x) const
110 {
111 return (__m128)__lsx_vfrintrm_s(x);
112 }
113#endif // __loongarch_sx
114};
115
116struct unary_op_ceil
117{
118 float func(const float& x) const
119 {
120 return (float)ceil(x);
121 }
122#if __loongarch_sx
123 __m128 func_pack4(const __m128& x) const
124 {
125 return (__m128)__lsx_vfrintrp_s(x);
126 }
127#endif // __loongarch_sx
128};
129
130struct unary_op_square
131{
132 float func(const float& x) const
133 {
134 return x * x;
135 }
136#if __loongarch_sx
137 __m128 func_pack4(const __m128& x) const
138 {
139 return __lsx_vfmul_s(x, x);
140 }
141#endif // __loongarch_sx
142};
143
144struct unary_op_sqrt
145{
146 float func(const float& x) const
147 {
148 return (float)sqrt(x);
149 }
150#if __loongarch_sx
151 __m128 func_pack4(const __m128& x) const
152 {
153 return __lsx_vfsqrt_s(x);
154 }
155#endif // __loongarch_sx
156};
157
158struct unary_op_rsqrt
159{
160 float func(const float& x) const
161 {
162 return (float)(1.f / sqrt(x));
163 }
164#if __loongarch_sx
165 __m128 func_pack4(const __m128& x) const
166 {
167 return __lsx_vfrsqrt_s(x);
168 }
169#endif // __loongarch_sx
170};
171
172struct unary_op_exp
173{
174 float func(const float& x) const
175 {
176 return (float)exp(x);
177 }
178#if __loongarch_sx
179 __m128 func_pack4(const __m128& x) const
180 {
181 return exp_ps(x);
182 }
183#endif // __loongarch_sx
184};
185
186struct unary_op_log
187{
188 float func(const float& x) const
189 {
190 return (float)log(x);
191 }
192#if __loongarch_sx
193 __m128 func_pack4(const __m128& x) const
194 {
195 return log_ps(x);
196 }
197#endif // __loongarch_sx
198};
199
200struct unary_op_sin
201{
202 float func(const float& x) const
203 {
204 return (float)sin(x);
205 }
206#if __loongarch_sx
207 __m128 func_pack4(const __m128& x) const
208 {
209 // TODO msa optimize
210 float tmp[4];
211 __lsx_vst(x, tmp, 0);
212 tmp[0] = sin(tmp[0]);
213 tmp[1] = sin(tmp[1]);
214 tmp[2] = sin(tmp[2]);
215 tmp[3] = sin(tmp[3]);
216 return (__m128)__lsx_vld(tmp, 0);
217 }
218#endif // __loongarch_sx
219};
220
221struct unary_op_cos
222{
223 float func(const float& x) const
224 {
225 return (float)cos(x);
226 }
227#if __loongarch_sx
228 __m128 func_pack4(const __m128& x) const
229 {
230 // TODO msa optimize
231 float tmp[4];
232 __lsx_vst(x, tmp, 0);
233 tmp[0] = cos(tmp[0]);
234 tmp[1] = cos(tmp[1]);
235 tmp[2] = cos(tmp[2]);
236 tmp[3] = cos(tmp[3]);
237 return (__m128)__lsx_vld(tmp, 0);
238 }
239#endif // __loongarch_sx
240};
241
242struct unary_op_tan
243{
244 float func(const float& x) const
245 {
246 return (float)tan(x);
247 }
248#if __loongarch_sx
249 __m128 func_pack4(const __m128& x) const
250 {
251 // TODO msa optimize
252 float tmp[4];
253 __lsx_vst(x, tmp, 0);
254 tmp[0] = tan(tmp[0]);
255 tmp[1] = tan(tmp[1]);
256 tmp[2] = tan(tmp[2]);
257 tmp[3] = tan(tmp[3]);
258 return (__m128)__lsx_vld(tmp, 0);
259 }
260#endif // __loongarch_sx
261};
262
263struct unary_op_asin
264{
265 float func(const float& x) const
266 {
267 return (float)asin(x);
268 }
269#if __loongarch_sx
270 __m128 func_pack4(const __m128& x) const
271 {
272 // TODO msa optimize
273 float tmp[4];
274 __lsx_vst(x, tmp, 0);
275 tmp[0] = asin(tmp[0]);
276 tmp[1] = asin(tmp[1]);
277 tmp[2] = asin(tmp[2]);
278 tmp[3] = asin(tmp[3]);
279 return (__m128)__lsx_vld(tmp, 0);
280 }
281#endif // __loongarch_sx
282};
283
284struct unary_op_acos
285{
286 float func(const float& x) const
287 {
288 return (float)acos(x);
289 }
290#if __loongarch_sx
291 __m128 func_pack4(const __m128& x) const
292 {
293 // TODO msa optimize
294 float tmp[4];
295 __lsx_vst(x, tmp, 0);
296 tmp[0] = acos(tmp[0]);
297 tmp[1] = acos(tmp[1]);
298 tmp[2] = acos(tmp[2]);
299 tmp[3] = acos(tmp[3]);
300 return (__m128)__lsx_vld(tmp, 0);
301 }
302#endif // __loongarch_sx
303};
304
305struct unary_op_atan
306{
307 float func(const float& x) const
308 {
309 return (float)atan(x);
310 }
311#if __loongarch_sx
312 __m128 func_pack4(const __m128& x) const
313 {
314 // TODO msa optimize
315 float tmp[4];
316 __lsx_vst(x, tmp, 0);
317 tmp[0] = atan(tmp[0]);
318 tmp[1] = atan(tmp[1]);
319 tmp[2] = atan(tmp[2]);
320 tmp[3] = atan(tmp[3]);
321 return (__m128)__lsx_vld(tmp, 0);
322 }
323#endif // __loongarch_sx
324};
325
326struct unary_op_reciprocal
327{
328 float func(const float& x) const
329 {
330 return 1.f / x;
331 }
332#if __loongarch_sx
333 __m128 func_pack4(const __m128& x) const
334 {
335 return __lsx_vfrecip_s(x);
336 }
337#endif // __loongarch_sx
338};
339
340struct unary_op_tanh
341{
342 float func(const float& x) const
343 {
344 return (float)tanh(x);
345 }
346#if __loongarch_sx
347 __m128 func_pack4(const __m128& x) const
348 {
349 return tanh_ps(x);
350 }
351#endif // __loongarch_sx
352};
353
354struct unary_op_log10
355{
356 float func(const float& x) const
357 {
358 return (float)log10(x);
359 }
360#if __loongarch_sx
361 __m128 func_pack4(const __m128& x) const
362 {
363 return __lsx_vfmul_s(log_ps(x), __lsx_vreplfr2vr_s(0.434294481903));
364 }
365#endif // __loongarch_sx
366};
367
368struct unary_op_round
369{
370 float func(const float& x) const
371 {
372 // round to nearest even
373#if NCNN_GNU_INLINE_ASM
374 // return (x + 12582912.f) - 12582912.f;
375 float y;
376 const float magic = 12582912.f;
377 asm volatile(
378 "fadd.s %0, %1, %2 \n"
379 "fsub.s %0, %0, %2 \n"
380 : "=f"(y)
381 : "f"(x), "f"(magic)
382 :);
383 return y;
384#else
385#ifdef FE_TONEAREST
386 int old_rm = fegetround();
387 fesetround(FE_TONEAREST);
388#endif
389 float y = nearbyintf(x);
390#ifdef FE_TONEAREST
391 fesetround(old_rm);
392#endif
393 return y;
394#endif
395 }
396#if __loongarch_sx
397 __m128 func_pack4(const __m128& x) const
398 {
399 return (__m128)__lsx_vfrintrne_s(x);
400 }
401#endif // __loongarch_sx
402};
403
404struct unary_op_trunc
405{
406 float func(const float& x) const
407 {
408 return (float)truncf(x);
409 }
410#if __loongarch_sx
411 __m128 func_pack4(const __m128& x) const
412 {
413 return (__m128)__lsx_vfrintrz_s(x);
414 }
415#endif // __loongarch_sx
416};
417
418} // namespace UnaryOp_loongarch_functor
419
420int UnaryOp_loongarch::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
421{
422 using namespace UnaryOp_loongarch_functor;
423
424 if (op_type == Operation_ABS)
425 return unary_op_inplace<unary_op_abs>(bottom_top_blob, opt);
426
427 if (op_type == Operation_NEG)
428 return unary_op_inplace<unary_op_neg>(bottom_top_blob, opt);
429
430 if (op_type == Operation_FLOOR)
431 return unary_op_inplace<unary_op_floor>(bottom_top_blob, opt);
432
433 if (op_type == Operation_CEIL)
434 return unary_op_inplace<unary_op_ceil>(bottom_top_blob, opt);
435
436 if (op_type == Operation_SQUARE)
437 return unary_op_inplace<unary_op_square>(bottom_top_blob, opt);
438
439 if (op_type == Operation_SQRT)
440 return unary_op_inplace<unary_op_sqrt>(bottom_top_blob, opt);
441
442 if (op_type == Operation_RSQRT)
443 return unary_op_inplace<unary_op_rsqrt>(bottom_top_blob, opt);
444
445 if (op_type == Operation_EXP)
446 return unary_op_inplace<unary_op_exp>(bottom_top_blob, opt);
447
448 if (op_type == Operation_LOG)
449 return unary_op_inplace<unary_op_log>(bottom_top_blob, opt);
450
451 if (op_type == Operation_SIN)
452 return unary_op_inplace<unary_op_sin>(bottom_top_blob, opt);
453
454 if (op_type == Operation_COS)
455 return unary_op_inplace<unary_op_cos>(bottom_top_blob, opt);
456
457 if (op_type == Operation_TAN)
458 return unary_op_inplace<unary_op_tan>(bottom_top_blob, opt);
459
460 if (op_type == Operation_ASIN)
461 return unary_op_inplace<unary_op_asin>(bottom_top_blob, opt);
462
463 if (op_type == Operation_ACOS)
464 return unary_op_inplace<unary_op_acos>(bottom_top_blob, opt);
465
466 if (op_type == Operation_ATAN)
467 return unary_op_inplace<unary_op_atan>(bottom_top_blob, opt);
468
469 if (op_type == Operation_RECIPROCAL)
470 return unary_op_inplace<unary_op_reciprocal>(bottom_top_blob, opt);
471
472 if (op_type == Operation_TANH)
473 return unary_op_inplace<unary_op_tanh>(bottom_top_blob, opt);
474
475 if (op_type == Operation_LOG10)
476 return unary_op_inplace<unary_op_log10>(bottom_top_blob, opt);
477
478 if (op_type == Operation_ROUND)
479 return unary_op_inplace<unary_op_round>(bottom_top_blob, opt);
480
481 if (op_type == Operation_TRUNC)
482 return unary_op_inplace<unary_op_trunc>(bottom_top_blob, opt);
483
484 return 0;
485}
486
487} // namespace ncnn