|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#if defined(__ARM_NEON__) || defined(__ARM_NEON) |
| 16 | + |
| 17 | +#include "NEONFunctions.h" |
| 18 | +#include <arm_neon.h> |
| 19 | + |
| 20 | +namespace paddle { |
| 21 | +namespace neon { |
| 22 | + |
| 23 | +// b[i] = a[i] > 0.0f ? a[i] : 0.0f |
| 24 | +void relu(const float* a, float* b, int len) { |
| 25 | + int offset = len % 16; |
| 26 | + float32x4_t ma0, ma1, ma2, ma3; |
| 27 | + float32x4_t mb0, mb1, mb2, mb3; |
| 28 | + |
| 29 | + float32x4_t zero = vdupq_n_f32(0.f); |
| 30 | + for (int k = 0; k < len / 16; k++, a += 16, b += 16) { |
| 31 | + ma0 = vld1q_f32(a); |
| 32 | + ma1 = vld1q_f32(a + 4); |
| 33 | + ma2 = vld1q_f32(a + 8); |
| 34 | + ma3 = vld1q_f32(a + 12); |
| 35 | + |
| 36 | + mb0 = vmaxq_f32(ma0, zero); |
| 37 | + mb1 = vmaxq_f32(ma1, zero); |
| 38 | + mb2 = vmaxq_f32(ma2, zero); |
| 39 | + mb3 = vmaxq_f32(ma3, zero); |
| 40 | + |
| 41 | + vst1q_f32(b, mb0); |
| 42 | + vst1q_f32(b + 4, mb1); |
| 43 | + vst1q_f32(b + 8, mb2); |
| 44 | + vst1q_f32(b + 12, mb3); |
| 45 | + } |
| 46 | + |
| 47 | + for (int i = 0; i < offset; i++) { |
| 48 | + b[i] = a[i] > 0.0f ? a[i] : 0.0f; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +} // namespace neon |
| 53 | +} // namespace paddle |
| 54 | + |
| 55 | +#endif |
0 commit comments