Elffuss BraidHub

kikocis/ncnn public

master · 2.7 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 "clip_x86.h"
16
17#if __SSE2__
18#include <emmintrin.h>
19#if __AVX__
20#include <immintrin.h>
21#endif // __AVX__
22#endif // __SSE2__
23
24namespace ncnn {
25
26Clip_x86::Clip_x86()
27{
28#if __SSE2__
29 support_packing = true;
30#endif // __SSE2__
31}
32
33int Clip_x86::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
34{
35 int w = bottom_top_blob.w;
36 int h = bottom_top_blob.h;
37 int d = bottom_top_blob.d;
38 int channels = bottom_top_blob.c;
39 int elempack = bottom_top_blob.elempack;
40 int size = w * h * d * elempack;
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 int i = 0;
48#if __SSE2__
49#if __AVX__
50#if __AVX512F__
51 __m512 _min_avx512 = _mm512_set1_ps(min);
52 __m512 _max_avx512 = _mm512_set1_ps(max);
53 for (; i + 15 < size; i += 16)
54 {
55 __m512 _p = _mm512_loadu_ps(ptr);
56 _p = _mm512_max_ps(_p, _min_avx512);
57 _p = _mm512_min_ps(_p, _max_avx512);
58 _mm512_storeu_ps(ptr, _p);
59 ptr += 16;
60 }
61#endif // __AVX512F__
62 __m256 _min_avx = _mm256_set1_ps(min);
63 __m256 _max_avx = _mm256_set1_ps(max);
64 for (; i + 7 < size; i += 8)
65 {
66 __m256 _p = _mm256_loadu_ps(ptr);
67 _p = _mm256_max_ps(_p, _min_avx);
68 _p = _mm256_min_ps(_p, _max_avx);
69 _mm256_storeu_ps(ptr, _p);
70 ptr += 8;
71 }
72#endif // __AVX__
73 __m128 _min = _mm_set1_ps(min);
74 __m128 _max = _mm_set1_ps(max);
75 for (; i + 3 < size; i += 4)
76 {
77 __m128 _p = _mm_load_ps(ptr);
78 _p = _mm_max_ps(_p, _min);
79 _p = _mm_min_ps(_p, _max);
80 _mm_store_ps(ptr, _p);
81 ptr += 4;
82 }
83#endif // __SSE2__
84 for (; i < size; i++)
85 {
86 if (*ptr < min)
87 *ptr = min;
88 if (*ptr > max)
89 *ptr = max;
90 ptr++;
91 }
92 }
93
94 return 0;
95}
96
97} //namespace ncnn