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 #include "x86_activation.h"
18 
19 #include <math.h>
20 
21 namespace ncnn {
22 
Sigmoid_x86()23 Sigmoid_x86::Sigmoid_x86()
24 {
25 #if __SSE2__
26     support_packing = true;
27 #endif // __SSE2__
28 }
29 
forward_inplace(Mat & bottom_top_blob,const Option & opt) const30 int Sigmoid_x86::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
31 {
32     int w = bottom_top_blob.w;
33     int h = bottom_top_blob.h;
34     int channels = bottom_top_blob.c;
35     int size = w * h;
36 #if __SSE2__
37     int elempack = bottom_top_blob.elempack;
38 
39 #if __AVX__
40     if (elempack == 8)
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             for (int i = 0; i < size; i++)
47             {
48                 __m256 _p = _mm256_loadu_ps(ptr);
49                 _mm256_storeu_ps(ptr, sigmoid_avx(_p));
50                 ptr += 8;
51             }
52         }
53 
54         return 0;
55     }
56 #endif // __AVX__
57 
58     if (elempack == 4)
59     {
60         #pragma omp parallel for num_threads(opt.num_threads)
61         for (int q = 0; q < channels; q++)
62         {
63             float* ptr = bottom_top_blob.channel(q);
64             for (int i = 0; i < size; i++)
65             {
66                 __m128 _p = _mm_loadu_ps(ptr);
67                 _mm_storeu_ps(ptr, sigmoid_sse(_p));
68                 ptr += 4;
69             }
70         }
71 
72         return 0;
73     }
74 #endif // __SSE2__
75 
76     #pragma omp parallel for num_threads(opt.num_threads)
77     for (int q = 0; q < channels; q++)
78     {
79         float* ptr = bottom_top_blob.channel(q);
80 
81         int i = 0;
82 #if __SSE2__
83 #if __AVX__
84         for (; i + 7 < size; i += 8)
85         {
86             __m256 _p = _mm256_loadu_ps(ptr);
87             _mm256_storeu_ps(ptr, sigmoid_avx(_p));
88             ptr += 8;
89         }
90 #endif // __AVX__
91         for (; i + 3 < size; i += 4)
92         {
93             __m128 _p = _mm_loadu_ps(ptr);
94             _mm_storeu_ps(ptr, sigmoid_sse(_p));
95             ptr += 4;
96         }
97 #endif // __SSE2__
98         for (; i < size; i++)
99         {
100             *ptr = 1.f / (1.f + exp(-*ptr));
101 
102             ptr++;
103         }
104     }
105 
106     return 0;
107 }
108 
109 } // namespace ncnn
110