Elffuss BraidHub

kikocis/ncnn public

master · 1.9 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 "bias_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
25int Bias_loongarch::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
26{
27 int w = bottom_top_blob.w;
28 int h = bottom_top_blob.h;
29 int d = bottom_top_blob.d;
30 int channels = bottom_top_blob.c;
31 int size = w * h * d;
32
33 const float* bias_ptr = bias_data;
34 #pragma omp parallel for num_threads(opt.num_threads)
35 for (int q = 0; q < channels; q++)
36 {
37 float* ptr = bottom_top_blob.channel(q);
38
39 float bias = bias_ptr[q];
40
41#if __loongarch_sx
42 int nn = size >> 2;
43 int remain = size - (nn << 2);
44#else
45 int remain = size;
46#endif // __loongarch_sx
47
48#if __loongarch_sx
49 __m128 _bias = (__m128)__lsx_vreplfr2vr_s(bias);
50 for (; nn > 0; nn--)
51 {
52 __m128 _p = (__m128)__lsx_vld(ptr, 0);
53 __m128 _outp = __lsx_vfadd_s(_p, _bias);
54 __lsx_vst(_outp, ptr, 0);
55
56 ptr += 4;
57 }
58#endif // __loongarch_sx
59
60 for (; remain > 0; remain--)
61 {
62 *ptr = *ptr + bias;
63 ptr++;
64 }
65 }
66
67 return 0;
68}
69
70} // namespace ncnn