Elffuss BraidHub

kikocis/ncnn public

master · 2.4 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 "hardswish_loongarch.h"
16
17#if __loongarch_sx
18#include <lsxintrin.h>
19#endif // __loongarch_sx
20
21#include "loongarch_usability.h"
22
23namespace ncnn {
24
25HardSwish_loongarch::HardSwish_loongarch()
26{
27#if __loongarch_sx
28 support_packing = true;
29#endif // __loongarch_sx
30}
31
32int HardSwish_loongarch::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
33{
34 int w = bottom_top_blob.w;
35 int h = bottom_top_blob.h;
36 int d = bottom_top_blob.d;
37 int channels = bottom_top_blob.c;
38 int elempack = bottom_top_blob.elempack;
39 int size = w * h * d * elempack;
40
41 #pragma omp parallel for num_threads(opt.num_threads)
42 for (int q = 0; q < channels; q++)
43 {
44 float* ptr = bottom_top_blob.channel(q);
45
46 int i = 0;
47#if __loongarch_sx
48 __m128 _zero = (__m128)__lsx_vreplgr2vr_w(0);
49 __m128 _one = (__m128)__lsx_vreplfr2vr_s(1.f);
50 __m128 _alpha = (__m128)__lsx_vreplfr2vr_s(alpha);
51 __m128 _beta = (__m128)__lsx_vreplfr2vr_s(beta);
52 for (; i + 3 < size; i += 4)
53 {
54 __builtin_prefetch(ptr + 16);
55 __m128 _p = (__m128)__lsx_vld(ptr, 0);
56 __m128 _outp = __lsx_vfmadd_s(_alpha, _p, _beta);
57 _outp = __lsx_vfmax_s(_outp, _zero);
58 _outp = __lsx_vfmin_s(_outp, _one);
59 _outp = __lsx_vfmul_s(_outp, _p);
60 __lsx_vst(_outp, ptr, 0);
61
62 ptr += 4;
63 }
64#endif // __loongarch_sx
65 for (; i < size; i++)
66 {
67 if (*ptr < lower)
68 *ptr = 0.f;
69 else if (*ptr > upper)
70 ;
71 else
72 *ptr = *ptr * (*ptr * alpha + beta);
73 ++ptr;
74 }
75 }
76
77 return 0;
78}
79
80} // namespace ncnn