Elffuss BraidHub

kikocis/ncnn public

master · 2.5 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#ifndef LOONGARCH_ACTIVATION_H
16#define LOONGARCH_ACTIVATION_H
17
18#include "fused_activation.h"
19
20#if __loongarch_sx
21#include <lsxintrin.h>
22#include "lsx_mathfun.h"
23
24static inline __m128 activation_ps(__m128 _v, int activation_type, const ncnn::Mat& activation_params)
25{
26 if (activation_type == 1)
27 {
28 __m128 _zero = (__m128)__lsx_vreplgr2vr_w(0);
29 _v = __lsx_vfmax_s(_v, _zero);
30 }
31 else if (activation_type == 2)
32 {
33 __m128 _zero = (__m128)__lsx_vreplgr2vr_w(0);
34 __m128 _slope = (__m128)__lsx_vreplfr2vr_s(activation_params[0]);
35 __m128i _lemask = __lsx_vfcmp_cle_s(_v, _zero);
36 __m128 _ps = __lsx_vfmul_s(_v, _slope);
37 _v = (__m128)__lsx_vbitsel_v((__m128i)_v, (__m128i)_ps, (__m128i)_lemask);
38 }
39 else if (activation_type == 3)
40 {
41 __m128 _min = (__m128)__lsx_vreplfr2vr_s(activation_params[0]);
42 __m128 _max = (__m128)__lsx_vreplfr2vr_s(activation_params[1]);
43 _v = __lsx_vfmax_s(_v, _min);
44 _v = __lsx_vfmin_s(_v, _max);
45 }
46 else if (activation_type == 4)
47 {
48 _v = sigmoid_ps(_v);
49 }
50 else if (activation_type == 5)
51 {
52 _v = __lsx_vfmul_s(_v, tanh_ps(log_ps(__lsx_vfadd_s(exp_ps(_v), (__m128)__lsx_vreplfr2vr_s(1.f)))));
53 }
54 else if (activation_type == 6)
55 {
56 __m128 _alpha = (__m128)__lsx_vreplfr2vr_s(activation_params[0]);
57 __m128 _beta = (__m128)__lsx_vreplfr2vr_s(activation_params[1]);
58 __m128 _zero = (__m128)__lsx_vreplgr2vr_w(0);
59 __m128 _one = (__m128)__lsx_vreplfr2vr_s(1.f);
60 __m128 _outp = __lsx_vfmadd_s(_alpha, _v, _beta);
61 _outp = __lsx_vfmax_s(_outp, _zero);
62 _outp = __lsx_vfmin_s(_outp, _one);
63 _v = __lsx_vfmul_s(_outp, _v);
64 }
65
66 return _v;
67}
68#endif // __loongarch_sx
69
70#endif // LOONGARCH_ACTIVATION_H