| 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 "tanh_x86.h" |
| 16 | |
| 17 | #include "x86_activation.h" |
| 18 | |
| 19 | namespace ncnn { |
| 20 | |
| 21 | TanH_x86::TanH_x86() |
| 22 | { |
| 23 | #if __SSE2__ |
| 24 | support_packing = true; |
| 25 | #endif // __SSE2__ |
| 26 | } |
| 27 | |
| 28 | int TanH_x86::forward_inplace(Mat& bottom_top_blob, const Option& opt) const |
| 29 | { |
| 30 | int w = bottom_top_blob.w; |
| 31 | int h = bottom_top_blob.h; |
| 32 | int d = bottom_top_blob.d; |
| 33 | int channels = bottom_top_blob.c; |
| 34 | int elempack = bottom_top_blob.elempack; |
| 35 | int size = w * h * d * elempack; |
| 36 | |
| 37 | #pragma omp parallel for num_threads(opt.num_threads) |
| 38 | for (int q = 0; q < channels; q++) |
| 39 | { |
| 40 | float* ptr = bottom_top_blob.channel(q); |
| 41 | |
| 42 | int i = 0; |
| 43 | #if __SSE2__ |
| 44 | #if __AVX__ |
| 45 | #if __AVX512F__ |
| 46 | for (; i + 15 < size; i += 16) |
| 47 | { |
| 48 | __m512 _p = _mm512_loadu_ps(ptr); |
| 49 | _p = tanh_avx512(_p); |
| 50 | _mm512_storeu_ps(ptr, _p); |
| 51 | ptr += 16; |
| 52 | } |
| 53 | #endif |
| 54 | for (; i + 7 < size; i += 8) |
| 55 | { |
| 56 | __m256 _p = _mm256_loadu_ps(ptr); |
| 57 | _p = tanh_avx(_p); |
| 58 | _mm256_storeu_ps(ptr, _p); |
| 59 | ptr += 8; |
| 60 | } |
| 61 | #endif // __AVX__ |
| 62 | for (; i + 3 < size; i += 4) |
| 63 | { |
| 64 | __m128 _p = _mm_loadu_ps(ptr); |
| 65 | _p = tanh_sse(_p); |
| 66 | _mm_storeu_ps(ptr, _p); |
| 67 | ptr += 4; |
| 68 | } |
| 69 | #endif // __SSE2__ |
| 70 | for (; i < size; i++) |
| 71 | { |
| 72 | *ptr = tanhf(*ptr); |
| 73 | ptr++; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | } // namespace ncnn |