| 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 | static void pooling3x3s2_max_neon(const Mat& bottom_blob, Mat& top_blob, const Option& opt) |
| 16 | { |
| 17 | int w = bottom_blob.w; |
| 18 | int inch = bottom_blob.c; |
| 19 | |
| 20 | int outw = top_blob.w; |
| 21 | int outh = top_blob.h; |
| 22 | |
| 23 | const int tailstep = w - 2 * outw + w; |
| 24 | |
| 25 | #pragma omp parallel for num_threads(opt.num_threads) |
| 26 | for (int q = 0; q < inch; q++) |
| 27 | { |
| 28 | const float* img0 = bottom_blob.channel(q); |
| 29 | float* outptr = top_blob.channel(q); |
| 30 | |
| 31 | const float* r0 = img0; |
| 32 | const float* r1 = img0 + w; |
| 33 | const float* r2 = img0 + w * 2; |
| 34 | |
| 35 | for (int i = 0; i < outh; i++) |
| 36 | { |
| 37 | #if __ARM_NEON |
| 38 | int nn = outw >> 2; |
| 39 | int remain = outw - (nn << 2); |
| 40 | #else |
| 41 | int remain = outw; |
| 42 | #endif // __ARM_NEON |
| 43 | |
| 44 | #if __ARM_NEON |
| 45 | #if __aarch64__ |
| 46 | if (nn > 0) |
| 47 | { |
| 48 | asm volatile( |
| 49 | "prfm pldl1keep, [%1, #256] \n" |
| 50 | "ld2 {v0.4s, v1.4s}, [%1], #32 \n" |
| 51 | "prfm pldl1keep, [%2, #256] \n" |
| 52 | "ld2 {v2.4s, v3.4s}, [%2], #32 \n" |
| 53 | "prfm pldl1keep, [%3, #256] \n" |
| 54 | "ld2 {v4.4s, v5.4s}, [%3], #32 \n" |
| 55 | "0: \n" |
| 56 | |
| 57 | "prfm pldl1keep, [%1, #256] \n" |
| 58 | "ld2 {v6.4s, v7.4s}, [%1], #32 \n" |
| 59 | |
| 60 | "fmax v12.4s, v0.4s, v1.4s \n" |
| 61 | "fmax v13.4s, v2.4s, v3.4s \n" |
| 62 | |
| 63 | "prfm pldl1keep, [%2, #256] \n" |
| 64 | "ld2 {v8.4s, v9.4s}, [%2], #32 \n" |
| 65 | |
| 66 | "fmax v14.4s, v4.4s, v5.4s \n" |
| 67 | "ext v0.16b, v0.16b, v6.16b, #4 \n" |
| 68 | |
| 69 | "prfm pldl1keep, [%3, #256] \n" |
| 70 | "ld2 {v10.4s, v11.4s}, [%3], #32 \n" |
| 71 | |
| 72 | "ext v2.16b, v2.16b, v8.16b, #4 \n" |
| 73 | |
| 74 | "fmax v12.4s, v12.4s, v0.4s \n" |
| 75 | "ext v4.16b, v4.16b, v10.16b, #4 \n" |
| 76 | |
| 77 | "fmax v13.4s, v13.4s, v2.4s \n" |
| 78 | "fmax v14.4s, v14.4s, v4.4s \n" |
| 79 | "fmax v12.4s, v12.4s, v13.4s \n" |
| 80 | |
| 81 | "orr v0.16b, v6.16b, v6.16b \n" |
| 82 | "orr v1.16b, v7.16b, v7.16b \n" |
| 83 | "fmax v12.4s, v12.4s, v14.4s \n" |
| 84 | |
| 85 | "orr v2.16b, v8.16b, v8.16b \n" |
| 86 | "orr v3.16b, v9.16b, v9.16b \n" |
| 87 | "orr v4.16b, v10.16b, v10.16b \n" |
| 88 | "orr v5.16b, v11.16b, v11.16b \n" |
| 89 | |
| 90 | "subs %w0, %w0, #1 \n" |
| 91 | "st1 {v12.4s}, [%4], #16 \n" |
| 92 | "bne 0b \n" |
| 93 | "sub %1, %1, #32 \n" |
| 94 | "sub %2, %2, #32 \n" |
| 95 | "sub %3, %3, #32 \n" |
| 96 | : "=r"(nn), // %0 |
| 97 | "=r"(r0), // %1 |
| 98 | "=r"(r1), // %2 |
| 99 | "=r"(r2), // %3 |
| 100 | "=r"(outptr) // %4 |
| 101 | : "0"(nn), |
| 102 | "1"(r0), |
| 103 | "2"(r1), |
| 104 | "3"(r2), |
| 105 | "4"(outptr) |
| 106 | : "cc", "memory", "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9", "v10", "v11", "v12", "v13", "v14"); |
| 107 | } |
| 108 | #else |
| 109 | if (nn > 0) |
| 110 | { |
| 111 | asm volatile( |
| 112 | "pld [%1, #256] \n" |
| 113 | "vld2.f32 {d0-d3}, [%1]! \n" // q0 = 0 2 4 6 q1 = 1 3 5 7 |
| 114 | "pld [%2, #256] \n" |
| 115 | "vld2.f32 {d4-d7}, [%2]! \n" |
| 116 | "pld [%3, #256] \n" |
| 117 | "vld2.f32 {d8-d11}, [%3]! \n" |
| 118 | "0: \n" |
| 119 | "pld [%1, #256] \n" |
| 120 | "vld2.f32 {d12-d15}, [%1]! \n" // q6 = 8 10 12 14 q7 = 9 11 13 15 |
| 121 | |
| 122 | "vmax.f32 q12, q0, q1 \n" |
| 123 | "vmax.f32 q13, q2, q3 \n" |
| 124 | |
| 125 | "pld [%2, #256] \n" |
| 126 | "vld2.f32 {d16-d19}, [%2]! \n" |
| 127 | |
| 128 | "vmax.f32 q14, q4, q5 \n" |
| 129 | "vext.32 q0, q0, q6, #1 \n" |
| 130 | |
| 131 | "pld [%3, #256] \n" |
| 132 | "vld2.f32 {d20-d23}, [%3]! \n" |
| 133 | |
| 134 | "vext.32 q2, q2, q8, #1 \n" |
| 135 | |
| 136 | "vmax.f32 q12, q12, q0 \n" |
| 137 | "vext.32 q4, q4, q10, #1 \n" |
| 138 | |
| 139 | "vmax.f32 q13, q13, q2 \n" |
| 140 | "vmax.f32 q14, q14, q4 \n" |
| 141 | "vmax.f32 q12, q12, q13 \n" |
| 142 | |
| 143 | "vorr q0, q6, q6 \n" |
| 144 | "vorr q1, q7, q7 \n" |
| 145 | "vmax.f32 q12, q12, q14 \n" |
| 146 | |
| 147 | "vorr q2, q8, q8 \n" |
| 148 | "vorr q3, q9, q9 \n" |
| 149 | "vorr q4, q10, q10 \n" |
| 150 | "vorr q5, q11, q11 \n" |
| 151 | |
| 152 | "subs %0, #1 \n" |
| 153 | "vst1.f32 {d24-d25}, [%4]! \n" |
| 154 | "bne 0b \n" |
| 155 | "sub %1, #32 \n" |
| 156 | "sub %2, #32 \n" |
| 157 | "sub %3, #32 \n" |
| 158 | : "=r"(nn), // %0 |
| 159 | "=r"(r0), // %1 |
| 160 | "=r"(r1), // %2 |
| 161 | "=r"(r2), // %3 |
| 162 | "=r"(outptr) // %4 |
| 163 | : "0"(nn), |
| 164 | "1"(r0), |
| 165 | "2"(r1), |
| 166 | "3"(r2), |
| 167 | "4"(outptr) |
| 168 | : "cc", "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8", "q9", "q10", "q11", "q12", "q13", "q14"); |
| 169 | } |
| 170 | #endif // __aarch64__ |
| 171 | #endif // __ARM_NEON |
| 172 | for (; remain > 0; remain--) |
| 173 | { |
| 174 | float max0 = std::max(std::max(r0[0], r0[1]), r0[2]); |
| 175 | float max1 = std::max(std::max(r1[0], r1[1]), r1[2]); |
| 176 | float max2 = std::max(std::max(r2[0], r2[1]), r2[2]); |
| 177 | |
| 178 | *outptr = std::max(std::max(max0, max1), max2); |
| 179 | |
| 180 | r0 += 2; |
| 181 | r1 += 2; |
| 182 | r2 += 2; |
| 183 | outptr++; |
| 184 | } |
| 185 | |
| 186 | r0 += tailstep; //1 + w; |
| 187 | r1 += tailstep; //1 + w; |
| 188 | r2 += tailstep; //1 + w; |
| 189 | } |
| 190 | } |
| 191 | } |