Elffuss BraidHub

kikocis/ncnn public

master · 3.3 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 "hardswish_x86.h"
16
17#if __SSE2__
18#include <emmintrin.h>
19#if __AVX__
20#include <immintrin.h>
21#endif // __AVX__
22#endif // __SSE2__
23
24#include "x86_usability.h"
25
26namespace ncnn {
27
28HardSwish_x86::HardSwish_x86()
29{
30#if __SSE2__
31 support_packing = true;
32#endif // __SSE2__
33}
34
35int HardSwish_x86::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
36{
37 int w = bottom_top_blob.w;
38 int h = bottom_top_blob.h;
39 int d = bottom_top_blob.d;
40 int channels = bottom_top_blob.c;
41 int elempack = bottom_top_blob.elempack;
42 int size = w * h * d * elempack;
43
44 #pragma omp parallel for num_threads(opt.num_threads)
45 for (int q = 0; q < channels; q++)
46 {
47 float* ptr = bottom_top_blob.channel(q);
48
49 int i = 0;
50#if __SSE2__
51#if __AVX__
52#if __AVX512F__
53 __m512 _zero_avx512 = _mm512_setzero_ps();
54 __m512 _one_avx512 = _mm512_set1_ps(1.f);
55 for (; i + 15 < size; i += 16)
56 {
57 __m512 _p = _mm512_loadu_ps(ptr);
58 __m512 _ans = _mm512_set1_ps(beta);
59 _ans = _mm512_fmadd_ps(_p, _mm512_set1_ps(alpha), _ans);
60 _ans = _mm512_max_ps(_ans, _zero_avx512);
61 _ans = _mm512_min_ps(_ans, _one_avx512);
62 _ans = _mm512_mul_ps(_ans, _p);
63 _mm512_storeu_ps(ptr, _ans);
64 ptr += 16;
65 }
66#endif // __AVX512F__
67 __m256 _zero_avx = _mm256_setzero_ps();
68 __m256 _one_avx = _mm256_set1_ps(1.f);
69 for (; i + 7 < size; i += 8)
70 {
71 __m256 _p = _mm256_loadu_ps(ptr);
72 __m256 _ans = _mm256_set1_ps(beta);
73 _ans = _mm256_comp_fmadd_ps(_p, _mm256_set1_ps(alpha), _ans);
74 _ans = _mm256_max_ps(_ans, _zero_avx);
75 _ans = _mm256_min_ps(_ans, _one_avx);
76 _ans = _mm256_mul_ps(_ans, _p);
77 _mm256_storeu_ps(ptr, _ans);
78 ptr += 8;
79 }
80#endif // __AVX__
81 __m128 _zero = _mm_setzero_ps();
82 __m128 _one = _mm_set1_ps(1.f);
83 for (; i + 3 < size; i += 4)
84 {
85 __m128 _p = _mm_load_ps(ptr);
86 __m128 _ans = _mm_set1_ps(beta);
87 _ans = _mm_add_ps(_mm_mul_ps(_p, _mm_set1_ps(alpha)), _ans);
88 _ans = _mm_max_ps(_ans, _zero);
89 _ans = _mm_min_ps(_ans, _one);
90 _ans = _mm_mul_ps(_ans, _p);
91 _mm_store_ps(ptr, _ans);
92 ptr += 4;
93 }
94#endif // __SSE2__
95 for (; i < size; i++)
96 {
97 if (*ptr < lower)
98 *ptr = 0.f;
99 else if (*ptr > upper)
100 ;
101 else
102 *ptr = *ptr * (*ptr * alpha + beta);
103 ptr++;
104 }
105 }
106
107 return 0;
108}
109
110} // namespace ncnn