| 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_FP16S_H |
| 16 | #define RVV_MATHFUN_FP16S_H |
| 17 | |
| 18 | #include <riscv_vector.h> |
| 19 | |
| 20 | #define c_inv_mant_mask_f16 -31745 // ~0x7c00u |
| 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_FLOAT16_LOG_OP(LMUL, MLEN) \ |
| 35 | static inline vfloat16m##LMUL##_t log_ps(vfloat16m##LMUL##_t x, size_t vl) \ |
| 36 | { \ |
| 37 | x = vfmax_vf_f16m##LMUL(x, 0.f, vl); /* force flush to zero on denormal values */ \ |
| 38 | vbool##MLEN##_t invalid_mask = vmfle_vf_f16m##LMUL##_b##MLEN(x, 0.f, vl); \ |
| 39 | \ |
| 40 | vint16m##LMUL##_t ux = vreinterpret_v_f16m##LMUL##_i16m##LMUL(x); \ |
| 41 | \ |
| 42 | vint16m##LMUL##_t emm0 = vsra_vx_i16m##LMUL(ux, 10, vl); \ |
| 43 | \ |
| 44 | /* keep only the fractional part */ \ |
| 45 | ux = vand_vx_i16m##LMUL(ux, c_inv_mant_mask_f16, vl); \ |
| 46 | ux = vor_vx_i16m##LMUL(ux, 14336 /* reinterpret_cast<short>((__fp16)0.5) */, vl); \ |
| 47 | x = vreinterpret_v_i16m##LMUL##_f16m##LMUL(ux); \ |
| 48 | \ |
| 49 | emm0 = vsub_vx_i16m##LMUL(emm0, 0xf, vl); \ |
| 50 | vfloat16m##LMUL##_t e = vfcvt_f_x_v_f16m##LMUL(emm0, vl); \ |
| 51 | \ |
| 52 | e = vfadd_vf_f16m##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_f16m##LMUL##_b##MLEN(x, c_cephes_SQRTHF, vl); \ |
| 60 | x = vfadd_vv_f16m##LMUL##_m(mask, x, x, x, vl); \ |
| 61 | x = vfsub_vf_f16m##LMUL(x, 1.f, vl); \ |
| 62 | e = vfsub_vf_f16m##LMUL##_m(mask, e, e, 1.f, vl); \ |
| 63 | \ |
| 64 | vfloat16m##LMUL##_t z = vfmul_vv_f16m##LMUL(x, x, vl); \ |
| 65 | \ |
| 66 | vfloat16m##LMUL##_t y = vfmul_vf_f16m##LMUL(x, c_cephes_log_p0, vl); \ |
| 67 | y = vfadd_vf_f16m##LMUL(y, c_cephes_log_p1, vl); \ |
| 68 | y = vfmul_vv_f16m##LMUL(y, x, vl); \ |
| 69 | y = vfadd_vf_f16m##LMUL(y, c_cephes_log_p2, vl); \ |
| 70 | y = vfmul_vv_f16m##LMUL(y, x, vl); \ |
| 71 | y = vfadd_vf_f16m##LMUL(y, c_cephes_log_p3, vl); \ |
| 72 | y = vfmul_vv_f16m##LMUL(y, x, vl); \ |
| 73 | y = vfadd_vf_f16m##LMUL(y, c_cephes_log_p4, vl); \ |
| 74 | y = vfmul_vv_f16m##LMUL(y, x, vl); \ |
| 75 | y = vfadd_vf_f16m##LMUL(y, c_cephes_log_p5, vl); \ |
| 76 | y = vfmul_vv_f16m##LMUL(y, x, vl); \ |
| 77 | y = vfadd_vf_f16m##LMUL(y, c_cephes_log_p6, vl); \ |
| 78 | y = vfmul_vv_f16m##LMUL(y, x, vl); \ |
| 79 | y = vfadd_vf_f16m##LMUL(y, c_cephes_log_p7, vl); \ |
| 80 | y = vfmul_vv_f16m##LMUL(y, x, vl); \ |
| 81 | y = vfadd_vf_f16m##LMUL(y, c_cephes_log_p8, vl); \ |
| 82 | y = vfmul_vv_f16m##LMUL(y, x, vl); \ |
| 83 | \ |
| 84 | y = vfmul_vv_f16m##LMUL(y, z, vl); \ |
| 85 | \ |
| 86 | vfloat16m##LMUL##_t tmp = vfmul_vf_f16m##LMUL(e, c_cephes_log_q1, vl); \ |
| 87 | y = vfadd_vv_f16m##LMUL(y, tmp, vl); \ |
| 88 | \ |
| 89 | tmp = vfmul_vf_f16m##LMUL(z, 0.5f, vl); \ |
| 90 | y = vfsub_vv_f16m##LMUL(y, tmp, vl); \ |
| 91 | \ |
| 92 | tmp = vfmul_vf_f16m##LMUL(e, c_cephes_log_q2, vl); \ |
| 93 | x = vfadd_vv_f16m##LMUL(x, y, vl); \ |
| 94 | x = vfadd_vv_f16m##LMUL(x, tmp, vl); \ |
| 95 | /* negative arg will be NAN */ \ |
| 96 | vuint16m##LMUL##_t xtmp = vreinterpret_v_f16m##LMUL##_u16m##LMUL(x); \ |
| 97 | x = vreinterpret_v_u16m##LMUL##_f16m##LMUL(vor_vx_u16m##LMUL##_m(invalid_mask, xtmp, xtmp, 0xffff, vl)); \ |
| 98 | return x; \ |
| 99 | } |
| 100 | |
| 101 | _RVV_FLOAT16_LOG_OP(1, 16) |
| 102 | _RVV_FLOAT16_LOG_OP(2, 8) |
| 103 | _RVV_FLOAT16_LOG_OP(4, 4) |
| 104 | _RVV_FLOAT16_LOG_OP(8, 2) |
| 105 | |
| 106 | #define c_exp_hi_f16 10.7421875f |
| 107 | #define c_exp_lo_f16 -10.7421875f |
| 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_FLOAT16_EXP_OP(LMUL, MLEN) \ |
| 121 | static inline vfloat16m##LMUL##_t exp_ps(vfloat16m##LMUL##_t x, size_t vl) \ |
| 122 | { \ |
| 123 | vfloat16m##LMUL##_t tmp, fx; \ |
| 124 | \ |
| 125 | x = vfmin_vf_f16m##LMUL(x, c_exp_hi_f16, vl); \ |
| 126 | x = vfmax_vf_f16m##LMUL(x, c_exp_lo_f16, vl); \ |
| 127 | \ |
| 128 | /* express exp(x) as exp(g + n*log(2)) */ \ |
| 129 | fx = vfmacc_vf_f16m##LMUL(vfmv_v_f_f16m##LMUL(0.5f, vl), c_cephes_LOG2EF, x, vl); \ |
| 130 | \ |
| 131 | /* perform a floorf */ \ |
| 132 | tmp = vfcvt_f_x_v_f16m##LMUL(vfcvt_x_f_v_i16m##LMUL(fx, vl), vl); \ |
| 133 | \ |
| 134 | /* if greater, substract 1 */ \ |
| 135 | vbool##MLEN##_t mask = vmfgt_vv_f16m##LMUL##_b##MLEN(tmp, fx, vl); \ |
| 136 | fx = vfsub_vf_f16m##LMUL##_m(mask, tmp, tmp, 1.f, vl); \ |
| 137 | \ |
| 138 | tmp = vfmul_vf_f16m##LMUL(fx, c_cephes_exp_C1, vl); \ |
| 139 | vfloat16m##LMUL##_t z = vfmul_vf_f16m##LMUL(fx, c_cephes_exp_C2, vl); \ |
| 140 | x = vfsub_vv_f16m##LMUL(x, tmp, vl); \ |
| 141 | x = vfsub_vv_f16m##LMUL(x, z, vl); \ |
| 142 | \ |
| 143 | vfloat16m##LMUL##_t y = vfmul_vf_f16m##LMUL(x, c_cephes_exp_p0, vl); \ |
| 144 | z = vfmul_vv_f16m##LMUL(x, x, vl); \ |
| 145 | \ |
| 146 | y = vfadd_vf_f16m##LMUL(y, c_cephes_exp_p1, vl); \ |
| 147 | y = vfmul_vv_f16m##LMUL(y, x, vl); \ |
| 148 | y = vfadd_vf_f16m##LMUL(y, c_cephes_exp_p2, vl); \ |
| 149 | y = vfmul_vv_f16m##LMUL(y, x, vl); \ |
| 150 | y = vfadd_vf_f16m##LMUL(y, c_cephes_exp_p3, vl); \ |
| 151 | y = vfmul_vv_f16m##LMUL(y, x, vl); \ |
| 152 | y = vfadd_vf_f16m##LMUL(y, c_cephes_exp_p4, vl); \ |
| 153 | y = vfmul_vv_f16m##LMUL(y, x, vl); \ |
| 154 | y = vfadd_vf_f16m##LMUL(y, c_cephes_exp_p5, vl); \ |
| 155 | \ |
| 156 | y = vfmul_vv_f16m##LMUL(y, z, vl); \ |
| 157 | y = vfadd_vv_f16m##LMUL(y, x, vl); \ |
| 158 | y = vfadd_vf_f16m##LMUL(y, 1.f, vl); \ |
| 159 | \ |
| 160 | /* build 2^n */ \ |
| 161 | vint16m##LMUL##_t mm = vfcvt_x_f_v_i16m##LMUL(fx, vl); \ |
| 162 | mm = vadd_vx_i16m##LMUL(mm, 0xf, vl); \ |
| 163 | mm = vsll_vx_i16m##LMUL(mm, 10, vl); \ |
| 164 | vfloat16m##LMUL##_t pow2n = vreinterpret_v_i16m##LMUL##_f16m##LMUL(mm); \ |
| 165 | \ |
| 166 | y = vfmul_vv_f16m##LMUL(y, pow2n, vl); \ |
| 167 | return y; \ |
| 168 | } |
| 169 | |
| 170 | _RVV_FLOAT16_EXP_OP(1, 16) |
| 171 | _RVV_FLOAT16_EXP_OP(2, 8) |
| 172 | _RVV_FLOAT16_EXP_OP(4, 4) |
| 173 | _RVV_FLOAT16_EXP_OP(8, 2) |
| 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_FLOAT16_SINCOS_OP(LMUL, MLEN) \ |
| 187 | static inline void sincos_ps(vfloat16m##LMUL##_t x, vfloat16m##LMUL##_t* ysin, vfloat16m##LMUL##_t* ycos, size_t vl) \ |
| 188 | { \ |
| 189 | /* any x */ \ |
| 190 | vfloat16m##LMUL##_t xmm1, xmm2, xmm3, y; \ |
| 191 | \ |
| 192 | vuint16m##LMUL##_t emm2; \ |
| 193 | \ |
| 194 | vbool##MLEN##_t sign_mask_sin, sign_mask_cos; \ |
| 195 | sign_mask_sin = vmflt_vf_f16m##LMUL##_b##MLEN(x, 0.f, vl); \ |
| 196 | x = vfsgnj_vf_f16m##LMUL(x, 1.f, vl); \ |
| 197 | \ |
| 198 | /* scale by 4/Pi */ \ |
| 199 | y = vfmul_vf_f16m##LMUL(x, c_cephes_FOPI, vl); \ |
| 200 | \ |
| 201 | /* store the integer part of y in mm0 */ \ |
| 202 | emm2 = vfcvt_xu_f_v_u16m##LMUL(y, vl); \ |
| 203 | /* j=(j+1) & (~1) (see the cephes sources) */ \ |
| 204 | emm2 = vadd_vx_u16m##LMUL(emm2, 1, vl); \ |
| 205 | emm2 = vand_vx_u16m##LMUL(emm2, ~1, vl); \ |
| 206 | y = vfcvt_f_xu_v_f16m##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_u16m##LMUL##_b##MLEN(vand_vx_u16m##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_f16m##LMUL(y, c_minus_cephes_DP1, vl); \ |
| 218 | xmm2 = vfmul_vf_f16m##LMUL(y, c_minus_cephes_DP2, vl); \ |
| 219 | xmm3 = vfmul_vf_f16m##LMUL(y, c_minus_cephes_DP3, vl); \ |
| 220 | x = vfadd_vv_f16m##LMUL(x, xmm1, vl); \ |
| 221 | x = vfadd_vv_f16m##LMUL(x, xmm2, vl); \ |
| 222 | x = vfadd_vv_f16m##LMUL(x, xmm3, vl); \ |
| 223 | \ |
| 224 | sign_mask_sin = vmxor_mm_b##MLEN(sign_mask_sin, vmsne_vx_u16m##LMUL##_b##MLEN(vand_vx_u16m##LMUL(emm2, 4, vl), 0, vl), vl); \ |
| 225 | sign_mask_cos = vmsne_vx_u16m##LMUL##_b##MLEN(vand_vx_u16m##LMUL(vsub_vx_u16m##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 | vfloat16m##LMUL##_t z = vfmul_vv_f16m##LMUL(x, x, vl); \ |
| 230 | vfloat16m##LMUL##_t y1, y2; \ |
| 231 | \ |
| 232 | y1 = vfmul_vf_f16m##LMUL(z, c_coscof_p0, vl); \ |
| 233 | y2 = vfmul_vf_f16m##LMUL(z, c_sincof_p0, vl); \ |
| 234 | y1 = vfadd_vf_f16m##LMUL(y1, c_coscof_p1, vl); \ |
| 235 | y2 = vfadd_vf_f16m##LMUL(y2, c_sincof_p1, vl); \ |
| 236 | y1 = vfmul_vv_f16m##LMUL(y1, z, vl); \ |
| 237 | y2 = vfmul_vv_f16m##LMUL(y2, z, vl); \ |
| 238 | y1 = vfadd_vf_f16m##LMUL(y1, c_coscof_p2, vl); \ |
| 239 | y2 = vfadd_vf_f16m##LMUL(y2, c_sincof_p2, vl); \ |
| 240 | y1 = vfmul_vv_f16m##LMUL(y1, z, vl); \ |
| 241 | y2 = vfmul_vv_f16m##LMUL(y2, z, vl); \ |
| 242 | y1 = vfmul_vv_f16m##LMUL(y1, z, vl); \ |
| 243 | y2 = vfmul_vv_f16m##LMUL(y2, x, vl); \ |
| 244 | y1 = vfsub_vv_f16m##LMUL(y1, vfmul_vf_f16m##LMUL(z, 0.5f, vl), vl); \ |
| 245 | y2 = vfadd_vv_f16m##LMUL(y2, x, vl); \ |
| 246 | y1 = vfadd_vf_f16m##LMUL(y1, 1.f, vl); \ |
| 247 | \ |
| 248 | /* select the correct result from the two polynoms */ \ |
| 249 | vfloat16m##LMUL##_t ys = vmerge_vvm_f16m##LMUL(poly_mask, y2, y1, vl); \ |
| 250 | vfloat16m##LMUL##_t yc = vmerge_vvm_f16m##LMUL(poly_mask, y1, y2, vl); \ |
| 251 | *ysin = vmerge_vvm_f16m##LMUL(sign_mask_sin, ys, vfneg_v_f16m##LMUL(ys, vl), vl); \ |
| 252 | *ycos = vmerge_vvm_f16m##LMUL(sign_mask_cos, vfneg_v_f16m##LMUL(yc, vl), yc, vl); \ |
| 253 | } |
| 254 | |
| 255 | _RVV_FLOAT16_SINCOS_OP(1, 16) |
| 256 | _RVV_FLOAT16_SINCOS_OP(2, 8) |
| 257 | _RVV_FLOAT16_SINCOS_OP(4, 4) |
| 258 | _RVV_FLOAT16_SINCOS_OP(8, 2) |
| 259 | |
| 260 | #define _RVV_FLOAT16_SIN_OP(LMUL, MLEN) \ |
| 261 | static inline vfloat16m##LMUL##_t sin_ps(vfloat16m##LMUL##_t x, size_t vl) \ |
| 262 | { \ |
| 263 | vfloat16m##LMUL##_t ysin, ycos; \ |
| 264 | sincos_ps(x, &ysin, &ycos, vl); \ |
| 265 | return ysin; \ |
| 266 | } |
| 267 | |
| 268 | _RVV_FLOAT16_SIN_OP(1, 16) |
| 269 | _RVV_FLOAT16_SIN_OP(2, 8) |
| 270 | _RVV_FLOAT16_SIN_OP(4, 4) |
| 271 | _RVV_FLOAT16_SIN_OP(8, 2) |
| 272 | |
| 273 | #define _RVV_FLOAT16_COS_OP(LMUL, MLEN) \ |
| 274 | static inline vfloat16m##LMUL##_t cos_ps(vfloat16m##LMUL##_t x, size_t vl) \ |
| 275 | { \ |
| 276 | vfloat16m##LMUL##_t ysin, ycos; \ |
| 277 | sincos_ps(x, &ysin, &ycos, vl); \ |
| 278 | return ycos; \ |
| 279 | } |
| 280 | |
| 281 | _RVV_FLOAT16_COS_OP(1, 16) |
| 282 | _RVV_FLOAT16_COS_OP(2, 8) |
| 283 | _RVV_FLOAT16_COS_OP(4, 4) |
| 284 | _RVV_FLOAT16_COS_OP(8, 2) |
| 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_FLOAT16_TANH_OP(LMUL, MLEN) \ |
| 303 | static inline vfloat16m##LMUL##_t tanh_ps(vfloat16m##LMUL##_t x, size_t vl) \ |
| 304 | { \ |
| 305 | vfloat16m##LMUL##_t x2 = vfsgnj_vf_f16m##LMUL(x, 1.f, vl); \ |
| 306 | \ |
| 307 | vbool##MLEN##_t tiny_mask = vmfge_vf_f16m##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_f16m##LMUL(x2, c_tanh_hi, vl); \ |
| 312 | \ |
| 313 | /* since the polynomials are odd/even, we need x**2. */ \ |
| 314 | vfloat16m##LMUL##_t z = vfmul_vv_f16m##LMUL(x2, x2, vl); \ |
| 315 | \ |
| 316 | /* evaluate the numerator polynomial y. */ \ |
| 317 | vfloat16m##LMUL##_t y = vfmul_vf_f16m##LMUL(z, c_tanh_alpha_13, vl); \ |
| 318 | y = vfadd_vf_f16m##LMUL(y, c_tanh_alpha_11, vl); \ |
| 319 | y = vfmul_vv_f16m##LMUL(y, z, vl); \ |
| 320 | y = vfadd_vf_f16m##LMUL(y, c_tanh_alpha_9, vl); \ |
| 321 | y = vfmul_vv_f16m##LMUL(y, z, vl); \ |
| 322 | y = vfadd_vf_f16m##LMUL(y, c_tanh_alpha_7, vl); \ |
| 323 | y = vfmul_vv_f16m##LMUL(y, z, vl); \ |
| 324 | y = vfadd_vf_f16m##LMUL(y, c_tanh_alpha_5, vl); \ |
| 325 | y = vfmul_vv_f16m##LMUL(y, z, vl); \ |
| 326 | y = vfadd_vf_f16m##LMUL(y, c_tanh_alpha_3, vl); \ |
| 327 | y = vfmul_vv_f16m##LMUL(y, z, vl); \ |
| 328 | y = vfadd_vf_f16m##LMUL(y, c_tanh_alpha_1, vl); \ |
| 329 | y = vfmul_vv_f16m##LMUL(y, x2, vl); \ |
| 330 | \ |
| 331 | /* evaluate the denominator polynomial w. */ \ |
| 332 | vfloat16m##LMUL##_t w = vfmul_vf_f16m##LMUL(z, c_tanh_beta_6, vl); \ |
| 333 | w = vfadd_vf_f16m##LMUL(w, c_tanh_beta_4, vl); \ |
| 334 | w = vfmul_vv_f16m##LMUL(w, z, vl); \ |
| 335 | w = vfadd_vf_f16m##LMUL(w, c_tanh_beta_2, vl); \ |
| 336 | w = vfmul_vv_f16m##LMUL(w, z, vl); \ |
| 337 | w = vfadd_vf_f16m##LMUL(w, c_tanh_beta_0, vl); \ |
| 338 | \ |
| 339 | /* divide the numerator by the denominator. */ \ |
| 340 | y = vfdiv_vv_f16m##LMUL(y, w, vl); \ |
| 341 | \ |
| 342 | /* reinstate the sign. */ \ |
| 343 | y = vfsgnj_vv_f16m##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_f16m##LMUL(tiny_mask, x, y, vl); \ |
| 347 | \ |
| 348 | return y; \ |
| 349 | } |
| 350 | |
| 351 | _RVV_FLOAT16_TANH_OP(1, 16) |
| 352 | _RVV_FLOAT16_TANH_OP(2, 8) |
| 353 | _RVV_FLOAT16_TANH_OP(4, 4) |
| 354 | _RVV_FLOAT16_TANH_OP(8, 2) |
| 355 | |
| 356 | #define _RVV_FLOAT16_POW_OP(LMUL, MLEN) \ |
| 357 | static inline vfloat16m##LMUL##_t pow_ps(vfloat16m##LMUL##_t a, vfloat16m##LMUL##_t b, size_t vl) \ |
| 358 | { \ |
| 359 | /* pow(x, m) = exp(m * log(x)) */ \ |
| 360 | return exp_ps(vfmul_vv_f16m##LMUL(b, log_ps(a, vl), vl), vl); \ |
| 361 | } |
| 362 | |
| 363 | _RVV_FLOAT16_POW_OP(1, 16) |
| 364 | _RVV_FLOAT16_POW_OP(2, 8) |
| 365 | _RVV_FLOAT16_POW_OP(4, 4) |
| 366 | _RVV_FLOAT16_POW_OP(8, 2) |
| 367 | |
| 368 | #if C906 |
| 369 | #define _RVV_FLOAT16_SIGMOID_OP(LMUL, MLEN) \ |
| 370 | static inline vfloat16m##LMUL##_t sigmoid_ps(vfloat16m##LMUL##_t _v, size_t vl) \ |
| 371 | { \ |
| 372 | _v = vfneg_v_f16m##LMUL(_v, vl); \ |
| 373 | _v = exp_ps(_v, vl); \ |
| 374 | _v = vfadd_vf_f16m##LMUL(_v, 1.f, vl); \ |
| 375 | vfloat16m##LMUL##_t _reciprocal = vfrdiv_vf_f16m##LMUL(_v, 1.f, vl); \ |
| 376 | _reciprocal = vfmul_vv_f16m##LMUL(vfrsub_vf_f16m##LMUL(vfmul_vv_f16m##LMUL(_v, _reciprocal, vl), 2.f, vl), _reciprocal, vl); \ |
| 377 | /* _reciprocal = vfmul_vv_f16m##LMUL(vfrsub_vf_f16m##LMUL(vfmul_vv_f16m##LMUL(_v, _reciprocal, vl), 2.f, vl), _reciprocal, vl); */ \ |
| 378 | return _reciprocal; \ |
| 379 | } |
| 380 | #else // C906 |
| 381 | #define _RVV_FLOAT16_SIGMOID_OP(LMUL, MLEN) \ |
| 382 | static inline vfloat16m##LMUL##_t sigmoid_ps(vfloat16m##LMUL##_t _v, size_t vl) \ |
| 383 | { \ |
| 384 | _v = vfneg_v_f16m##LMUL(_v, vl); \ |
| 385 | _v = exp_ps(_v, vl); \ |
| 386 | _v = vfadd_vf_f16m##LMUL(_v, 1.f, vl); \ |
| 387 | vfloat16m##LMUL##_t _reciprocal = vfrec7_v_f16m##LMUL(_v, vl); \ |
| 388 | _reciprocal = vfmul_vv_f16m##LMUL(vfrsub_vf_f16m##LMUL(vfmul_vv_f16m##LMUL(_v, _reciprocal, vl), 2.f, vl), _reciprocal, vl); \ |
| 389 | /* _reciprocal = vfmul_vv_f16m##LMUL(vfrsub_vf_f16m##LMUL(vfmul_vv_f16m##LMUL(_v, _reciprocal, vl), 2.f, vl), _reciprocal, vl); */ \ |
| 390 | return _reciprocal; \ |
| 391 | } |
| 392 | #endif // C906 |
| 393 | |
| 394 | _RVV_FLOAT16_SIGMOID_OP(1, 16) |
| 395 | _RVV_FLOAT16_SIGMOID_OP(2, 8) |
| 396 | _RVV_FLOAT16_SIGMOID_OP(4, 4) |
| 397 | _RVV_FLOAT16_SIGMOID_OP(8, 2) |
| 398 | |
| 399 | //TODO rvv optimize |
| 400 | #define _RVV_FLOAT16_ATAN2_OP(LMUL, MLEN) \ |
| 401 | static inline vfloat16m##LMUL##_t atan2_ps(vfloat16m##LMUL##_t a, vfloat16m##LMUL##_t b, size_t vl) \ |
| 402 | { \ |
| 403 | std::vector<__fp16> tmpx(vl); \ |
| 404 | std::vector<__fp16> tmpy(vl); \ |
| 405 | vse16_v_f16m##LMUL(tmpx.data(), a, vl); \ |
| 406 | vse16_v_f16m##LMUL(tmpy.data(), b, vl); \ |
| 407 | for (size_t i = 0; i < vl; i++) \ |
| 408 | { \ |
| 409 | tmpx[i] = (__fp16)atan2((float)tmpx[i], (float)tmpy[i]); \ |
| 410 | } \ |
| 411 | return vle16_v_f16m##LMUL(tmpx.data(), vl); \ |
| 412 | } |
| 413 | |
| 414 | _RVV_FLOAT16_ATAN2_OP(1, 32) |
| 415 | _RVV_FLOAT16_ATAN2_OP(2, 16) |
| 416 | _RVV_FLOAT16_ATAN2_OP(4, 8) |
| 417 | _RVV_FLOAT16_ATAN2_OP(8, 4) |
| 418 | |
| 419 | #endif // RVV_MATHFUN_FP16S_H |