Elffuss BraidHub

kikocis/ncnn public

master · 3.6 KiBRawPermalink
1// JasonZhang892 is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2022 JasonZhang892 <zqhy_0929@163.com>. 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 "bnll_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
31BNLL_x86::BNLL_x86()
32{
33#if __SSE2__
34 support_packing = true;
35#endif // __SSE2__
36}
37
38int BNLL_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 int i = 0;
52
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 __mmask16 mask = _mm512_cmp_ps_mask(_p, _zero_avx512, _CMP_GT_OQ);
62 __m512 _abs_p = _mm512_castsi512_ps(_mm512_and_epi32(_mm512_castps_si512(_p), _mm512_set1_epi32(0x7fffffff)));
63 __m512 _tmp = log512_ps(_mm512_add_ps(_one_avx512, exp512_ps(_mm512_sub_ps(_zero_avx512, _abs_p))));
64 _p = _mm512_mask_add_ps(_tmp, mask, _tmp, _p);
65 _mm512_storeu_ps(ptr, _p);
66 ptr += 16;
67 }
68#endif // __AVX512F__
69 __m256 _one_avx = _mm256_set1_ps(1.f);
70 __m256 _zero_avx = _mm256_setzero_ps();
71 for (; i + 7 < size; i += 8)
72 {
73 __m256 _p = _mm256_loadu_ps(ptr);
74 __m256 mask = _mm256_cmp_ps(_p, _mm256_setzero_ps(), _CMP_GT_OQ);
75 __m256 _abs_p = _mm256_and_ps(_p, *(__m256*)_ps256_inv_sign_mask);
76 __m256 _tmp = log256_ps(_mm256_add_ps(_one_avx, exp256_ps(_mm256_sub_ps(_zero_avx, _abs_p))));
77 __m256 _x = _mm256_and_ps(_p, mask);
78 _p = _mm256_add_ps(_x, _tmp);
79 _mm256_storeu_ps(ptr, _p);
80 ptr += 8;
81 }
82#endif // __AVX__
83 __m128 _one = _mm_set1_ps(1.f);
84 __m128 _zero = _mm_setzero_ps();
85 for (; i + 3 < size; i += 4)
86 {
87 __m128 _p = _mm_load_ps(ptr);
88 __m128 mask = _mm_cmpgt_ps(_p, _zero);
89 __m128 _abs_p = _mm_and_ps(_p, *(__m128*)_ps_inv_sign_mask);
90 __m128 _tmp = log_ps(_mm_add_ps(_one, exp_ps(_mm_sub_ps(_zero, _abs_p))));
91 __m128 _x = _mm_and_ps(_p, mask);
92 _p = _mm_add_ps(_x, _tmp);
93 _mm_store_ps(ptr, _p);
94 ptr += 4;
95 }
96#endif // __SSE2__
97 for (; i < size; i++)
98 {
99 if (*ptr > 0)
100 *ptr = *ptr + logf(1.f + expf(-(*ptr)));
101 else
102 *ptr = logf(1.f + expf(*ptr));
103 ptr++;
104 }
105 }
106 return 0;
107}
108
109} // namespace ncnn