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