Elffuss BraidHub

kikocis/ncnn public

master · 3.2 KiBRawPermalink
1// Xavier Hsinyuan is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2021 Xavier Hsinyuan <thelastlinex@hotmail.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 "absval_riscv.h"
16
17#if __riscv_vector
18#include <riscv_vector.h>
19
20static inline vfloat32m8_t vfabs_v_f32m8_absval(vfloat32m8_t op1, size_t vl)
21{
22 return vfsgnjx_vv_f32m8(op1, op1, vl);
23}
24static inline vfloat16m8_t vfabs_v_f16m8_absval(vfloat16m8_t op1, size_t vl)
25{
26 return vfsgnjx_vv_f16m8(op1, op1, vl);
27}
28#endif // __riscv_vector
29
30namespace ncnn {
31
32AbsVal_riscv::AbsVal_riscv()
33{
34#if __riscv_vector
35 support_packing = true;
36#if __riscv_zfh
37 support_fp16_storage = true;
38#endif
39#endif // __riscv_vector
40}
41
42int AbsVal_riscv::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
43{
44#if __riscv_vector && __riscv_zfh
45 int elembits = bottom_top_blob.elembits();
46
47 if (opt.use_fp16_storage && elembits == 16)
48 {
49 return forward_inplace_fp16s(bottom_top_blob, opt);
50 }
51#endif
52
53 int w = bottom_top_blob.w;
54 int h = bottom_top_blob.h;
55 int d = bottom_top_blob.d;
56 int channels = bottom_top_blob.c;
57 int elempack = bottom_top_blob.elempack;
58 int size = w * h * d * elempack;
59
60 #pragma omp parallel for num_threads(opt.num_threads)
61 for (int q = 0; q < channels; q++)
62 {
63 float* ptr = bottom_top_blob.channel(q);
64
65#if __riscv_vector
66 int n = size;
67 while (n > 0)
68 {
69 size_t vl = vsetvl_e32m8(n);
70
71 vfloat32m8_t _p = vle32_v_f32m8(ptr, vl);
72 _p = vfabs_v_f32m8_absval(_p, vl);
73 vse32_v_f32m8(ptr, _p, vl);
74
75 ptr += vl;
76 n -= vl;
77 }
78#else // __riscv_vector
79 for (int i = 0; i < size; i++)
80 {
81 *ptr = (*ptr > 0) ? (*ptr) : (-*ptr);
82
83 ptr++;
84 }
85#endif // __riscv_vector
86 }
87
88 return 0;
89}
90
91#if __riscv_vector && __riscv_zfh
92int AbsVal_riscv::forward_inplace_fp16s(Mat& bottom_top_blob, const Option& opt) const
93{
94 int w = bottom_top_blob.w;
95 int h = bottom_top_blob.h;
96 int d = bottom_top_blob.d;
97 int channels = bottom_top_blob.c;
98 int elempack = bottom_top_blob.elempack;
99 int size = w * h * d * elempack;
100
101 #pragma omp parallel for num_threads(opt.num_threads)
102 for (int q = 0; q < channels; q++)
103 {
104 __fp16* ptr = bottom_top_blob.channel(q);
105
106 int n = size;
107 while (n > 0)
108 {
109 size_t vl = vsetvl_e16m8(n);
110
111 vfloat16m8_t _p = vle16_v_f16m8(ptr, vl);
112 _p = vfabs_v_f16m8_absval(_p, vl);
113 vse16_v_f16m8(ptr, _p, vl);
114
115 ptr += vl;
116 n -= vl;
117 }
118 }
119
120 return 0;
121}
122#endif
123
124} // namespace ncnn