| 1 | // yala is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // |
| 4 | // Copyright (C) 2022 yala <zhaojunchao@loongson.cn>;<junchao82@qq.com>. All rights reserved. |
| 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 "relu_loongarch.h" |
| 16 | |
| 17 | #if __loongarch_sx |
| 18 | #include <lsxintrin.h> |
| 19 | #endif // __loongarch_sx |
| 20 | |
| 21 | #include "loongarch_usability.h" |
| 22 | |
| 23 | namespace ncnn { |
| 24 | |
| 25 | ReLU_loongarch::ReLU_loongarch() |
| 26 | { |
| 27 | #if __loongarch_sx |
| 28 | support_packing = true; |
| 29 | #endif |
| 30 | } |
| 31 | |
| 32 | int ReLU_loongarch::forward_inplace(Mat& bottom_top_blob, const Option& opt) const |
| 33 | { |
| 34 | int w = bottom_top_blob.w; |
| 35 | int h = bottom_top_blob.h; |
| 36 | int d = bottom_top_blob.d; |
| 37 | int channels = bottom_top_blob.c; |
| 38 | int elempack = bottom_top_blob.elempack; |
| 39 | int size = w * h * d * elempack; |
| 40 | |
| 41 | #pragma omp parallel for num_threads(opt.num_threads) |
| 42 | for (int q = 0; q < channels; q++) |
| 43 | { |
| 44 | float* ptr = bottom_top_blob.channel(q); |
| 45 | |
| 46 | if (slope == 0.f) |
| 47 | { |
| 48 | int i = 0; |
| 49 | #if __loongarch_sx |
| 50 | __m128 _zero = (__m128)__lsx_vreplgr2vr_w(0); |
| 51 | for (; i + 3 < size; i += 4) |
| 52 | { |
| 53 | __builtin_prefetch(ptr + 16); |
| 54 | __m128 _p = (__m128)__lsx_vld(ptr, 0); |
| 55 | _p = __lsx_vfmax_s(_p, _zero); |
| 56 | __lsx_vst(_p, ptr, 0); |
| 57 | |
| 58 | ptr += 4; |
| 59 | } |
| 60 | #endif // __loongarch_sx |
| 61 | for (; i < size; i++) |
| 62 | { |
| 63 | if (*ptr < 0) |
| 64 | *ptr = 0; |
| 65 | ptr++; |
| 66 | } |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | int i = 0; |
| 71 | #if __loongarch_sx |
| 72 | __m128 _zero = (__m128)__lsx_vreplgr2vr_w(0); |
| 73 | __m128 _slope = (__m128)__lsx_vreplfr2vr_s(slope); |
| 74 | for (; i + 3 < size; i += 4) |
| 75 | { |
| 76 | __builtin_prefetch(ptr + 16); |
| 77 | __m128 _p = (__m128)__lsx_vld(ptr, 0); |
| 78 | __m128i _lemask = __lsx_vfcmp_cle_s(_p, _zero); |
| 79 | __m128 _ps = __lsx_vfmul_s(_p, _slope); |
| 80 | _p = (__m128)__lsx_vbitsel_v((__m128i)_p, (__m128i)_ps, (__m128i)_lemask); |
| 81 | __lsx_vst(_p, ptr, 0); |
| 82 | |
| 83 | ptr += 4; |
| 84 | } |
| 85 | #endif // __loongarch_sx |
| 86 | for (; i < size; i++) |
| 87 | { |
| 88 | if (*ptr < 0) |
| 89 | *ptr *= slope; |
| 90 | ptr++; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | } // namespace ncnn |