1
// Tencent is pleased to support the open source community by making ncnn available.
3
// Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
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
8
// https://opensource.org/licenses/BSD-3-Clause
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.
15
#include "tanh_riscv.h"
18
#include <riscv_vector.h>
19
#include "rvv_mathfun.h"
20
#include "rvv_mathfun_fp16s.h"
21
#endif // __riscv_vector
25
TanH_riscv::TanH_riscv()
28
support_packing = true;
30
support_fp16_storage = true;
32
#endif // __riscv_vector
35
int TanH_riscv::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
37
#if __riscv_vector && __riscv_zfh
38
int elembits = bottom_top_blob.elembits();
40
if (opt.use_fp16_storage && elembits == 16)
42
if (opt.use_fp16_arithmetic)
43
return forward_inplace_fp16sa(bottom_top_blob, opt);
45
return forward_inplace_fp16s(bottom_top_blob, opt);
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;
56
#pragma omp parallel for num_threads(opt.num_threads)
57
for (int q = 0; q < channels; q++)
59
float* ptr = bottom_top_blob.channel(q);
65
size_t vl = vsetvl_e32m8(n);
67
vfloat32m8_t _p = vle32_v_f32m8(ptr, vl);
69
vse32_v_f32m8(ptr, _p, vl);
74
#else // __riscv_vector
75
for (int i = 0; i < size; i++)
80
#endif // __riscv_vector
86
#if __riscv_vector && __riscv_zfh
87
int TanH_riscv::forward_inplace_fp16s(Mat& bottom_top_blob, const Option& opt) const
89
int w = bottom_top_blob.w;
90
int h = bottom_top_blob.h;
91
int d = bottom_top_blob.d;
92
int channels = bottom_top_blob.c;
93
int elempack = bottom_top_blob.elempack;
94
int size = w * h * d * elempack;
96
#pragma omp parallel for num_threads(opt.num_threads)
97
for (int q = 0; q < channels; q++)
99
__fp16* ptr = bottom_top_blob.channel(q);
104
size_t vl = vsetvl_e16m4(n);
106
vfloat32m8_t _p = vfwcvt_f_f_v_f32m8(vle16_v_f16m4(ptr, vl), vl);
107
_p = tanh_ps(_p, vl);
108
vse16_v_f16m4(ptr, vfncvt_f_f_w_f16m4(_p, vl), vl);
118
int TanH_riscv::forward_inplace_fp16sa(Mat& bottom_top_blob, const Option& opt) const
120
int w = bottom_top_blob.w;
121
int h = bottom_top_blob.h;
122
int d = bottom_top_blob.d;
123
int channels = bottom_top_blob.c;
124
int elempack = bottom_top_blob.elempack;
125
int size = w * h * d * elempack;
127
#pragma omp parallel for num_threads(opt.num_threads)
128
for (int q = 0; q < channels; q++)
130
__fp16* ptr = bottom_top_blob.channel(q);
135
size_t vl = vsetvl_e16m8(n);
137
vfloat16m8_t _p = vle16_v_f16m8(ptr, vl);
138
_p = tanh_ps(_p, vl);
139
vse16_v_f16m8(ptr, _p, vl);
148
#endif // __riscv_vector && __riscv_zfh