| 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 | #ifndef MIPS_ACTIVATION_H |
| 16 | #define MIPS_ACTIVATION_H |
| 17 | |
| 18 | #include "fused_activation.h" |
| 19 | |
| 20 | #if __mips_msa |
| 21 | #include <msa.h> |
| 22 | #include "msa_mathfun.h" |
| 23 | |
| 24 | static inline v4f32 activation_ps(v4f32 _v, int activation_type, const ncnn::Mat& activation_params) |
| 25 | { |
| 26 | if (activation_type == 1) |
| 27 | { |
| 28 | v4f32 _zero = (v4f32)__msa_fill_w(0); |
| 29 | _v = __msa_fmax_w(_v, _zero); |
| 30 | } |
| 31 | else if (activation_type == 2) |
| 32 | { |
| 33 | v4f32 _zero = (v4f32)__msa_fill_w(0); |
| 34 | v4f32 _slope = (v4f32)__msa_fill_w_f32(activation_params[0]); |
| 35 | v4i32_w _lemask = __msa_fcle_w(_v, _zero); |
| 36 | v4f32 _ps = __msa_fmul_w(_v, _slope); |
| 37 | _v = (v4f32)__msa_bsel_v((v16u8)_lemask, (v16u8)_v, (v16u8)_ps); |
| 38 | } |
| 39 | else if (activation_type == 3) |
| 40 | { |
| 41 | v4f32 _min = (v4f32)__msa_fill_w_f32(activation_params[0]); |
| 42 | v4f32 _max = (v4f32)__msa_fill_w_f32(activation_params[1]); |
| 43 | _v = __msa_fmax_w(_v, _min); |
| 44 | _v = __msa_fmin_w(_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 = __msa_fmul_w(_v, tanh_ps(log_ps(__msa_fadd_w(exp_ps(_v), (v4f32)__msa_fill_w_f32(1.f))))); |
| 53 | } |
| 54 | else if (activation_type == 6) |
| 55 | { |
| 56 | v4f32 _alpha = (v4f32)__msa_fill_w_f32(activation_params[0]); |
| 57 | v4f32 _beta = (v4f32)__msa_fill_w_f32(activation_params[1]); |
| 58 | v4f32 _zero = (v4f32)__msa_fill_w(0); |
| 59 | v4f32 _one = (v4f32)__msa_fill_w_f32(1.f); |
| 60 | v4f32 _outp = __msa_fmadd_w(_beta, _v, _alpha); |
| 61 | _outp = __msa_fmax_w(_outp, _zero); |
| 62 | _outp = __msa_fmin_w(_outp, _one); |
| 63 | _v = __msa_fmul_w(_outp, _v); |
| 64 | } |
| 65 | |
| 66 | return _v; |
| 67 | } |
| 68 | #endif // __mips_msa |
| 69 | |
| 70 | #endif // MIPS_ACTIVATION_H |