ncnn
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 "hardsigmoid_mips.h"16
17#if __mips_msa18#include <msa.h>19#endif // __mips_msa20
21#include "mips_usability.h"22
23namespace ncnn {24
25HardSigmoid_mips::HardSigmoid_mips()26{
27#if __mips_msa28support_packing = true;29#endif // __mips_msa30}
31
32int HardSigmoid_mips::forward_inplace(Mat& bottom_top_blob, const Option& opt) const33{
34int w = bottom_top_blob.w;35int h = bottom_top_blob.h;36int d = bottom_top_blob.d;37int channels = bottom_top_blob.c;38int elempack = bottom_top_blob.elempack;39int size = w * h * d * elempack;40
41#pragma omp parallel for num_threads(opt.num_threads)42for (int q = 0; q < channels; q++)43{44float* ptr = bottom_top_blob.channel(q);45
46int i = 0;47#if __mips_msa48v4f32 _zero = (v4f32)__msa_fill_w(0);49v4f32 _one = (v4f32)__msa_fill_w_f32(1.f);50v4f32 _alpha = (v4f32)__msa_fill_w_f32(alpha);51v4f32 _beta = (v4f32)__msa_fill_w_f32(beta);52for (; i + 3 < size; i += 4)53{54__builtin_prefetch(ptr + 16);55v4f32 _p = (v4f32)__msa_ld_w(ptr, 0);56_p = __msa_fmadd_w(_beta, _p, _alpha);57_p = __msa_fmax_w(_p, _zero);58_p = __msa_fmin_w(_p, _one);59__msa_st_w((v4i32)_p, ptr, 0);60
61ptr += 4;62}63#endif // __mips_msa64for (; i < size; i++)65{66if (*ptr < lower)67*ptr = 0.f;68else if (*ptr > upper)69*ptr = 1.f;70else71*ptr = *ptr * alpha + beta;72++ptr;73}74}75
76return 0;77}
78
79} // namespace ncnn80