Elffuss BraidHub

kikocis/ncnn public

master · 59.7 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2021 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#ifndef RVV_MATHFUN_H
16#define RVV_MATHFUN_H
17
18#include <riscv_vector.h>
19
20#define c_inv_mant_mask ~0x7f800000u
21#define c_cephes_SQRTHF 0.707106781186547524
22#define c_cephes_log_p0 7.0376836292E-2
23#define c_cephes_log_p1 -1.1514610310E-1
24#define c_cephes_log_p2 1.1676998740E-1
25#define c_cephes_log_p3 -1.2420140846E-1
26#define c_cephes_log_p4 +1.4249322787E-1
27#define c_cephes_log_p5 -1.6668057665E-1
28#define c_cephes_log_p6 +2.0000714765E-1
29#define c_cephes_log_p7 -2.4999993993E-1
30#define c_cephes_log_p8 +3.3333331174E-1
31#define c_cephes_log_q1 -2.12194440e-4
32#define c_cephes_log_q2 0.693359375
33
34#define _RVV_FLOAT32_LOG_OP(LMUL, MLEN) \
35 static inline vfloat32m##LMUL##_t log_ps(vfloat32m##LMUL##_t x, size_t vl) \
36 { \
37 x = vfmax_vf_f32m##LMUL(x, 0.f, vl); /* force flush to zero on denormal values */ \
38 vbool##MLEN##_t invalid_mask = vmfle_vf_f32m##LMUL##_b##MLEN(x, 0.f, vl); \
39 \
40 vint32m##LMUL##_t ux = vreinterpret_v_f32m##LMUL##_i32m##LMUL(x); \
41 \
42 vint32m##LMUL##_t emm0 = vsra_vx_i32m##LMUL(ux, 23, vl); \
43 \
44 /* keep only the fractional part */ \
45 ux = vand_vx_i32m##LMUL(ux, c_inv_mant_mask, vl); \
46 ux = vor_vx_i32m##LMUL(ux, 1056964608 /* reinterpret_cast<int>(0.5) */, vl); \
47 x = vreinterpret_v_i32m##LMUL##_f32m##LMUL(ux); \
48 \
49 emm0 = vsub_vx_i32m##LMUL(emm0, 0x7f, vl); \
50 vfloat32m##LMUL##_t e = vfcvt_f_x_v_f32m##LMUL(emm0, vl); \
51 \
52 e = vfadd_vf_f32m##LMUL(e, 1.f, vl); \
53 \
54 /* part2: */ \
55 /* if( x < SQRTHF ) { */ \
56 /* e -= 1; */ \
57 /* x = x + x - 1.0; */ \
58 /* } else { x = x - 1.0; } */ \
59 vbool##MLEN##_t mask = vmflt_vf_f32m##LMUL##_b##MLEN(x, c_cephes_SQRTHF, vl); \
60 x = vfadd_vv_f32m##LMUL##_m(mask, x, x, x, vl); \
61 x = vfsub_vf_f32m##LMUL(x, 1.f, vl); \
62 e = vfsub_vf_f32m##LMUL##_m(mask, e, e, 1.f, vl); \
63 \
64 vfloat32m##LMUL##_t z = vfmul_vv_f32m##LMUL(x, x, vl); \
65 \
66 vfloat32m##LMUL##_t y = vfmul_vf_f32m##LMUL(x, c_cephes_log_p0, vl); \
67 y = vfadd_vf_f32m##LMUL(y, c_cephes_log_p1, vl); \
68 y = vfmul_vv_f32m##LMUL(y, x, vl); \
69 y = vfadd_vf_f32m##LMUL(y, c_cephes_log_p2, vl); \
70 y = vfmul_vv_f32m##LMUL(y, x, vl); \
71 y = vfadd_vf_f32m##LMUL(y, c_cephes_log_p3, vl); \
72 y = vfmul_vv_f32m##LMUL(y, x, vl); \
73 y = vfadd_vf_f32m##LMUL(y, c_cephes_log_p4, vl); \
74 y = vfmul_vv_f32m##LMUL(y, x, vl); \
75 y = vfadd_vf_f32m##LMUL(y, c_cephes_log_p5, vl); \
76 y = vfmul_vv_f32m##LMUL(y, x, vl); \
77 y = vfadd_vf_f32m##LMUL(y, c_cephes_log_p6, vl); \
78 y = vfmul_vv_f32m##LMUL(y, x, vl); \
79 y = vfadd_vf_f32m##LMUL(y, c_cephes_log_p7, vl); \
80 y = vfmul_vv_f32m##LMUL(y, x, vl); \
81 y = vfadd_vf_f32m##LMUL(y, c_cephes_log_p8, vl); \
82 y = vfmul_vv_f32m##LMUL(y, x, vl); \
83 \
84 y = vfmul_vv_f32m##LMUL(y, z, vl); \
85 \
86 vfloat32m##LMUL##_t tmp = vfmul_vf_f32m##LMUL(e, c_cephes_log_q1, vl); \
87 y = vfadd_vv_f32m##LMUL(y, tmp, vl); \
88 \
89 tmp = vfmul_vf_f32m##LMUL(z, 0.5f, vl); \
90 y = vfsub_vv_f32m##LMUL(y, tmp, vl); \
91 \
92 tmp = vfmul_vf_f32m##LMUL(e, c_cephes_log_q2, vl); \
93 x = vfadd_vv_f32m##LMUL(x, y, vl); \
94 x = vfadd_vv_f32m##LMUL(x, tmp, vl); \
95 /* negative arg will be NAN */ \
96 vuint32m##LMUL##_t xtmp = vreinterpret_v_f32m##LMUL##_u32m##LMUL(x); \
97 x = vreinterpret_v_u32m##LMUL##_f32m##LMUL(vor_vx_u32m##LMUL##_m(invalid_mask, xtmp, xtmp, 0xffffffff, vl)); \
98 return x; \
99 }
100
101_RVV_FLOAT32_LOG_OP(1, 32)
102_RVV_FLOAT32_LOG_OP(2, 16)
103_RVV_FLOAT32_LOG_OP(4, 8)
104_RVV_FLOAT32_LOG_OP(8, 4)
105
106#define c_exp_hi 88.3762626647949f
107#define c_exp_lo -88.3762626647949f
108
109#define c_cephes_LOG2EF 1.44269504088896341
110#define c_cephes_exp_C1 0.693359375
111#define c_cephes_exp_C2 -2.12194440e-4
112
113#define c_cephes_exp_p0 1.9875691500E-4
114#define c_cephes_exp_p1 1.3981999507E-3
115#define c_cephes_exp_p2 8.3334519073E-3
116#define c_cephes_exp_p3 4.1665795894E-2
117#define c_cephes_exp_p4 1.6666665459E-1
118#define c_cephes_exp_p5 5.0000001201E-1
119
120#define _RVV_FLOAT32_EXP_OP(LMUL, MLEN) \
121 static inline vfloat32m##LMUL##_t exp_ps(vfloat32m##LMUL##_t x, size_t vl) \
122 { \
123 vfloat32m##LMUL##_t tmp, fx; \
124 \
125 x = vfmin_vf_f32m##LMUL(x, c_exp_hi, vl); \
126 x = vfmax_vf_f32m##LMUL(x, c_exp_lo, vl); \
127 \
128 /* express exp(x) as exp(g + n*log(2)) */ \
129 fx = vfmacc_vf_f32m##LMUL(vfmv_v_f_f32m##LMUL(0.5f, vl), c_cephes_LOG2EF, x, vl); \
130 \
131 /* perform a floorf */ \
132 tmp = vfcvt_f_x_v_f32m##LMUL(vfcvt_x_f_v_i32m##LMUL(fx, vl), vl); \
133 \
134 /* if greater, substract 1 */ \
135 vbool##MLEN##_t mask = vmfgt_vv_f32m##LMUL##_b##MLEN(tmp, fx, vl); \
136 fx = vfsub_vf_f32m##LMUL##_m(mask, tmp, tmp, 1.f, vl); \
137 \
138 tmp = vfmul_vf_f32m##LMUL(fx, c_cephes_exp_C1, vl); \
139 vfloat32m##LMUL##_t z = vfmul_vf_f32m##LMUL(fx, c_cephes_exp_C2, vl); \
140 x = vfsub_vv_f32m##LMUL(x, tmp, vl); \
141 x = vfsub_vv_f32m##LMUL(x, z, vl); \
142 \
143 vfloat32m##LMUL##_t y = vfmul_vf_f32m##LMUL(x, c_cephes_exp_p0, vl); \
144 z = vfmul_vv_f32m##LMUL(x, x, vl); \
145 \
146 y = vfadd_vf_f32m##LMUL(y, c_cephes_exp_p1, vl); \
147 y = vfmul_vv_f32m##LMUL(y, x, vl); \
148 y = vfadd_vf_f32m##LMUL(y, c_cephes_exp_p2, vl); \
149 y = vfmul_vv_f32m##LMUL(y, x, vl); \
150 y = vfadd_vf_f32m##LMUL(y, c_cephes_exp_p3, vl); \
151 y = vfmul_vv_f32m##LMUL(y, x, vl); \
152 y = vfadd_vf_f32m##LMUL(y, c_cephes_exp_p4, vl); \
153 y = vfmul_vv_f32m##LMUL(y, x, vl); \
154 y = vfadd_vf_f32m##LMUL(y, c_cephes_exp_p5, vl); \
155 \
156 y = vfmul_vv_f32m##LMUL(y, z, vl); \
157 y = vfadd_vv_f32m##LMUL(y, x, vl); \
158 y = vfadd_vf_f32m##LMUL(y, 1.f, vl); \
159 \
160 /* build 2^n */ \
161 vint32m##LMUL##_t mm = vfcvt_x_f_v_i32m##LMUL(fx, vl); \
162 mm = vadd_vx_i32m##LMUL(mm, 0x7f, vl); \
163 mm = vsll_vx_i32m##LMUL(mm, 23, vl); \
164 vfloat32m##LMUL##_t pow2n = vreinterpret_v_i32m##LMUL##_f32m##LMUL(mm); \
165 \
166 y = vfmul_vv_f32m##LMUL(y, pow2n, vl); \
167 return y; \
168 }
169
170_RVV_FLOAT32_EXP_OP(1, 32)
171_RVV_FLOAT32_EXP_OP(2, 16)
172_RVV_FLOAT32_EXP_OP(4, 8)
173_RVV_FLOAT32_EXP_OP(8, 4)
174
175#define c_minus_cephes_DP1 -0.78515625
176#define c_minus_cephes_DP2 -2.4187564849853515625e-4
177#define c_minus_cephes_DP3 -3.77489497744594108e-8
178#define c_sincof_p0 -1.9515295891E-4
179#define c_sincof_p1 8.3321608736E-3
180#define c_sincof_p2 -1.6666654611E-1
181#define c_coscof_p0 2.443315711809948E-005
182#define c_coscof_p1 -1.388731625493765E-003
183#define c_coscof_p2 4.166664568298827E-002
184#define c_cephes_FOPI 1.27323954473516 // 4 / M_PI
185
186#define _RVV_FLOAT32_SINCOS_OP(LMUL, MLEN) \
187 static inline void sincos_ps(vfloat32m##LMUL##_t x, vfloat32m##LMUL##_t* ysin, vfloat32m##LMUL##_t* ycos, size_t vl) \
188 { \
189 /* any x */ \
190 vfloat32m##LMUL##_t xmm1, xmm2, xmm3, y; \
191 \
192 vuint32m##LMUL##_t emm2; \
193 \
194 vbool##MLEN##_t sign_mask_sin, sign_mask_cos; \
195 sign_mask_sin = vmflt_vf_f32m##LMUL##_b##MLEN(x, 0.f, vl); \
196 x = vfsgnj_vf_f32m##LMUL(x, 1.f, vl); \
197 \
198 /* scale by 4/Pi */ \
199 y = vfmul_vf_f32m##LMUL(x, c_cephes_FOPI, vl); \
200 \
201 /* store the integer part of y in mm0 */ \
202 emm2 = vfcvt_xu_f_v_u32m##LMUL(y, vl); \
203 /* j=(j+1) & (~1) (see the cephes sources) */ \
204 emm2 = vadd_vx_u32m##LMUL(emm2, 1, vl); \
205 emm2 = vand_vx_u32m##LMUL(emm2, ~1, vl); \
206 y = vfcvt_f_xu_v_f32m##LMUL(emm2, vl); \
207 \
208 /* get the polynom selection mask */ \
209 /* there is one polynom for 0 <= x <= Pi/4 */ \
210 /* and another one for Pi/4<x<=Pi/2 */ \
211 /* */ \
212 /* Both branches will be computed. */ \
213 vbool##MLEN##_t poly_mask = vmsne_vx_u32m##LMUL##_b##MLEN(vand_vx_u32m##LMUL(emm2, 2, vl), 0, vl); \
214 \
215 /* The magic pass: "Extended precision modular arithmetic" */ \
216 /* x = ((x - y * DP1) - y * DP2) - y * DP3; */ \
217 xmm1 = vfmul_vf_f32m##LMUL(y, c_minus_cephes_DP1, vl); \
218 xmm2 = vfmul_vf_f32m##LMUL(y, c_minus_cephes_DP2, vl); \
219 xmm3 = vfmul_vf_f32m##LMUL(y, c_minus_cephes_DP3, vl); \
220 x = vfadd_vv_f32m##LMUL(x, xmm1, vl); \
221 x = vfadd_vv_f32m##LMUL(x, xmm2, vl); \
222 x = vfadd_vv_f32m##LMUL(x, xmm3, vl); \
223 \
224 sign_mask_sin = vmxor_mm_b##MLEN(sign_mask_sin, vmsne_vx_u32m##LMUL##_b##MLEN(vand_vx_u32m##LMUL(emm2, 4, vl), 0, vl), vl); \
225 sign_mask_cos = vmsne_vx_u32m##LMUL##_b##MLEN(vand_vx_u32m##LMUL(vsub_vx_u32m##LMUL(emm2, 2, vl), 4, vl), 0, vl); \
226 \
227 /* Evaluate the first polynom (0 <= x <= Pi/4) in y1, */ \
228 /* and the second polynom (Pi/4 <= x <= 0) in y2 */ \
229 vfloat32m##LMUL##_t z = vfmul_vv_f32m##LMUL(x, x, vl); \
230 vfloat32m##LMUL##_t y1, y2; \
231 \
232 y1 = vfmul_vf_f32m##LMUL(z, c_coscof_p0, vl); \
233 y2 = vfmul_vf_f32m##LMUL(z, c_sincof_p0, vl); \
234 y1 = vfadd_vf_f32m##LMUL(y1, c_coscof_p1, vl); \
235 y2 = vfadd_vf_f32m##LMUL(y2, c_sincof_p1, vl); \
236 y1 = vfmul_vv_f32m##LMUL(y1, z, vl); \
237 y2 = vfmul_vv_f32m##LMUL(y2, z, vl); \
238 y1 = vfadd_vf_f32m##LMUL(y1, c_coscof_p2, vl); \
239 y2 = vfadd_vf_f32m##LMUL(y2, c_sincof_p2, vl); \
240 y1 = vfmul_vv_f32m##LMUL(y1, z, vl); \
241 y2 = vfmul_vv_f32m##LMUL(y2, z, vl); \
242 y1 = vfmul_vv_f32m##LMUL(y1, z, vl); \
243 y2 = vfmul_vv_f32m##LMUL(y2, x, vl); \
244 y1 = vfsub_vv_f32m##LMUL(y1, vfmul_vf_f32m##LMUL(z, 0.5f, vl), vl); \
245 y2 = vfadd_vv_f32m##LMUL(y2, x, vl); \
246 y1 = vfadd_vf_f32m##LMUL(y1, 1.f, vl); \
247 \
248 /* select the correct result from the two polynoms */ \
249 vfloat32m##LMUL##_t ys = vmerge_vvm_f32m##LMUL(poly_mask, y2, y1, vl); \
250 vfloat32m##LMUL##_t yc = vmerge_vvm_f32m##LMUL(poly_mask, y1, y2, vl); \
251 *ysin = vmerge_vvm_f32m##LMUL(sign_mask_sin, ys, vfneg_v_f32m##LMUL(ys, vl), vl); \
252 *ycos = vmerge_vvm_f32m##LMUL(sign_mask_cos, vfneg_v_f32m##LMUL(yc, vl), yc, vl); \
253 }
254
255_RVV_FLOAT32_SINCOS_OP(1, 32)
256_RVV_FLOAT32_SINCOS_OP(2, 16)
257_RVV_FLOAT32_SINCOS_OP(4, 8)
258_RVV_FLOAT32_SINCOS_OP(8, 4)
259
260#define _RVV_FLOAT32_SIN_OP(LMUL, MLEN) \
261 static inline vfloat32m##LMUL##_t sin_ps(vfloat32m##LMUL##_t x, size_t vl) \
262 { \
263 vfloat32m##LMUL##_t ysin, ycos; \
264 sincos_ps(x, &ysin, &ycos, vl); \
265 return ysin; \
266 }
267
268_RVV_FLOAT32_SIN_OP(1, 32)
269_RVV_FLOAT32_SIN_OP(2, 16)
270_RVV_FLOAT32_SIN_OP(4, 8)
271_RVV_FLOAT32_SIN_OP(8, 4)
272
273#define _RVV_FLOAT32_COS_OP(LMUL, MLEN) \
274 static inline vfloat32m##LMUL##_t cos_ps(vfloat32m##LMUL##_t x, size_t vl) \
275 { \
276 vfloat32m##LMUL##_t ysin, ycos; \
277 sincos_ps(x, &ysin, &ycos, vl); \
278 return ycos; \
279 }
280
281_RVV_FLOAT32_COS_OP(1, 32)
282_RVV_FLOAT32_COS_OP(2, 16)
283_RVV_FLOAT32_COS_OP(4, 8)
284_RVV_FLOAT32_COS_OP(8, 4)
285
286#define c_tanh_tiny 1e-4f
287#define c_tanh_hi 9.0f
288// The monomial coefficients of the numerator polynomial (odd).
289#define c_tanh_alpha_1 4.89352455891786e-3f
290#define c_tanh_alpha_3 6.37261928875436e-4f
291#define c_tanh_alpha_5 1.48572235717979e-5f
292#define c_tanh_alpha_7 5.12229709037114e-8f
293#define c_tanh_alpha_9 -8.60467152213735e-11f
294#define c_tanh_alpha_11 2.00018790482477e-13f
295#define c_tanh_alpha_13 -2.76076847742355e-16f
296// The monomial coefficients of the denominator polynomial (even).
297#define c_tanh_beta_0 4.89352518554385e-3f
298#define c_tanh_beta_2 2.26843463243900e-3f
299#define c_tanh_beta_4 1.18534705686654e-4f
300#define c_tanh_beta_6 1.19825839466702e-6f
301
302#define _RVV_FLOAT32_TANH_OP(LMUL, MLEN) \
303 static inline vfloat32m##LMUL##_t tanh_ps(vfloat32m##LMUL##_t x, size_t vl) \
304 { \
305 vfloat32m##LMUL##_t x2 = vfsgnj_vf_f32m##LMUL(x, 1.f, vl); \
306 \
307 vbool##MLEN##_t tiny_mask = vmfge_vf_f32m##LMUL##_b##MLEN(x2, c_tanh_tiny, vl); \
308 \
309 /* clamp the inputs to the range [-9, 9] since anything outside */ \
310 /* this range is -/+1.0f in single-precision. */ \
311 x2 = vfmin_vf_f32m##LMUL(x2, c_tanh_hi, vl); \
312 \
313 /* since the polynomials are odd/even, we need x**2. */ \
314 vfloat32m##LMUL##_t z = vfmul_vv_f32m##LMUL(x2, x2, vl); \
315 \
316 /* evaluate the numerator polynomial y. */ \
317 vfloat32m##LMUL##_t y = vfmul_vf_f32m##LMUL(z, c_tanh_alpha_13, vl); \
318 y = vfadd_vf_f32m##LMUL(y, c_tanh_alpha_11, vl); \
319 y = vfmul_vv_f32m##LMUL(y, z, vl); \
320 y = vfadd_vf_f32m##LMUL(y, c_tanh_alpha_9, vl); \
321 y = vfmul_vv_f32m##LMUL(y, z, vl); \
322 y = vfadd_vf_f32m##LMUL(y, c_tanh_alpha_7, vl); \
323 y = vfmul_vv_f32m##LMUL(y, z, vl); \
324 y = vfadd_vf_f32m##LMUL(y, c_tanh_alpha_5, vl); \
325 y = vfmul_vv_f32m##LMUL(y, z, vl); \
326 y = vfadd_vf_f32m##LMUL(y, c_tanh_alpha_3, vl); \
327 y = vfmul_vv_f32m##LMUL(y, z, vl); \
328 y = vfadd_vf_f32m##LMUL(y, c_tanh_alpha_1, vl); \
329 y = vfmul_vv_f32m##LMUL(y, x2, vl); \
330 \
331 /* evaluate the denominator polynomial w. */ \
332 vfloat32m##LMUL##_t w = vfmul_vf_f32m##LMUL(z, c_tanh_beta_6, vl); \
333 w = vfadd_vf_f32m##LMUL(w, c_tanh_beta_4, vl); \
334 w = vfmul_vv_f32m##LMUL(w, z, vl); \
335 w = vfadd_vf_f32m##LMUL(w, c_tanh_beta_2, vl); \
336 w = vfmul_vv_f32m##LMUL(w, z, vl); \
337 w = vfadd_vf_f32m##LMUL(w, c_tanh_beta_0, vl); \
338 \
339 /* divide the numerator by the denominator. */ \
340 y = vfdiv_vv_f32m##LMUL(y, w, vl); \
341 \
342 /* reinstate the sign. */ \
343 y = vfsgnj_vv_f32m##LMUL(y, x, vl); \
344 \
345 /* when the argument is very small in magnitude it's more accurate to just return it. */ \
346 y = vmerge_vvm_f32m##LMUL(tiny_mask, x, y, vl); \
347 \
348 return y; \
349 }
350
351_RVV_FLOAT32_TANH_OP(1, 32)
352_RVV_FLOAT32_TANH_OP(2, 16)
353_RVV_FLOAT32_TANH_OP(4, 8)
354_RVV_FLOAT32_TANH_OP(8, 4)
355
356#define _RVV_FLOAT32_POW_OP(LMUL, MLEN) \
357 static inline vfloat32m##LMUL##_t pow_ps(vfloat32m##LMUL##_t a, vfloat32m##LMUL##_t b, size_t vl) \
358 { \
359 /* pow(x, m) = exp(m * log(x)) */ \
360 return exp_ps(vfmul_vv_f32m##LMUL(b, log_ps(a, vl), vl), vl); \
361 }
362
363_RVV_FLOAT32_POW_OP(1, 32)
364_RVV_FLOAT32_POW_OP(2, 16)
365_RVV_FLOAT32_POW_OP(4, 8)
366_RVV_FLOAT32_POW_OP(8, 4)
367
368#if C906
369#define _RVV_FLOAT32_SIGMOID_OP(LMUL, MLEN) \
370 static inline vfloat32m##LMUL##_t sigmoid_ps(vfloat32m##LMUL##_t _v, size_t vl) \
371 { \
372 _v = vfneg_v_f32m##LMUL(_v, vl); \
373 _v = exp_ps(_v, vl); \
374 _v = vfadd_vf_f32m##LMUL(_v, 1.f, vl); \
375 vfloat32m##LMUL##_t _reciprocal = vfrdiv_vf_f32m##LMUL(_v, 1.f, vl); \
376 _reciprocal = vfmul_vv_f32m##LMUL(vfrsub_vf_f32m##LMUL(vfmul_vv_f32m##LMUL(_v, _reciprocal, vl), 2.f, vl), _reciprocal, vl); \
377 /* _reciprocal = vfmul_vv_f32m##LMUL(vfrsub_vf_f32m##LMUL(vfmul_vv_f32m##LMUL(_v, _reciprocal, vl), 2.f, vl), _reciprocal, vl); */ \
378 return _reciprocal; \
379 }
380#else // C906
381#define _RVV_FLOAT32_SIGMOID_OP(LMUL, MLEN) \
382 static inline vfloat32m##LMUL##_t sigmoid_ps(vfloat32m##LMUL##_t _v, size_t vl) \
383 { \
384 _v = vfneg_v_f32m##LMUL(_v, vl); \
385 _v = exp_ps(_v, vl); \
386 _v = vfadd_vf_f32m##LMUL(_v, 1.f, vl); \
387 vfloat32m##LMUL##_t _reciprocal = vfrec7_v_f32m##LMUL(_v, vl); \
388 _reciprocal = vfmul_vv_f32m##LMUL(vfrsub_vf_f32m##LMUL(vfmul_vv_f32m##LMUL(_v, _reciprocal, vl), 2.f, vl), _reciprocal, vl); \
389 /* _reciprocal = vfmul_vv_f32m##LMUL(vfrsub_vf_f32m##LMUL(vfmul_vv_f32m##LMUL(_v, _reciprocal, vl), 2.f, vl), _reciprocal, vl); */ \
390 return _reciprocal; \
391 }
392#endif // C906
393
394_RVV_FLOAT32_SIGMOID_OP(1, 32)
395_RVV_FLOAT32_SIGMOID_OP(2, 16)
396_RVV_FLOAT32_SIGMOID_OP(4, 8)
397_RVV_FLOAT32_SIGMOID_OP(8, 4)
398
399/*
400 * ====================================================
401 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
402 *
403 * Developed at SunPro, a Sun Microsystems, Inc. business.
404 * Permission to use, copy, modify, and distribute this
405 * software is freely granted, provided that this notice
406 * is preserved.
407 * ====================================================
408 */
409#define c_erfc_erx_f 8.4506291151e-01f /* 0x3f58560b */
410// Coefficients for approximation to erf on [00.84375]
411#define c_erfc_efx 1.2837916613e-01f /* 0x3e0375d4 */
412#define c_erfc_efx8 1.0270333290e+00f /* 0x3f8375d4 */
413
414#define c_erfc_pp0 1.2837916613e-01f /* 0x3e0375d4 */
415#define c_erfc_pp1 -3.2504209876e-01f /* 0xbea66beb */
416#define c_erfc_pp2 -2.8481749818e-02f /* 0xbce9528f */
417#define c_erfc_pp3 -5.7702702470e-03f /* 0xbbbd1489 */
418#define c_erfc_pp4 -2.3763017452e-05f /* 0xb7c756b1 */
419#define c_erfc_qq1 3.9791721106e-01f /* 0x3ecbbbce */
420#define c_erfc_qq2 6.5022252500e-02f /* 0x3d852a63 */
421#define c_erfc_qq3 5.0813062117e-03f /* 0x3ba68116 */
422#define c_erfc_qq4 1.3249473704e-04f /* 0x390aee49 */
423#define c_erfc_qq5 -3.9602282413e-06f /* 0xb684e21a */
424
425// Coefficients for approximation to erf in [0.843751.25]
426#define c_erfc_pa0 -2.3621185683e-03f /* 0xbb1acdc6 */
427#define c_erfc_pa1 4.1485610604e-01f /* 0x3ed46805 */
428#define c_erfc_pa2 -3.7220788002e-01f /* 0xbebe9208 */
429#define c_erfc_pa3 3.1834661961e-01f /* 0x3ea2fe54 */
430#define c_erfc_pa4 -1.1089469492e-01f /* 0xbde31cc2 */
431#define c_erfc_pa5 3.5478305072e-02f /* 0x3d1151b3 */
432#define c_erfc_pa6 -2.1663755178e-03f /* 0xbb0df9c0 */
433#define c_erfc_qa1 1.0642088205e-01f /* 0x3dd9f331 */
434#define c_erfc_qa2 5.4039794207e-01f /* 0x3f0a5785 */
435#define c_erfc_qa3 7.1828655899e-02f /* 0x3d931ae7 */
436#define c_erfc_qa4 1.2617121637e-01f /* 0x3e013307 */
437#define c_erfc_qa5 1.3637083583e-02f /* 0x3c5f6e13 */
438#define c_erfc_qa6 1.1984500103e-02f /* 0x3c445aa3 */
439
440// Coefficients for approximation to erfc in [1.251/0.35]
441#define c_erfc_ra0 -9.8649440333e-03f /* 0xbc21a093 */
442#define c_erfc_ra1 -6.9385856390e-01f /* 0xbf31a0b7 */
443#define c_erfc_ra2 -1.0558626175e+01f /* 0xc128f022 */
444#define c_erfc_ra3 -6.2375331879e+01f /* 0xc2798057 */
445#define c_erfc_ra4 -1.6239666748e+02f /* 0xc322658c */
446#define c_erfc_ra5 -1.8460508728e+02f /* 0xc3389ae7 */
447#define c_erfc_ra6 -8.1287437439e+01f /* 0xc2a2932b */
448#define c_erfc_ra7 -9.8143291473e+00f /* 0xc11d077e */
449#define c_erfc_sa1 1.9651271820e+01f /* 0x419d35ce */
450#define c_erfc_sa2 1.3765776062e+02f /* 0x4309a863 */
451#define c_erfc_sa3 4.3456588745e+02f /* 0x43d9486f */
452#define c_erfc_sa4 6.4538726807e+02f /* 0x442158c9 */
453#define c_erfc_sa5 4.2900814819e+02f /* 0x43d6810b */
454#define c_erfc_sa6 1.0863500214e+02f /* 0x42d9451f */
455#define c_erfc_sa7 6.5702495575e+00f /* 0x40d23f7c */
456#define c_erfc_sa8 -6.0424413532e-02f /* 0xbd777f97 */
457
458// Coefficients for approximation to erfc in [1/.3528]
459
460#define c_erfc_rb0 -9.8649431020e-03f /* 0xbc21a092 */
461#define c_erfc_rb1 -7.9928326607e-01f /* 0xbf4c9dd4 */
462#define c_erfc_rb2 -1.7757955551e+01f /* 0xc18e104b */
463#define c_erfc_rb3 -1.6063638306e+02f /* 0xc320a2ea */
464#define c_erfc_rb4 -6.3756646729e+02f /* 0xc41f6441 */
465#define c_erfc_rb5 -1.0250950928e+03f /* 0xc480230b */
466#define c_erfc_rb6 -4.8351919556e+02f /* 0xc3f1c275 */
467#define c_erfc_sb1 3.0338060379e+01f /* 0x41f2b459 */
468#define c_erfc_sb2 3.2579251099e+02f /* 0x43a2e571 */
469#define c_erfc_sb3 1.5367296143e+03f /* 0x44c01759 */
470#define c_erfc_sb4 3.1998581543e+03f /* 0x4547fdbb */
471#define c_erfc_sb5 2.5530502930e+03f /* 0x451f90ce */
472#define c_erfc_sb6 4.7452853394e+02f /* 0x43ed43a7 */
473#define c_erfc_sb7 -2.2440952301e+01f /* 0xc1b38712 */
474
475#define _RVV_FLOAT32_FMA_HELPER(LMUL) \
476 static inline vfloat32m##LMUL##_t vfmadd_vff_f32m##LMUL(vfloat32m##LMUL##_t a, float b, \
477 float c, size_t vl) \
478 { \
479 vfloat32m##LMUL##_t ret = vfmul_vf_f32m##LMUL(a, b, vl); \
480 ret = vfadd_vf_f32m##LMUL(ret, c, vl); \
481 return ret; \
482 } \
483 \
484 static inline vfloat32m##LMUL##_t vfmadd_vvf_f32m##LMUL(vfloat32m##LMUL##_t a, vfloat32m##LMUL##_t b, \
485 float c, size_t vl) \
486 { \
487 vfloat32m##LMUL##_t ret = vfmul_vv_f32m##LMUL(a, b, vl); \
488 ret = vfadd_vf_f32m##LMUL(ret, c, vl); \
489 return ret; \
490 }
491
492_RVV_FLOAT32_FMA_HELPER(8)
493_RVV_FLOAT32_FMA_HELPER(4)
494_RVV_FLOAT32_FMA_HELPER(2)
495_RVV_FLOAT32_FMA_HELPER(1)
496
497#define _RVV_FLOAT32_ERFC_OP(LMUL, MLEN) \
498 static inline vfloat32m##LMUL##_t erfc_ps(vfloat32m##LMUL##_t x, size_t vl) \
499 { \
500 /* Argument for polys */ \
501 vfloat32m##LMUL##_t absx = vfsgnjx_vv_f32m##LMUL(x, x, vl); \
502 vfloat32m##LMUL##_t x2 = vfmul_vv_f32m##LMUL(x, x, vl); \
503 vfloat32m##LMUL##_t t = vfrdiv_vf_f32m##LMUL(x2, 1.0f, vl); \
504 vfloat32m##LMUL##_t tt = vfsub_vf_f32m##LMUL(absx, 1.0f, vl); \
505 /* absx < 1.25f ? tt:t */ \
506 t = vmerge_vvm_f32m##LMUL(vmfge_vf_f32m##LMUL##_b##MLEN(absx, 1.25f, vl), tt, t, vl); \
507 /* absx < 0.84375f ? x2:t*/ \
508 t = vmerge_vvm_f32m##LMUL(vmfge_vf_f32m##LMUL##_b##MLEN(absx, 0.84375f, vl), x2, t, vl); \
509 \
510 vfloat32m##LMUL##_t u = vfmadd_vvf_f32m##LMUL(t, vfmadd_vvf_f32m##LMUL(t, vfmadd_vvf_f32m##LMUL(t, vfmadd_vvf_f32m##LMUL(t, vfmadd_vvf_f32m##LMUL(t, vfmadd_vff_f32m##LMUL(t, c_erfc_rb6, c_erfc_rb5, vl), c_erfc_rb4, vl), c_erfc_rb3, vl), c_erfc_rb2, vl), c_erfc_rb1, vl), c_erfc_rb0, vl); \
511 vfloat32m##LMUL##_t v = vfmadd_vvf_f32m##LMUL(t, vfmadd_vvf_f32m##LMUL(t, vfmadd_vvf_f32m##LMUL(t, vfmadd_vvf_f32m##LMUL(t, vfmadd_vvf_f32m##LMUL(t, vfmadd_vff_f32m##LMUL(t, c_erfc_sb7, c_erfc_sb6, vl), c_erfc_sb5, vl), c_erfc_sb4, vl), c_erfc_sb3, vl), c_erfc_sb2, vl), c_erfc_sb1, vl); \
512 \
513 vfloat32m##LMUL##_t tu = vfmadd_vvf_f32m##LMUL(t, vfmadd_vvf_f32m##LMUL(t, vfmadd_vvf_f32m##LMUL(t, vfmadd_vvf_f32m##LMUL(t, vfmadd_vvf_f32m##LMUL(t, vfmadd_vvf_f32m##LMUL(t, vfmadd_vff_f32m##LMUL(t, c_erfc_ra7, c_erfc_ra6, vl), c_erfc_ra5, vl), c_erfc_ra4, vl), c_erfc_ra3, vl), c_erfc_ra2, vl), c_erfc_ra1, vl), c_erfc_ra0, vl); \
514 vfloat32m##LMUL##_t tv = vfmadd_vvf_f32m##LMUL(t, vfmadd_vvf_f32m##LMUL(t, vfmadd_vvf_f32m##LMUL(t, vfmadd_vvf_f32m##LMUL(t, vfmadd_vvf_f32m##LMUL(t, vfmadd_vvf_f32m##LMUL(t, vfmadd_vff_f32m##LMUL(t, c_erfc_sa8, c_erfc_sa7, vl), c_erfc_sa6, vl), c_erfc_sa5, vl), c_erfc_sa4, vl), c_erfc_sa3, vl), c_erfc_sa2, vl), c_erfc_sa1, vl); \
515 u = vmerge_vvm_f32m##LMUL(vmfge_vf_f32m##LMUL##_b##MLEN(absx, 2.857143f, vl), tu, u, vl); /* u = absx < 0x1.6db6dap+1f ? tu : u;*/ \
516 v = vmerge_vvm_f32m##LMUL(vmfge_vf_f32m##LMUL##_b##MLEN(absx, 2.857143f, vl), tv, v, vl); /* v = absx < 0x1.6db6dap+1f ? tv : v;*/ \
517 \
518 tu = vfmadd_vvf_f32m##LMUL(t, vfmadd_vvf_f32m##LMUL(t, vfmadd_vvf_f32m##LMUL(t, vfmadd_vvf_f32m##LMUL(t, vfmadd_vvf_f32m##LMUL(t, vfmadd_vff_f32m##LMUL(t, c_erfc_pa6, c_erfc_pa5, vl), c_erfc_pa4, vl), c_erfc_pa3, vl), c_erfc_pa2, vl), c_erfc_pa1, vl), c_erfc_pa0, vl); \
519 tv = vfmadd_vvf_f32m##LMUL(t, vfmadd_vvf_f32m##LMUL(t, vfmadd_vvf_f32m##LMUL(t, vfmadd_vvf_f32m##LMUL(t, vfmadd_vff_f32m##LMUL(t, c_erfc_qa6, c_erfc_qa5, vl), c_erfc_qa4, vl), c_erfc_qa3, vl), c_erfc_qa2, vl), c_erfc_qa1, vl); \
520 u = vmerge_vvm_f32m##LMUL(vmfge_vf_f32m##LMUL##_b##MLEN(absx, 1.25f, vl), tu, u, \
521 vl); /* absx < 1.25f ? tu : u */ \
522 v = vmerge_vvm_f32m##LMUL(vmfge_vf_f32m##LMUL##_b##MLEN(absx, 1.25f, vl), tv, v, \
523 vl); /* absx < 1.25f ? tv : v */ \
524 \
525 tu = vfmadd_vvf_f32m##LMUL(t, vfmadd_vvf_f32m##LMUL(t, vfmadd_vvf_f32m##LMUL(t, vfmadd_vff_f32m##LMUL(t, c_erfc_pp4, c_erfc_pp3, vl), c_erfc_pp2, vl), c_erfc_pp1, vl), c_erfc_pp0, vl); \
526 tv = vfmadd_vvf_f32m##LMUL(t, vfmadd_vvf_f32m##LMUL(t, vfmadd_vvf_f32m##LMUL(t, vfmadd_vff_f32m##LMUL(t, c_erfc_qq5, c_erfc_qq4, vl), c_erfc_qq3, vl), c_erfc_qq2, vl), c_erfc_qq1, vl); \
527 u = vmerge_vvm_f32m##LMUL(vmfge_vf_f32m##LMUL##_b##MLEN(absx, 0.84375f, vl), tu, u, vl); /* absx < 0.84375f ? tu : u */ \
528 v = vmerge_vvm_f32m##LMUL(vmfge_vf_f32m##LMUL##_b##MLEN(absx, 0.84375f, vl), tv, v, vl); /* absx < 0.84375f ? tv : v */ \
529 \
530 v = vfmadd_vvf_f32m##LMUL(t, v, 1.f, vl); \
531 \
532 vfloat32m##LMUL##_t q = vfdiv_vv_f32m##LMUL(u, v, vl); \
533 vfloat32m##LMUL##_t ret = vfmv_v_f_f32m##LMUL(0.f, vl); \
534 \
535 vfloat32m##LMUL##_t z = vreinterpret_v_u32m##LMUL##_f32m##LMUL(vand_vx_u32m##LMUL(vreinterpret_v_f32m##LMUL##_u32m##LMUL(absx), 0xfffff000, vl)); \
536 \
537 vfloat32m##LMUL##_t r = vfmul_vv_f32m##LMUL(exp_ps(vfmadd_vvf_f32m##LMUL(vfneg_v_f32m##LMUL(z, vl), z, -0.5625f, vl), vl), exp_ps(vfmadd_vv_f32m##LMUL(vfsub_vv_f32m##LMUL(z, absx, vl), vfadd_vv_f32m##LMUL(z, absx, vl), q, vl), vl), vl); \
538 r = vfdiv_vv_f32m##LMUL(r, absx, vl); \
539 t = vfrsub_vf_f32m##LMUL(r, 2.f, vl); \
540 r = vmerge_vvm_f32m##LMUL(vmfge_vf_f32m##LMUL##_b##MLEN(x, 0.f, vl), t, r, vl); /* x < 0.f ? t:r */ \
541 ret = vmerge_vvm_f32m##LMUL(vmfge_vf_f32m##LMUL##_b##MLEN(absx, 28.f, vl), r, ret, vl); /* abs < 28.f ? r : ret */ \
542 \
543 r = vfrsub_vf_f32m##LMUL(q, 1.f - c_erfc_erx_f, vl); \
544 t = vfadd_vf_f32m##LMUL(q, 1.f + c_erfc_erx_f, vl); \
545 r = vmerge_vvm_f32m##LMUL(vmfge_vf_f32m##LMUL##_b##MLEN(x, 0.f, vl), t, r, vl); /* x < 0.f ? t:r*/ \
546 ret = vmerge_vvm_f32m##LMUL(vmfge_vf_f32m##LMUL##_b##MLEN(absx, 1.25f, vl), r, ret, vl); /* absx < 1.25f ? r : ret*/ \
547 \
548 r = vfrsub_vf_f32m##LMUL(vfmadd_vv_f32m##LMUL(x, q, vfsub_vf_f32m##LMUL(x, 0.5f, vl), vl), .5, vl); \
549 ret = vmerge_vvm_f32m##LMUL(vmfge_vf_f32m##LMUL##_b##MLEN(absx, 0.84375f, vl), r, ret, vl); /* absx < 0.84375f ? r : ret*/ \
550 \
551 ret = vfmerge_vfm_f32m##LMUL(vmflt_vf_f32m##LMUL##_b##MLEN(x, -6.0f, vl), ret, 2.f, vl); /* x< -6.0f ? 2.0f: ret*/ \
552 \
553 ret = vmerge_vvm_f32m##LMUL(vmfeq_vv_f32m##LMUL##_b##MLEN(x, x, vl), x, ret, vl); /* erfc(NaN) = NaN*/ \
554 \
555 return ret; \
556 }
557
558_RVV_FLOAT32_ERFC_OP(1, 32)
559_RVV_FLOAT32_ERFC_OP(2, 16)
560_RVV_FLOAT32_ERFC_OP(4, 8)
561_RVV_FLOAT32_ERFC_OP(8, 4)
562
563//TODO rvv optimize
564#define _RVV_FLOAT32_ATAN2_OP(LMUL, MLEN) \
565 static inline vfloat32m##LMUL##_t atan2_ps(vfloat32m##LMUL##_t a, vfloat32m##LMUL##_t b, size_t vl) \
566 { \
567 std::vector<float> tmpx(vl); \
568 std::vector<float> tmpy(vl); \
569 vse32_v_f32m##LMUL(tmpx.data(), a, vl); \
570 vse32_v_f32m##LMUL(tmpy.data(), b, vl); \
571 for (size_t i = 0; i < vl; i++) \
572 { \
573 tmpx[i] = atan2(tmpx[i], tmpy[i]); \
574 } \
575 return vle32_v_f32m##LMUL(tmpx.data(), vl); \
576 }
577
578_RVV_FLOAT32_ATAN2_OP(1, 32)
579_RVV_FLOAT32_ATAN2_OP(2, 16)
580_RVV_FLOAT32_ATAN2_OP(4, 8)
581_RVV_FLOAT32_ATAN2_OP(8, 4)
582
583#endif // RVV_MATHFUN_H