Elffuss BraidHub

kikocis/ncnn public

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