Elffuss BraidHub

kikocis/ncnn public

master · 3.2 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 "hardsigmoid_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
28HardSigmoid_x86::HardSigmoid_x86()
29{
30#if __SSE2__
31 support_packing = true;
32#endif // __SSE2__
33}
34
35int HardSigmoid_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 _mm512_storeu_ps(ptr, _ans);
63 ptr += 16;
64 }
65#endif // __AVX512F__
66 __m256 _zero_avx = _mm256_setzero_ps();
67 __m256 _one_avx = _mm256_set1_ps(1.f);
68 for (; i + 7 < size; i += 8)
69 {
70 __m256 _p = _mm256_loadu_ps(ptr);
71 __m256 _ans = _mm256_set1_ps(beta);
72 _ans = _mm256_comp_fmadd_ps(_p, _mm256_set1_ps(alpha), _ans);
73 _ans = _mm256_max_ps(_ans, _zero_avx);
74 _ans = _mm256_min_ps(_ans, _one_avx);
75 _mm256_storeu_ps(ptr, _ans);
76 ptr += 8;
77 }
78#endif // __AVX__
79 __m128 _zero = _mm_setzero_ps();
80 __m128 _one = _mm_set1_ps(1.f);
81 for (; i + 3 < size; i += 4)
82 {
83 __m128 _p = _mm_load_ps(ptr);
84 __m128 _ans = _mm_set1_ps(beta);
85 _ans = _mm_add_ps(_mm_mul_ps(_p, _mm_set1_ps(alpha)), _ans);
86 _ans = _mm_max_ps(_ans, _zero);
87 _ans = _mm_min_ps(_ans, _one);
88 _mm_store_ps(ptr, _ans);
89 ptr += 4;
90 }
91#endif // __SSE2__
92 for (; i < size; i++)
93 {
94 if (*ptr < lower)
95 *ptr = 0.f;
96 else if (*ptr > upper)
97 *ptr = 1.f;
98 else
99 *ptr = *ptr * alpha + beta;
100 ptr++;
101 }
102 }
103
104 return 0;
105}
106
107} // namespace ncnn