Elffuss BraidHub

kikocis/ncnn public

master · 2.1 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2017 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#include "bias_x86.h"
16
17#if __SSE2__
18#include <emmintrin.h>
19#if __AVX__
20#include <immintrin.h>
21#endif // __AVX__
22#endif // __SSE2__
23
24namespace ncnn {
25
26int Bias_x86::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
27{
28 int w = bottom_top_blob.w;
29 int h = bottom_top_blob.h;
30 int d = bottom_top_blob.d;
31 int channels = bottom_top_blob.c;
32 int size = w * h * d;
33
34 const float* bias_ptr = bias_data;
35 #pragma omp parallel for num_threads(opt.num_threads)
36 for (int q = 0; q < channels; q++)
37 {
38 float* ptr = bottom_top_blob.channel(q);
39
40 float bias = bias_ptr[q];
41
42 int i = 0;
43#if __SSE2__
44#if __AVX__
45 {
46 __m256 _bias256 = _mm256_set1_ps(bias);
47 for (; i + 7 < size; i += 8)
48 {
49 __m256 _p = _mm256_loadu_ps(ptr);
50 __m256 _outp = _mm256_add_ps(_p, _bias256);
51 _mm256_storeu_ps(ptr, _outp);
52
53 ptr += 8;
54 }
55 }
56#endif // __AVX__
57 {
58 __m128 _bias = _mm_set1_ps(bias);
59 for (; i + 3 < size; i += 4)
60 {
61 __m128 _p = _mm_loadu_ps(ptr);
62 __m128 _outp = _mm_add_ps(_p, _bias);
63 _mm_storeu_ps(ptr, _outp);
64
65 ptr += 4;
66 }
67 }
68#endif // __SSE2__
69
70 for (; i < size; i++)
71 {
72 *ptr = *ptr + bias;
73
74 ptr++;
75 }
76 }
77
78 return 0;
79}
80
81} // namespace ncnn