| 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 | #include "unaryop_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 | |
| 23 | namespace ncnn { |
| 24 | |
| 25 | UnaryOp_riscv::UnaryOp_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 | |
| 35 | #if __riscv_vector |
| 36 | template<typename Op> |
| 37 | static int unary_op_inplace(Mat& a, const Option& opt) |
| 38 | { |
| 39 | Op op; |
| 40 | |
| 41 | int w = a.w; |
| 42 | int h = a.h; |
| 43 | int d = a.d; |
| 44 | int channels = a.c; |
| 45 | int size = w * h * d; |
| 46 | int elempack = a.elempack; |
| 47 | |
| 48 | #pragma omp parallel for num_threads(opt.num_threads) |
| 49 | for (int q = 0; q < channels; q++) |
| 50 | { |
| 51 | float* ptr = a.channel(q); |
| 52 | |
| 53 | int n = size * elempack; |
| 54 | while (n > 0) |
| 55 | { |
| 56 | size_t vl = vsetvl_e32m8(n); |
| 57 | |
| 58 | vfloat32m8_t _p = vle32_v_f32m8(ptr, vl); |
| 59 | _p = op(_p, vl); |
| 60 | vse32_v_f32m8(ptr, _p, vl); |
| 61 | |
| 62 | ptr += vl; |
| 63 | n -= vl; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | namespace UnaryOp_riscv_functor { |
| 71 | |
| 72 | struct unary_op_abs |
| 73 | { |
| 74 | vfloat32m8_t operator()(const vfloat32m8_t& x, const size_t& vl) const |
| 75 | { |
| 76 | return vfsgnj_vf_f32m8(x, 1.f, vl); |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | struct unary_op_neg |
| 81 | { |
| 82 | vfloat32m8_t operator()(const vfloat32m8_t& x, const size_t& vl) const |
| 83 | { |
| 84 | return vfneg_v_f32m8(x, vl); |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | struct unary_op_floor |
| 89 | { |
| 90 | vfloat32m8_t operator()(const vfloat32m8_t& x, const size_t& vl) const |
| 91 | { |
| 92 | vint32m8_t _xi = vfcvt_x_f_v_i32m8(x, vl); |
| 93 | vbool4_t _mask = vmfgt_vv_f32m8_b4(vfcvt_f_x_v_f32m8(_xi, vl), x, vl); |
| 94 | return vfcvt_f_x_v_f32m8(vsub_vx_i32m8_m(_mask, _xi, _xi, 1, vl), vl); |
| 95 | } |
| 96 | }; |
| 97 | |
| 98 | struct unary_op_ceil |
| 99 | { |
| 100 | vfloat32m8_t operator()(const vfloat32m8_t& x, const size_t& vl) const |
| 101 | { |
| 102 | vint32m8_t _xi = vfcvt_x_f_v_i32m8(x, vl); |
| 103 | vbool4_t _mask = vmflt_vv_f32m8_b4(vfcvt_f_x_v_f32m8(_xi, vl), x, vl); |
| 104 | return vfcvt_f_x_v_f32m8(vadd_vx_i32m8_m(_mask, _xi, _xi, 1, vl), vl); |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | struct unary_op_square |
| 109 | { |
| 110 | vfloat32m8_t operator()(const vfloat32m8_t& x, const size_t& vl) const |
| 111 | { |
| 112 | return vfmul_vv_f32m8(x, x, vl); |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | struct unary_op_sqrt |
| 117 | { |
| 118 | vfloat32m8_t operator()(const vfloat32m8_t& x, const size_t& vl) const |
| 119 | { |
| 120 | return vfsqrt_v_f32m8(x, vl); |
| 121 | } |
| 122 | }; |
| 123 | |
| 124 | struct unary_op_rsqrt |
| 125 | { |
| 126 | vfloat32m8_t operator()(const vfloat32m8_t& x, const size_t& vl) const |
| 127 | { |
| 128 | #if C906 |
| 129 | vfloat32m8_t _reciprocal = vfrdiv_vf_f32m8(vfsqrt_v_f32m8(x, vl), 1.f, vl); |
| 130 | #else |
| 131 | vfloat32m8_t _reciprocal = vfrsqrt7_v_f32m8(x, vl); |
| 132 | _reciprocal = vfmul_vv_f32m8(vfrsub_vf_f32m8(vfmul_vv_f32m8(vfmul_vf_f32m8(x, 0.5f, vl), vfmul_vv_f32m8(_reciprocal, _reciprocal, vl), vl), 1.5f, vl), _reciprocal, vl); |
| 133 | // _reciprocal = vfmul_vv_f32m8(vfrsub_vf_f32m8(vfmul_vv_f32m8(vfmul_vf_f32m8(x, 0.5f, vl), vfmul_vv_f32m8(_reciprocal, _reciprocal, vl), vl), 1.5f, vl), _reciprocal, vl); |
| 134 | #endif |
| 135 | return _reciprocal; |
| 136 | } |
| 137 | }; |
| 138 | |
| 139 | struct unary_op_exp |
| 140 | { |
| 141 | vfloat32m8_t operator()(const vfloat32m8_t& x, const size_t& vl) const |
| 142 | { |
| 143 | return exp_ps(x, vl); |
| 144 | } |
| 145 | }; |
| 146 | |
| 147 | struct unary_op_log |
| 148 | { |
| 149 | vfloat32m8_t operator()(const vfloat32m8_t& x, const size_t& vl) const |
| 150 | { |
| 151 | return log_ps(x, vl); |
| 152 | } |
| 153 | }; |
| 154 | |
| 155 | struct unary_op_sin |
| 156 | { |
| 157 | vfloat32m8_t operator()(const vfloat32m8_t& x, const size_t& vl) const |
| 158 | { |
| 159 | return sin_ps(x, vl); |
| 160 | } |
| 161 | }; |
| 162 | |
| 163 | struct unary_op_cos |
| 164 | { |
| 165 | vfloat32m8_t operator()(const vfloat32m8_t& x, const size_t& vl) const |
| 166 | { |
| 167 | return cos_ps(x, vl); |
| 168 | } |
| 169 | }; |
| 170 | |
| 171 | struct unary_op_tan |
| 172 | { |
| 173 | vfloat32m8_t operator()(const vfloat32m8_t& x, const size_t& vl) const |
| 174 | { |
| 175 | // TODO rvv optimize |
| 176 | std::vector<float> tmp(vl); |
| 177 | vse32_v_f32m8(tmp.data(), x, vl); |
| 178 | for (size_t i = 0; i < vl; i++) |
| 179 | { |
| 180 | tmp[i] = tanf(tmp[i]); |
| 181 | } |
| 182 | return vle32_v_f32m8(tmp.data(), vl); |
| 183 | } |
| 184 | }; |
| 185 | |
| 186 | struct unary_op_asin |
| 187 | { |
| 188 | vfloat32m8_t operator()(const vfloat32m8_t& x, const size_t& vl) const |
| 189 | { |
| 190 | // TODO rvv optimize |
| 191 | std::vector<float> tmp(vl); |
| 192 | vse32_v_f32m8(tmp.data(), x, vl); |
| 193 | for (size_t i = 0; i < vl; i++) |
| 194 | { |
| 195 | tmp[i] = asinf(tmp[i]); |
| 196 | } |
| 197 | return vle32_v_f32m8(tmp.data(), vl); |
| 198 | } |
| 199 | }; |
| 200 | |
| 201 | struct unary_op_acos |
| 202 | { |
| 203 | vfloat32m8_t operator()(const vfloat32m8_t& x, const size_t& vl) const |
| 204 | { |
| 205 | // TODO rvv optimize |
| 206 | std::vector<float> tmp(vl); |
| 207 | vse32_v_f32m8(tmp.data(), x, vl); |
| 208 | for (size_t i = 0; i < vl; i++) |
| 209 | { |
| 210 | tmp[i] = acosf(tmp[i]); |
| 211 | } |
| 212 | return vle32_v_f32m8(tmp.data(), vl); |
| 213 | } |
| 214 | }; |
| 215 | |
| 216 | struct unary_op_atan |
| 217 | { |
| 218 | vfloat32m8_t operator()(const vfloat32m8_t& x, const size_t& vl) const |
| 219 | { |
| 220 | // TODO rvv optimize |
| 221 | std::vector<float> tmp(vl); |
| 222 | vse32_v_f32m8(tmp.data(), x, vl); |
| 223 | for (size_t i = 0; i < vl; i++) |
| 224 | { |
| 225 | tmp[i] = atanf(tmp[i]); |
| 226 | } |
| 227 | return vle32_v_f32m8(tmp.data(), vl); |
| 228 | } |
| 229 | }; |
| 230 | |
| 231 | struct unary_op_reciprocal |
| 232 | { |
| 233 | vfloat32m8_t operator()(const vfloat32m8_t& x, const size_t& vl) const |
| 234 | { |
| 235 | #if C906 |
| 236 | vfloat32m8_t _reciprocal = vfrdiv_vf_f32m8(x, 1.f, vl); |
| 237 | #else |
| 238 | vfloat32m8_t _reciprocal = vfrec7_v_f32m8(x, vl); |
| 239 | _reciprocal = vfmul_vv_f32m8(vfrsub_vf_f32m8(vfmul_vv_f32m8(x, _reciprocal, vl), 2.f, vl), _reciprocal, vl); |
| 240 | // _reciprocal = vfmul_vv_f32m8(vfrsub_vf_f32m8(vfmul_vv_f32m8(x, _reciprocal, vl), 2.f, vl), _reciprocal, vl); |
| 241 | #endif |
| 242 | return _reciprocal; |
| 243 | } |
| 244 | }; |
| 245 | |
| 246 | struct unary_op_tanh |
| 247 | { |
| 248 | vfloat32m8_t operator()(const vfloat32m8_t& x, const size_t& vl) const |
| 249 | { |
| 250 | return tanh_ps(x, vl); |
| 251 | } |
| 252 | }; |
| 253 | |
| 254 | struct unary_op_log10 |
| 255 | { |
| 256 | vfloat32m8_t operator()(const vfloat32m8_t& x, const size_t& vl) const |
| 257 | { |
| 258 | return vfmul_vf_f32m8(log_ps(x, vl), 0.434294481903, vl); |
| 259 | } |
| 260 | }; |
| 261 | |
| 262 | struct unary_op_round |
| 263 | { |
| 264 | vfloat32m8_t operator()(const vfloat32m8_t& x, const size_t& vl) const |
| 265 | { |
| 266 | return vfcvt_f_x_v_f32m8(vfcvt_x_f_v_i32m8(x, vl), vl); |
| 267 | } |
| 268 | }; |
| 269 | |
| 270 | struct unary_op_trunc |
| 271 | { |
| 272 | vfloat32m8_t operator()(const vfloat32m8_t& x, const size_t& vl) const |
| 273 | { |
| 274 | #if C906 |
| 275 | // simulate trunc with floor positives and ceil negative |
| 276 | // xi = round(x) |
| 277 | // floorx = xi - (xi > x) |
| 278 | // ceilx = xi + (xi < x) |
| 279 | // truncx = x >= 0 ? floorx : ceilx |
| 280 | vint32m8_t _xi = vfcvt_x_f_v_i32m8(x, vl); |
| 281 | vfloat32m8_t _xf = vfcvt_f_x_v_f32m8(_xi, vl); |
| 282 | vbool4_t _floormask = vmfgt_vv_f32m8_b4(_xf, x, vl); |
| 283 | vint32m8_t _floorx = vsub_vx_i32m8_m(_floormask, _xi, _xi, 1, vl); |
| 284 | vbool4_t _ceilmask = vmflt_vv_f32m8_b4(_xf, x, vl); |
| 285 | vint32m8_t _ceilx = vadd_vx_i32m8_m(_ceilmask, _xi, _xi, 1, vl); |
| 286 | vbool4_t _negative = vmflt_vf_f32m8_b4(x, 0.f, vl); |
| 287 | return vfcvt_f_x_v_f32m8(vmerge_vvm_i32m8(_negative, _floorx, _ceilx, vl), vl); |
| 288 | #else |
| 289 | return vfcvt_f_x_v_f32m8(vfcvt_rtz_x_f_v_i32m8(x, vl), vl); |
| 290 | #endif |
| 291 | } |
| 292 | }; |
| 293 | |
| 294 | } // namespace UnaryOp_riscv_functor |
| 295 | #endif // __riscv_vector |
| 296 | |
| 297 | int UnaryOp_riscv::forward_inplace(Mat& bottom_top_blob, const Option& opt) const |
| 298 | { |
| 299 | int elembits = bottom_top_blob.elembits(); |
| 300 | |
| 301 | #if __riscv_vector && __riscv_zfh |
| 302 | if (opt.use_fp16_storage && elembits == 16) |
| 303 | return forward_inplace_fp16s(bottom_top_blob, opt); |
| 304 | #endif |
| 305 | |
| 306 | #if __riscv_vector |
| 307 | using namespace UnaryOp_riscv_functor; |
| 308 | |
| 309 | if (op_type == Operation_ABS) |
| 310 | return unary_op_inplace<unary_op_abs>(bottom_top_blob, opt); |
| 311 | |
| 312 | if (op_type == Operation_NEG) |
| 313 | return unary_op_inplace<unary_op_neg>(bottom_top_blob, opt); |
| 314 | |
| 315 | if (op_type == Operation_FLOOR) |
| 316 | return unary_op_inplace<unary_op_floor>(bottom_top_blob, opt); |
| 317 | |
| 318 | if (op_type == Operation_CEIL) |
| 319 | return unary_op_inplace<unary_op_ceil>(bottom_top_blob, opt); |
| 320 | |
| 321 | if (op_type == Operation_SQUARE) |
| 322 | return unary_op_inplace<unary_op_square>(bottom_top_blob, opt); |
| 323 | |
| 324 | if (op_type == Operation_SQRT) |
| 325 | return unary_op_inplace<unary_op_sqrt>(bottom_top_blob, opt); |
| 326 | |
| 327 | if (op_type == Operation_RSQRT) |
| 328 | return unary_op_inplace<unary_op_rsqrt>(bottom_top_blob, opt); |
| 329 | |
| 330 | if (op_type == Operation_EXP) |
| 331 | return unary_op_inplace<unary_op_exp>(bottom_top_blob, opt); |
| 332 | |
| 333 | if (op_type == Operation_LOG) |
| 334 | return unary_op_inplace<unary_op_log>(bottom_top_blob, opt); |
| 335 | |
| 336 | if (op_type == Operation_SIN) |
| 337 | return unary_op_inplace<unary_op_sin>(bottom_top_blob, opt); |
| 338 | |
| 339 | if (op_type == Operation_COS) |
| 340 | return unary_op_inplace<unary_op_cos>(bottom_top_blob, opt); |
| 341 | |
| 342 | if (op_type == Operation_TAN) |
| 343 | return unary_op_inplace<unary_op_tan>(bottom_top_blob, opt); |
| 344 | |
| 345 | if (op_type == Operation_ASIN) |
| 346 | return unary_op_inplace<unary_op_asin>(bottom_top_blob, opt); |
| 347 | |
| 348 | if (op_type == Operation_ACOS) |
| 349 | return unary_op_inplace<unary_op_acos>(bottom_top_blob, opt); |
| 350 | |
| 351 | if (op_type == Operation_ATAN) |
| 352 | return unary_op_inplace<unary_op_atan>(bottom_top_blob, opt); |
| 353 | |
| 354 | if (op_type == Operation_RECIPROCAL) |
| 355 | return unary_op_inplace<unary_op_reciprocal>(bottom_top_blob, opt); |
| 356 | |
| 357 | if (op_type == Operation_TANH) |
| 358 | return unary_op_inplace<unary_op_tanh>(bottom_top_blob, opt); |
| 359 | |
| 360 | if (op_type == Operation_LOG10) |
| 361 | return unary_op_inplace<unary_op_log10>(bottom_top_blob, opt); |
| 362 | |
| 363 | if (op_type == Operation_ROUND) |
| 364 | return unary_op_inplace<unary_op_round>(bottom_top_blob, opt); |
| 365 | |
| 366 | if (op_type == Operation_TRUNC) |
| 367 | return unary_op_inplace<unary_op_trunc>(bottom_top_blob, opt); |
| 368 | |
| 369 | return 0; |
| 370 | #else // __riscv_vector |
| 371 | return UnaryOp::forward_inplace(bottom_top_blob, opt); |
| 372 | #endif // __riscv_vector |
| 373 | } |
| 374 | |
| 375 | #if __riscv_vector && __riscv_zfh |
| 376 | template<typename Op> |
| 377 | static int unary_op_inplace_fp16s(Mat& a, const Option& opt) |
| 378 | { |
| 379 | Op op; |
| 380 | |
| 381 | int w = a.w; |
| 382 | int h = a.h; |
| 383 | int d = a.d; |
| 384 | int channels = a.c; |
| 385 | int size = w * h * d; |
| 386 | int elempack = a.elempack; |
| 387 | |
| 388 | #pragma omp parallel for num_threads(opt.num_threads) |
| 389 | for (int q = 0; q < channels; q++) |
| 390 | { |
| 391 | __fp16* ptr = a.channel(q); |
| 392 | |
| 393 | int n = size * elempack; |
| 394 | while (n > 0) |
| 395 | { |
| 396 | size_t vl = vsetvl_e16m8(n); |
| 397 | |
| 398 | vfloat16m8_t _p = vle16_v_f16m8(ptr, vl); |
| 399 | _p = op(_p, vl); |
| 400 | vse16_v_f16m8(ptr, _p, vl); |
| 401 | |
| 402 | ptr += vl; |
| 403 | n -= vl; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | namespace UnaryOp_riscv_functor { |
| 411 | |
| 412 | struct unary_op_abs_fp16s |
| 413 | { |
| 414 | vfloat16m8_t operator()(const vfloat16m8_t& x, const size_t& vl) const |
| 415 | { |
| 416 | return vfsgnj_vf_f16m8(x, 1.f, vl); |
| 417 | } |
| 418 | }; |
| 419 | |
| 420 | struct unary_op_neg_fp16s |
| 421 | { |
| 422 | vfloat16m8_t operator()(const vfloat16m8_t& x, const size_t& vl) const |
| 423 | { |
| 424 | return vfneg_v_f16m8(x, vl); |
| 425 | } |
| 426 | }; |
| 427 | |
| 428 | struct unary_op_floor_fp16s |
| 429 | { |
| 430 | vfloat16m8_t operator()(const vfloat16m8_t& x, const size_t& vl) const |
| 431 | { |
| 432 | vint16m8_t _xi = vfcvt_x_f_v_i16m8(x, vl); |
| 433 | vbool2_t _mask = vmfgt_vv_f16m8_b2(vfcvt_f_x_v_f16m8(_xi, vl), x, vl); |
| 434 | return vfcvt_f_x_v_f16m8(vsub_vx_i16m8_m(_mask, _xi, _xi, 1, vl), vl); |
| 435 | } |
| 436 | }; |
| 437 | |
| 438 | struct unary_op_ceil_fp16s |
| 439 | { |
| 440 | vfloat16m8_t operator()(const vfloat16m8_t& x, const size_t& vl) const |
| 441 | { |
| 442 | vint16m8_t _xi = vfcvt_x_f_v_i16m8(x, vl); |
| 443 | vbool2_t _mask = vmflt_vv_f16m8_b2(vfcvt_f_x_v_f16m8(_xi, vl), x, vl); |
| 444 | return vfcvt_f_x_v_f16m8(vadd_vx_i16m8_m(_mask, _xi, _xi, 1, vl), vl); |
| 445 | } |
| 446 | }; |
| 447 | |
| 448 | struct unary_op_square_fp16s |
| 449 | { |
| 450 | vfloat16m8_t operator()(const vfloat16m8_t& x, const size_t& vl) const |
| 451 | { |
| 452 | return vfmul_vv_f16m8(x, x, vl); |
| 453 | } |
| 454 | }; |
| 455 | |
| 456 | struct unary_op_sqrt_fp16s |
| 457 | { |
| 458 | vfloat16m8_t operator()(const vfloat16m8_t& x, const size_t& vl) const |
| 459 | { |
| 460 | return vfsqrt_v_f16m8(x, vl); |
| 461 | } |
| 462 | }; |
| 463 | |
| 464 | struct unary_op_rsqrt_fp16s |
| 465 | { |
| 466 | vfloat16m8_t operator()(const vfloat16m8_t& x, const size_t& vl) const |
| 467 | { |
| 468 | #if C906 |
| 469 | vfloat16m8_t _reciprocal = vfrdiv_vf_f16m8(vfsqrt_v_f16m8(x, vl), 1.f, vl); |
| 470 | #else |
| 471 | vfloat16m8_t _reciprocal = vfrsqrt7_v_f16m8(x, vl); |
| 472 | _reciprocal = vfmul_vv_f16m8(vfrsub_vf_f16m8(vfmul_vv_f16m8(vfmul_vf_f16m8(x, 0.5f, vl), vfmul_vv_f16m8(_reciprocal, _reciprocal, vl), vl), 1.5f, vl), _reciprocal, vl); |
| 473 | // _reciprocal = vfmul_vv_f16m8(vfrsub_vf_f16m8(vfmul_vv_f16m8(vfmul_vf_f16m8(x, 0.5f, vl), vfmul_vv_f16m8(_reciprocal, _reciprocal, vl), vl), 1.5f, vl), _reciprocal, vl); |
| 474 | #endif |
| 475 | return _reciprocal; |
| 476 | } |
| 477 | }; |
| 478 | |
| 479 | struct unary_op_exp_fp16s |
| 480 | { |
| 481 | vfloat16m8_t operator()(const vfloat16m8_t& x, const size_t& vl) const |
| 482 | { |
| 483 | return exp_ps(x, vl); |
| 484 | } |
| 485 | }; |
| 486 | |
| 487 | struct unary_op_log_fp16s |
| 488 | { |
| 489 | vfloat16m8_t operator()(const vfloat16m8_t& x, const size_t& vl) const |
| 490 | { |
| 491 | return log_ps(x, vl); |
| 492 | } |
| 493 | }; |
| 494 | |
| 495 | struct unary_op_sin_fp16s |
| 496 | { |
| 497 | vfloat16m8_t operator()(const vfloat16m8_t& x, const size_t& vl) const |
| 498 | { |
| 499 | return sin_ps(x, vl); |
| 500 | } |
| 501 | }; |
| 502 | |
| 503 | struct unary_op_cos_fp16s |
| 504 | { |
| 505 | vfloat16m8_t operator()(const vfloat16m8_t& x, const size_t& vl) const |
| 506 | { |
| 507 | return cos_ps(x, vl); |
| 508 | } |
| 509 | }; |
| 510 | |
| 511 | struct unary_op_tan_fp16s |
| 512 | { |
| 513 | vfloat16m8_t operator()(const vfloat16m8_t& x, const size_t& vl) const |
| 514 | { |
| 515 | // TODO rvv optimize |
| 516 | std::vector<__fp16> tmp(vl); |
| 517 | vse16_v_f16m8(tmp.data(), x, vl); |
| 518 | for (size_t i = 0; i < vl; i++) |
| 519 | { |
| 520 | tmp[i] = tanf((float)tmp[i]); |
| 521 | } |
| 522 | return vle16_v_f16m8(tmp.data(), vl); |
| 523 | } |
| 524 | }; |
| 525 | |
| 526 | struct unary_op_asin_fp16s |
| 527 | { |
| 528 | vfloat16m8_t operator()(const vfloat16m8_t& x, const size_t& vl) const |
| 529 | { |
| 530 | // TODO rvv optimize |
| 531 | std::vector<__fp16> tmp(vl); |
| 532 | vse16_v_f16m8(tmp.data(), x, vl); |
| 533 | for (size_t i = 0; i < vl; i++) |
| 534 | { |
| 535 | tmp[i] = asinf((float)tmp[i]); |
| 536 | } |
| 537 | return vle16_v_f16m8(tmp.data(), vl); |
| 538 | } |
| 539 | }; |
| 540 | |
| 541 | struct unary_op_acos_fp16s |
| 542 | { |
| 543 | vfloat16m8_t operator()(const vfloat16m8_t& x, const size_t& vl) const |
| 544 | { |
| 545 | // TODO rvv optimize |
| 546 | std::vector<__fp16> tmp(vl); |
| 547 | vse16_v_f16m8(tmp.data(), x, vl); |
| 548 | for (size_t i = 0; i < vl; i++) |
| 549 | { |
| 550 | tmp[i] = acosf((float)tmp[i]); |
| 551 | } |
| 552 | return vle16_v_f16m8(tmp.data(), vl); |
| 553 | } |
| 554 | }; |
| 555 | |
| 556 | struct unary_op_atan_fp16s |
| 557 | { |
| 558 | vfloat16m8_t operator()(const vfloat16m8_t& x, const size_t& vl) const |
| 559 | { |
| 560 | // TODO rvv optimize |
| 561 | std::vector<__fp16> tmp(vl); |
| 562 | vse16_v_f16m8(tmp.data(), x, vl); |
| 563 | for (size_t i = 0; i < vl; i++) |
| 564 | { |
| 565 | tmp[i] = atanf((float)tmp[i]); |
| 566 | } |
| 567 | return vle16_v_f16m8(tmp.data(), vl); |
| 568 | } |
| 569 | }; |
| 570 | |
| 571 | struct unary_op_reciprocal_fp16s |
| 572 | { |
| 573 | vfloat16m8_t operator()(const vfloat16m8_t& x, const size_t& vl) const |
| 574 | { |
| 575 | #if C906 |
| 576 | vfloat16m8_t _reciprocal = vfrdiv_vf_f16m8(x, 1.f, vl); |
| 577 | #else |
| 578 | vfloat16m8_t _reciprocal = vfrec7_v_f16m8(x, vl); |
| 579 | _reciprocal = vfmul_vv_f16m8(vfrsub_vf_f16m8(vfmul_vv_f16m8(x, _reciprocal, vl), 2.f, vl), _reciprocal, vl); |
| 580 | // _reciprocal = vfmul_vv_f16m8(vfrsub_vf_f16m8(vfmul_vv_f16m8(x, _reciprocal, vl), 2.f, vl), _reciprocal, vl); |
| 581 | #endif |
| 582 | return _reciprocal; |
| 583 | } |
| 584 | }; |
| 585 | |
| 586 | struct unary_op_tanh_fp16s |
| 587 | { |
| 588 | vfloat16m8_t operator()(const vfloat16m8_t& x, const size_t& vl) const |
| 589 | { |
| 590 | return tanh_ps(x, vl); |
| 591 | } |
| 592 | }; |
| 593 | |
| 594 | struct unary_op_log10_fp16s |
| 595 | { |
| 596 | vfloat16m8_t operator()(const vfloat16m8_t& x, const size_t& vl) const |
| 597 | { |
| 598 | return vfmul_vf_f16m8(log_ps(x, vl), 0.434294481903, vl); |
| 599 | } |
| 600 | }; |
| 601 | |
| 602 | struct unary_op_round_fp16s |
| 603 | { |
| 604 | vfloat16m8_t operator()(const vfloat16m8_t& x, const size_t& vl) const |
| 605 | { |
| 606 | return vfcvt_f_x_v_f16m8(vfcvt_x_f_v_i16m8(x, vl), vl); |
| 607 | } |
| 608 | }; |
| 609 | |
| 610 | struct unary_op_trunc_fp16s |
| 611 | { |
| 612 | vfloat16m8_t operator()(const vfloat16m8_t& x, const size_t& vl) const |
| 613 | { |
| 614 | #if C906 |
| 615 | // simulate trunc with floor positives and ceil negative |
| 616 | // xi = round(x) |
| 617 | // floorx = xi - (xi > x) |
| 618 | // ceilx = xi + (xi < x) |
| 619 | // truncx = x >= 0 ? floorx : ceilx |
| 620 | vint16m8_t _xi = vfcvt_x_f_v_i16m8(x, vl); |
| 621 | vfloat16m8_t _xf = vfcvt_f_x_v_f16m8(_xi, vl); |
| 622 | vbool2_t _floormask = vmfgt_vv_f16m8_b2(_xf, x, vl); |
| 623 | vint16m8_t _floorx = vsub_vx_i16m8_m(_floormask, _xi, _xi, 1, vl); |
| 624 | vbool2_t _ceilmask = vmflt_vv_f16m8_b2(_xf, x, vl); |
| 625 | vint16m8_t _ceilx = vadd_vx_i16m8_m(_ceilmask, _xi, _xi, 1, vl); |
| 626 | vbool2_t _negative = vmflt_vf_f16m8_b2(x, 0.f, vl); |
| 627 | return vfcvt_f_x_v_f16m8(vmerge_vvm_i16m8(_negative, _floorx, _ceilx, vl), vl); |
| 628 | #else |
| 629 | return vfcvt_f_x_v_f16m8(vfcvt_rtz_x_f_v_i16m8(x, vl), vl); |
| 630 | #endif |
| 631 | } |
| 632 | }; |
| 633 | |
| 634 | } // namespace UnaryOp_riscv_functor |
| 635 | |
| 636 | int UnaryOp_riscv::forward_inplace_fp16s(Mat& bottom_top_blob, const Option& opt) const |
| 637 | { |
| 638 | using namespace UnaryOp_riscv_functor; |
| 639 | |
| 640 | if (op_type == Operation_ABS) |
| 641 | return unary_op_inplace_fp16s<unary_op_abs_fp16s>(bottom_top_blob, opt); |
| 642 | |
| 643 | if (op_type == Operation_NEG) |
| 644 | return unary_op_inplace_fp16s<unary_op_neg_fp16s>(bottom_top_blob, opt); |
| 645 | |
| 646 | if (op_type == Operation_FLOOR) |
| 647 | return unary_op_inplace_fp16s<unary_op_floor_fp16s>(bottom_top_blob, opt); |
| 648 | |
| 649 | if (op_type == Operation_CEIL) |
| 650 | return unary_op_inplace_fp16s<unary_op_ceil_fp16s>(bottom_top_blob, opt); |
| 651 | |
| 652 | if (op_type == Operation_SQUARE) |
| 653 | return unary_op_inplace_fp16s<unary_op_square_fp16s>(bottom_top_blob, opt); |
| 654 | |
| 655 | if (op_type == Operation_SQRT) |
| 656 | return unary_op_inplace_fp16s<unary_op_sqrt_fp16s>(bottom_top_blob, opt); |
| 657 | |
| 658 | if (op_type == Operation_RSQRT) |
| 659 | return unary_op_inplace_fp16s<unary_op_rsqrt_fp16s>(bottom_top_blob, opt); |
| 660 | |
| 661 | if (op_type == Operation_EXP) |
| 662 | return unary_op_inplace_fp16s<unary_op_exp_fp16s>(bottom_top_blob, opt); |
| 663 | |
| 664 | if (op_type == Operation_LOG) |
| 665 | return unary_op_inplace_fp16s<unary_op_log_fp16s>(bottom_top_blob, opt); |
| 666 | |
| 667 | if (op_type == Operation_SIN) |
| 668 | return unary_op_inplace_fp16s<unary_op_sin_fp16s>(bottom_top_blob, opt); |
| 669 | |
| 670 | if (op_type == Operation_COS) |
| 671 | return unary_op_inplace_fp16s<unary_op_cos_fp16s>(bottom_top_blob, opt); |
| 672 | |
| 673 | if (op_type == Operation_TAN) |
| 674 | return unary_op_inplace_fp16s<unary_op_tan_fp16s>(bottom_top_blob, opt); |
| 675 | |
| 676 | if (op_type == Operation_ASIN) |
| 677 | return unary_op_inplace_fp16s<unary_op_asin_fp16s>(bottom_top_blob, opt); |
| 678 | |
| 679 | if (op_type == Operation_ACOS) |
| 680 | return unary_op_inplace_fp16s<unary_op_acos_fp16s>(bottom_top_blob, opt); |
| 681 | |
| 682 | if (op_type == Operation_ATAN) |
| 683 | return unary_op_inplace_fp16s<unary_op_atan_fp16s>(bottom_top_blob, opt); |
| 684 | |
| 685 | if (op_type == Operation_RECIPROCAL) |
| 686 | return unary_op_inplace_fp16s<unary_op_reciprocal_fp16s>(bottom_top_blob, opt); |
| 687 | |
| 688 | if (op_type == Operation_TANH) |
| 689 | return unary_op_inplace_fp16s<unary_op_tanh_fp16s>(bottom_top_blob, opt); |
| 690 | |
| 691 | if (op_type == Operation_LOG10) |
| 692 | return unary_op_inplace_fp16s<unary_op_log10_fp16s>(bottom_top_blob, opt); |
| 693 | |
| 694 | if (op_type == Operation_ROUND) |
| 695 | return unary_op_inplace_fp16s<unary_op_round_fp16s>(bottom_top_blob, opt); |
| 696 | |
| 697 | if (op_type == Operation_TRUNC) |
| 698 | return unary_op_inplace_fp16s<unary_op_trunc_fp16s>(bottom_top_blob, opt); |
| 699 | |
| 700 | return 0; |
| 701 | } |
| 702 | #endif // __riscv_vector && __riscv_zfh |
| 703 | |
| 704 | } // namespace ncnn |