Elffuss BraidHub

kikocis/ncnn public

master · 2.8 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 "sigmoid_x86.h"
16
17#if __SSE2__
18#include <emmintrin.h>
19#include "sse_mathfun.h"
20#if __AVX__
21#include <immintrin.h>
22#include "avx_mathfun.h"
23#if __AVX512F__
24#include "avx512_mathfun.h"
25#endif // __AVX512F__
26#endif // __AVX__
27#endif // __SSE2__
28
29namespace ncnn {
30
31Sigmoid_x86::Sigmoid_x86()
32{
33#if __SSE2__
34 support_packing = true;
35#endif // __SSE2__
36}
37
38int Sigmoid_x86::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
39{
40 int w = bottom_top_blob.w;
41 int h = bottom_top_blob.h;
42 int d = bottom_top_blob.d;
43 int channels = bottom_top_blob.c;
44 int elempack = bottom_top_blob.elempack;
45 int size = w * h * d * elempack;
46
47 #pragma omp parallel for num_threads(opt.num_threads)
48 for (int q = 0; q < channels; q++)
49 {
50 float* ptr = bottom_top_blob.channel(q);
51
52 int i = 0;
53#if __SSE2__
54#if __AVX__
55#if __AVX512F__
56 __m512 _one_avx512 = _mm512_set1_ps(1.f);
57 __m512 _zero_avx512 = _mm512_setzero_ps();
58 for (; i + 15 < size; i += 16)
59 {
60 __m512 _p = _mm512_loadu_ps(ptr);
61 _p = _mm512_div_ps(_one_avx512, _mm512_add_ps(_one_avx512, exp512_ps(_mm512_sub_ps(_zero_avx512, _p))));
62 _mm512_storeu_ps(ptr, _p);
63 ptr += 16;
64 }
65#endif // __AVX512F__
66 __m256 _one_avx = _mm256_set1_ps(1.f);
67 __m256 _zero_avx = _mm256_setzero_ps();
68 for (; i + 7 < size; i += 8)
69 {
70 __m256 _p = _mm256_loadu_ps(ptr);
71 _p = _mm256_div_ps(_one_avx, _mm256_add_ps(_one_avx, exp256_ps(_mm256_sub_ps(_zero_avx, _p))));
72 _mm256_storeu_ps(ptr, _p);
73 ptr += 8;
74 }
75#endif // __AVX__
76 __m128 _one = _mm_set1_ps(1.f);
77 __m128 _zero = _mm_setzero_ps();
78 for (; i + 3 < size; i += 4)
79 {
80 __m128 _p = _mm_load_ps(ptr);
81 _p = _mm_div_ps(_one, _mm_add_ps(_one, exp_ps(_mm_sub_ps(_zero, _p))));
82 _mm_store_ps(ptr, _p);
83 ptr += 4;
84 }
85#endif // __SSE2__
86 for (; i < size; i++)
87 {
88 *ptr = 1.f / (1.f + expf(-*ptr));
89 ptr++;
90 }
91 }
92
93 return 0;
94}
95
96} // namespace ncnn