1 // Tencent is pleased to support the open source community by making ncnn available.
2 //
3 // Copyright (C) 2019 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 "tanh_x86.h"
16 
17 #include "x86_activation.h"
18 
19 #include <math.h>
20 
21 namespace ncnn {
22 
TanH_x86()23 TanH_x86::TanH_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 TanH_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 
47             for (int i = 0; i < size; i++)
48             {
49                 __m256 _p = _mm256_loadu_ps(ptr);
50                 _p = tanh_avx(_p);
51                 _mm256_storeu_ps(ptr, _p);
52                 ptr += 8;
53             }
54         }
55 
56         return 0;
57     }
58 #endif // __AVX__
59 
60     if (elempack == 4)
61     {
62         #pragma omp parallel for num_threads(opt.num_threads)
63         for (int q = 0; q < channels; q++)
64         {
65             float* ptr = bottom_top_blob.channel(q);
66 
67             for (int i = 0; i < size; i++)
68             {
69                 __m128 _p = _mm_loadu_ps(ptr);
70                 _p = tanh_sse(_p);
71                 _mm_storeu_ps(ptr, _p);
72                 ptr += 4;
73             }
74         }
75 
76         return 0;
77     }
78 #endif // __SSE2__
79 
80     #pragma omp parallel for num_threads(opt.num_threads)
81     for (int q = 0; q < channels; q++)
82     {
83         float* ptr = bottom_top_blob.channel(q);
84 
85         int i = 0;
86 #if __SSE2__
87 #if __AVX__
88         for (; i + 7 < size; i += 8)
89         {
90             __m256 _p = _mm256_loadu_ps(ptr);
91             _p = tanh_avx(_p);
92             _mm256_storeu_ps(ptr, _p);
93             ptr += 8;
94         }
95 #endif // __AVX__
96         for (; i + 3 < size; i += 4)
97         {
98             __m128 _p = _mm_loadu_ps(ptr);
99             _p = tanh_sse(_p);
100             _mm_storeu_ps(ptr, _p);
101             ptr += 4;
102         }
103 #endif // __SSE2__
104         for (; i < size; i++)
105         {
106             *ptr = tanh(*ptr);
107             ptr++;
108         }
109     }
110 
111     return 0;
112 }
113 
114 } // namespace ncnn
115