| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2019 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_arm.h" |
| 16 | |
| 17 | // #include <fenv.h> |
| 18 | #include <float.h> |
| 19 | |
| 20 | #if __ARM_NEON |
| 21 | #include <arm_neon.h> |
| 22 | #include "neon_mathfun.h" |
| 23 | #endif // __ARM_NEON |
| 24 | |
| 25 | #include "arm_usability.h" |
| 26 | |
| 27 | #include "cpu.h" |
| 28 | |
| 29 | namespace ncnn { |
| 30 | |
| 31 | UnaryOp_arm::UnaryOp_arm() |
| 32 | { |
| 33 | #if __ARM_NEON |
| 34 | support_packing = true; |
| 35 | #if NCNN_ARM82 |
| 36 | support_fp16_storage = cpu_support_arm_asimdhp(); |
| 37 | #endif |
| 38 | #endif // __ARM_NEON |
| 39 | |
| 40 | #if NCNN_BF16 |
| 41 | support_bf16_storage = true; |
| 42 | #endif |
| 43 | } |
| 44 | |
| 45 | template<typename Op> |
| 46 | static int unary_op_inplace(Mat& a, const Option& opt) |
| 47 | { |
| 48 | Op op; |
| 49 | |
| 50 | int w = a.w; |
| 51 | int h = a.h; |
| 52 | int d = a.d; |
| 53 | int channels = a.c; |
| 54 | int elempack = a.elempack; |
| 55 | int size = w * h * d * elempack; |
| 56 | |
| 57 | #pragma omp parallel for num_threads(opt.num_threads) |
| 58 | for (int q = 0; q < channels; q++) |
| 59 | { |
| 60 | float* ptr = a.channel(q); |
| 61 | |
| 62 | int i = 0; |
| 63 | #if __ARM_NEON |
| 64 | for (; i + 7 < size; i += 8) |
| 65 | { |
| 66 | float32x4_t _p0 = vld1q_f32(ptr); |
| 67 | float32x4_t _p1 = vld1q_f32(ptr + 4); |
| 68 | _p0 = op.func_pack4(_p0); |
| 69 | _p1 = op.func_pack4(_p1); |
| 70 | vst1q_f32(ptr, _p0); |
| 71 | vst1q_f32(ptr + 4, _p1); |
| 72 | ptr += 8; |
| 73 | } |
| 74 | for (; i + 3 < size; i += 4) |
| 75 | { |
| 76 | float32x4_t _p = vld1q_f32(ptr); |
| 77 | _p = op.func_pack4(_p); |
| 78 | vst1q_f32(ptr, _p); |
| 79 | ptr += 4; |
| 80 | } |
| 81 | #endif // __ARM_NEON |
| 82 | for (; i < size; i++) |
| 83 | { |
| 84 | *ptr = op.func(*ptr); |
| 85 | ptr++; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | namespace UnaryOp_arm_functor { |
| 93 | |
| 94 | struct unary_op_abs |
| 95 | { |
| 96 | float func(const float& x) const |
| 97 | { |
| 98 | return (float)fabsf(x); |
| 99 | } |
| 100 | #if __ARM_NEON |
| 101 | float32x4_t func_pack4(const float32x4_t& x) const |
| 102 | { |
| 103 | return vabsq_f32(x); |
| 104 | } |
| 105 | #endif // __ARM_NEON |
| 106 | }; |
| 107 | |
| 108 | struct unary_op_neg |
| 109 | { |
| 110 | float func(const float& x) const |
| 111 | { |
| 112 | return -x; |
| 113 | } |
| 114 | #if __ARM_NEON |
| 115 | float32x4_t func_pack4(const float32x4_t& x) const |
| 116 | { |
| 117 | return vnegq_f32(x); |
| 118 | } |
| 119 | #endif // __ARM_NEON |
| 120 | }; |
| 121 | |
| 122 | struct unary_op_floor |
| 123 | { |
| 124 | float func(const float& x) const |
| 125 | { |
| 126 | return (float)floorf(x); |
| 127 | } |
| 128 | #if __ARM_NEON |
| 129 | float32x4_t func_pack4(const float32x4_t& x) const |
| 130 | { |
| 131 | #if __aarch64__ |
| 132 | return vrndmq_f32(x); |
| 133 | #else // __aarch64__ |
| 134 | int32x4_t _xi = vcvtq_s32_f32(x); |
| 135 | uint32x4_t _mask = vcgtq_f32(vcvtq_f32_s32(_xi), x); |
| 136 | return vcvtq_f32_s32(vaddq_s32(_xi, vreinterpretq_s32_u32(_mask))); |
| 137 | #endif // __aarch64__ |
| 138 | } |
| 139 | #endif // __ARM_NEON |
| 140 | }; |
| 141 | |
| 142 | struct unary_op_ceil |
| 143 | { |
| 144 | float func(const float& x) const |
| 145 | { |
| 146 | return (float)ceilf(x); |
| 147 | } |
| 148 | #if __ARM_NEON |
| 149 | float32x4_t func_pack4(const float32x4_t& x) const |
| 150 | { |
| 151 | #if __aarch64__ |
| 152 | return vrndpq_f32(x); |
| 153 | #else // __aarch64__ |
| 154 | int32x4_t _xi = vcvtq_s32_f32(x); |
| 155 | uint32x4_t _mask = vcgtq_f32(x, vcvtq_f32_s32(_xi)); |
| 156 | return vcvtq_f32_s32(vsubq_s32(_xi, vreinterpretq_s32_u32(_mask))); |
| 157 | #endif // __aarch64__ |
| 158 | } |
| 159 | #endif // __ARM_NEON |
| 160 | }; |
| 161 | |
| 162 | struct unary_op_square |
| 163 | { |
| 164 | float func(const float& x) const |
| 165 | { |
| 166 | return x * x; |
| 167 | } |
| 168 | #if __ARM_NEON |
| 169 | float32x4_t func_pack4(const float32x4_t& x) const |
| 170 | { |
| 171 | return vmulq_f32(x, x); |
| 172 | } |
| 173 | #endif // __ARM_NEON |
| 174 | }; |
| 175 | |
| 176 | struct unary_op_sqrt |
| 177 | { |
| 178 | float func(const float& x) const |
| 179 | { |
| 180 | return (float)sqrtf(x); |
| 181 | } |
| 182 | #if __ARM_NEON |
| 183 | float32x4_t func_pack4(const float32x4_t& x) const |
| 184 | { |
| 185 | #if __aarch64__ |
| 186 | return vsqrtq_f32(x); |
| 187 | #else |
| 188 | float32x4_t _reciprocal = vrsqrteq_f32(x); |
| 189 | _reciprocal = vmulq_f32(vrsqrtsq_f32(vmulq_f32(x, _reciprocal), _reciprocal), _reciprocal); |
| 190 | // _reciprocal = vmulq_f32(vrsqrtsq_f32(vmulq_f32(x, _reciprocal), _reciprocal), _reciprocal); |
| 191 | return vmulq_f32(x, _reciprocal); |
| 192 | #endif |
| 193 | } |
| 194 | #endif // __ARM_NEON |
| 195 | }; |
| 196 | |
| 197 | struct unary_op_rsqrt |
| 198 | { |
| 199 | float func(const float& x) const |
| 200 | { |
| 201 | return (float)(1.f / sqrtf(x)); |
| 202 | } |
| 203 | #if __ARM_NEON |
| 204 | float32x4_t func_pack4(const float32x4_t& x) const |
| 205 | { |
| 206 | float32x4_t _reciprocal = vrsqrteq_f32(x); |
| 207 | _reciprocal = vmulq_f32(vrsqrtsq_f32(vmulq_f32(x, _reciprocal), _reciprocal), _reciprocal); |
| 208 | // _reciprocal = vmulq_f32(vrsqrtsq_f32(vmulq_f32(x, _reciprocal), _reciprocal), _reciprocal); |
| 209 | return _reciprocal; |
| 210 | } |
| 211 | #endif // __ARM_NEON |
| 212 | }; |
| 213 | |
| 214 | struct unary_op_exp |
| 215 | { |
| 216 | float func(const float& x) const |
| 217 | { |
| 218 | return (float)expf(x); |
| 219 | } |
| 220 | #if __ARM_NEON |
| 221 | float32x4_t func_pack4(const float32x4_t& x) const |
| 222 | { |
| 223 | return exp_ps(x); |
| 224 | } |
| 225 | #endif // __ARM_NEON |
| 226 | }; |
| 227 | |
| 228 | struct unary_op_log |
| 229 | { |
| 230 | float func(const float& x) const |
| 231 | { |
| 232 | return (float)logf(x); |
| 233 | } |
| 234 | #if __ARM_NEON |
| 235 | float32x4_t func_pack4(const float32x4_t& x) const |
| 236 | { |
| 237 | return log_ps(x); |
| 238 | } |
| 239 | #endif // __ARM_NEON |
| 240 | }; |
| 241 | |
| 242 | struct unary_op_sin |
| 243 | { |
| 244 | float func(const float& x) const |
| 245 | { |
| 246 | return (float)sinf(x); |
| 247 | } |
| 248 | #if __ARM_NEON |
| 249 | float32x4_t func_pack4(const float32x4_t& x) const |
| 250 | { |
| 251 | return sin_ps(x); |
| 252 | } |
| 253 | #endif // __ARM_NEON |
| 254 | }; |
| 255 | |
| 256 | struct unary_op_cos |
| 257 | { |
| 258 | float func(const float& x) const |
| 259 | { |
| 260 | return (float)cosf(x); |
| 261 | } |
| 262 | #if __ARM_NEON |
| 263 | float32x4_t func_pack4(const float32x4_t& x) const |
| 264 | { |
| 265 | return cos_ps(x); |
| 266 | } |
| 267 | #endif // __ARM_NEON |
| 268 | }; |
| 269 | |
| 270 | struct unary_op_tan |
| 271 | { |
| 272 | float func(const float& x) const |
| 273 | { |
| 274 | return (float)tanf(x); |
| 275 | } |
| 276 | #if __ARM_NEON |
| 277 | float32x4_t func_pack4(const float32x4_t& x) const |
| 278 | { |
| 279 | return tan_ps(x); |
| 280 | } |
| 281 | #endif // __ARM_NEON |
| 282 | }; |
| 283 | |
| 284 | struct unary_op_asin |
| 285 | { |
| 286 | float func(const float& x) const |
| 287 | { |
| 288 | return (float)asinf(x); |
| 289 | } |
| 290 | #if __ARM_NEON |
| 291 | float32x4_t func_pack4(const float32x4_t& x) const |
| 292 | { |
| 293 | return asin_ps(x); |
| 294 | } |
| 295 | #endif // __ARM_NEON |
| 296 | }; |
| 297 | |
| 298 | struct unary_op_acos |
| 299 | { |
| 300 | float func(const float& x) const |
| 301 | { |
| 302 | return (float)acosf(x); |
| 303 | } |
| 304 | #if __ARM_NEON |
| 305 | float32x4_t func_pack4(const float32x4_t& x) const |
| 306 | { |
| 307 | return acos_ps(x); |
| 308 | } |
| 309 | #endif // __ARM_NEON |
| 310 | }; |
| 311 | |
| 312 | struct unary_op_atan |
| 313 | { |
| 314 | float func(const float& x) const |
| 315 | { |
| 316 | return (float)atanf(x); |
| 317 | } |
| 318 | #if __ARM_NEON |
| 319 | float32x4_t func_pack4(const float32x4_t& x) const |
| 320 | { |
| 321 | // TODO neon optimize |
| 322 | float tmp[4]; |
| 323 | vst1q_f32(tmp, x); |
| 324 | tmp[0] = atanf(tmp[0]); |
| 325 | tmp[1] = atanf(tmp[1]); |
| 326 | tmp[2] = atanf(tmp[2]); |
| 327 | tmp[3] = atanf(tmp[3]); |
| 328 | return vld1q_f32(tmp); |
| 329 | } |
| 330 | #endif // __ARM_NEON |
| 331 | }; |
| 332 | |
| 333 | struct unary_op_reciprocal |
| 334 | { |
| 335 | float func(const float& x) const |
| 336 | { |
| 337 | return 1.f / x; |
| 338 | } |
| 339 | #if __ARM_NEON |
| 340 | float32x4_t func_pack4(const float32x4_t& x) const |
| 341 | { |
| 342 | float32x4_t _reciprocal = vrecpeq_f32(x); |
| 343 | _reciprocal = vmulq_f32(vrecpsq_f32(x, _reciprocal), _reciprocal); |
| 344 | // _reciprocal = vmulq_f32(vrecpsq_f32(x, _reciprocal), _reciprocal); |
| 345 | return _reciprocal; |
| 346 | } |
| 347 | #endif // __ARM_NEON |
| 348 | }; |
| 349 | |
| 350 | struct unary_op_tanh |
| 351 | { |
| 352 | float func(const float& x) const |
| 353 | { |
| 354 | return (float)tanhf(x); |
| 355 | } |
| 356 | #if __ARM_NEON |
| 357 | float32x4_t func_pack4(const float32x4_t& x) const |
| 358 | { |
| 359 | return tanh_ps(x); |
| 360 | } |
| 361 | #endif // __ARM_NEON |
| 362 | }; |
| 363 | |
| 364 | struct unary_op_log10 |
| 365 | { |
| 366 | float func(const float& x) const |
| 367 | { |
| 368 | return (float)log10f(x); |
| 369 | } |
| 370 | #if __ARM_NEON |
| 371 | float32x4_t func_pack4(const float32x4_t& x) const |
| 372 | { |
| 373 | return vmulq_f32(log_ps(x), vdupq_n_f32(0.434294481903)); |
| 374 | } |
| 375 | #endif // __ARM_NEON |
| 376 | }; |
| 377 | |
| 378 | struct unary_op_round |
| 379 | { |
| 380 | float func(const float& x) const |
| 381 | { |
| 382 | // round to nearest even |
| 383 | #if NCNN_GNU_INLINE_ASM && __ARM_NEON |
| 384 | // return (x + 12582912.f) - 12582912.f; |
| 385 | float y; |
| 386 | const float magic = 12582912.f; |
| 387 | #if __aarch64__ |
| 388 | asm volatile( |
| 389 | "fadd %s0, %s1, %s2 \n" |
| 390 | "fsub %s0, %s0, %s2 \n" |
| 391 | : "=w"(y) |
| 392 | : "w"(x), "w"(magic) |
| 393 | :); |
| 394 | #else |
| 395 | asm volatile( |
| 396 | "vadd.f32 %0, %1, %2 \n" |
| 397 | "vsub.f32 %0, %0, %2 \n" |
| 398 | : "=t"(y) |
| 399 | : "t"(x), "t"(magic) |
| 400 | :); |
| 401 | #endif |
| 402 | return y; |
| 403 | #else |
| 404 | #ifdef FE_TONEAREST |
| 405 | int old_rm = fegetround(); |
| 406 | fesetround(FE_TONEAREST); |
| 407 | #endif |
| 408 | float y = nearbyintf(x); |
| 409 | #ifdef FE_TONEAREST |
| 410 | fesetround(old_rm); |
| 411 | #endif |
| 412 | return y; |
| 413 | #endif |
| 414 | } |
| 415 | #if __ARM_NEON |
| 416 | #if __aarch64__ |
| 417 | float32x4_t func_pack4(const float32x4_t& x) const |
| 418 | { |
| 419 | return vrndnq_f32(x); |
| 420 | } |
| 421 | #else |
| 422 | float32x4_t func_pack4(const float32x4_t& x) const |
| 423 | { |
| 424 | #if NCNN_GNU_INLINE_ASM |
| 425 | float32x4_t y; |
| 426 | float32x4_t _magic = vdupq_n_f32(12582912.f); // 1.5 * 2^23 |
| 427 | asm volatile( |
| 428 | "vadd.f32 %q0, %q1, %q2 \n" |
| 429 | "vsub.f32 %q0, %q0, %q2 \n" |
| 430 | : "=w"(y) |
| 431 | : "w"(x), "w"(_magic) |
| 432 | :); |
| 433 | return y; |
| 434 | #else |
| 435 | float tmp[4]; |
| 436 | vst1q_f32(tmp, x); |
| 437 | #ifdef FE_TONEAREST |
| 438 | int old_rm = fegetround(); |
| 439 | fesetround(FE_TONEAREST); |
| 440 | #endif |
| 441 | tmp[0] = nearbyintf(tmp[0]); |
| 442 | tmp[1] = nearbyintf(tmp[1]); |
| 443 | tmp[2] = nearbyintf(tmp[2]); |
| 444 | tmp[3] = nearbyintf(tmp[3]); |
| 445 | #ifdef FE_TONEAREST |
| 446 | fesetround(old_rm); |
| 447 | #endif |
| 448 | float32x4_t y = vld1q_f32(tmp); |
| 449 | return y; |
| 450 | #endif |
| 451 | } |
| 452 | #endif |
| 453 | #endif // __ARM_NEON |
| 454 | }; |
| 455 | |
| 456 | struct unary_op_trunc |
| 457 | { |
| 458 | float func(const float& x) const |
| 459 | { |
| 460 | return (float)truncf(x); |
| 461 | } |
| 462 | #if __ARM_NEON |
| 463 | float32x4_t func_pack4(const float32x4_t& x) const |
| 464 | { |
| 465 | #if __aarch64__ |
| 466 | return vrndq_f32(x); |
| 467 | #else |
| 468 | return vcvtq_f32_s32(vcvtq_s32_f32(x)); |
| 469 | #endif |
| 470 | } |
| 471 | #endif // __ARM_NEON |
| 472 | }; |
| 473 | |
| 474 | } // namespace UnaryOp_arm_functor |
| 475 | |
| 476 | int UnaryOp_arm::forward_inplace(Mat& bottom_top_blob, const Option& opt) const |
| 477 | { |
| 478 | int elembits = bottom_top_blob.elembits(); |
| 479 | |
| 480 | #if NCNN_ARM82 |
| 481 | if (support_fp16_storage && opt.use_fp16_storage && elembits == 16) |
| 482 | return forward_inplace_fp16s(bottom_top_blob, opt); |
| 483 | #endif |
| 484 | |
| 485 | #if NCNN_BF16 |
| 486 | if (opt.use_bf16_storage && elembits == 16) |
| 487 | return forward_inplace_bf16s(bottom_top_blob, opt); |
| 488 | #endif |
| 489 | |
| 490 | using namespace UnaryOp_arm_functor; |
| 491 | |
| 492 | if (op_type == Operation_ABS) |
| 493 | return unary_op_inplace<unary_op_abs>(bottom_top_blob, opt); |
| 494 | |
| 495 | if (op_type == Operation_NEG) |
| 496 | return unary_op_inplace<unary_op_neg>(bottom_top_blob, opt); |
| 497 | |
| 498 | if (op_type == Operation_FLOOR) |
| 499 | return unary_op_inplace<unary_op_floor>(bottom_top_blob, opt); |
| 500 | |
| 501 | if (op_type == Operation_CEIL) |
| 502 | return unary_op_inplace<unary_op_ceil>(bottom_top_blob, opt); |
| 503 | |
| 504 | if (op_type == Operation_SQUARE) |
| 505 | return unary_op_inplace<unary_op_square>(bottom_top_blob, opt); |
| 506 | |
| 507 | if (op_type == Operation_SQRT) |
| 508 | return unary_op_inplace<unary_op_sqrt>(bottom_top_blob, opt); |
| 509 | |
| 510 | if (op_type == Operation_RSQRT) |
| 511 | return unary_op_inplace<unary_op_rsqrt>(bottom_top_blob, opt); |
| 512 | |
| 513 | if (op_type == Operation_EXP) |
| 514 | return unary_op_inplace<unary_op_exp>(bottom_top_blob, opt); |
| 515 | |
| 516 | if (op_type == Operation_LOG) |
| 517 | return unary_op_inplace<unary_op_log>(bottom_top_blob, opt); |
| 518 | |
| 519 | if (op_type == Operation_SIN) |
| 520 | return unary_op_inplace<unary_op_sin>(bottom_top_blob, opt); |
| 521 | |
| 522 | if (op_type == Operation_COS) |
| 523 | return unary_op_inplace<unary_op_cos>(bottom_top_blob, opt); |
| 524 | |
| 525 | if (op_type == Operation_TAN) |
| 526 | return unary_op_inplace<unary_op_tan>(bottom_top_blob, opt); |
| 527 | |
| 528 | if (op_type == Operation_ASIN) |
| 529 | return unary_op_inplace<unary_op_asin>(bottom_top_blob, opt); |
| 530 | |
| 531 | if (op_type == Operation_ACOS) |
| 532 | return unary_op_inplace<unary_op_acos>(bottom_top_blob, opt); |
| 533 | |
| 534 | if (op_type == Operation_ATAN) |
| 535 | return unary_op_inplace<unary_op_atan>(bottom_top_blob, opt); |
| 536 | |
| 537 | if (op_type == Operation_RECIPROCAL) |
| 538 | return unary_op_inplace<unary_op_reciprocal>(bottom_top_blob, opt); |
| 539 | |
| 540 | if (op_type == Operation_TANH) |
| 541 | return unary_op_inplace<unary_op_tanh>(bottom_top_blob, opt); |
| 542 | |
| 543 | if (op_type == Operation_LOG10) |
| 544 | return unary_op_inplace<unary_op_log10>(bottom_top_blob, opt); |
| 545 | |
| 546 | if (op_type == Operation_ROUND) |
| 547 | return unary_op_inplace<unary_op_round>(bottom_top_blob, opt); |
| 548 | |
| 549 | if (op_type == Operation_TRUNC) |
| 550 | return unary_op_inplace<unary_op_trunc>(bottom_top_blob, opt); |
| 551 | |
| 552 | return 0; |
| 553 | } |
| 554 | |
| 555 | #if NCNN_BF16 |
| 556 | template<typename Op> |
| 557 | static int unary_op_inplace_bf16s(Mat& a, const Option& opt) |
| 558 | { |
| 559 | Op op; |
| 560 | |
| 561 | int w = a.w; |
| 562 | int h = a.h; |
| 563 | int d = a.d; |
| 564 | int channels = a.c; |
| 565 | int elempack = a.elempack; |
| 566 | int size = w * h * d * elempack; |
| 567 | |
| 568 | #pragma omp parallel for num_threads(opt.num_threads) |
| 569 | for (int q = 0; q < channels; q++) |
| 570 | { |
| 571 | unsigned short* ptr = a.channel(q); |
| 572 | |
| 573 | int i = 0; |
| 574 | #if __ARM_NEON |
| 575 | #if __aarch64__ |
| 576 | for (; i + 15 < size; i += 16) |
| 577 | { |
| 578 | uint16x8_t _p01 = vld1q_u16(ptr); |
| 579 | uint16x8_t _p23 = vld1q_u16(ptr + 8); |
| 580 | float32x4_t _p0 = bfloat2float(vget_low_u16(_p01)); |
| 581 | float32x4_t _p1 = bfloat2float(vget_high_u16(_p01)); |
| 582 | float32x4_t _p2 = bfloat2float(vget_low_u16(_p23)); |
| 583 | float32x4_t _p3 = bfloat2float(vget_high_u16(_p23)); |
| 584 | _p0 = op.func_pack4(_p0); |
| 585 | _p1 = op.func_pack4(_p1); |
| 586 | _p2 = op.func_pack4(_p2); |
| 587 | _p3 = op.func_pack4(_p3); |
| 588 | _p01 = vcombine_u16(float2bfloat(_p0), float2bfloat(_p1)); |
| 589 | _p23 = vcombine_u16(float2bfloat(_p2), float2bfloat(_p3)); |
| 590 | vst1q_u16(ptr, _p01); |
| 591 | vst1q_u16(ptr + 8, _p23); |
| 592 | ptr += 16; |
| 593 | } |
| 594 | #endif // __aarch64__ |
| 595 | for (; i + 7 < size; i += 8) |
| 596 | { |
| 597 | uint16x8_t _p = vld1q_u16(ptr); |
| 598 | float32x4_t _p0 = bfloat2float(vget_low_u16(_p)); |
| 599 | float32x4_t _p1 = bfloat2float(vget_high_u16(_p)); |
| 600 | _p0 = op.func_pack4(_p0); |
| 601 | _p1 = op.func_pack4(_p1); |
| 602 | _p = vcombine_u16(float2bfloat(_p0), float2bfloat(_p1)); |
| 603 | vst1q_u16(ptr, _p); |
| 604 | ptr += 8; |
| 605 | } |
| 606 | for (; i + 3 < size; i += 4) |
| 607 | { |
| 608 | float32x4_t _p = bfloat2float(vld1_u16(ptr)); |
| 609 | _p = op.func_pack4(_p); |
| 610 | vst1_u16(ptr, float2bfloat(_p)); |
| 611 | ptr += 4; |
| 612 | } |
| 613 | #endif // __ARM_NEON |
| 614 | for (; i < size; i++) |
| 615 | { |
| 616 | *ptr = float32_to_bfloat16(op.func(bfloat16_to_float32(*ptr))); |
| 617 | ptr++; |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | int UnaryOp_arm::forward_inplace_bf16s(Mat& bottom_top_blob, const Option& opt) const |
| 625 | { |
| 626 | using namespace UnaryOp_arm_functor; |
| 627 | |
| 628 | if (op_type == Operation_ABS) |
| 629 | return unary_op_inplace_bf16s<unary_op_abs>(bottom_top_blob, opt); |
| 630 | |
| 631 | if (op_type == Operation_NEG) |
| 632 | return unary_op_inplace_bf16s<unary_op_neg>(bottom_top_blob, opt); |
| 633 | |
| 634 | if (op_type == Operation_FLOOR) |
| 635 | return unary_op_inplace_bf16s<unary_op_floor>(bottom_top_blob, opt); |
| 636 | |
| 637 | if (op_type == Operation_CEIL) |
| 638 | return unary_op_inplace_bf16s<unary_op_ceil>(bottom_top_blob, opt); |
| 639 | |
| 640 | if (op_type == Operation_SQUARE) |
| 641 | return unary_op_inplace_bf16s<unary_op_square>(bottom_top_blob, opt); |
| 642 | |
| 643 | if (op_type == Operation_SQRT) |
| 644 | return unary_op_inplace_bf16s<unary_op_sqrt>(bottom_top_blob, opt); |
| 645 | |
| 646 | if (op_type == Operation_RSQRT) |
| 647 | return unary_op_inplace_bf16s<unary_op_rsqrt>(bottom_top_blob, opt); |
| 648 | |
| 649 | if (op_type == Operation_EXP) |
| 650 | return unary_op_inplace_bf16s<unary_op_exp>(bottom_top_blob, opt); |
| 651 | |
| 652 | if (op_type == Operation_LOG) |
| 653 | return unary_op_inplace_bf16s<unary_op_log>(bottom_top_blob, opt); |
| 654 | |
| 655 | if (op_type == Operation_SIN) |
| 656 | return unary_op_inplace_bf16s<unary_op_sin>(bottom_top_blob, opt); |
| 657 | |
| 658 | if (op_type == Operation_COS) |
| 659 | return unary_op_inplace_bf16s<unary_op_cos>(bottom_top_blob, opt); |
| 660 | |
| 661 | if (op_type == Operation_TAN) |
| 662 | return unary_op_inplace_bf16s<unary_op_tan>(bottom_top_blob, opt); |
| 663 | |
| 664 | if (op_type == Operation_ASIN) |
| 665 | return unary_op_inplace_bf16s<unary_op_asin>(bottom_top_blob, opt); |
| 666 | |
| 667 | if (op_type == Operation_ACOS) |
| 668 | return unary_op_inplace_bf16s<unary_op_acos>(bottom_top_blob, opt); |
| 669 | |
| 670 | if (op_type == Operation_ATAN) |
| 671 | return unary_op_inplace_bf16s<unary_op_atan>(bottom_top_blob, opt); |
| 672 | |
| 673 | if (op_type == Operation_RECIPROCAL) |
| 674 | return unary_op_inplace_bf16s<unary_op_reciprocal>(bottom_top_blob, opt); |
| 675 | |
| 676 | if (op_type == Operation_TANH) |
| 677 | return unary_op_inplace_bf16s<unary_op_tanh>(bottom_top_blob, opt); |
| 678 | |
| 679 | if (op_type == Operation_LOG10) |
| 680 | return unary_op_inplace_bf16s<unary_op_log10>(bottom_top_blob, opt); |
| 681 | |
| 682 | if (op_type == Operation_ROUND) |
| 683 | return unary_op_inplace_bf16s<unary_op_round>(bottom_top_blob, opt); |
| 684 | |
| 685 | if (op_type == Operation_TRUNC) |
| 686 | return unary_op_inplace_bf16s<unary_op_trunc>(bottom_top_blob, opt); |
| 687 | |
| 688 | return 0; |
| 689 | } |
| 690 | #endif // NCNN_BF16 |
| 691 | |
| 692 | } // namespace ncnn |