Elffuss BraidHub

kikocis/ncnn public

master · 4.1 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2020 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_riscv.h"
16
17#if __riscv_vector
18#include <riscv_vector.h>
19#include "rvv_mathfun.h"
20#include "rvv_mathfun_fp16s.h"
21#endif // __riscv_vector
22
23namespace ncnn {
24
25Clip_riscv::Clip_riscv()
26{
27#if __riscv_vector
28 support_packing = true;
29#if __riscv_zfh
30 support_fp16_storage = true;
31#endif
32#endif // __riscv_vector
33}
34
35int Clip_riscv::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
36{
37#if __riscv_vector && __riscv_zfh
38 int elembits = bottom_top_blob.elembits();
39
40 if (opt.use_fp16_storage && elembits == 16)
41 {
42 if (opt.use_fp16_arithmetic)
43 return forward_inplace_fp16sa(bottom_top_blob, opt);
44 else
45 return forward_inplace_fp16s(bottom_top_blob, opt);
46 }
47#endif
48
49 int w = bottom_top_blob.w;
50 int h = bottom_top_blob.h;
51 int d = bottom_top_blob.d;
52 int channels = bottom_top_blob.c;
53 int elempack = bottom_top_blob.elempack;
54 int size = w * h * d * elempack;
55
56 #pragma omp parallel for num_threads(opt.num_threads)
57 for (int q = 0; q < channels; q++)
58 {
59 float* ptr = bottom_top_blob.channel(q);
60
61#if __riscv_vector
62 int n = size;
63 while (n > 0)
64 {
65 size_t vl = vsetvl_e32m8(n);
66
67 vfloat32m8_t _p = vle32_v_f32m8(ptr, vl);
68 _p = vfmax_vf_f32m8(_p, min, vl);
69 _p = vfmin_vf_f32m8(_p, max, vl);
70 vse32_v_f32m8(ptr, _p, vl);
71
72 ptr += vl;
73 n -= vl;
74 }
75#else // __riscv_vector
76 for (int i = 0; i < size; i++)
77 {
78 if (*ptr < min)
79 *ptr = min;
80
81 if (*ptr > max)
82 *ptr = max;
83
84 ptr++;
85 }
86#endif // __riscv_vector
87 }
88
89 return 0;
90}
91
92#if __riscv_vector && __riscv_zfh
93int Clip_riscv::forward_inplace_fp16s(Mat& bottom_top_blob, const Option& opt) const
94{
95 int w = bottom_top_blob.w;
96 int h = bottom_top_blob.h;
97 int d = bottom_top_blob.d;
98 int channels = bottom_top_blob.c;
99 int elempack = bottom_top_blob.elempack;
100 int size = w * h * d * elempack;
101
102 #pragma omp parallel for num_threads(opt.num_threads)
103 for (int q = 0; q < channels; q++)
104 {
105 __fp16* ptr = bottom_top_blob.channel(q);
106
107 int n = size;
108 while (n > 0)
109 {
110 size_t vl = vsetvl_e16m4(n);
111
112 vfloat32m8_t _p = vfwcvt_f_f_v_f32m8(vle16_v_f16m4(ptr, vl), vl);
113 _p = vfmax_vf_f32m8(_p, min, vl);
114 _p = vfmin_vf_f32m8(_p, max, vl);
115 vse16_v_f16m4(ptr, vfncvt_f_f_w_f16m4(_p, vl), vl);
116
117 ptr += vl;
118 n -= vl;
119 }
120 }
121
122 return 0;
123}
124
125int Clip_riscv::forward_inplace_fp16sa(Mat& bottom_top_blob, const Option& opt) const
126{
127 int w = bottom_top_blob.w;
128 int h = bottom_top_blob.h;
129 int d = bottom_top_blob.d;
130 int channels = bottom_top_blob.c;
131 int elempack = bottom_top_blob.elempack;
132 int size = w * h * d * elempack;
133
134 #pragma omp parallel for num_threads(opt.num_threads)
135 for (int q = 0; q < channels; q++)
136 {
137 __fp16* ptr = bottom_top_blob.channel(q);
138
139 int n = size;
140 while (n > 0)
141 {
142 size_t vl = vsetvl_e16m8(n);
143
144 vfloat16m8_t _p = vle16_v_f16m8(ptr, vl);
145 _p = vfmax_vf_f16m8(_p, min, vl);
146 _p = vfmin_vf_f16m8(_p, max, vl);
147 vse16_v_f16m8(ptr, _p, vl);
148
149 ptr += vl;
150 n -= vl;
151 }
152 }
153
154 return 0;
155}
156#endif // __riscv_vector && __riscv_zfh
157
158} //namespace ncnn