| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2017 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 "absval_arm.h" |
| 16 | |
| 17 | #if __ARM_NEON |
| 18 | #include <arm_neon.h> |
| 19 | #endif // __ARM_NEON |
| 20 | |
| 21 | namespace ncnn { |
| 22 | |
| 23 | AbsVal_arm::AbsVal_arm() |
| 24 | { |
| 25 | #if __ARM_NEON |
| 26 | support_packing = true; |
| 27 | #endif // __ARM_NEON |
| 28 | } |
| 29 | |
| 30 | int AbsVal_arm::forward_inplace(Mat& bottom_top_blob, const Option& opt) const |
| 31 | { |
| 32 | int w = bottom_top_blob.w; |
| 33 | int h = bottom_top_blob.h; |
| 34 | int d = bottom_top_blob.d; |
| 35 | int channels = bottom_top_blob.c; |
| 36 | int elempack = bottom_top_blob.elempack; |
| 37 | int size = w * h * d * elempack; |
| 38 | |
| 39 | #pragma omp parallel for num_threads(opt.num_threads) |
| 40 | for (int q = 0; q < channels; q++) |
| 41 | { |
| 42 | float* ptr = bottom_top_blob.channel(q); |
| 43 | |
| 44 | int i = 0; |
| 45 | #if __ARM_NEON |
| 46 | for (; i + 15 < size; i += 16) |
| 47 | { |
| 48 | #if NCNN_GNU_INLINE_ASM |
| 49 | #if __aarch64__ |
| 50 | asm volatile( |
| 51 | "prfm pldl1keep, [%0, #512] \n" |
| 52 | "ld1 {v0.4s, v1.4s, v2.4s, v3.4s}, [%0] \n" |
| 53 | "fabs v0.4s, v0.4s \n" |
| 54 | "fabs v1.4s, v1.4s \n" |
| 55 | "fabs v2.4s, v2.4s \n" |
| 56 | "fabs v3.4s, v3.4s \n" |
| 57 | "st1 {v0.4s, v1.4s, v2.4s, v3.4s}, [%0], #64 \n" |
| 58 | : "=r"(ptr) // %0 |
| 59 | : "0"(ptr) |
| 60 | : "memory", "v0", "v1", "v2", "v3"); |
| 61 | #else // __aarch64__ |
| 62 | asm volatile( |
| 63 | "pld [%0, #512] \n" |
| 64 | "vldm %0, {d0-d7} \n" |
| 65 | "vabs.f32 q0, q0 \n" |
| 66 | "vabs.f32 q1, q1 \n" |
| 67 | "vabs.f32 q2, q2 \n" |
| 68 | "vabs.f32 q3, q3 \n" |
| 69 | "vstm %0!, {d0-d7} \n" |
| 70 | : "=r"(ptr) // %0 |
| 71 | : "0"(ptr) |
| 72 | : "memory", "q0", "q1", "q2", "q3"); |
| 73 | #endif // __aarch64__ |
| 74 | #else // NCNN_GNU_INLINE_ASM |
| 75 | float32x4_t _p0 = vld1q_f32(ptr); |
| 76 | float32x4_t _p1 = vld1q_f32(ptr + 4); |
| 77 | float32x4_t _p2 = vld1q_f32(ptr + 8); |
| 78 | float32x4_t _p3 = vld1q_f32(ptr + 12); |
| 79 | _p0 = vabsq_f32(_p0); |
| 80 | _p1 = vabsq_f32(_p1); |
| 81 | _p2 = vabsq_f32(_p2); |
| 82 | _p3 = vabsq_f32(_p3); |
| 83 | vst1q_f32(ptr, _p0); |
| 84 | vst1q_f32(ptr + 4, _p1); |
| 85 | vst1q_f32(ptr + 8, _p2); |
| 86 | vst1q_f32(ptr + 12, _p3); |
| 87 | ptr += 16; |
| 88 | #endif // NCNN_GNU_INLINE_ASM |
| 89 | } |
| 90 | for (; i + 7 < size; i += 8) |
| 91 | { |
| 92 | float32x4_t _p0 = vld1q_f32(ptr); |
| 93 | float32x4_t _p1 = vld1q_f32(ptr + 4); |
| 94 | _p0 = vabsq_f32(_p0); |
| 95 | _p1 = vabsq_f32(_p1); |
| 96 | vst1q_f32(ptr, _p0); |
| 97 | vst1q_f32(ptr + 4, _p1); |
| 98 | ptr += 8; |
| 99 | } |
| 100 | for (; i + 3 < size; i += 4) |
| 101 | { |
| 102 | float32x4_t _p = vld1q_f32(ptr); |
| 103 | _p = vabsq_f32(_p); |
| 104 | vst1q_f32(ptr, _p); |
| 105 | ptr += 4; |
| 106 | } |
| 107 | #endif // __ARM_NEON |
| 108 | for (; i < size; i++) |
| 109 | { |
| 110 | *ptr = *ptr > 0 ? *ptr : -*ptr; |
| 111 | |
| 112 | ptr++; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | } // namespace ncnn |