Elffuss BraidHub

kikocis/ncnn public

master · 4.3 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2022 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_arm.h"
16
17#ifdef __ARM_NEON
18#include <arm_neon.h>
19#include "arm_usability.h"
20#endif // __ARM_NEON
21
22namespace ncnn {
23
24#if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
25int Clip_arm::forward_inplace_fp16s(Mat& bottom_top_blob, const Option& opt) const
26{
27 int w = bottom_top_blob.w;
28 int h = bottom_top_blob.h;
29 int d = bottom_top_blob.d;
30 int channels = bottom_top_blob.c;
31 int elempack = bottom_top_blob.elempack;
32 int size = w * h * d * elempack;
33
34 #pragma omp parallel for num_threads(opt.num_threads)
35 for (int q = 0; q < channels; q++)
36 {
37 __fp16* ptr = bottom_top_blob.channel(q);
38
39 __fp16 min_fp16 = min;
40 __fp16 max_fp16 = max;
41
42 float16x8_t _min = vdupq_n_f16(min_fp16);
43 float16x8_t _max = vdupq_n_f16(max_fp16);
44
45 int i = 0;
46 for (; i + 31 < size; i += 32)
47 {
48#if NCNN_GNU_INLINE_ASM
49 asm volatile(
50 "prfm pldl1keep, [%0, #512] \n"
51 "ld1 {v0.8h, v1.8h, v2.8h, v3.8h}, [%0] \n"
52 "fmax v0.8h, v0.8h, %2.8h \n"
53 "fmax v1.8h, v1.8h, %2.8h \n"
54 "fmax v2.8h, v2.8h, %2.8h \n"
55 "fmax v3.8h, v3.8h, %2.8h \n"
56 "fmin v0.8h, v0.8h, %3.8h \n"
57 "fmin v1.8h, v1.8h, %3.8h \n"
58 "fmin v2.8h, v2.8h, %3.8h \n"
59 "fmin v3.8h, v3.8h, %3.8h \n"
60 "st1 {v0.8h, v1.8h, v2.8h, v3.8h}, [%0], #64 \n"
61 : "=r"(ptr) // %0
62 : "0"(ptr),
63 "w"(_min), // %2
64 "w"(_max) // %3
65 : "memory", "v0", "v1", "v2", "v3");
66#else // NCNN_GNU_INLINE_ASM
67 float16x8_t _p0 = vld1q_f16(ptr);
68 float16x8_t _p1 = vld1q_f16(ptr + 8);
69 float16x8_t _p2 = vld1q_f16(ptr + 16);
70 float16x8_t _p3 = vld1q_f16(ptr + 24);
71 _p0 = vmaxq_f16(_p0, _min);
72 _p1 = vmaxq_f16(_p1, _min);
73 _p2 = vmaxq_f16(_p2, _min);
74 _p3 = vmaxq_f16(_p3, _min);
75 _p0 = vminq_f16(_p0, _max);
76 _p1 = vminq_f16(_p1, _max);
77 _p2 = vminq_f16(_p2, _max);
78 _p3 = vminq_f16(_p3, _max);
79 vst1q_f16(ptr, _p0);
80 vst1q_f16(ptr + 8, _p1);
81 vst1q_f16(ptr + 16, _p2);
82 vst1q_f16(ptr + 24, _p3);
83 ptr += 32;
84#endif // NCNN_GNU_INLINE_ASM
85 }
86 for (; i + 15 < size; i += 16)
87 {
88 float16x8_t _p0 = vld1q_f16(ptr);
89 float16x8_t _p1 = vld1q_f16(ptr + 8);
90 _p0 = vmaxq_f16(_p0, _min);
91 _p1 = vmaxq_f16(_p1, _min);
92 _p0 = vminq_f16(_p0, _max);
93 _p1 = vminq_f16(_p1, _max);
94 vst1q_f16(ptr, _p0);
95 vst1q_f16(ptr + 8, _p1);
96 ptr += 16;
97 }
98 for (; i + 7 < size; i += 8)
99 {
100 float16x8_t _p = vld1q_f16(ptr);
101 _p = vmaxq_f16(_p, _min);
102 _p = vminq_f16(_p, _max);
103 vst1q_f16(ptr, _p);
104 ptr += 8;
105 }
106 for (; i + 3 < size; i += 4)
107 {
108 float16x4_t _p = vld1_f16(ptr);
109 _p = vmax_f16(_p, vget_low_f16(_min));
110 _p = vmin_f16(_p, vget_low_f16(_max));
111 vst1_f16(ptr, _p);
112 ptr += 4;
113 }
114 for (; i < size; i++)
115 {
116 __fp16 v = *ptr;
117 if (v < min_fp16)
118 v = min_fp16;
119
120 if (v > max_fp16)
121 v = max_fp16;
122
123 *ptr = v;
124 ptr++;
125 }
126 }
127
128 return 0;
129}
130#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
131
132} // namespace ncnn