pooling.cc 131.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "paddle/fluid/lite/arm/math/pooling.h"
#include <algorithm>
#include <limits>
#include "paddle/fluid/lite/arm/math/funcs.h"

namespace paddle {
namespace lite {
namespace arm {
namespace math {

H
hong19860320 已提交
25
void pooling_basic(const float* din, float* dout, int num, int chout, int hout,
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
                   int wout, int chin, int hin, int win,
                   const std::vector<int>& ksize,
                   const std::vector<int>& strides,
                   const std::vector<int>& paddings, bool global_pooling,
                   bool exclusive, bool adaptive, bool ceil_mode,
                   bool use_quantizer, const std::string& pooling_type) {
  // no need to pad input tensor, border is zero pad inside this function
  int kernel_h = ksize[0];
  int kernel_w = ksize[1];
  int stride_h = strides[0];
  int stride_w = strides[1];
  int pad_h = paddings[0];
  int pad_w = paddings[1];
  int size_channel_in = win * hin;
  int size_channel_out = wout * hout;
  if (global_pooling) {
    if (pooling_type == "max") {  // Pooling_max
      for (int n = 0; n < num; ++n) {
H
hong19860320 已提交
44 45
        float* dout_batch = dout + n * chout * size_channel_out;
        const float* din_batch = din + n * chin * size_channel_in;
46 47
#pragma omp parallel for
        for (int c = 0; c < chout; ++c) {
H
hong19860320 已提交
48 49
          const float* din_ch = din_batch + c * size_channel_in;  // in address
          float tmp1 = din_ch[0];
50
          for (int i = 0; i < size_channel_in; ++i) {
H
hong19860320 已提交
51 52
            float tmp2 = din_ch[i];
            tmp1 = tmp1 > tmp2 ? tmp1 : tmp2;
53
          }
H
hong19860320 已提交
54
          dout_batch[c] = tmp1;
55 56 57 58 59 60
        }
      }
    } else if (pooling_type == "avg") {
      // Pooling_average_include_padding
      // Pooling_average_exclude_padding
      for (int n = 0; n < num; ++n) {
H
hong19860320 已提交
61 62
        float* dout_batch = dout + n * chout * size_channel_out;
        const float* din_batch = din + n * chin * size_channel_in;
63 64
#pragma omp parallel for
        for (int c = 0; c < chout; ++c) {
H
hong19860320 已提交
65
          const float* din_ch = din_batch + c * size_channel_in;  // in address
66 67
          float sum = 0.f;
          for (int i = 0; i < size_channel_in; ++i) {
H
hong19860320 已提交
68
            sum += din_ch[i];
69
          }
H
hong19860320 已提交
70
          dout_batch[c] = sum / size_channel_in;
71 72 73
        }
      }
    } else {
H
hong19860320 已提交
74
      LOG(FATAL) << "unsupported pooling type: " << pooling_type;
75
    }
H
hong19860320 已提交
76 77 78
  } else {
    if (pooling_type == "max") {
      // Pooling_max
79
      for (int n = 0; n < num; ++n) {
H
hong19860320 已提交
80 81
        float* dout_ch = dout + n * chout * size_channel_out;
        const float* din_batch = din + n * chin * size_channel_in;
82
#pragma omp parallel for
H
hong19860320 已提交
83 84 85
        for (int c = 0; c < chout; c++) {
          float* dout_row = dout_ch + c * size_channel_out;
          const float* din_ch = din_batch + c * size_channel_in;
86 87 88 89 90 91 92 93 94 95
          for (int i = 0; i < hout; i++) {
            for (int j = 0; j < wout; j++) {
              int hstart = i * stride_h - pad_h;
              int wstart = j * stride_w - pad_w;
              int hend = std::min(hstart + kernel_h, hin + pad_h);
              int wend = std::min(wstart + kernel_w, win + pad_w);
              hstart = std::max(hstart, 0);
              wstart = std::max(wstart, 0);
              hend = std::min(hend, hin);
              wend = std::min(wend, win);
H
hong19860320 已提交
96 97 98
              int pool_size = (hend - hstart) * (wend - wstart);
              if (pool_size == 0) continue;
              float tmp1 = din_ch[hstart * win + wstart];
99 100
              for (int h = hstart; h < hend; ++h) {
                for (int w = wstart; w < wend; ++w) {
H
hong19860320 已提交
101 102
                  float tmp2 = din_ch[h * win + w];
                  tmp1 = tmp1 > tmp2 ? tmp1 : tmp2;
103 104
                }
              }
H
hong19860320 已提交
105
              dout_row[j] = tmp1;
106
            }
H
hong19860320 已提交
107
            dout_row += wout;
108 109 110
          }
        }
      }
H
hong19860320 已提交
111 112 113 114 115 116
    } else if (pooling_type == "avg") {
      if (exclusive) {
        // Pooling_average_exclude_padding
        for (int n = 0; n < num; ++n) {
          float* dout_ch = dout + n * chout * size_channel_out;
          const float* din_batch = din + n * chin * size_channel_in;
117
#pragma omp parallel for
H
hong19860320 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
          for (int c = 0; c < chout; c++) {
            float* dout_row = dout_ch + c * size_channel_out;
            const float* din_ch = din_batch + c * size_channel_in;
            for (int i = 0; i < hout; i++) {
              for (int j = 0; j < wout; j++) {
                int hstart = i * stride_h - pad_h;
                int wstart = j * stride_w - pad_w;
                int hend = std::min(hstart + kernel_h, hin + pad_h);
                int wend = std::min(wstart + kernel_w, win + pad_w);
                hstart = std::max(hstart, 0);
                wstart = std::max(wstart, 0);
                hend = std::min(hend, hin);
                wend = std::min(wend, win);
                int pool_size = (hend - hstart) * (wend - wstart);
                if (pool_size == 0) continue;
                float sum = 0.f;
                for (int h = hstart; h < hend; ++h) {
                  for (int w = wstart; w < wend; ++w) {
                    sum += din_ch[h * win + w];
                  }
138
                }
H
hong19860320 已提交
139
                dout_row[j] = sum / pool_size;
140
              }
H
hong19860320 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
              dout_row += wout;
            }
          }
        }
      } else {  // Pooling_average_include_padding
        for (int n = 0; n < num; ++n) {
          float* dout_ch = dout + n * chout * size_channel_out;
          const float* din_batch = din + n * chin * size_channel_in;
#pragma omp parallel for
          for (int c = 0; c < chout; c++) {
            float* dout_row = dout_ch + c * size_channel_out;
            const float* din_ch = din_batch + c * size_channel_in;
            for (int i = 0; i < hout; i++) {
              for (int j = 0; j < wout; j++) {
                int hstart = i * stride_h - pad_h;
                int wstart = j * stride_w - pad_w;
                int hend = std::min(hstart + kernel_h, hin + pad_h);
                int wend = std::min(wstart + kernel_w, win + pad_w);
                hstart = std::max(hstart, 0);
                wstart = std::max(wstart, 0);
                hend = std::min(hend, hin);
                wend = std::min(wend, win);
                int pool_size = (hend - hstart) * (wend - wstart);
                if (pool_size == 0) continue;
                float sum = 0.f;
                for (int h = hstart; h < hend; ++h) {
                  for (int w = wstart; w < wend; ++w) {
                    sum += din_ch[h * win + w];
                  }
                }
                dout_row[j] = sum / (kernel_w * kernel_h);
              }
              dout_row += wout;
174 175 176 177
            }
          }
        }
      }
H
hong19860320 已提交
178 179
    } else {
      LOG(FATAL) << "unsupported pooling type: " << pooling_type;
180 181 182 183
    }
  }
}

H
hong19860320 已提交
184 185
void pooling_global_max(const float* din, float* dout, int num, int chout,
                        int hout, int wout, int chin, int hin, int win) {
186 187 188
  int size_channel_in = win * hin;
  int cnt = size_channel_in / 8;
  for (int n = 0; n < num; ++n) {
H
hong19860320 已提交
189 190
    float* dout_batch = dout + n * chout;
    const float* din_batch = din + n * chin * size_channel_in;
191
#pragma omp parallel for
H
hong19860320 已提交
192 193 194 195 196
    for (int c = 0; c < chout; ++c) {
      const float* din_ch = din_batch + c * size_channel_in;
      int i = 0;
      float minval = std::numeric_limits<float>::lowest();
      float32x4_t vmax = vdupq_n_f32(minval);
197
#ifdef __aarch64__
H
hong19860320 已提交
198 199 200 201 202 203 204
      for (; i < cnt; i++) {
        float32x4_t vdin1 = vld1q_f32(din_ch);
        vmax = vmaxq_f32(vdin1, vmax);
        float32x4_t vdin2 = vld1q_f32(din_ch + 4);
        vmax = vmaxq_f32(vmax, vdin2);
        din_ch += 8;
      }
205
#else
H
hong19860320 已提交
206 207 208 209 210 211 212 213 214 215 216 217 218 219
      int cnt_num = cnt;
      if (cnt_num > 0) {
        asm volatile(
            "max_loop:                          @main loop\n"
            "vld1.f32   {d0-d1}, [%[din_ch]]!   @load q1,din_ch\n"
            "vmax.f32   %q[vmax], %q[vmax], q0  @max vmax,vmax,din_ch\n"
            "vld1.f32   {d2-d3}, [%[din_ch]]!   @load 2nd 4 data\n"
            "vmax.f32   %q[vmax], %q[vmax], q1  @compare 2nd 4 datas\n"
            "subs       %[cnt_num], #1          @cnt_num--\n"
            "bne        max_loop                @bne cnt_num\n"
            : [din_ch] "+r"(din_ch), [cnt_num] "+r"(cnt_num), [vmax] "+w"(vmax)
            :
            : "cc", "memory", "q0", "q1");
      }
220
#endif  // __aarch64__
H
hong19860320 已提交
221 222 223 224 225 226 227 228
      float32x2_t vmax_tmp = vmax_f32(vget_low_f32(vmax), vget_high_f32(vmax));
      float tmp1 = vget_lane_f32(vmax_tmp, 0);
      float tmp2 = vget_lane_f32(vmax_tmp, 1);
      float max_tmp = tmp1 > tmp2 ? tmp1 : tmp2;
      for (i = cnt * 8; i < size_channel_in; ++i) {
        /* code */
        max_tmp = max_tmp > din_ch[0] ? max_tmp : din_ch[0];
        din_ch++;
229
      }
H
hong19860320 已提交
230 231 232 233 234 235 236 237 238 239 240 241
      dout_batch[c] = max_tmp;
    }
  }
}

void pooling_global_avg(const float* din, float* dout, int num, int chout,
                        int hout, int wout, int chin, int hin, int win) {
  int size_channel_in = win * hin;
  int cnt = size_channel_in / 4;
  for (int n = 0; n < num; ++n) {
    float* dout_batch = dout + n * chout;
    const float* din_batch = din + n * chin * size_channel_in;
242
#pragma omp parallel for
H
hong19860320 已提交
243 244 245 246
    for (int c = 0; c < chout; c++) {
      const float* din_ch = din_batch + c * size_channel_in;  // in address
      int i = 0;
      float32x4_t vsum = vdupq_n_f32(0.0f);
247
#ifdef __aarch64__
H
hong19860320 已提交
248 249 250 251
      for (; i < cnt; i++) {
        vsum = vaddq_f32(vld1q_f32(din_ch), vsum);
        din_ch += 4;
      }
252
#else
H
hong19860320 已提交
253 254 255 256 257 258 259 260 261 262 263 264
      int cnt_num = cnt;
      if (cnt_num > 0) {
        asm volatile(
            "add_loop:                          @main loop\n"
            "vld1.f32   {d0-d1}, [%[din_ch]]!   @load q1,din_ch\n"
            "vadd.f32   %q[vsum], %q[vsum], q0  @add vmax,vmax, din_ch\n"
            "subs       %[cnt_num], #1          @cnt_num--\n"
            "bne        add_loop                @bne num\n"
            : [din_ch] "+r"(din_ch), [cnt_num] "+r"(cnt_num), [vsum] "+w"(vsum)
            :
            : "cc", "memory", "q0");
      }
265
#endif  // __aarch64__
H
hong19860320 已提交
266 267 268 269 270
      float32x2_t vsum_tmp = vadd_f32(vget_low_f32(vsum), vget_high_f32(vsum));
      float sum = vget_lane_f32(vsum_tmp, 0) + vget_lane_f32(vsum_tmp, 1);
      for (i = cnt * 4; i < size_channel_in; i++) {
        sum += din_ch[0];
        din_ch++;
271
      }
H
hong19860320 已提交
272
      dout_batch[c] = sum / size_channel_in;
273 274 275 276
    }
  }
}

H
hong19860320 已提交
277 278 279 280 281
void pooling2x2s2_max(const float* din, float* dout, int num, int chout,
                      int hout, int wout, int chin, int hin, int win) {
  int kernel = 2;
  int stride = 2;
  int padding = 0;
282 283 284
  int size_channel_out = wout * hout;
  int size_channel_in = win * hin;

H
hong19860320 已提交
285 286 287 288 289 290
  int w_needed = (wout << 1);
  int h_needed = (hout << 1);
  int w_limit = w_needed > win ? win : w_needed;
  int h_limit = h_needed > hin ? hin : h_needed;
  int w_even = (w_limit >> 1) << 1;
  int h_even = (h_limit >> 1) << 1;
291
  int w_unroll_size = (w_even >> 3) << 3;
H
hong19860320 已提交
292
  // int w_unroll_remain = w_even - w_unroll_size;
293 294
  int w_in_2 = win << 1;
  for (int n = 0; n < num; ++n) {
H
hong19860320 已提交
295 296
    float* dout_batch = dout + n * chout * size_channel_out;
    const float* din_batch = din + n * chin * size_channel_in;
297 298
#pragma omp parallel for
    for (int c = 0; c < chout; c++) {
H
hong19860320 已提交
299 300 301
      float* dout_ch = dout_batch + c * size_channel_out;
      const float* din_ch = din_batch + c * size_channel_in;
      const float* r0 = din_ch;
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
      const float* r1 = r0 + win;
      int h = 0;
      for (; h < h_even; h += 2) {
        int w = 0;
#ifdef __aarch64__
        for (; w < w_unroll_size; w += 8) {
          float32x4_t dr00 = vld1q_f32(&r0[w]);
          float32x4_t dr01 = vld1q_f32(&r0[w + 4]);
          float32x4_t dr10 = vld1q_f32(&r1[w]);
          float32x4_t dr11 = vld1q_f32(&r1[w + 4]);
          float32x4_t dmax1 = vmaxq_f32(dr00, dr10);
          float32x4_t dmax2 = vmaxq_f32(dr01, dr11);
#ifdef __aarch64__
          float32x4_t dmax = vpmaxq_f32(dmax1, dmax2);
#else
          float32x2_t dmaxl =
              vpmax_f32(vget_low_f32(dmax1), vget_high_f32(dmax1));
          float32x2_t dmaxh =
              vpmax_f32(vget_low_f32(dmax2), vget_high_f32(dmax2));
          float32x4_t dmax = vcombine_f32(dmaxl, dmaxh);
#endif
H
hong19860320 已提交
323
          vst1q_f32(&dout_ch[w >> 1], dmax);
324 325
        }
#else
H
hong19860320 已提交
326
        float* dr_out = dout_ch;
327 328
        const float* dr0 = r0;
        const float* dr1 = r1;
H
hong19860320 已提交
329 330
        int cnt_num = w_unroll_size >> 3;
        if (cnt_num > 0) {
331
          asm volatile(
H
hong19860320 已提交
332 333 334 335 336 337 338 339 340 341
              "s2_max_loop:                      @main loop\n"
              "vld1.f32   {d0-d3}, [%[dr0]]!     @load q0,dr0\n"
              "vld1.f32   {d4-d7}, [%[dr1]]!     @load q1,dr1\n"
              "vmax.f32   q0, q0, q2             @max q0,q0,q2\n"
              "vmax.f32   q1, q1, q3             @max q1,q1,q2\n"
              "vpmax.f32  d4, d0, d1             @max d4,d0,d1\n"
              "vpmax.f32  d5, d2, d3             @max d5,d2,d3\n"
              "vst1.f32   {d4-d5}, [%[dr_out]]!  @vst1 q2,dr_out\n"
              "subs       %[cnt_num], #1         @cnt_num--\n"
              "bne        s2_max_loop            @bne cnt_num\n"
342
              : [dr0] "+r"(dr0), [dr1] "+r"(dr1), [dr_out] "+r"(dr_out),
H
hong19860320 已提交
343
                [cnt_num] "+r"(cnt_num)
344 345 346
              :
              : "cc", "memory", "q0", "q1", "q2", "q3");
        }
H
hong19860320 已提交
347
        w = w_unroll_size;
348 349
#endif  // __aarch64__
        for (; w < w_even; w += 2) {
H
hong19860320 已提交
350
          dout_ch[w >> 1] =
351 352
              std::max(std::max(r0[w], r0[w + 1]), std::max(r1[w], r1[w + 1]));
        }
H
hong19860320 已提交
353 354
        for (; w < w_limit; ++w) {  // run 0 or 1 time
          dout_ch[w >> 1] = std::max(r0[w], r1[w]);
355 356 357
        }
        r0 += w_in_2;  // << 1;
        r1 += w_in_2;  // << 1;
H
hong19860320 已提交
358
        dout_ch += wout;
359 360
      }
      // process remain row (odd, last row)
H
hong19860320 已提交
361
      for (; h < h_limit; h++) {  // run 0 or 1 time
362 363 364 365 366 367 368 369 370 371 372 373 374 375
        int w = 0;
#ifdef __aarch64__
        for (; w < w_unroll_size; w += 8) {
          float32x4_t dr00 = vld1q_f32(&r0[w]);
          float32x4_t dr01 = vld1q_f32(&r0[w + 4]);
#ifdef __aarch64__
          float32x4_t dmax = vpmaxq_f32(dr00, dr01);
#else
          float32x2_t dmaxl =
              vpmax_f32(vget_low_f32(dr00), vget_high_f32(dr00));
          float32x2_t dmaxh =
              vpmax_f32(vget_low_f32(dr01), vget_high_f32(dr01));
          float32x4_t dmax = vcombine_f32(dmaxl, dmaxh);
#endif
H
hong19860320 已提交
376
          vst1q_f32(&dout_ch[w >> 1], dmax);
377 378
        }
#else
H
hong19860320 已提交
379
        float* dr_out = dout_ch;
380
        const float* dr0 = r0;
H
hong19860320 已提交
381 382
        int cnt_num = w_unroll_size >> 3;
        if (cnt_num > 0) {
383
          asm volatile(
H
hong19860320 已提交
384 385 386 387 388 389 390 391
              "s2_max_loop1:                      @main loop\n"
              "vld1.f32   {d0-d3}, [%[dr0]]!      @load q0,dr0\n"
              "vpmax.f32  d4, d0, d1              @max d4,d0,d1\n"
              "vpmax.f32  d5, d2, d3              @max d5,d2,d3\n"
              "vst1.f32   {d4-d5}, [%[dr_out]]!   @vst1 q2,dr_out\n"
              "subs       %[cnt_num], #1          @cnt_num--\n"
              "bne        s2_max_loop1            @bne cnt_num\n"
              : [dr0] "+r"(dr0), [dr_out] "+r"(dr_out), [cnt_num] "+r"(cnt_num)
392 393 394
              :
              : "cc", "memory", "q0", "q1", "q2");
        }
H
hong19860320 已提交
395
        w = w_unroll_size;
396 397
#endif  // __aarch64__
        for (; w < w_even; w += 2) {
H
hong19860320 已提交
398
          dout_ch[w >> 1] = std::max(r0[w], r0[w + 1]);
399
        }
H
hong19860320 已提交
400 401
        for (; w < w_limit; ++w) {  // run 0 or 1 time
          dout_ch[w >> 1] = r0[w];
402 403 404 405 406 407
        }
      }
    }
  }
}

H
hong19860320 已提交
408 409 410 411 412 413
void pooling2x2s2_avg(const float* din, float* dout, int num, int chout,
                      int hout, int wout, int chin, int hin, int win,
                      bool exclusive) {
  int kernel = 2;
  int stride = 2;
  int padding = 0;
414 415 416
  int size_channel_out = wout * hout;
  int size_channel_in = win * hin;

H
hong19860320 已提交
417 418 419 420 421 422
  int w_needed = (wout << 1);
  int h_needed = (hout << 1);
  int w_limit = w_needed > win ? win : w_needed;
  int h_limit = h_needed > hin ? hin : h_needed;
  int w_even = (w_limit >> 1) << 1;
  int h_even = (h_limit >> 1) << 1;
423
  int w_unroll_size = (w_even >> 3) << 3;
H
hong19860320 已提交
424
  // int w_unroll_remain = w_even - w_unroll_size;
425
  int w_in_2 = win << 1;
H
hong19860320 已提交
426 427 428 429 430 431
  const float coef = 1.f / 4.f;
  const float coef_1 = exclusive ? 1.f : coef;
  const float coef_2 = exclusive ? 1.f / 2.f : coef;
  float32x4_t vcoef = vdupq_n_f32(coef);
  float32x4_t vcoef_1 = vdupq_n_f32(coef_1);
  float32x4_t vcoef_2 = vdupq_n_f32(coef_2);
432
  for (int n = 0; n < num; ++n) {
H
hong19860320 已提交
433 434
    float* dout_batch = dout + n * chout * size_channel_out;
    const float* din_batch = din + n * chin * size_channel_in;
435 436
#pragma omp parallel for
    for (int c = 0; c < chout; c++) {
H
hong19860320 已提交
437 438 439
      float* dout_ch = dout_batch + c * size_channel_out;
      const float* din_ch = din_batch + c * size_channel_in;
      const float* r0 = din_ch;
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
      const float* r1 = r0 + win;
      int h = 0;
      for (; h < h_even; h += 2) {
        int w = 0;
#ifdef __aarch64__
        for (; w < w_unroll_size; w += 8) {
          float32x4_t dr00 = vld1q_f32(&r0[w]);
          float32x4_t dr01 = vld1q_f32(&r0[w + 4]);
          float32x4_t dr10 = vld1q_f32(&r1[w]);
          float32x4_t dr11 = vld1q_f32(&r1[w + 4]);
          float32x4_t dsum1 = vaddq_f32(dr00, dr10);
          float32x4_t dsum2 = vaddq_f32(dr01, dr11);
#ifdef __aarch64__
          float32x4_t dsum = vpaddq_f32(dsum1, dsum2);
#else
          float32x2_t dsuml =
              vpadd_f32(vget_low_f32(dsum1), vget_high_f32(dsum1));
          float32x2_t dsumh =
              vpadd_f32(vget_low_f32(dsum2), vget_high_f32(dsum2));
          float32x4_t dsum = vcombine_f32(dsuml, dsumh);
#endif
          float32x4_t res = vmulq_f32(dsum, vcoef);
H
hong19860320 已提交
462
          vst1q_f32(&dout_ch[w >> 1], res);
463 464
        }
#else
H
hong19860320 已提交
465
        float* dr_out = dout_ch;
466 467
        const float* dr0 = r0;
        const float* dr1 = r1;
H
hong19860320 已提交
468 469
        int cnt_num = w_unroll_size >> 3;
        if (cnt_num > 0) {
470
          asm volatile(
H
hong19860320 已提交
471 472 473 474 475 476 477 478 479 480 481
              "1:                                @main loop\n"
              "vld1.f32   {d0-d3}, [%[dr0]]!     @load q0,dr0\n"
              "vld1.f32   {d4-d7}, [%[dr1]]!     @load q1,dr1\n"
              "vadd.f32   q0, q0, q2             @add q0,q0,q2\n"
              "vadd.f32   q1, q1, q3             @add q1,q1,q2\n"
              "vpadd.f32  d4, d0, d1             @add d4,d0,d1\n"
              "vpadd.f32  d5, d2, d3             @add d5,d2,d3\n"
              "vmul.f32   q2, q2, %q[vcoef]    @mul q2,q2,vcoef\n"
              "vst1.f32   {d4-d5}, [%[dr_out]]!  @vst1 q2,dr_out\n"
              "subs       %[cnt_num], #1         @cnt_num--\n"
              "bne        1b                     @bne cnt_num\n"
482
              : [dr0] "+r"(dr0), [dr1] "+r"(dr1), [dr_out] "+r"(dr_out),
H
hong19860320 已提交
483 484
                [vcoef] "+w"(vcoef), [cnt_num] "+r"(cnt_num)
              : "r"(dr0), "r"(dr1), "r"(dr_out), "r"(cnt_num), "w"(vcoef)
485 486
              : "cc", "memory", "q0", "q1", "q2", "q3");
        }
H
hong19860320 已提交
487
        w = w_unroll_size;
488 489
#endif  // __aarch64__
        for (; w < w_even; w += 2) {
H
hong19860320 已提交
490
          dout_ch[w >> 1] = (r0[w] + r0[w + 1] + r1[w] + r1[w + 1]) * coef;
491
        }
H
hong19860320 已提交
492 493
        for (; w < w_limit; ++w) {  // run 0 or 1 time
          dout_ch[w >> 1] = (r0[w] + r1[w]) * coef_2;
494 495 496
        }
        r0 += w_in_2;  // << 1;
        r1 += w_in_2;  // << 1;
H
hong19860320 已提交
497
        dout_ch += wout;
498 499
      }
      // process remain row (odd, last row)
H
hong19860320 已提交
500
      for (; h < h_limit; h++) {  // run 0 or 1 time
501 502 503 504 505 506 507 508 509 510 511 512 513 514
        int w = 0;
#ifdef __aarch64__
        for (; w < w_unroll_size; w += 8) {
          float32x4_t dr00 = vld1q_f32(&r0[w]);
          float32x4_t dr01 = vld1q_f32(&r0[w + 4]);
#ifdef __aarch64__
          float32x4_t dsum = vpaddq_f32(dr00, dr01);
#else
          float32x2_t dsuml =
              vpadd_f32(vget_low_f32(dr00), vget_high_f32(dr00));
          float32x2_t dsumh =
              vpadd_f32(vget_low_f32(dr01), vget_high_f32(dr01));
          float32x4_t dsum = vcombine_f32(dsuml, dsumh);
#endif
H
hong19860320 已提交
515 516
          float32x4_t res = vmulq_f32(dsum, vcoef_2);
          vst1q_f32(&dout_ch[w >> 1], res);
517 518
        }
#else
H
hong19860320 已提交
519
        float* dr_out = dout_ch;
520
        const float* dr0 = r0;
H
hong19860320 已提交
521 522
        int cnt_num = w_unroll_size >> 3;
        if (cnt_num > 0) {
523
          asm volatile(
H
hong19860320 已提交
524 525 526 527 528 529 530 531 532 533 534
              "1:                                @main loop\n"
              "vld1.f32   {d0-d3}, [%[dr0]]!     @load q0,dr0\n"
              "vpadd.f32  d4, d0, d1             @add d4,d0,d1\n"
              "vpadd.f32  d5, d2, d3             @add d5,d2,d3\n"
              "vmul.f32   q2, q2, %q[vcoef_2]    @mul q2,q2,vcoef_2\n"
              "vst1.f32   {d4-d5}, [%[dr_out]]!  @vst1 q2,dr_out\n"
              "subs       %[cnt_num], #1         @cnt_num--\n"
              "bne        1b                     @bne cnt_num\n"
              : [dr0] "+r"(dr0), [dr_out] "+r"(dr_out), [vcoef_2] "+w"(vcoef_2),
                [cnt_num] "+r"(cnt_num)
              : "r"(dr0), "r"(dr_out), "r"(cnt_num), "w"(vcoef_2)
535 536
              : "cc", "memory", "q0", "q1", "q2");
        }
H
hong19860320 已提交
537
        w = w_unroll_size;
538 539
#endif  // __aarch64__
        for (; w < w_even; w += 2) {
H
hong19860320 已提交
540
          dout_ch[w >> 1] = (r0[w] + r0[w + 1]) * coef_2;
541
        }
H
hong19860320 已提交
542 543
        for (; w < w_limit; ++w) {  // run 0 or 1 time
          dout_ch[w >> 1] = r0[w] * coef_1;
544 545 546 547 548 549
        }
      }
    }
  }
}

H
hong19860320 已提交
550 551 552 553 554 555
void pooling3x3s1p1_max(const float* din, float* dout, int num, int chout,
                        int hout, int wout, int chin, int hin, int win) {
  int kernel = 3;
  int stride = 1;
  int padding = 1;
  int size_channel_out = wout * hout;
556 557
  int size_channel_in = win * hin;

H
hong19860320 已提交
558 559 560
  int w_unroll_size = ((win - 2) >> 2) << 2;
  int w_unroll_remain = win - 2 - w_unroll_size;
  const float minval = std::numeric_limits<float>::lowest();
561
  for (int n = 0; n < num; ++n) {
H
hong19860320 已提交
562 563
    float* dout_batch = dout + n * chout * size_channel_out;
    const float* din_batch = din + n * chin * size_channel_in;
564
#pragma omp parallel for
H
hong19860320 已提交
565 566 567 568 569 570 571 572
    for (int c = 0; c < chout; c++) {
      float* dout_ch = dout_batch + c * size_channel_out;
      const float* din_ch = din_batch + c * size_channel_in;
      const float* r0 = din_ch;
      const float* r1 = r0 + win;
      const float* r2 = r1 + win;
      int cnt_num = w_unroll_size >> 2;  // w_unroll_size / 4
      float* dr_out = dout_ch;
573 574 575 576 577 578
      const float* dr0 = r0;
      const float* dr1 = r1;
      const float* dr2 = r2;
      int w = 0;
      int cnt = 1;
      // left
H
hong19860320 已提交
579
      dout_ch[0] = std::max(std::max(r0[0], r0[1]), std::max(r1[0], r1[1]));
580 581
// first row with zero pad
#ifdef __aarch64__
H
hong19860320 已提交
582
      for (; w < w_unroll_size; w += 4) {
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
        float32x4_t vr0_1234 = vld1q_f32(&r0[w]);
        float32x4_t vr1_1234 = vld1q_f32(&r1[w]);
        float32x4_t vr0_5678 = vld1q_f32(&r0[w + 4]);
        float32x4_t vr1_5678 = vld1q_f32(&r1[w + 4]);
        float32x4_t vmax_1234 = vmaxq_f32(vr0_1234, vr1_1234);
        float32x4_t vmax_5678 = vmaxq_f32(vr0_5678, vr1_5678);

        float32x4_t vmax_2345 = vextq_f32(vmax_1234, vmax_5678, 1);
        float32x4_t vmax_3456 = vextq_f32(vmax_1234, vmax_5678, 2);
        float32x2_t vmax_12_34 =
            vpmax_f32(vget_low_f32(vmax_1234), vget_high_f32(vmax_1234));
        float32x2_t vmax_23_45 =
            vpmax_f32(vget_low_f32(vmax_2345), vget_high_f32(vmax_2345));
        float32x2_t vmax_34_56 =
            vpmax_f32(vget_low_f32(vmax_3456), vget_high_f32(vmax_3456));
        float32x2_t vmax_123_345 = vmax_f32(vmax_12_34, vmax_23_45);
        float32x2_t vmax_234_456 = vmax_f32(vmax_23_45, vmax_34_56);
        float32x4_t vmax = vdupq_n_f32(vget_lane_f32(vmax_123_345, 0));
        vmax = vsetq_lane_f32(vget_lane_f32(vmax_234_456, 0), vmax, 1);
        vmax = vsetq_lane_f32(vget_lane_f32(vmax_123_345, 1), vmax, 2);
        vmax = vsetq_lane_f32(vget_lane_f32(vmax_234_456, 1), vmax, 3);
H
hong19860320 已提交
604
        vst1q_f32(&dout_ch[cnt], vmax);
605 606 607 608 609 610 611
        cnt += 4;
      }

#else
      dr_out = dr_out + 1;
      if (cnt_num > 0) {
        asm volatile(
H
hong19860320 已提交
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
            "1:                              @main loop\n"
            "vld1.f32  {d0-d1}, [%[dr0]]!    @load d0-d5,dr0\n"
            "vld1.f32  {d4-d5}, [%[dr1]]!    @load d4-d7,dr1\n"
            "vld1.f32  {d2}, [%[dr0]]!       @load d0-d5,dr0\n"
            "vld1.f32  {d6}, [%[dr1]]!       @load d4-d7,dr1\n"
            "vmax.f32  q5, q0, q2            @max r0_1234,r1_1234\n"
            "vmax.f32  d12, d2, d6           @max r0_5678,r1_5678\n"
            //"vmov.f32  s7,s6               @mov s7,s6\n"
            "vext.f32  q0, q5, q6, #1        @vext max_2345\n"
            "vext.f32  q2, q5, q6, #2        @vext max_3456\n"
            "vpmax.f32 d2, d10, d11          @pmax d4,max_1234,max_1234\n"
            "vpmax.f32 d3, d0, d1            @pmax d4,max_2345,max_2345\n"
            "vpmax.f32 d6, d4, d5            @pmax d6,max_3456,max_3456\n"
            "vmax.f32  d8, d2, d3            @max d2,vmax_12_34,vmax_23_45\n"
            "vmax.f32  d9, d3, d6            @max d2,vmax_23_45,vmax_34_56\n"
            "sub       %[dr0], #8            @sub w,8\n"
            "sub       %[dr1], #8            @sub w,8\n"
629
            // swap
H
hong19860320 已提交
630 631 632 633 634 635 636
            "vmov.f32  s0, s17               @mov\n"
            "vmov.f32  s17, s18              @mov\n"
            "vmov.f32  s18, s0               @mov\n"
            "subs      %[cnt_num], #1        @subs cnt_num,#1\n"
            "vst1.f32  d8, [%[dr_out]]!      @vst1 d0,dr_out\n"
            "vst1.f32  d9, [%[dr_out]]!      @vst1 d0,dr_out\n"
            "bne       1b                    @bne s1_max_loop\n"
637 638 639 640 641 642 643
            : [dr0] "+r"(dr0), [dr1] "+r"(dr1), [dr_out] "+r"(dr_out),
              [cnt_num] "+r"(cnt_num)
            : "r"(dr0), "r"(dr1), "r"(dr_out), "r"(cnt_num)
            : "cc", "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6");
      }

#endif
H
hong19860320 已提交
644 645 646
      // remain
      w = w_unroll_size;
      for (int j = 0; j < w_unroll_remain; j++) {
647 648 649
        float tmp_max = std::max(r0[j + w], r1[j + w]);
        tmp_max = std::max(tmp_max, std::max(r0[j + w + 1], r1[j + w + 1]));
        tmp_max = std::max(tmp_max, std::max(r0[j + w + 2], r1[j + w + 2]));
H
hong19860320 已提交
650
        dout_ch[j + w + 1] = tmp_max;
651 652
      }
      // right
H
hong19860320 已提交
653 654 655
      float tmp = std::max(r0[win - 2], r1[win - 2]);
      tmp = std::max(tmp, std::max(r0[win - 1], r1[win - 1]));
      dout_ch[wout - 1] = tmp;
656 657 658 659

      // r0 = r1;
      // r1 = r0 + w_in;
      // r2 = r1 + w_in;
H
hong19860320 已提交
660
      dout_ch += wout;
661
      int h = 0;
H
hong19860320 已提交
662
      for (; h < hin - 2; h += 1) {
663 664 665 666
        // deal with left pad
        float maxr0 = std::max(r0[0], r0[1]);
        float maxr1 = std::max(r1[0], r1[1]);
        float maxr2 = std::max(r2[0], r2[1]);
H
hong19860320 已提交
667
        dout_ch[0] = std::max(std::max(maxr0, maxr1), maxr2);
668 669 670
#ifdef __aarch64__
        w = 0;
        cnt = 1;
H
hong19860320 已提交
671
        for (; w < w_unroll_size; w += 4) {
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
          float32x4_t vr0_1234 = vld1q_f32(&r0[w]);
          float32x4_t vr1_1234 = vld1q_f32(&r1[w]);
          float32x4_t vr2_1234 = vld1q_f32(&r2[w]);
          float32x4_t vr0_5678 = vld1q_f32(&r0[w + 4]);
          float32x4_t vr1_5678 = vld1q_f32(&r1[w + 4]);
          float32x4_t vr2_5678 = vld1q_f32(&r2[w + 4]);
          float32x4_t vmax_1234 = vmaxq_f32(vr0_1234, vr1_1234);
          vmax_1234 = vmaxq_f32(vmax_1234, vr2_1234);
          float32x4_t vmax_5678 = vmaxq_f32(vr0_5678, vr1_5678);
          vmax_5678 = vmaxq_f32(vmax_5678, vr2_5678);

          float32x4_t vmax_2345 = vextq_f32(vmax_1234, vmax_5678, 1);
          float32x4_t vmax_3456 = vextq_f32(vmax_1234, vmax_5678, 2);
          float32x2_t vmax_12_34 =
              vpmax_f32(vget_low_f32(vmax_1234), vget_high_f32(vmax_1234));
          float32x2_t vmax_23_45 =
              vpmax_f32(vget_low_f32(vmax_2345), vget_high_f32(vmax_2345));
          float32x2_t vmax_34_56 =
              vpmax_f32(vget_low_f32(vmax_3456), vget_high_f32(vmax_3456));
          float32x2_t vmax_123_345 = vmax_f32(vmax_12_34, vmax_23_45);
          float32x2_t vmax_234_456 = vmax_f32(vmax_23_45, vmax_34_56);
          float32x4_t vmax = vdupq_n_f32(vget_lane_f32(vmax_123_345, 0));
          vmax = vsetq_lane_f32(vget_lane_f32(vmax_234_456, 0), vmax, 1);
          vmax = vsetq_lane_f32(vget_lane_f32(vmax_123_345, 1), vmax, 2);
          vmax = vsetq_lane_f32(vget_lane_f32(vmax_234_456, 1), vmax, 3);
H
hong19860320 已提交
697
          vst1q_f32(&dout_ch[cnt], vmax);
698 699 700
          cnt += 4;
        }
#else
H
hong19860320 已提交
701
        dr_out = dout_ch + 1;
702 703 704
        dr0 = r0;
        dr1 = r1;
        dr2 = r2;
H
hong19860320 已提交
705
        cnt_num = w_unroll_size >> 2;
706 707
        if (cnt_num > 0) {
          asm volatile(
H
hong19860320 已提交
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
              "1:                             @main loop\n"
              "vld1.f32  {d0-d1}, [%[dr0]]!   @load d0-d5,dr0\n"
              "vld1.f32  {d4-d5}, [%[dr1]]!   @load d4-d7,dr1\n"
              "vld1.f32  {d8-d9}, [%[dr2]]!   @load d4-d7,dr1\n"
              "vld1.f32  {d2}, [%[dr0]]!      @load d0-d5,dr0\n"
              "vld1.f32  {d6}, [%[dr1]]!      @load d4-d7,dr1\n"
              "vld1.f32  {d10}, [%[dr2]]!     @load d4-d7, dr1\n"
              "vmax.f32  q7, q0, q2           @max r0_1234,r1_1234\n"
              "vmax.f32  d16, d2, d6          @max r0_5678,r1_5678\n"
              "vmax.f32  q3, q7, q4           @max r0_1234,r1_1234\n"
              "vmax.f32  d12, d16, d10        @max r0_5678,r1_5678\n"
              //"vmov.f32  s7,s6              @mov s7,s6\n"
              "vext.f32  q0, q3, q6, #1       @vext max_2345\n"
              "vext.f32  q2, q3, q6, #2       @vext max_3456\n"
              "vpmax.f32 d2, d6, d7           @pmax d4,max_1234,max_1234\n"
              "vpmax.f32 d3, d0, d1           @pmax d4,max_2345,max_2345\n"
              "vpmax.f32 d6, d4, d5           @pmax d6,max_3456,max_3456\n"
              "vmax.f32  d8, d2, d3           @max d2,vmax_12_34,vmax_23_45\n"
              "vmax.f32  d9, d3, d6           @max d2,vmax_23_45,vmax_34_56\n"
              "sub       %[dr0], #8           @sub w,8\n"
              "sub       %[dr1], #8           @sub w,8\n"
              "sub       %[dr2], #8           @sub w,8\n"
730
              // swap
H
hong19860320 已提交
731 732 733 734 735 736 737
              "vmov.f32  s0, s17              @mov\n"
              "vmov.f32  s17, s18             @mov\n"
              "vmov.f32  s18, s0              @mov\n"
              "subs      %[cnt_num], #1       @subs cnt_num,#1\n"
              "vst1.f32  d8, [%[dr_out]]!     @vst1 d0,dr_out\n"
              "vst1.f32  d9, [%[dr_out]]!     @vst1 d0,dr_out\n"
              "bne       1b                   @bne s1_max_loop\n"
738 739 740 741 742 743 744
              : [dr0] "+r"(dr0), [dr1] "+r"(dr1), [dr2] "+r"(dr2),
                [dr_out] "+r"(dr_out), [cnt_num] "+r"(cnt_num)
              : "r"(dr0), "r"(dr1), "r"(dr_out), "r"(cnt_num)
              : "cc", "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
                "q8");
        }
#endif
H
hong19860320 已提交
745 746 747
        // remain
        w = w_unroll_size;
        for (int j = 0; j < w_unroll_remain; j++) {
748 749 750 751 752
          float tmp_max = std::max(r0[j + w], r1[j + w]);
          tmp_max = std::max(tmp_max, std::max(r0[j + w + 1], r1[j + w + 1]));
          tmp_max = std::max(tmp_max, std::max(r0[j + w + 2], r1[j + w + 2]));
          tmp_max = std::max(tmp_max, std::max(r2[j + w], r2[j + w + 1]));
          tmp_max = std::max(tmp_max, r2[j + w + 2]);
H
hong19860320 已提交
753
          dout_ch[j + w + 1] = tmp_max;
754 755
        }
        // right
H
hong19860320 已提交
756 757 758 759
        tmp = std::max(r0[win - 2], r1[win - 2]);
        tmp = std::max(tmp, std::max(r0[win - 1], r1[win - 1]));
        tmp = std::max(tmp, std::max(r2[win - 2], r2[win - 1]));
        dout_ch[wout - 1] = tmp;
760 761 762

        r0 = r1;
        r1 = r2;
H
hong19860320 已提交
763 764
        r2 = r1 + win;
        dout_ch += wout;
765 766 767 768 769
      }

      // the last two line
      float maxr0 = std::max(r0[0], r0[1]);
      float maxr1 = std::max(r1[0], r1[1]);
H
hong19860320 已提交
770
      dout_ch[0] = std::max(maxr0, maxr1);
771 772 773
#ifdef __aarch64__
      w = 0;
      cnt = 1;
H
hong19860320 已提交
774
      for (; w < w_unroll_size; w += 4) {
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
        float32x4_t vr0_1234 = vld1q_f32(&r0[w]);
        float32x4_t vr1_1234 = vld1q_f32(&r1[w]);
        float32x4_t vr0_5678 = vld1q_f32(&r0[w + 4]);
        float32x4_t vr1_5678 = vld1q_f32(&r1[w + 4]);
        float32x4_t vmax_1234 = vmaxq_f32(vr0_1234, vr1_1234);
        float32x4_t vmax_5678 = vmaxq_f32(vr0_5678, vr1_5678);

        float32x4_t vmax_2345 = vextq_f32(vmax_1234, vmax_5678, 1);
        float32x4_t vmax_3456 = vextq_f32(vmax_1234, vmax_5678, 2);
        float32x2_t vmax_12_34 =
            vpmax_f32(vget_low_f32(vmax_1234), vget_high_f32(vmax_1234));
        float32x2_t vmax_23_45 =
            vpmax_f32(vget_low_f32(vmax_2345), vget_high_f32(vmax_2345));
        float32x2_t vmax_34_56 =
            vpmax_f32(vget_low_f32(vmax_3456), vget_high_f32(vmax_3456));
        float32x2_t vmax_123_345 = vmax_f32(vmax_12_34, vmax_23_45);
        float32x2_t vmax_234_456 = vmax_f32(vmax_23_45, vmax_34_56);
        float32x4_t vmax = vdupq_n_f32(vget_lane_f32(vmax_123_345, 0));
        vmax = vsetq_lane_f32(vget_lane_f32(vmax_234_456, 0), vmax, 1);
        vmax = vsetq_lane_f32(vget_lane_f32(vmax_123_345, 1), vmax, 2);
        vmax = vsetq_lane_f32(vget_lane_f32(vmax_234_456, 1), vmax, 3);
H
hong19860320 已提交
796
        vst1q_f32(&dout_ch[cnt], vmax);
797 798 799
        cnt += 4;
      }
#else
H
hong19860320 已提交
800
      dr_out = dout_ch + 1;
801 802
      dr0 = r0;
      dr1 = r1;
H
hong19860320 已提交
803
      cnt_num = w_unroll_size >> 2;
804 805
      if (cnt_num > 0) {
        asm volatile(
H
hong19860320 已提交
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
            "1:                              @main loop\n"
            "vld1.f32  {d0-d1}, [%[dr0]]!    @load d0-d5,dr0\n"
            "vld1.f32  {d4-d5}, [%[dr1]]!    @load d4-d7,dr1\n"
            "vld1.f32  {d2}, [%[dr0]]!       @load d0-d5,dr0\n"
            "vld1.f32  {d6}, [%[dr1]]!       @load d4-d7,dr1\n"
            "vmax.f32  q5, q0, q2            @max r0_1234,r1_1234\n"
            "vmax.f32  d12, d2, d6           @max r0_5678,r1_5678\n"
            //"vmov.f32  s7,s6               @mov s7,s6\n"
            "vext.f32  q0, q5, q6, #1        @vext max_2345\n"
            "vext.f32  q2, q5, q6, #2        @vext max_3456\n"
            "vpmax.f32 d2, d10, d11          @pmax d4,max_1234,max_1234\n"
            "vpmax.f32 d3, d0, d1            @pmax d4,max_2345,max_2345\n"
            "vpmax.f32 d6, d4, d5            @pmax d6,max_3456,max_3456\n"
            "vmax.f32  d8, d2, d3            @max d2,vmax_12_34,vmax_23_45\n"
            "vmax.f32  d9, d3, d6            @max d2,vmax_23_45,vmax_34_56\n"
            "sub       %[dr0], #8            @sub w,8\n"
            "sub       %[dr1], #8            @sub w,8\n"
823
            // swap
H
hong19860320 已提交
824 825 826 827 828 829 830
            "vmov.f32  s0, s17               @mov\n"
            "vmov.f32  s17, s18              @mov\n"
            "vmov.f32  s18, s0               @mov\n"
            "subs      %[cnt_num], #1        @subs cnt_num,#1\n"
            "vst1.f32  d8, [%[dr_out]]!      @vst1 d0,dr_out\n"
            "vst1.f32  d9, [%[dr_out]]!      @vst1 d0,dr_out\n"
            "bne       1b                    @bne s1_max_loop\n"
831 832 833 834 835 836 837
            : [dr0] "+r"(dr0), [dr1] "+r"(dr1), [dr_out] "+r"(dr_out),
              [cnt_num] "+r"(cnt_num)
            : "r"(dr0), "r"(dr1), "r"(dr_out), "r"(cnt_num)
            : "cc", "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6");
      }
#endif
      // remian
H
hong19860320 已提交
838 839
      w = w_unroll_size;
      for (int j = 0; j < w_unroll_remain; j++) {
840 841 842
        float tmp_max = std::max(r0[j + w], r1[j + w]);
        tmp_max = std::max(tmp_max, std::max(r0[j + w + 1], r1[j + w + 1]));
        tmp_max = std::max(tmp_max, std::max(r0[j + w + 2], r1[j + w + 2]));
H
hong19860320 已提交
843
        dout_ch[j + w + 1] = tmp_max;
844
      }
H
hong19860320 已提交
845 846 847
      tmp = std::max(r0[win - 2], r1[win - 2]);
      tmp = std::max(tmp, std::max(r0[win - 1], r1[win - 1]));
      dout_ch[wout - 1] = tmp;
848 849 850 851
    }
  }
}

H
hong19860320 已提交
852
void pooling3x3s1p1_avg(const float* din, float* dout, int num, int chout,
853
                        int hout, int wout, int chin, int hin, int win,
H
hong19860320 已提交
854 855 856 857 858 859
                        bool exclusive) {
  int kernel = 3;
  int stride = 1;
  int padding = 1;
  int size_channel_out = wout * hout;
  int size_channel_in = win * hin;
860

H
hong19860320 已提交
861 862 863 864 865 866 867 868 869 870
  int w_unroll_size = ((win - 2) >> 2) << 2;
  int w_unroll_remain = win - 2 - w_unroll_size;
  const float coef = 1.f / 9.f;
  const float coef_2 = exclusive ? 1.f / 2.f : coef;
  const float coef_4 = exclusive ? 1.f / 4.f : coef;
  const float coef_6 = exclusive ? 1.f / 6.f : coef;
  float32x4_t vcoef = vdupq_n_f32(coef);
  float32x4_t vcoef_2 = vdupq_n_f32(coef_2);
  float32x4_t vcoef_4 = vdupq_n_f32(coef_4);
  float32x4_t vcoef_6 = vdupq_n_f32(coef_6);
871
  for (int n = 0; n < num; ++n) {
H
hong19860320 已提交
872 873
    float* dout_batch = dout + n * chout * size_channel_out;
    const float* din_batch = din + n * chin * size_channel_in;
874
#pragma omp parallel for
H
hong19860320 已提交
875 876 877 878 879 880 881 882
    for (int c = 0; c < chout; c++) {
      float* dout_ch = dout_batch + c * size_channel_out;
      const float* din_ch = din_batch + c * size_channel_in;
      const float* r0 = din_ch;
      const float* r1 = r0 + win;
      const float* r2 = r1 + win;
      int cnt_num = w_unroll_size >> 2;  // w_unroll_size / 4
      float* dr_out = dout_ch;
883 884 885 886 887 888
      const float* dr0 = r0;
      const float* dr1 = r1;
      const float* dr2 = r2;
      int w = 0;
      int cnt = 1;
      // left
H
hong19860320 已提交
889
      dout_ch[0] = (r0[0] + r0[1] + r1[0] + r1[1]) * coef_4;
890 891
// first row with zero pad
#ifdef __aarch64__
H
hong19860320 已提交
892
      for (; w < w_unroll_size; w += 4) {
893 894 895 896 897 898 899 900 901 902 903
        float32x4_t vr0_1234 = vld1q_f32(&r0[w]);
        float32x4_t vr1_1234 = vld1q_f32(&r1[w]);
        float32x4_t vr0_5678 = vld1q_f32(&r0[w + 4]);
        float32x4_t vr1_5678 = vld1q_f32(&r1[w + 4]);
        float32x4_t vsum_1234 = vaddq_f32(vr0_1234, vr1_1234);
        float32x4_t vsum_5678 = vaddq_f32(vr0_5678, vr1_5678);

        float32x4_t vsum_2345 = vextq_f32(vsum_1234, vsum_5678, 1);
        float32x4_t vsum_3456 = vextq_f32(vsum_1234, vsum_5678, 2);
        float32x4_t vsum = vaddq_f32(vsum_1234, vsum_2345);
        vsum = vaddq_f32(vsum, vsum_3456);
H
hong19860320 已提交
904 905
        vsum = vmulq_f32(vsum, vcoef_6);
        vst1q_f32(&dout_ch[cnt], vsum);
906 907 908 909 910 911
        cnt += 4;
      }
#else
      dr_out = dr_out + 1;
      if (cnt_num > 0) {
        asm volatile(
H
hong19860320 已提交
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
            "1:                               @main loop\n"
            "vld1.f32  {d0-d1}, [%[dr0]]!     @load d0-d5,dr0\n"
            "vld1.f32  {d4-d5}, [%[dr1]]!     @load d4-d7,dr1\n"
            "vld1.f32  {d2}, [%[dr0]]!        @load d0-d5,dr0\n"
            "vld1.f32  {d6}, [%[dr1]]!        @load d4-d7,dr1\n"
            "vadd.f32  q5, q0, q2             @max r0_1234,r1_1234\n"
            "vadd.f32  d12, d2, d6            @max r0_5678,r1_5678\n"
            //"vmov.f32  s7,s6                @mov s7,s6\n"
            "vext.f32  q0, q5, q6, #1         @vext max_2345\n"
            "vext.f32  q2, q5, q6, #2         @vext max_3456\n"
            "vadd.f32  q1, q5, q0             @add 1234+2345\n"
            "vadd.f32  q1, q1, q2             @add + 3456\n"
            "vmul.f32  q4, q1, %q[vcoef_6]    @mul * 1/9.f\n"
            "sub       %[dr0], #8             @sub w,8\n"
            "sub       %[dr1], #8             @sub w,8\n"
            "subs      %[cnt_num], #1         @subs cnt_num,#1\n"
            "vst1.f32  d8, [%[dr_out]]!       @vst1 d0,dr_out\n"
            "vst1.f32  d9, [%[dr_out]]!       @vst1 d0,dr_out\n"
            "bne       1b                     @bne s1_max_loop\n"
931
            : [dr0] "+r"(dr0), [dr1] "+r"(dr1), [dr_out] "+r"(dr_out),
H
hong19860320 已提交
932
              [cnt_num] "+r"(cnt_num), [vcoef_6] "+w"(vcoef_6)
933 934 935 936 937
            : "r"(dr0), "r"(dr1), "r"(dr_out), "r"(cnt_num)
            : "cc", "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6");
      }

#endif
H
hong19860320 已提交
938 939 940
      // remain
      w = w_unroll_size;
      for (int j = 0; j < w_unroll_remain; j++) {
941 942 943
        float tmp_sum = r0[j + w] + r1[j + w];
        tmp_sum += (r0[j + w + 1] + r1[j + w + 1]);
        tmp_sum += (r0[j + w + 2] + r1[j + w + 2]);
H
hong19860320 已提交
944
        dout_ch[j + w + 1] = tmp_sum * coef_6;
945 946
      }
      // right
H
hong19860320 已提交
947 948 949
      float tmp = r0[win - 2] + r1[win - 2];
      tmp += (r0[win - 1] + r1[win - 1]);
      dout_ch[wout - 1] = tmp * coef_4;
950 951 952 953

      // r0 = r1;
      // r1 = r0 + w_in;
      // r2 = r1 + w_in;
H
hong19860320 已提交
954
      dout_ch += wout;
955
      int h = 0;
H
hong19860320 已提交
956
      for (; h < hin - 2; h += 1) {
957 958 959 960
        // deal with left pad
        float maxr0 = r0[0] + r0[1];
        float maxr1 = r1[0] + r1[1];
        float maxr2 = r2[0] + r2[1];
H
hong19860320 已提交
961
        dout_ch[0] = (maxr0 + maxr1 + maxr2) * coef_6;
962 963 964
#ifdef __aarch64__
        w = 0;
        cnt = 1;
H
hong19860320 已提交
965
        for (; w < w_unroll_size; w += 4) {
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
          float32x4_t vr0_1234 = vld1q_f32(&r0[w]);
          float32x4_t vr1_1234 = vld1q_f32(&r1[w]);
          float32x4_t vr2_1234 = vld1q_f32(&r2[w]);
          float32x4_t vr0_5678 = vld1q_f32(&r0[w + 4]);
          float32x4_t vr1_5678 = vld1q_f32(&r1[w + 4]);
          float32x4_t vr2_5678 = vld1q_f32(&r2[w + 4]);
          float32x4_t vsum_1234 = vaddq_f32(vr0_1234, vr1_1234);
          vsum_1234 = vaddq_f32(vsum_1234, vr2_1234);
          float32x4_t vsum_5678 = vaddq_f32(vr0_5678, vr1_5678);
          vsum_5678 = vaddq_f32(vsum_5678, vr2_5678);

          float32x4_t vsum_2345 = vextq_f32(vsum_1234, vsum_5678, 1);
          float32x4_t vsum_3456 = vextq_f32(vsum_1234, vsum_5678, 2);
          float32x4_t vsum = vaddq_f32(vsum_1234, vsum_2345);
          vsum = vaddq_f32(vsum, vsum_3456);
          vsum = vmulq_f32(vsum, vcoef);
H
hong19860320 已提交
982
          vst1q_f32(&dout_ch[cnt], vsum);
983 984 985
          cnt += 4;
        }
#else
H
hong19860320 已提交
986
        dr_out = dout_ch + 1;
987 988 989
        dr0 = r0;
        dr1 = r1;
        dr2 = r2;
H
hong19860320 已提交
990
        cnt_num = w_unroll_size >> 2;
991 992
        if (cnt_num > 0) {
          asm volatile(
H
hong19860320 已提交
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
              "1:                            @main loop\n"
              "vld1.f32  {d0-d1}, [%[dr0]]!  @load d0-d5,dr0\n"
              "vld1.f32  {d4-d5}, [%[dr1]]!  @load d4-d7,dr1\n"
              "vld1.f32  {d8-d9}, [%[dr2]]!  @load d4-d7,dr1\n"
              "vld1.f32  {d2}, [%[dr0]]!     @load d0-d5,dr0\n"
              "vld1.f32  {d6}, [%[dr1]]!     @load d4-d7,dr1\n"
              "vld1.f32  {d10}, [%[dr2]]!    @load d4-d7,dr1\n"
              "vadd.f32  q7, q0, q2          @max r0_1234,r1_1234\n"
              "vadd.f32  d16, d2, d6         @max r0_5678,r1_5678\n"
              "vadd.f32  q3, q7, q4          @max r0_1234,r1_1234\n"
              "vadd.f32  d12, d16, d10       @max r0_5678,r1_5678\n"
              //"vmov.f32  s7,s6             @mov s7,s6\n"
              "vext.f32  q0, q3, q6, #1      @vext max_2345\n"
              "vext.f32  q2, q3, q6, #2      @vext max_3456\n"
              "vadd.f32  q1, q3, q0          @add 1234+2345\n"
              "vadd.f32  q1, q1, q2          @add+3456\n"
              "vmul.f32  q4, q1, %q[vcoef]   @mul*1/9.f\n"
              "sub       %[dr0], #8          @sub w,8\n"
              "sub       %[dr1], #8          @sub w,8\n"
              "sub       %[dr2], #8          @sub w,8\n"
              "subs      %[cnt_num], #1      @subs cnt_num,#1\n"
              "vst1.f32  d8, [%[dr_out]]!    @vst1 d0,dr_out\n"
              "vst1.f32  d9, [%[dr_out]]!    @vst1 d0,dr_out\n"
              "bne       1b                  @bne s1_max_loop\n"
1017 1018 1019 1020 1021 1022 1023 1024
              : [dr0] "+r"(dr0), [dr1] "+r"(dr1), [dr2] "+r"(dr2),
                [dr_out] "+r"(dr_out), [cnt_num] "+r"(cnt_num),
                [vcoef] "+w"(vcoef)
              : "r"(dr0), "r"(dr1), "r"(dr_out), "r"(cnt_num)
              : "cc", "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
                "q8");
        }
#endif
H
hong19860320 已提交
1025 1026 1027
        // remain
        w = w_unroll_size;
        for (int j = 0; j < w_unroll_remain; j++) {
1028 1029 1030 1031 1032
          float tmp_sum = r0[j + w] + r1[j + w];
          tmp_sum += (r0[j + w + 1] + r1[j + w + 1]);
          tmp_sum += (r0[j + w + 2] + r1[j + w + 2]);
          tmp_sum += (r2[j + w + 1] + r2[j + w + 2]);
          tmp_sum += r2[j + w];
H
hong19860320 已提交
1033
          dout_ch[j + w + 1] = tmp_sum * coef;
1034 1035
        }
        // right
H
hong19860320 已提交
1036 1037 1038 1039
        tmp = r0[win - 2] + r1[win - 2];
        tmp += (r0[win - 1] + r1[win - 1]);
        tmp += (r2[win - 2] + r2[win - 1]);
        dout_ch[wout - 1] = tmp * coef_6;
1040 1041 1042

        r0 = r1;
        r1 = r2;
H
hong19860320 已提交
1043 1044
        r2 = r1 + win;
        dout_ch += wout;
1045 1046
      }

H
hong19860320 已提交
1047
      // last line
1048 1049
      float maxr0 = (r0[0] + r0[1]);
      float maxr1 = (r1[0] + r1[1]);
H
hong19860320 已提交
1050
      dout_ch[0] = (maxr0 + maxr1) * coef_4;
1051 1052 1053
#ifdef __aarch64__
      w = 0;
      cnt = 1;
H
hong19860320 已提交
1054
      for (; w < w_unroll_size; w += 4) {
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
        float32x4_t vr0_1234 = vld1q_f32(&r0[w]);
        float32x4_t vr1_1234 = vld1q_f32(&r1[w]);
        float32x4_t vr0_5678 = vld1q_f32(&r0[w + 4]);
        float32x4_t vr1_5678 = vld1q_f32(&r1[w + 4]);
        float32x4_t vsum_1234 = vaddq_f32(vr0_1234, vr1_1234);
        float32x4_t vsum_5678 = vaddq_f32(vr0_5678, vr1_5678);

        float32x4_t vsum_2345 = vextq_f32(vsum_1234, vsum_5678, 1);
        float32x4_t vsum_3456 = vextq_f32(vsum_1234, vsum_5678, 2);
        float32x4_t vsum = vaddq_f32(vsum_1234, vsum_2345);
        vsum = vaddq_f32(vsum, vsum_3456);
H
hong19860320 已提交
1066 1067
        vsum = vmulq_f32(vsum, vcoef_6);
        vst1q_f32(&dout_ch[cnt], vsum);
1068 1069 1070
        cnt += 4;
      }
#else
H
hong19860320 已提交
1071
      dr_out = dout_ch + 1;
1072 1073
      dr0 = r0;
      dr1 = r1;
H
hong19860320 已提交
1074
      cnt_num = w_unroll_size >> 2;
1075 1076
      if (cnt_num > 0) {
        asm volatile(
H
hong19860320 已提交
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
            "1:                              @main loop\n"
            "vld1.f32  {d0-d1}, [%[dr0]]!    @load d0-d5,dr0\n"
            "vld1.f32  {d4-d5}, [%[dr1]]!    @load d4-d7,dr1\n"
            "vld1.f32  {d2}, [%[dr0]]!       @load d0-d5,dr0\n"
            "vld1.f32  {d6}, [%[dr1]]!       @load d4-d7,dr1\n"
            "vadd.f32  q5, q0, q2            @max r0_1234,r1_1234\n"
            "vadd.f32  d12, d2, d6           @max r0_5678,r1_5678\n"
            //"vmov.f32  s7,s6               @mov s7,s6\n"
            "vext.f32  q0, q5, q6, #1        @vext max_2345\n"
            "vext.f32  q2, q5, q6, #2        @vext max_3456\n"
            "vadd.f32  q1, q5, q0            @add 1234+2345\n"
            "vadd.f32  q1, q1, q2            @add + 3456\n"
            "vmul.f32  q4, q1, %q[vcoef_6]   @mul * 1/9.f\n"
            "sub       %[dr0], #8            @sub w,8\n"
            "sub       %[dr1], #8            @sub w,8\n"
            "subs      %[cnt_num], #1        @subs cnt_num,#1\n"
            "vst1.f32  d8, [%[dr_out]]!      @vst1 d0,dr_out\n"
            "vst1.f32  d9, [%[dr_out]]!      @vst1 d0,dr_out\n"
            "bne       1b                    @bne s1_max_loop\n"
1096
            : [dr0] "+r"(dr0), [dr1] "+r"(dr1), [dr_out] "+r"(dr_out),
H
hong19860320 已提交
1097
              [cnt_num] "+r"(cnt_num), [vcoef_6] "+w"(vcoef_6)
1098 1099 1100 1101
            : "r"(dr0), "r"(dr1), "r"(dr_out), "r"(cnt_num)
            : "cc", "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6");
      }
#endif
H
hong19860320 已提交
1102 1103 1104
      // remain
      w = w_unroll_size;
      for (int j = 0; j < w_unroll_remain; j++) {
1105 1106 1107
        float tmp_sum = r0[j + w] + r1[j + w];
        tmp_sum += (r0[j + w + 1] + r1[j + w + 1]);
        tmp_sum += (r0[j + w + 2] + r1[j + w + 2]);
H
hong19860320 已提交
1108
        dout_ch[j + w + 1] = tmp_sum * coef_6;
1109 1110
      }
      // right
H
hong19860320 已提交
1111 1112 1113
      tmp = r0[win - 2] + r1[win - 2];
      tmp += (r0[win - 1] + r1[win - 1]);
      dout_ch[wout - 1] = tmp * coef_4;
1114 1115 1116 1117
    }
  }
}

H
hong19860320 已提交
1118 1119 1120 1121 1122
void pooling3x3s2p1_max(const float* din, float* dout, int num, int chout,
                        int hout, int wout, int chin, int hin, int win) {
  int kernel = 3;
  int stride = 2;
  int padding = 1;
1123 1124 1125
  int size_channel_out = wout * hout;
  int size_channel_in = win * hin;

H
hong19860320 已提交
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
  int w_needed = (wout << 1) + 1;
  int h_needed = (hout << 1) + 1;
  int w_limit = w_needed > win ? win : w_needed;
  int h_limit = h_needed > hin ? hin : h_needed;
  int w_even = (w_limit >> 1) << 1;
  int h_even = (h_limit >> 1) << 1;
  int w_unroll_size = ((w_even - 1) >> 3) << 3;
  int w_unroll_remain = w_even - 1 - w_unroll_size;
  int w_remain = w_needed - w_limit - padding;
  int h_remain = h_needed - h_limit - padding;
1136 1137 1138
  int w_in_2 = win << 1;
  float minval = std::numeric_limits<float>::lowest();
  for (int n = 0; n < num; ++n) {
H
hong19860320 已提交
1139 1140
    float* dout_batch = dout + n * chout * size_channel_out;
    const float* din_batch = din + n * chin * size_channel_in;
1141 1142
#pragma omp parallel for
    for (int c = 0; c < chout; c++) {
H
hong19860320 已提交
1143 1144 1145
      float* dout_ch = dout_batch + c * size_channel_out;
      const float* din_ch = din_batch + c * size_channel_in;
      const float* r0 = din_ch;
1146 1147
      const float* r1 = r0 + win;
      const float* r2 = r1 + win;
H
hong19860320 已提交
1148 1149 1150
      int cnt_num = w_unroll_size >> 3;
      int cnt_num_remain = w_unroll_remain >> 1;
      float* dr_out = dout_ch;
1151 1152 1153 1154 1155
      const float* dr0 = r0;
      const float* dr1 = r1;
      const float* dr2 = r2;
      int w = 1;
      int cnt = 1;
H
hong19860320 已提交
1156
      dout_ch[0] = std::max(std::max(r0[0], r0[1]), std::max(r1[0], r1[1]));
1157
// first row with zero pad
H
hong19860320 已提交
1158 1159
#if __aarch64__
      for (; w < w_unroll_size; w += 8) {
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
        float32x4_t vr0_1234 = vld1q_f32(&r0[w]);
        float32x4_t vr0_5678 = vld1q_f32(&r0[w + 4]);
        float32x4_t vr0_9101112 = vld1q_f32(&r0[w + 8]);
        float32x4_t vr1_1234 = vld1q_f32(&r1[w]);
        float32x4_t vr1_5678 = vld1q_f32(&r1[w + 4]);
        float32x4_t vr1_9101112 = vld1q_f32(&r1[w + 8]);
        float32x4_t vmax_1234 = vmaxq_f32(vr0_1234, vr1_1234);
        float32x4_t vmax_5678 = vmaxq_f32(vr0_5678, vr1_5678);
        float32x4_t vmax_9101112 = vmaxq_f32(vr0_9101112, vr1_9101112);
        float32x4_t vmax_2345 = vextq_f32(vmax_1234, vmax_5678, 1);
        float32x4_t vmax_6789 = vextq_f32(vmax_5678, vmax_9101112, 1);
        float32x2_t vmax_12_34 =
            vpmax_f32(vget_low_f32(vmax_1234), vget_high_f32(vmax_1234));
        float32x2_t vmax_23_45 =
            vpmax_f32(vget_low_f32(vmax_2345), vget_high_f32(vmax_2345));
        float32x2_t vmax_56_78 =
            vpmax_f32(vget_low_f32(vmax_5678), vget_high_f32(vmax_5678));
        float32x2_t vmax_67_89 =
            vpmax_f32(vget_low_f32(vmax_6789), vget_high_f32(vmax_6789));
        float32x2_t vmax_123_345 = vmax_f32(vmax_12_34, vmax_23_45);
        float32x2_t vmax_567_789 = vmax_f32(vmax_56_78, vmax_67_89);
H
hong19860320 已提交
1181 1182
        vst1_f32(&dout_ch[cnt], vmax_123_345);
        vst1_f32(&dout_ch[cnt + 2], vmax_567_789);
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
        cnt += 4;
      }
      for (; w < w_even - 1; w += 2) {
        float32x4_t vr0 = vld1q_f32(&r0[w]);
        float32x4_t vr1 = vld1q_f32(&r1[w]);
        vr0 = vsetq_lane_f32(minval, vr0, 3);
        vr1 = vsetq_lane_f32(minval, vr1, 3);
        float32x4_t vmax1 = vmaxq_f32(vr0, vr1);
        float32x2_t vmax2 =
            vpmax_f32(vget_low_f32(vmax1), vget_high_f32(vmax1));
        vmax2 = vpmax_f32(vmax2, vmax2);
H
hong19860320 已提交
1194
        dout_ch[cnt] = vget_lane_f32(vmax2, 0);
1195 1196 1197 1198 1199 1200
        cnt++;
      }
#else
      dr0 = dr0 + 1;
      dr1 = dr1 + 1;
      dr_out = dr_out + 1;
H
hong19860320 已提交
1201 1202 1203
      // LOG(INFO) << "cnt_num: " << cnt_num << " cnt_num_remain: " <<
      // cnt_num_remain;
      if (cnt_num > 0 || cnt_num_remain > 0) {
1204
        asm volatile(
H
hong19860320 已提交
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
            "cmp       %[cnt_num], #0        @cmp cnt_num,0\n"
            "ble       3f                    @ble exit\n"
            "1:                              @main loop\n"
            "vld1.f32  {d0-d3}, [%[dr0]]!    @load d0-d5,dr0\n"
            "vld1.f32  {d6-d9}, [%[dr1]]!    @load d4-d7,dr1\n"
            "vld1.f32  {d4-d5}, [%[dr0]]!    @load d0-d5,dr0\n"
            "vld1.f32  {d10-d11}, [%[dr1]]!  @load d4-d7,dr1\n"
            "vmax.f32  q6, q0, q3            @max r0_1234,r1_1234\n"
            "vmax.f32  q7, q1, q4            @max r0_5678,r1_5678\n"
            "vmax.f32  q8, q2, q5            @max r0_9101112,r1_9101112\n"
            //"vmov.f32  s7,s6               @mov s7,s6\n"
            "vext.f32  q0, q6, q7, #1        @vext max_2345\n"
            "vext.f32  q1, q7, q8, #1        @vext max_6789\n"
            "vpmax.f32 d4, d12, d13          @pmax d4,vmax_1234,vmax_1234\n"
            "vpmax.f32 d6, d14, d15          @pmax d6,vmax_5678,vmax_5678\n"
            "vpmax.f32 d5, d0, d1            @pmax d5,vmax_2345,vmax_2345\n"
            "vpmax.f32 d7, d2, d3            @pmax d7,vmax_6789,vmax_6789\n"
            "vmax.f32 d8, d4, d5             @max d2,vmax_12_34,vmax_23_45\n"
            "vmax.f32 d9, d6, d7             @max d2,vmax_56_78,vmax_67_89\n"
            "sub       %[dr0], #16           @add w,8\n"
            "sub       %[dr1], #16           @add w, 8\n"
            "vst1.f32  d8, [%[dr_out]]!      @vst1 d0,dr_out\n"
            "vst1.f32  d9, [%[dr_out]]!      @vst1 d0,dr_out\n"
            "subs      %[cnt_num], #1        @subs cnt_num, #1\n"
            "bne       1b                    @bne s3_max_loop\n"
            "3:                              @loop \n"
            "cmp       %[cnt_num_remain], #0 @cmp cnt_num,0\n"
            "ble       4f                    @ble exit\n"
            "2:                              @main loop\n"
            "vld1.f32  {d0-d1}, [%[dr0]]!    @load d0-d1,dr0\n"
            "vld1.f32  {d2-d3}, [%[dr1]]!    @load d2-d3,dr1\n"
            "vmov.f32  s3,s2                 @movs3,s2\n"
            "vmov.f32  s7,s6                 @movs7,s6\n"
            "vmax.f32  q0, q0, q1            @max q0,q0,q1\n"
            "vpmax.f32 d0, d0, d1            @pmax d0,d0,d1\n"
            "vpmax.f32 d0, d0, d0            @pmax d0,d0,d0\n"
            "vst1.f32  d0[0], [%[dr_out]]!   @vst d0[0],dr_out\n"
            "sub       %[dr0], #8            @add w,6\n"
            "sub       %[dr1], #8            @add w,6\n"
            "subs      %[cnt_num_remain], #1 @subs cnt_num,#1\n"
            "bne       2b                    @bne s3_max_loop_1\n"
            "4:                              @exit\n"
1247
            : [dr0] "+r"(dr0), [dr1] "+r"(dr1), [dr_out] "+r"(dr_out),
H
hong19860320 已提交
1248 1249
              [cnt_num] "+r"(cnt_num), [cnt_num_remain] "+r"(cnt_num_remain)
            : "r"(dr0), "r"(dr1), "r"(dr_out), "r"(cnt_num), "r"(cnt_num_remain)
1250 1251 1252 1253 1254
            : "cc", "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
              "q8", "q9");
      }
#endif
      // int w = w_even - 1;
H
hong19860320 已提交
1255
      if (w_remain > 0) {
1256
        // deal with right pad
H
hong19860320 已提交
1257 1258
        int wstart = (w_even >> 1) * stride - padding;
        int wend = std::min(std::min(wstart + kernel, win + padding), win);
1259 1260 1261 1262
        float tmp = r0[wstart];  // std::numeric_limits<float>::min();
        for (int i = wstart; i < wend; i++) {  // only run 1 or 2 times
          tmp = std::max(tmp, std::max(r0[i], r1[i]));
        }
H
hong19860320 已提交
1263
        dout_ch[w_even >> 1] = tmp;
1264 1265 1266 1267 1268 1269
        // cnt ++;
      }

      r0 = r1;
      r1 = r0 + win;
      r2 = r1 + win;
H
hong19860320 已提交
1270
      dout_ch += wout;
1271 1272 1273 1274 1275 1276
      int h = 2;
      for (; h < h_even; h += 2) {
        // deal with left pad
        float maxr0 = std::max(r0[0], r0[1]);
        float maxr1 = std::max(r1[0], r1[1]);
        float maxr2 = std::max(r2[0], r2[1]);
H
hong19860320 已提交
1277 1278
        dout_ch[0] = std::max(std::max(maxr0, maxr1), maxr2);
#if __aarch64__
1279 1280
        w = 1;
        cnt = 1;
H
hong19860320 已提交
1281
        for (; w < w_unroll_size; w += 8) {
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
          float32x4_t vr0_1234 = vld1q_f32(&r0[w]);
          float32x4_t vr0_5678 = vld1q_f32(&r0[w + 4]);
          float32x4_t vr0_9101112 = vld1q_f32(&r0[w + 8]);
          float32x4_t vr1_1234 = vld1q_f32(&r1[w]);
          float32x4_t vr1_5678 = vld1q_f32(&r1[w + 4]);
          float32x4_t vr1_9101112 = vld1q_f32(&r1[w + 8]);
          float32x4_t vr2_1234 = vld1q_f32(&r2[w]);
          float32x4_t vr2_5678 = vld1q_f32(&r2[w + 4]);
          float32x4_t vr2_9101112 = vld1q_f32(&r2[w + 8]);
          float32x4_t vmax_1234 = vmaxq_f32(vr0_1234, vr1_1234);
          vmax_1234 = vmaxq_f32(vmax_1234, vr2_1234);
          float32x4_t vmax_5678 = vmaxq_f32(vr0_5678, vr1_5678);
          vmax_5678 = vmaxq_f32(vmax_5678, vr2_5678);
          float32x4_t vmax_9101112 = vmaxq_f32(vr0_9101112, vr1_9101112);
          vmax_9101112 = vmaxq_f32(vmax_9101112, vr2_9101112);
          float32x4_t vmax_2345 = vextq_f32(vmax_1234, vmax_5678, 1);
          float32x4_t vmax_6789 = vextq_f32(vmax_5678, vmax_9101112, 1);
          float32x2_t vmax_12_34 =
              vpmax_f32(vget_low_f32(vmax_1234), vget_high_f32(vmax_1234));
          float32x2_t vmax_23_45 =
              vpmax_f32(vget_low_f32(vmax_2345), vget_high_f32(vmax_2345));
          float32x2_t vmax_56_78 =
              vpmax_f32(vget_low_f32(vmax_5678), vget_high_f32(vmax_5678));
          float32x2_t vmax_67_89 =
              vpmax_f32(vget_low_f32(vmax_6789), vget_high_f32(vmax_6789));
          float32x2_t vmax_123_345 = vmax_f32(vmax_12_34, vmax_23_45);
          float32x2_t vmax_567_789 = vmax_f32(vmax_56_78, vmax_67_89);
H
hong19860320 已提交
1309 1310
          vst1_f32(&dout_ch[cnt], vmax_123_345);
          vst1_f32(&dout_ch[cnt + 2], vmax_567_789);
1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
          cnt += 4;
        }
        for (; w < w_even - 1; w += 2) {
          float32x4_t vr0 = vld1q_f32(&r0[w]);
          float32x4_t vr1 = vld1q_f32(&r1[w]);
          float32x4_t vr2 = vld1q_f32(&r2[w]);
          vr0 = vsetq_lane_f32(minval, vr0, 3);
          vr1 = vsetq_lane_f32(minval, vr1, 3);
          vr2 = vsetq_lane_f32(minval, vr2, 3);
          float32x4_t vmax1 = vmaxq_f32(vr0, vr1);
          vmax1 = vmaxq_f32(vmax1, vr2);
          float32x2_t vmax2 =
              vpmax_f32(vget_low_f32(vmax1), vget_high_f32(vmax1));
          float32x2_t vmax = vpmax_f32(vmax2, vmax2);
H
hong19860320 已提交
1325
          dout_ch[cnt] = vget_lane_f32(vmax, 0);
1326 1327 1328
          cnt++;
        }
#else
H
hong19860320 已提交
1329
        dr_out = dout_ch + 1;
1330 1331 1332
        dr0 = (r0 + 1);
        dr1 = (r1 + 1);
        dr2 = (r2 + 1);
H
hong19860320 已提交
1333 1334 1335
        cnt_num = w_unroll_size >> 3;
        cnt_num_remain = w_unroll_remain >> 1;
        if (cnt_num > 0 || cnt_num_remain > 0) {
1336
          asm volatile(
H
hong19860320 已提交
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
              "cmp       %[cnt_num], #0        @cmp cnt_num,0\n"
              "ble       3f                    @ble exit\n"
              "1:                              @main loop\n"
              "vld1.f32  {d0-d3}, [%[dr0]]!    @load d0-d5,dr0\n"
              "vld1.f32  {d6-d9}, [%[dr1]]!    @load d4-d7,dr1\n"
              "vld1.f32  {d12-d15}, [%[dr2]]!  @load d4-d7,dr1\n"
              "vld1.f32  {d4-d5}, [%[dr0]]!    @load d0-d5,dr0\n"
              "vld1.f32  {d10-d11}, [%[dr1]]!  @load d4-d7,dr1\n"
              "vld1.f32  {d16-d17}, [%[dr2]]!  @load d4-d7,dr1\n"
              "vmax.f32  q9, q0, q3            @max q0,q0,q2\n"
              "vmax.f32  q10, q1, q4           @max q1,q1,q3\n"
              "vmax.f32  q11, q2, q5           @max q1,q1,q3\n"
              "vmax.f32  q0, q9, q6            @max q0,q0,q2 1234\n"
              "vmax.f32  q3, q10, q7           @max q1,q1,q3 5678\n"
              "vmax.f32  q1, q11, q8           @max q1,q1,q3 9101112\n"
              //"vmov.f32  s7,s6               @mov s7, s6\n"
              "vext.f32  q4, q0, q3, #1        @vext 2345\n"
              "vext.f32  q2, q3, q1, #1        @vext 6789\n"
              "vpmax.f32 d10, d0, d1           @pmax d10,vmax_1234,vmax_1234\n"
              "vpmax.f32 d12, d6, d7           @pmax d12,vmax_5678,vmax_5678\n"
              "vpmax.f32 d11, d8, d9           @pmax d11,vmax_2345,vmax_2345\n"
              "vpmax.f32 d13, d4, d5           @pmax d13,vmax_6789,vmax_6789\n"
              "vmax.f32 d0, d10, d11           @pmax d0,vmax_12_34,vmax_23_45\n"
              "vmax.f32 d1, d12, d13           @pmax d1,vmax_56_78,vmax_67_89\n"
              "sub       %[dr0], #16           @add w,8\n"
              "sub       %[dr1], #16           @add w,8\n"
              "sub       %[dr2], #16           @add w,8\n"
              "vst1.f32  d0, [%[dr_out]]!      @vst1 d0,dr_out\n"
              "vst1.f32  d1, [%[dr_out]]!      @vst1 d0,dr_out\n"
              "subs      %[cnt_num], #1        @subs cnt_num,#1\n"
              "bne       1b                    @bne s3_max_loop_mid\n"
              "3:                              @loop \n"
              "cmp       %[cnt_num_remain], #0 @cmp cnt_num,0\n"
              "ble       4f                    @ble exit1\n"
1371
              "2:                              @mid loop\n"
H
hong19860320 已提交
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
              "vld1.f32  {d0-d1}, [%[dr0]]!    @load d0-d1,dr0\n"
              "vld1.f32  {d2-d3}, [%[dr1]]!    @load d2-d3,dr1\n"
              "vld1.f32  {d4-d5}, [%[dr2]]!    @load d2-d3,dr1\n"
              "vmov.f32  s3,s2                 @movs3,s2\n"
              "vmov.f32  s7,s6                 @movs7,s6\n"
              "vmov.f32  s11,s10               @movs11,s10\n"
              "vmax.f32  q0, q0, q1            @max q0,q0,q1\n"
              "vmax.f32  q0, q0, q2            @max q0,q0,q2\n"
              "vpmax.f32 d0, d0, d1            @pmax d0,d0,d1\n"
              "vpmax.f32 d0, d0, d0            @pmax d0, d0,d0\n"
              "vst1.f32  d0[0], [%[dr_out]]!   @vst d0[0],dr_out\n"
              "sub       %[dr0], #8            @add w,6\n"
              "sub       %[dr1], #8            @add w,6\n"
              "sub       %[dr2], #8            @add w,6\n"
              "subs      %[cnt_num_remain], #1 @subs cnt_num,#1\n"
              "bne       2b                    @bne s3_max_loop_mid_1\n"
              "4:                              @exit\n"
1389 1390
              : [dr0] "+r"(dr0), [dr1] "+r"(dr1), [dr2] "+r"(dr2),
                [dr_out] "+r"(dr_out), [cnt_num] "+r"(cnt_num),
H
hong19860320 已提交
1391
                [cnt_num_remain] "+r"(cnt_num_remain)
1392
              : "r"(dr0), "r"(dr1), "r"(dr2), "r"(dr_out), "r"(cnt_num),
H
hong19860320 已提交
1393
                "r"(cnt_num_remain)
1394 1395 1396 1397
              : "cc", "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
                "q8", "q9", "q10", "q11", "q12");
        }
#endif
H
hong19860320 已提交
1398
        if (w_remain > 0) {
1399
          // deal with right pad
H
hong19860320 已提交
1400 1401
          int wstart = (w_even >> 1) * stride - padding;
          int wend = std::min(std::min(wstart + kernel, win + padding), win);
1402 1403 1404 1405 1406
          float tmp = r0[wstart];  // std::numeric_limits<float>::min();
          for (int i = wstart; i < wend; i++) {
            tmp = std::max(tmp, std::max(r0[i], r1[i]));
            tmp = std::max(tmp, r2[i]);
          }
H
hong19860320 已提交
1407
          dout_ch[w_even >> 1] = tmp;
1408 1409 1410 1411 1412
          // cnt ++;
        }
        r0 = r2;
        r1 = r0 + win;
        r2 = r1 + win;
H
hong19860320 已提交
1413
        dout_ch += wout;
1414 1415
      }

H
hong19860320 已提交
1416
      if (h_remain > 0) {
1417 1418
        // deal with bottom pad
        // first row with zero pad
H
hong19860320 已提交
1419 1420
        int hstart = (h >> 1) * stride - padding;
        int hend = std::min(std::min(hstart + kernel, hin + padding), hin);
1421
        if (hstart == hend - 1) {  // only one lline
H
hong19860320 已提交
1422 1423
          dout_ch[0] = std::max(r0[0], r0[1]);
#if __aarch64__
1424 1425
          w = 1;
          cnt = 1;
H
hong19860320 已提交
1426
          for (; w < w_unroll_size; w += 8) {
1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
            float32x4_t vmax_1234 = vld1q_f32(&r0[w]);
            float32x4_t vmax_5678 = vld1q_f32(&r0[w + 4]);
            float32x4_t vmax_9101112 = vld1q_f32(&r0[w + 8]);
            float32x4_t vmax_2345 = vextq_f32(vmax_1234, vmax_5678, 1);
            float32x4_t vmax_6789 = vextq_f32(vmax_5678, vmax_9101112, 1);
            float32x2_t vmax_12_34 =
                vpmax_f32(vget_low_f32(vmax_1234), vget_high_f32(vmax_1234));
            float32x2_t vmax_23_45 =
                vpmax_f32(vget_low_f32(vmax_2345), vget_high_f32(vmax_2345));
            float32x2_t vmax_56_78 =
                vpmax_f32(vget_low_f32(vmax_5678), vget_high_f32(vmax_5678));
            float32x2_t vmax_67_89 =
                vpmax_f32(vget_low_f32(vmax_6789), vget_high_f32(vmax_6789));
            float32x2_t vmax_123_345 = vmax_f32(vmax_12_34, vmax_23_45);
            float32x2_t vmax_567_789 = vmax_f32(vmax_56_78, vmax_67_89);
H
hong19860320 已提交
1442 1443
            vst1_f32(&dout_ch[cnt], vmax_123_345);
            vst1_f32(&dout_ch[cnt + 2], vmax_567_789);
1444 1445 1446 1447 1448 1449 1450
            cnt += 4;
          }
          for (; w < w_even - 1; w += 2) {
            float32x4_t vr0 = vld1q_f32(&r0[w]);
            vr0 = vsetq_lane_f32(minval, vr0, 3);
            float32x2_t vmax = vpmax_f32(vget_low_f32(vr0), vget_high_f32(vr0));
            vmax = vpmax_f32(vmax, vmax);
H
hong19860320 已提交
1451
            dout_ch[cnt] = vget_lane_f32(vmax, 0);
1452 1453 1454
            cnt++;
          }
#else
H
hong19860320 已提交
1455
          dr_out = dout_ch + 1;
1456
          dr0 = (r0 + 1);
H
hong19860320 已提交
1457 1458 1459 1460 1461
          cnt_num = w_unroll_size >> 3;
          cnt_num_remain = w_unroll_remain >> 1;
          // LOG(INFO) << "cnt_num: " << cnt_num << " cnt_num_remain: " <<
          // cnt_num_remain;
          if (cnt_num > 0 || cnt_num_remain > 0) {
1462
            asm volatile(
H
hong19860320 已提交
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
                "cmp       %[cnt_num], #0            @cmp cnt_num,0\n"
                "ble       3f                        @ble exit\n"
                "1:                                  @main loop\n"
                "vld1.f32  {d0-d3}, [%[dr0]]!        @load d0-d3,dr0\n"
                "vld1.f32  {d4-d5}, [%[dr0]]!        @load d0-d3,dr0\n"
                "vext.f32  q4, q0, q1, #1            @vmax_2345\n"
                "vext.f32  q5, q1, q2, #1            @vmax_6789\n"
                "vpmax.f32 d12, d0, d1               @vmax_12_34\n"
                "vpmax.f32 d14, d2, d3               @vmax_56_78\n"
                "vpmax.f32 d13, d8, d9               @vmax_23_45\n"
                "vpmax.f32 d15, d10, d11             @vmax_67_89\n"
                "vmax.f32  d0, d12, d13              @12_34,23_45\n"
                "vmax.f32  d1, d14, d15              @56_78,67_89\n"
                "sub       %[dr0], #16               @add w,6\n"
                "vst1.f32  d0, [%[dr_out]]!          @vst1 d0,dr_out\n"
                "vst1.f32  d1, [%[dr_out]]!          @vst1 d0,dr_out\n"
                "subs      %[cnt_num], #1            @subs cnt_num,#1\n"
                "bne       1b                        @bne s3_max_loop_bot\n"
                "3:                                  @loop \n"
                "cmp       %[cnt_num_remain], #0     @cmp cnt_num,0\n"
                "ble       4f                        @ble exit\n"
                "2:                                  @bot loop\n"
                "vld1.f32  {d0-d1}, [%[dr0]]!        @load d0-d1,dr0\n"
                "vmov.f32  s3,s2                     @movs3, s2\n"
                "vpmax.f32 d0, d0, d1                @pmax d0,d0,d1\n"
                "vpmax.f32 d0, d0, d0                @pmax d0,d0,d0\n"
                "vst1.f32  d0[0], [%[dr_out]]!       @vst d0[0],dr_out\n"
                "sub       %[dr0], #8                @add w,2\n"
                "subs      %[cnt_num_remain], #1     @subs cnt_num,#1\n"
                "bne       2b                        @bne s3_max_loop_bot_1\n"
                "4:                                  @exit\n"
1494
                : [dr0] "+r"(dr0), [dr1] "+r"(dr1), [dr_out] "+r"(dr_out),
H
hong19860320 已提交
1495 1496 1497
                  [cnt_num] "+r"(cnt_num), [cnt_num_remain] "+r"(cnt_num_remain)
                : "r"(dr0), "r"(dr1), "r"(dr_out), "r"(cnt_num),
                  "r"(cnt_num_remain)
1498 1499 1500 1501
                : "cc", "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6",
                  "q7", "q8");
          }
#endif
H
hong19860320 已提交
1502
          if (w_remain > 0) {
1503
            // deal with right pad
H
hong19860320 已提交
1504 1505
            int wstart = (w_even >> 1) * stride - padding;
            int wend = std::min(std::min(wstart + kernel, win + padding), win);
1506 1507 1508 1509
            float tmp = r0[wstart];  // std::numeric_limits<float>::min();
            for (int i = wstart; i < wend; i++) {
              tmp = std::max(tmp, r0[i]);
            }
H
hong19860320 已提交
1510
            dout_ch[w_even >> 1] = tmp;
1511 1512
          }
        } else {  // two lines
H
hong19860320 已提交
1513
          dout_ch[0] = std::max(std::max(r0[0], r0[1]), std::max(r1[0], r1[1]));
1514 1515 1516
#ifdef __aarch64__
          w = 1;
          cnt = 1;
H
hong19860320 已提交
1517
          for (; w < w_unroll_size; w += 8) {
1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
            float32x4_t vr0_1234 = vld1q_f32(&r0[w]);
            float32x4_t vr0_5678 = vld1q_f32(&r0[w + 4]);
            float32x4_t vr0_9101112 = vld1q_f32(&r0[w + 8]);
            float32x4_t vr1_1234 = vld1q_f32(&r1[w]);
            float32x4_t vr1_5678 = vld1q_f32(&r1[w + 4]);
            float32x4_t vr1_9101112 = vld1q_f32(&r1[w + 8]);
            float32x4_t vmax_1234 = vmaxq_f32(vr0_1234, vr1_1234);
            float32x4_t vmax_5678 = vmaxq_f32(vr0_5678, vr1_5678);
            float32x4_t vmax_9101112 = vmaxq_f32(vr0_9101112, vr1_9101112);
            float32x4_t vmax_2345 = vextq_f32(vmax_1234, vmax_5678, 1);
            float32x4_t vmax_6789 = vextq_f32(vmax_5678, vmax_9101112, 1);
            float32x2_t vmax_12_34 =
                vpmax_f32(vget_low_f32(vmax_1234), vget_high_f32(vmax_1234));
            float32x2_t vmax_23_45 =
                vpmax_f32(vget_low_f32(vmax_2345), vget_high_f32(vmax_2345));
            float32x2_t vmax_56_78 =
                vpmax_f32(vget_low_f32(vmax_5678), vget_high_f32(vmax_5678));
            float32x2_t vmax_67_89 =
                vpmax_f32(vget_low_f32(vmax_6789), vget_high_f32(vmax_6789));
            float32x2_t vmax_123_345 = vmax_f32(vmax_12_34, vmax_23_45);
            float32x2_t vmax_567_789 = vmax_f32(vmax_56_78, vmax_67_89);
H
hong19860320 已提交
1539 1540
            vst1_f32(&dout_ch[cnt], vmax_123_345);
            vst1_f32(&dout_ch[cnt + 2], vmax_567_789);
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
            cnt += 4;
          }
          for (; w < w_even - 1; w += 2) {
            float32x4_t vr0 = vld1q_f32(&r0[w]);
            float32x4_t vr1 = vld1q_f32(&r1[w]);
            vr0 = vsetq_lane_f32(minval, vr0, 3);
            vr1 = vsetq_lane_f32(minval, vr1, 3);
            float32x4_t vmax1 = vmaxq_f32(vr0, vr1);
            float32x2_t vmax2 =
                vpmax_f32(vget_low_f32(vmax1), vget_high_f32(vmax1));
            vmax2 = vpmax_f32(vmax2, vmax2);
H
hong19860320 已提交
1552
            dout_ch[cnt] = vget_lane_f32(vmax2, 0);
1553 1554 1555
            cnt++;
          }
#else
H
hong19860320 已提交
1556
          dr_out = dout_ch + 1;
1557 1558
          dr0 = (r0 + 1);
          dr1 = (r1 + 1);
H
hong19860320 已提交
1559 1560 1561 1562 1563
          cnt_num = w_unroll_size >> 3;
          cnt_num_remain = w_unroll_remain >> 1;
          // LOG(INFO) << "cnt_num: " << cnt_num << " cnt_num_remain: " <<
          // cnt_num_remain;
          if (cnt_num > 0 || cnt_num_remain > 0) {
1564
            asm volatile(
H
hong19860320 已提交
1565 1566
                "cmp       %[cnt_num], #0         @cmp cnt_num,0\n"
                "ble       3f                     @ble exit\n"
1567
                "1:                               @main loop\n"
H
hong19860320 已提交
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612
                "vld1.f32  {d0-d3}, [%[dr0]]!     @load d0-d5,dr0\n"
                "vld1.f32  {d6-d9}, [%[dr1]]!     @load d4-d7,dr1\n"
                "vld1.f32  {d4-d5}, [%[dr0]]!     @load d0-d3,dr0\n"
                "vld1.f32  {d10-d11}, [%[dr1]]!   @load d4-d7,dr1\n"
                "vmax.f32  q6, q0, q3             @max q0,q0,q2 1234\n"
                "vmax.f32  q7, q1, q4             @max q1,q1,q3 5678\n"
                "vmax.f32  q8, q2, q5             @max q1,q1,q3 9101112\n"
                //"vmov.f32  s7,s6                @mov s7, s6\n"
                "vext.f32  q0, q6, q7, #1         @vext q0,2345\n"
                "vext.f32  q1, q7, q8, #1         @vext q1,6789\n"
                "vpmax.f32 d4, d12, d13           @pmax "
                "d4,vmax_1234,vmax_1234\n"
                "vpmax.f32 d6, d14, d15           @pmax "
                "d6,vmax_5678,vmax_5678\n"
                "vpmax.f32 d5, d0, d1             @pmax "
                "d5,vmax_2345,vmax_2345\n"
                "vpmax.f32 d7, d2, d3             @pmax "
                "d7,vmax_6789,vmax_6789\n"
                "vmax.f32 d8, d4, d5              @max "
                "d2,vmax_12_34,vmax_23_45\n"
                "vmax.f32 d9, d6, d7              @max "
                "d2,vmax_56_78,vmax_67_89\n"
                "sub       %[dr0], #16            @add w,8\n"
                "sub       %[dr1], #16            @add w,8\n"
                "vst1.f32  d8, [%[dr_out]]!       @vst1 d0,dr_out\n"
                "vst1.f32  d9, [%[dr_out]]!       @vst1 d0,dr_out\n"
                "subs      %[cnt_num], #1         @subs cnt_num,#1\n"
                "bne       1b                     @bne s3_max_loop_bot\n"
                "3:                               @loop \n"
                "cmp       %[cnt_num_remain], #0  @cmp cnt_num,0\n"
                "ble       4f                     @ble exit\n"
                "2:                               @bot loop\n"
                "vld1.f32  {d0-d1}, [%[dr0]]!     @load d0-d1,dr0\n"
                "vld1.f32  {d2-d3}, [%[dr1]]!     @load d2-d3,dr1\n"
                "vmov.f32  s3,s2                  @movs3, s2\n"
                "vmov.f32  s7,s6                  @movs7, s6\n"
                "vmax.f32  q0, q0, q1             @max q0,q0,q1\n"
                "vpmax.f32 d0, d0, d1             @pmax d0,d0,d1\n"
                "vpmax.f32 d0, d0, d0             @pmax d0,d0,d0\n"
                "vst1.f32  d0[0], [%[dr_out]]!    @vst d0[0],dr_out\n"
                "sub       %[dr0], #8             @add w,6\n"
                "sub       %[dr1], #8             @add w,6\n"
                "subs      %[cnt_num_remain], #1  @subs cnt_num,#1\n"
                "bne       2b                     @bne s3_max_loop_bot_1\n"
                "4:                               @exit\n"
1613
                : [dr0] "+r"(dr0), [dr1] "+r"(dr1), [dr_out] "+r"(dr_out),
H
hong19860320 已提交
1614 1615 1616
                  [cnt_num] "+r"(cnt_num), [cnt_num_remain] "+r"(cnt_num_remain)
                : "r"(dr0), "r"(dr1), "r"(dr_out), "r"(cnt_num),
                  "r"(cnt_num_remain)
1617 1618 1619 1620
                : "cc", "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6",
                  "q7", "q8", "q9");
          }
#endif
H
hong19860320 已提交
1621
          if (w_remain > 0) {
1622
            // deal with right pad
H
hong19860320 已提交
1623 1624
            int wstart = (w_even >> 1) * stride - padding;
            int wend = std::min(std::min(wstart + kernel, win + padding), win);
1625 1626 1627 1628
            float tmp = r0[wstart];  // std::numeric_limits<float>::min();
            for (int i = wstart; i < wend; i++) {  // only run 1 or 2 times
              tmp = std::max(tmp, std::max(r0[i], r1[i]));
            }
H
hong19860320 已提交
1629
            dout_ch[w_even >> 1] = tmp;
1630 1631 1632 1633 1634 1635 1636
          }
        }
      }
    }
  }
}

H
hong19860320 已提交
1637
void pooling3x3s2p1_avg(const float* din, float* dout, int num, int chout,
1638
                        int hout, int wout, int chin, int hin, int win,
H
hong19860320 已提交
1639 1640 1641 1642
                        bool exclusive) {
  int kernel = 3;
  int stride = 2;
  int padding = 1;
1643 1644 1645
  int size_channel_out = wout * hout;
  int size_channel_in = win * hin;

H
hong19860320 已提交
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655
  int w_needed = (wout << 1) + 1;
  int h_needed = (hout << 1) + 1;
  int w_limit = w_needed > win ? win : w_needed;
  int h_limit = h_needed > hin ? hin : h_needed;
  int w_even = (w_limit >> 1) << 1;
  int h_even = (h_limit >> 1) << 1;
  int w_unroll_size = ((w_even - 1) >> 3) << 3;
  int w_unroll_remain = w_even - 1 - w_unroll_size;
  int w_remain = w_needed - w_limit - padding;
  int h_remain = h_needed - h_limit - padding;
1656
  int w_in_2 = win << 1;
H
hong19860320 已提交
1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668
  const float coef = 1.f / 9.f;
  const float coef_1 = exclusive ? 1.f : coef;
  const float coef_2 = exclusive ? 1.f / 2.f : coef;
  const float coef_3 = exclusive ? 1.f / 3.f : coef;
  const float coef_4 = exclusive ? 1.f / 4.f : coef;
  const float coef_6 = exclusive ? 1.f / 6.f : coef;
  float32x4_t vcoef = vdupq_n_f32(coef);
  float32x4_t vcoef_1 = vdupq_n_f32(coef_1);
  float32x4_t vcoef_2 = vdupq_n_f32(coef_2);
  float32x4_t vcoef_3 = vdupq_n_f32(coef_3);
  float32x4_t vcoef_4 = vdupq_n_f32(coef_4);
  float32x4_t vcoef_6 = vdupq_n_f32(coef_6);
1669
  for (int n = 0; n < num; ++n) {
H
hong19860320 已提交
1670 1671
    float* dout_batch = dout + n * chout * size_channel_out;
    const float* din_batch = din + n * chin * size_channel_in;
1672 1673
#pragma omp parallel for
    for (int c = 0; c < chout; c++) {
H
hong19860320 已提交
1674 1675 1676
      float* dout_ch = dout_batch + c * size_channel_out;
      const float* din_ch = din_batch + c * size_channel_in;
      const float* r0 = din_ch;
1677 1678
      const float* r1 = r0 + win;
      const float* r2 = r1 + win;
H
hong19860320 已提交
1679 1680 1681
      int cnt_num = w_unroll_size >> 3;
      int cnt_num_remain = w_unroll_remain >> 1;
      float* dr_out = dout_ch;
1682 1683 1684 1685 1686 1687
      const float* dr0 = r0;
      const float* dr1 = r1;
      const float* dr2 = r2;
      int w = 1;
      int cnt = 1;
      float32x4_t vzero = vdupq_n_f32(0.f);
H
hong19860320 已提交
1688
      dout_ch[0] = (r0[0] + r0[1] + r1[0] + r1[1]) * coef_4;
1689 1690
// first row with zero pad
#ifdef __aarch64__
H
hong19860320 已提交
1691
      for (; w < w_unroll_size; w += 8) {
1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715
        float32x4_t vr0_1234 = vld1q_f32(&r0[w]);
        float32x4_t vr0_5678 = vld1q_f32(&r0[w + 4]);
        float32x4_t vr0_9101112 = vld1q_f32(&r0[w + 8]);
        float32x4_t vr1_1234 = vld1q_f32(&r1[w]);
        float32x4_t vr1_5678 = vld1q_f32(&r1[w + 4]);
        float32x4_t vr1_9101112 = vld1q_f32(&r1[w + 8]);
        float32x4_t vsum_1234 = vaddq_f32(vr0_1234, vr1_1234);
        float32x4_t vsum_5678 = vaddq_f32(vr0_5678, vr1_5678);
        float32x4_t vsum_9101112 = vaddq_f32(vr0_9101112, vr1_9101112);

        float32x4_t vsum_2345 = vextq_f32(vsum_1234, vsum_5678, 1);
        float32x4_t vsum_3456 = vextq_f32(vsum_1234, vsum_5678, 2);
        float32x4_t vsum_4567 = vextq_f32(vsum_1234, vsum_5678, 3);
        float32x4_t vsum_6789 = vextq_f32(vsum_5678, vsum_9101112, 1);
        float32x4_t vsum_123_345 = vaddq_f32(vsum_1234, vsum_2345);
        vsum_123_345 = vaddq_f32(vsum_123_345, vsum_3456);
        float32x4_t vsum_567_789 = vaddq_f32(vsum_4567, vsum_5678);
        vsum_567_789 = vaddq_f32(vsum_567_789, vsum_6789);
        vsum_123_345 =
            vsetq_lane_f32(vgetq_lane_f32(vsum_123_345, 2), vsum_123_345, 1);
        vsum_123_345 =
            vsetq_lane_f32(vgetq_lane_f32(vsum_567_789, 1), vsum_123_345, 2);
        vsum_123_345 =
            vsetq_lane_f32(vgetq_lane_f32(vsum_567_789, 3), vsum_123_345, 3);
H
hong19860320 已提交
1716 1717
        float32x4_t vrst = vmulq_f32(vsum_123_345, vcoef_6);
        vst1q_f32(&dout_ch[cnt], vrst);
1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728
        cnt += 4;
      }
      for (; w < w_even - 1; w += 2) {
        float32x4_t vr0 = vld1q_f32(&r0[w]);
        float32x4_t vr1 = vld1q_f32(&r1[w]);
        vr0 = vsetq_lane_f32(0.f, vr0, 3);
        vr1 = vsetq_lane_f32(0.f, vr1, 3);
        float32x4_t vsum1 = vaddq_f32(vr0, vr1);
        float32x2_t vsum2 =
            vpadd_f32(vget_low_f32(vsum1), vget_high_f32(vsum1));
        vsum2 = vpadd_f32(vsum2, vsum2);
H
hong19860320 已提交
1729 1730
        float32x2_t vrst = vmul_f32(vsum2, vget_low_f32(vcoef_6));
        dout_ch[cnt] = vget_lane_f32(vrst, 0);
1731 1732 1733 1734 1735 1736
        cnt++;
      }
#else
      dr0 = dr0 + 1;
      dr1 = dr1 + 1;
      dr_out = dr_out + 1;
H
hong19860320 已提交
1737 1738 1739
      // LOG(INFO) << "cnt_num: " << cnt_num << " cnt_num_remain: " <<
      // cnt_num_remain;
      if (cnt_num > 0 || cnt_num_remain > 0) {
1740
        asm volatile(
H
hong19860320 已提交
1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787
            "cmp       %[cnt_num], #0        @cmp cnt_num,0\n"
            "ble       3f                    @ble exit\n"
            "1:                              @main loop\n"
            "vld1.f32  {d0-d3}, [%[dr0]]!    @load d0-d5,dr0\n"
            "vld1.f32  {d6-d9}, [%[dr1]]!    @load d4-d7,dr1\n"
            "vld1.f32  {d4-d5}, [%[dr0]]!    @load d0-d5,dr0\n"
            "vld1.f32  {d10-d11}, [%[dr1]]!  @load d4-d7,dr1\n"
            "vadd.f32  q6, q0, q3            @max r0_1234,r1_1234\n"
            "vadd.f32  q7, q1, q4            @max r0_5678,r1_5678\n"
            "vadd.f32  q8, q2, q5            @max r0_9101112,r1_9101112\n"
            //"vmov.f32  s7,s6               @mov s7, s6\n"
            "vext.f32  q0, q6, q7, #1        @vext max_2345\n"
            "vext.f32  q1, q6, q7, #3        @vext max_4567\n"
            "vext.f32  q2, q6, q7, #2        @vext max_3456\n"
            "vext.f32  q3, q7, q8, #1        @vext max_6789\n"
            "vadd.f32  q4, q6, q0            @add 1234, 2345\n"
            "vadd.f32  q5, q7, q1            @add 5678, 4567\n"
            "vadd.f32  q4, q4, q2            @add 3456, sum1\n"
            "vadd.f32  q5, q5, q3            @add 6789, sum2\n"
            "vmov.f32  s17, s18              @mov\n"
            "vmov.f32  s18, s21              @mov\n"
            "vmov.f32  s19, s23              @mov\n"
            "vmul.f32  q4, q4, %q[vcoef_6]   @mul\n"
            "sub       %[dr0], #16           @add w,8\n"
            "sub       %[dr1], #16           @add w,8\n"
            "subs      %[cnt_num], #1        @subs cnt_num,#1\n"
            "vst1.f32  d8, [%[dr_out]]!      @vst1 d0,dr_out\n"
            "vst1.f32  d9, [%[dr_out]]!      @vst1 d0,dr_out\n"
            "bne       1b                    @bne s3_max_loop\n"
            "3:                              @loop\n"
            "cmp       %[cnt_num_remain], #0 @cnt_num_remain<=0\n"
            "ble       4f                    @ble exit\n"
            "2:                              @main loop\n"
            "vld1.f32  {d0-d1}, [%[dr0]]!    @load d0-d1,dr0\n"
            "vld1.f32  {d2-d3}, [%[dr1]]!    @load d2-d3,dr1\n"
            "vext.f32  q0, %q[vzero], q0, #3 @ext v0_0123\n"
            "vext.f32  q1, %q[vzero], q1, #3 @ext v1_0123\n"
            "vadd.f32  q0, q0, q1            @add q0,q0,q1\n"
            "vpadd.f32 d0, d0, d1            @padd d0,d0,d1\n"
            "vpadd.f32 d0, d0, d0            @padd d0, d0,d0\n"
            "vmul.f32  d0, d0, %e[vcoef_6]   @mul\n"
            "sub       %[dr0], #8            @add w,6\n"
            "sub       %[dr1], #8            @add w,6\n"
            "subs      %[cnt_num_remain], #1 @subs cnt_num,#1\n"
            "vst1.f32  d0[0], [%[dr_out]]!   @vst d0[0],dr_out\n"
            "bne       2b                    @bne s3_max_loop_1\n"
            "4:                              @exit\n"
1788
            : [dr0] "+r"(dr0), [dr1] "+r"(dr1), [dr_out] "+r"(dr_out),
H
hong19860320 已提交
1789 1790 1791
              [cnt_num] "+r"(cnt_num), [cnt_num_remain] "+r"(cnt_num_remain),
              [vcoef_6] "+w"(vcoef_6), [vzero] "+w"(vzero)
            : "r"(dr0), "r"(dr1), "r"(dr_out), "r"(cnt_num), "r"(cnt_num_remain)
1792 1793 1794 1795 1796
            : "cc", "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
              "q8", "q9");
      }
#endif
      // int w = w_even - 1;
H
hong19860320 已提交
1797
      if (w_remain > 0) {
1798
        // deal with right pad
H
hong19860320 已提交
1799 1800 1801 1802
        int wstart = (w_even >> 1) * stride - padding;
        int wend = std::min(std::min(wstart + kernel, win + padding), win);
        float tmp1 = 0.f;  // std::numeric_limits<float>::min();
        float tmp2 = exclusive ? 1.0f / (2.f * (wend - wstart)) : coef;
1803
        for (int i = wstart; i < wend; i++) {  // only run 1 or 2 times
H
hong19860320 已提交
1804
          tmp1 += (r0[i] + r1[i]);
1805
        }
H
hong19860320 已提交
1806
        dout_ch[w_even >> 1] = tmp1 * tmp2;
1807 1808 1809 1810 1811 1812
        // cnt ++;
      }

      r0 = r1;
      r1 = r0 + win;
      r2 = r1 + win;
H
hong19860320 已提交
1813
      dout_ch += wout;
1814 1815 1816 1817 1818 1819
      int h = 2;
      for (; h < h_even; h += 2) {
        // deal with left pad
        float sum0 = r0[0] + r0[1];
        float sum1 = r1[0] + r1[1];
        float sum2 = r2[0] + r2[1];
H
hong19860320 已提交
1820
        dout_ch[0] = (sum0 + sum1 + sum2) * coef_6;
1821 1822 1823
#ifdef __aarch64__
        w = 1;
        cnt = 1;
H
hong19860320 已提交
1824
        for (; w < w_unroll_size; w += 8) {
1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855
          float32x4_t vr0_1234 = vld1q_f32(&r0[w]);
          float32x4_t vr0_5678 = vld1q_f32(&r0[w + 4]);
          float32x4_t vr0_9101112 = vld1q_f32(&r0[w + 8]);
          float32x4_t vr1_1234 = vld1q_f32(&r1[w]);
          float32x4_t vr1_5678 = vld1q_f32(&r1[w + 4]);
          float32x4_t vr1_9101112 = vld1q_f32(&r1[w + 8]);
          float32x4_t vr2_1234 = vld1q_f32(&r2[w]);
          float32x4_t vr2_5678 = vld1q_f32(&r2[w + 4]);
          float32x4_t vr2_9101112 = vld1q_f32(&r2[w + 8]);
          float32x4_t vsum_1234 = vaddq_f32(vr0_1234, vr1_1234);
          float32x4_t vsum_5678 = vaddq_f32(vr0_5678, vr1_5678);
          float32x4_t vsum_9101112 = vaddq_f32(vr0_9101112, vr1_9101112);
          vsum_1234 = vaddq_f32(vsum_1234, vr2_1234);
          vsum_5678 = vaddq_f32(vsum_5678, vr2_5678);
          vsum_9101112 = vaddq_f32(vsum_9101112, vr2_9101112);

          float32x4_t vsum_2345 = vextq_f32(vsum_1234, vsum_5678, 1);
          float32x4_t vsum_3456 = vextq_f32(vsum_1234, vsum_5678, 2);
          float32x4_t vsum_4567 = vextq_f32(vsum_1234, vsum_5678, 3);
          float32x4_t vsum_6789 = vextq_f32(vsum_5678, vsum_9101112, 1);
          float32x4_t vsum_123_345 = vaddq_f32(vsum_1234, vsum_2345);
          vsum_123_345 = vaddq_f32(vsum_123_345, vsum_3456);
          float32x4_t vsum_567_789 = vaddq_f32(vsum_4567, vsum_5678);
          vsum_567_789 = vaddq_f32(vsum_567_789, vsum_6789);
          vsum_123_345 =
              vsetq_lane_f32(vgetq_lane_f32(vsum_123_345, 2), vsum_123_345, 1);
          vsum_123_345 =
              vsetq_lane_f32(vgetq_lane_f32(vsum_567_789, 1), vsum_123_345, 2);
          vsum_123_345 =
              vsetq_lane_f32(vgetq_lane_f32(vsum_567_789, 3), vsum_123_345, 3);
          float32x4_t vrst = vmulq_f32(vsum_123_345, vcoef);
H
hong19860320 已提交
1856
          vst1q_f32(&dout_ch[cnt], vrst);
1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870
          cnt += 4;
        }
        for (; w < w_even - 1; w += 2) {
          float32x4_t vr0 = vld1q_f32(&r0[w]);
          float32x4_t vr1 = vld1q_f32(&r1[w]);
          float32x4_t vr2 = vld1q_f32(&r2[w]);
          vr0 = vsetq_lane_f32(0.f, vr0, 3);
          vr1 = vsetq_lane_f32(0.f, vr1, 3);
          vr2 = vsetq_lane_f32(0.f, vr2, 3);
          float32x4_t vsum1 = vaddq_f32(vr0, vr1);
          vsum1 = vaddq_f32(vsum1, vr2);
          float32x2_t vsum2 =
              vpadd_f32(vget_low_f32(vsum1), vget_high_f32(vsum1));
          float32x2_t vsum = vpadd_f32(vsum2, vsum2);
H
hong19860320 已提交
1871
          dout_ch[cnt] = vget_lane_f32(vsum, 0) * coef;
1872 1873 1874
          cnt++;
        }
#else
H
hong19860320 已提交
1875
        dr_out = dout_ch + 1;
1876 1877 1878
        dr0 = (r0 + 1);
        dr1 = (r1 + 1);
        dr2 = (r2 + 1);
H
hong19860320 已提交
1879 1880 1881
        cnt_num = w_unroll_size >> 3;
        cnt_num_remain = w_unroll_remain >> 1;
        if (cnt_num > 0 || cnt_num_remain > 0) {
1882
          asm volatile(
H
hong19860320 已提交
1883 1884 1885 1886
              "cmp       %[cnt_num], #0        @cmp cnt_num,0\n"
              "ble       3f                    @ble exit\n"
              "1:                              @main loop\n"
              "vld1.f32  {d0-d3}, [%[dr0]]!    @load d0-d5, "
1887
              "dr0\n"
H
hong19860320 已提交
1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921
              "vld1.f32  {d6-d9}, [%[dr1]]!    @load d4-d7,dr1\n"
              "vld1.f32  {d12-d15}, [%[dr2]]!  @load d4-d7,dr1\n"
              "vld1.f32  {d4-d5}, [%[dr0]]!    @load d0-d5,dr0\n"
              "vld1.f32  {d10-d11}, [%[dr1]]!  @load d4-d7,dr1\n"
              "vld1.f32  {d16-d17}, [%[dr2]]!  @load d4-d7,dr1\n"
              "vadd.f32  q9, q0, q3            @max q0,q0,q2\n"
              "vadd.f32  q10, q1, q4           @max q1,q1,q3\n"
              "vadd.f32  q11, q2, q5           @max q1,q1,q3\n"
              "vadd.f32  q6, q9, q6            @max q0,q0,q2 1234\n"
              "vadd.f32  q7, q10, q7           @max q1,q1,q3 5678\n"
              "vadd.f32  q8, q11, q8           @max q1,q1,q3 9101112\n"
              //"vmov.f32  s7,s6               @mov s7, s6\n"
              "vext.f32  q0, q6, q7, #1        @vext max_2345\n"
              "vext.f32  q1, q6, q7, #3        @vext max_4567\n"
              "vext.f32  q2, q6, q7, #2        @vext max_3456\n"
              "vext.f32  q3, q7, q8, #1        @vext max_6789\n"
              "vadd.f32  q4, q6, q0            @add 1234,2345\n"
              "vadd.f32  q5, q7, q1            @add 5678,4567\n"
              "vadd.f32  q4, q4, q2            @add 3456,sum1\n"
              "vadd.f32  q5, q5, q3            @add 6789,sum2\n"
              "vmov.f32  s17, s18              @mov\n"
              "vmov.f32  s18, s21              @mov\n"
              "vmov.f32  s19, s23              @mov\n"
              "vmul.f32  q4, q4, %q[vcoef]     @mul\n"
              "sub       %[dr0], #16           @add w,8\n"
              "sub       %[dr1], #16           @add w,8\n"
              "sub       %[dr2], #16           @add w, 8\n"
              "subs      %[cnt_num], #1        @subs cnt_num,#1\n"
              "vst1.f32  d8, [%[dr_out]]!      @vst1 d0,dr_out\n"
              "vst1.f32  d9, [%[dr_out]]!      @vst1 d0,dr_out\n"
              "bne       1b                    @bne s3_max_loop_mid\n"
              "3:                              @loop\n"
              "cmp       %[cnt_num_remain], #0 @cnt_num_remain<=0\n"
              "ble       4f                    @ble exit1\n"
1922
              "2:                              @mid loop\n"
H
hong19860320 已提交
1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940
              "vld1.f32  {d0-d1}, [%[dr0]]!    @load d0-d1,dr0\n"
              "vld1.f32  {d2-d3}, [%[dr1]]!    @load d2-d3,dr1\n"
              "vld1.f32  {d4-d5}, [%[dr2]]!    @load d2-d3,dr1\n"
              "vext.f32  q0, %q[vzero], q0, #3 @ext v0_0123\n"
              "vext.f32  q1, %q[vzero], q1, #3 @ext v1_0123\n"
              "vext.f32  q2, %q[vzero], q2, #3 @ext v1_0123\n"
              "vadd.f32  q0, q0, q1            @add q0,q0,q1\n"
              "vadd.f32  q0, q0, q2            @add q0,q0,q1\n"
              "vpadd.f32 d0, d0, d1            @padd d0,d0,d1\n"
              "vpadd.f32 d0, d0, d0            @padd d0,d0,d0\n"
              "vmul.f32  d0, d0, %e[vcoef]     @mul\n"
              "sub       %[dr0], #8            @add w,6\n"
              "sub       %[dr1], #8            @add w,6\n"
              "sub       %[dr2], #8            @add w,6\n"
              "subs      %[cnt_num_remain], #1 @cnt_num_remain--\n"
              "vst1.f32  d0[0], [%[dr_out]]!   @vst d0[0],dr_out\n"
              "bne       2b                    @bne s3_max_loop_mid_1\n"
              "4:                              @exit\n"
1941 1942
              : [dr0] "+r"(dr0), [dr1] "+r"(dr1), [dr2] "+r"(dr2),
                [dr_out] "+r"(dr_out), [cnt_num] "+r"(cnt_num),
H
hong19860320 已提交
1943
                [cnt_num_remain] "+r"(cnt_num_remain), [vcoef] "+w"(vcoef),
1944 1945
                [vzero] "+w"(vzero)
              : "r"(dr0), "r"(dr1), "r"(dr2), "r"(dr_out), "r"(cnt_num),
H
hong19860320 已提交
1946
                "r"(cnt_num_remain)
1947 1948 1949 1950
              : "cc", "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
                "q8", "q9", "q10", "q11", "q12");
        }
#endif
H
hong19860320 已提交
1951
        if (w_remain > 0) {
1952
          // deal with right pad
H
hong19860320 已提交
1953 1954 1955 1956
          int wstart = (w_even >> 1) * stride - padding;
          int wend = std::min(std::min(wstart + kernel, win + padding), win);
          float tmp1 = 0.f;
          float tmp2 = exclusive ? 1.0f / (3.f * (wend - wstart)) : coef;
1957
          for (int i = wstart; i < wend; i++) {
H
hong19860320 已提交
1958
            tmp1 += (r0[i] + r1[i] + r2[i]);
1959
          }
H
hong19860320 已提交
1960
          dout_ch[w_even >> 1] = tmp1 * tmp2;
1961 1962 1963 1964 1965
          // cnt ++;
        }
        r0 = r2;
        r1 = r0 + win;
        r2 = r1 + win;
H
hong19860320 已提交
1966
        dout_ch += wout;
1967 1968
      }

H
hong19860320 已提交
1969
      if (h_remain > 0) {
1970 1971
        // deal with bottom pad
        // first row with zero pad
H
hong19860320 已提交
1972 1973 1974 1975
        int hstart = (h >> 1) * stride - padding;
        int hend = std::min(std::min(hstart + kernel, hin + padding), hin);
        if (hstart == hend - 1) {  // only one line
          dout_ch[0] = (r0[0] + r0[1]) * coef_2;
1976 1977 1978
#ifdef __aarch64__
          w = 1;
          cnt = 1;
H
hong19860320 已提交
1979
          for (; w < w_unroll_size; w += 8) {
1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997
            float32x4_t vsum_1234 = vld1q_f32(&r0[w]);
            float32x4_t vsum_5678 = vld1q_f32(&r0[w + 4]);
            float32x4_t vsum_9101112 = vld1q_f32(&r0[w + 8]);

            float32x4_t vsum_2345 = vextq_f32(vsum_1234, vsum_5678, 1);
            float32x4_t vsum_3456 = vextq_f32(vsum_1234, vsum_5678, 2);
            float32x4_t vsum_4567 = vextq_f32(vsum_1234, vsum_5678, 3);
            float32x4_t vsum_6789 = vextq_f32(vsum_5678, vsum_9101112, 1);
            float32x4_t vsum_123_345 = vaddq_f32(vsum_1234, vsum_2345);
            vsum_123_345 = vaddq_f32(vsum_123_345, vsum_3456);
            float32x4_t vsum_567_789 = vaddq_f32(vsum_4567, vsum_5678);
            vsum_567_789 = vaddq_f32(vsum_567_789, vsum_6789);
            vsum_123_345 = vsetq_lane_f32(vgetq_lane_f32(vsum_123_345, 2),
                                          vsum_123_345, 1);
            vsum_123_345 = vsetq_lane_f32(vgetq_lane_f32(vsum_567_789, 1),
                                          vsum_123_345, 2);
            vsum_123_345 = vsetq_lane_f32(vgetq_lane_f32(vsum_567_789, 3),
                                          vsum_123_345, 3);
H
hong19860320 已提交
1998 1999
            float32x4_t vrst = vmulq_f32(vsum_123_345, vcoef_3);
            vst1q_f32(&dout_ch[cnt], vrst);
2000 2001 2002 2003 2004 2005 2006
            cnt += 4;
          }
          for (; w < w_even - 1; w += 2) {
            float32x4_t vr0 = vld1q_f32(&r0[w]);
            vr0 = vsetq_lane_f32(0.f, vr0, 3);
            float32x2_t vsum = vpadd_f32(vget_low_f32(vr0), vget_high_f32(vr0));
            vsum = vpadd_f32(vsum, vsum);
H
hong19860320 已提交
2007
            dout_ch[cnt] = vget_lane_f32(vsum, 0) * coef_3;
2008 2009 2010
            cnt++;
          }
#else
H
hong19860320 已提交
2011
          dr_out = dout_ch + 1;
2012
          dr0 = (r0 + 1);
H
hong19860320 已提交
2013 2014 2015
          cnt_num = w_unroll_size >> 3;
          cnt_num_remain = w_unroll_remain >> 1;
          if (cnt_num > 0 || cnt_num_remain > 0) {
2016
            asm volatile(
H
hong19860320 已提交
2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050
                "cmp       %[cnt_num], #0         @cmp cnt_num,0\n"
                "ble       3f                     @ble exit\n"
                "1:                               @main loop\n"
                "vld1.f32  {d12-d15}, [%[dr0]]!   @load d0-d3,dr0\n"
                "vld1.f32  {d16-d17}, [%[dr0]]!   @load d0-d3,dr0\n"
                "vext.f32  q0, q6, q7, #1         @vext max_2345\n"
                "vext.f32  q1, q6, q7, #3         @vext max_4567\n"
                "vext.f32  q2, q6, q7, #2         @vext max_3456\n"
                "vext.f32  q3, q7, q8, #1         @vext max_6789\n"
                "vadd.f32  q4, q6, q0             @add 1234,2345\n"
                "vadd.f32  q5, q7, q1             @add 5678,4567\n"
                "vadd.f32  q4, q4, q2             @add 3456,sum1\n"
                "vadd.f32  q5, q5, q3             @add 6789,sum2\n"
                "vmov.f32  s17, s18               @mov\n"
                "vmov.f32  s18, s21               @mov\n"
                "vmov.f32  s19, s23               @mov\n"
                "vmul.f32  q4, q4, %q[vcoef_3]    @mul\n"
                "sub       %[dr0], #16            @add w,6\n"
                "subs      %[cnt_num], #1         @subs cnt_num,#1\n"
                "vst1.f32  d8, [%[dr_out]]!       @vst1 d0,dr_out\n"
                "vst1.f32  d9, [%[dr_out]]!       @vst1 d0,dr_out\n"
                "bne       1b                     @bne s3_max_loop_bot\n"
                "3:                               @loop\n"
                "cmp       %[cnt_num_remain], #0  @cnt_num_remain<=0\n"
                "ble       4f                     @ble exit\n"
                "2:                               @bot loop\n"
                "vld1.f32  {d0-d1}, [%[dr0]]!     @load d0-d1,dr0\n"
                "vext.f32  q0, %q[vzero], q0, #3  @ext v0_0123\n"
                "vpadd.f32 d0, d0, d1             @padd d0,d0,d1\n"
                "vpadd.f32 d0, d0, d0             @padd d0,d0,d0\n"
                "vmul.f32  d0, d0, %e[vcoef_3]    @mul\n"
                "sub       %[dr0], #8             @add w,2\n"
                "subs      %[cnt_num_remain], #1  @cnt_num_remain--\n"
                "vst1.f32  d0[0], [%[dr_out]]!    @vst d0[0],dr_out\n"
2051
                "bne       2b                     @bne s3_max_loop_bot_1\n"
H
hong19860320 已提交
2052
                "4:                               @exit\n"
2053
                : [dr0] "+r"(dr0), [dr1] "+r"(dr1), [dr_out] "+r"(dr_out),
H
hong19860320 已提交
2054 2055 2056 2057 2058
                  [cnt_num] "+r"(cnt_num),
                  [cnt_num_remain] "+r"(cnt_num_remain),
                  [vcoef_3] "+w"(vcoef_3), [vzero] "+w"(vzero)
                : "r"(dr0), "r"(dr1), "r"(dr_out), "r"(cnt_num),
                  "r"(cnt_num_remain)
2059 2060 2061 2062
                : "cc", "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6",
                  "q7", "q8");
          }
#endif
H
hong19860320 已提交
2063
          if (w_remain > 0) {
2064
            // deal with right pad
H
hong19860320 已提交
2065 2066 2067 2068
            int wstart = (w_even >> 1) * stride - padding;
            int wend = std::min(std::min(wstart + kernel, win + padding), win);
            float tmp1 = 0.f;
            float tmp2 = exclusive ? 1.0f / (1.f * (wend - wstart)) : coef;
2069
            for (int i = wstart; i < wend; i++) {
H
hong19860320 已提交
2070
              tmp1 += r0[i];
2071
            }
H
hong19860320 已提交
2072
            dout_ch[w_even >> 1] = tmp1 * tmp2;
2073 2074
          }
        } else {  // two lines
H
hong19860320 已提交
2075
          dout_ch[0] = (r0[0] + r0[1] + r1[0] + r1[1]) * coef_4;
2076 2077 2078
#ifdef __aarch64__
          w = 1;
          cnt = 1;
H
hong19860320 已提交
2079
          for (; w < w_unroll_size; w += 8) {
2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103
            float32x4_t vr0_1234 = vld1q_f32(&r0[w]);
            float32x4_t vr0_5678 = vld1q_f32(&r0[w + 4]);
            float32x4_t vr0_9101112 = vld1q_f32(&r0[w + 8]);
            float32x4_t vr1_1234 = vld1q_f32(&r1[w]);
            float32x4_t vr1_5678 = vld1q_f32(&r1[w + 4]);
            float32x4_t vr1_9101112 = vld1q_f32(&r1[w + 8]);

            float32x4_t vsum_1234 = vaddq_f32(vr0_1234, vr1_1234);
            float32x4_t vsum_5678 = vaddq_f32(vr0_5678, vr1_5678);
            float32x4_t vsum_9101112 = vaddq_f32(vr0_9101112, vr1_9101112);
            float32x4_t vsum_2345 = vextq_f32(vsum_1234, vsum_5678, 1);
            float32x4_t vsum_3456 = vextq_f32(vsum_1234, vsum_5678, 2);
            float32x4_t vsum_4567 = vextq_f32(vsum_1234, vsum_5678, 3);
            float32x4_t vsum_6789 = vextq_f32(vsum_5678, vsum_9101112, 1);
            float32x4_t vsum_123_345 = vaddq_f32(vsum_1234, vsum_2345);
            vsum_123_345 = vaddq_f32(vsum_123_345, vsum_3456);
            float32x4_t vsum_567_789 = vaddq_f32(vsum_4567, vsum_5678);
            vsum_567_789 = vaddq_f32(vsum_567_789, vsum_6789);
            vsum_123_345 = vsetq_lane_f32(vgetq_lane_f32(vsum_123_345, 2),
                                          vsum_123_345, 1);
            vsum_123_345 = vsetq_lane_f32(vgetq_lane_f32(vsum_567_789, 1),
                                          vsum_123_345, 2);
            vsum_123_345 = vsetq_lane_f32(vgetq_lane_f32(vsum_567_789, 3),
                                          vsum_123_345, 3);
H
hong19860320 已提交
2104 2105
            float32x4_t vrst = vmulq_f32(vsum_123_345, vcoef_6);
            vst1q_f32(&dout_ch[cnt], vrst);
2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116
            cnt += 4;
          }
          for (; w < w_even - 1; w += 2) {
            float32x4_t vr0 = vld1q_f32(&r0[w]);
            float32x4_t vr1 = vld1q_f32(&r1[w]);
            vr0 = vsetq_lane_f32(0.f, vr0, 3);
            vr1 = vsetq_lane_f32(0.f, vr1, 3);
            float32x4_t vsum1 = vaddq_f32(vr0, vr1);
            float32x2_t vsum2 =
                vpadd_f32(vget_low_f32(vsum1), vget_high_f32(vsum1));
            vsum2 = vpadd_f32(vsum2, vsum2);
H
hong19860320 已提交
2117 2118
            float32x2_t vrst = vmul_f32(vsum2, vget_low_f32(vcoef_6));
            dout_ch[cnt] = vget_lane_f32(vrst, 0);
2119 2120 2121
            cnt++;
          }
#else
H
hong19860320 已提交
2122
          dr_out = dout_ch + 1;
2123 2124
          dr0 = (r0 + 1);
          dr1 = (r1 + 1);
H
hong19860320 已提交
2125 2126 2127
          cnt_num = w_unroll_size >> 3;
          cnt_num_remain = w_unroll_remain >> 1;
          if (cnt_num > 0 || cnt_num_remain > 0) {
2128
            asm volatile(
H
hong19860320 已提交
2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173
                "cmp       %[cnt_num], #0        @cmp cnt_num,0\n"
                "ble       3f                    @ble exit\n"
                "1:                              @main loop\n"
                "vld1.f32  {d0-d3}, [%[dr0]]!    @load d0-d5,dr0\n"
                "vld1.f32  {d6-d9}, [%[dr1]]!    @load d4-d7,dr1\n"
                "vld1.f32  {d4-d5}, [%[dr0]]!    @load d0-d3,dr0\n"
                "vld1.f32  {d10-d11}, [%[dr1]]!  @load d4-d7,dr1\n"
                "vadd.f32  q6, q0, q3            @add q0,q0,q2 1234\n"
                "vadd.f32  q7, q1, q4            @add q1,q1,q3 5678\n"
                "vadd.f32  q8, q2, q5            @add q1,q1,q3 9101112\n"
                //"vmov.f32  s7,s6               @mov s7,s6\n"
                "vext.f32  q0, q6, q7, #1        @vext max_2345\n"
                "vext.f32  q1, q6, q7, #3        @vext max_4567\n"
                "vext.f32  q2, q6, q7, #2        @vext max_3456\n"
                "vext.f32  q3, q7, q8, #1        @vext max_6789\n"
                "vadd.f32  q4, q6, q0            @add 1234,2345\n"
                "vadd.f32  q5, q7, q1            @add 5678,4567\n"
                "vadd.f32  q4, q4, q2            @add 3456,sum1\n"
                "vadd.f32  q5, q5, q3            @add 6789,sum2\n"
                "vmov.f32  s17, s18              @mov\n"
                "vmov.f32  s18, s21              @mov\n"
                "vmov.f32  s19, s23              @mov\n"
                "vmul.f32  q4, q4, %q[vcoef_6]   @mul\n"
                "sub       %[dr0], #16           @add w,8\n"
                "sub       %[dr1], #16           @add w,8\n"
                "subs      %[cnt_num], #1        @subs cnt_num,#1\n"
                "vst1.f32  d8, [%[dr_out]]!      @vst1 d0,dr_out\n"
                "vst1.f32  d9, [%[dr_out]]!      @vst1 d0, dr_out\n"
                "bne       1b                    @bne s3_max_loop_bot\n"
                "3:                              @loop\n"
                "cmp       %[cnt_num_remain], #0 @cnt_num_remain<=0\n"
                "ble       4f                    @ble exit\n"
                "2:                              @bot loop\n"
                "vld1.f32  {d0-d1}, [%[dr0]]!    @load d0-d1,dr0\n"
                "vld1.f32  {d2-d3}, [%[dr1]]!    @load d2-d3,dr1\n"
                "vext.f32  q0, %q[vzero], q0, #3 @ext v0_0123\n"
                "vext.f32  q1, %q[vzero], q1, #3 @ext v1_0123\n"
                "vadd.f32  q0, q0, q1            @add q0,q0,q1\n"
                "vpadd.f32 d0, d0, d1            @padd d0,d0,d1\n"
                "vpadd.f32 d0, d0, d0            @padd d0,d0,d0\n"
                "vmul.f32  d0, d0, %e[vcoef_6]   @mul\n"
                "sub       %[dr0], #8            @add w,6\n"
                "sub       %[dr1], #8            @add w,6\n"
                "subs      %[cnt_num_remain], #1 @cnt_num_remain--\n"
                "vst1.f32  d0[0], [%[dr_out]]!   @vst d0[0],dr_out\n"
2174
                "bne       2b                    @bne s3_max_loop_bot_1\n"
H
hong19860320 已提交
2175
                "4:                              @exit\n"
2176
                : [dr0] "+r"(dr0), [dr1] "+r"(dr1), [dr_out] "+r"(dr_out),
H
hong19860320 已提交
2177 2178 2179 2180 2181
                  [cnt_num] "+r"(cnt_num),
                  [cnt_num_remain] "+r"(cnt_num_remain),
                  [vcoef_6] "+w"(vcoef_6), [vzero] "+w"(vzero)
                : "r"(dr0), "r"(dr1), "r"(dr_out), "r"(cnt_num),
                  "r"(cnt_num_remain)
2182 2183 2184 2185
                : "cc", "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6",
                  "q7", "q8", "q9");
          }
#endif
H
hong19860320 已提交
2186
          if (w_remain > 0) {
2187
            // deal with right pad
H
hong19860320 已提交
2188 2189 2190 2191
            int wstart = (w_even >> 1) * stride - padding;
            int wend = std::min(std::min(wstart + kernel, win + padding), win);
            float tmp1 = 0.f;
            float tmp2 = exclusive ? 1.0f / (2.f * (wend - wstart)) : coef;
2192
            for (int i = wstart; i < wend; i++) {  // only run 1 or 2 times
H
hong19860320 已提交
2193
              tmp1 += (r0[i] + r1[i]);
2194
            }
H
hong19860320 已提交
2195
            dout_ch[w_even >> 1] = tmp1 * tmp2;
2196 2197 2198 2199 2200 2201 2202
          }
        }
      }
    }
  }
}

H
hong19860320 已提交
2203 2204 2205 2206 2207 2208 2209
void pooling3x3s2p0_max(const float* din, float* dout, int num, int chout,
                        int hout, int wout, int chin, int hin, int win) {
  int kernel = 3;
  int stride = 2;
  int padding = 0;
  int size_channel_out = wout * hout;
  int size_channel_in = win * hin;
2210

H
hong19860320 已提交
2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221
  int w_needed = (wout << 1) + 1;
  int h_needed = (hout << 1) + 1;
  int w_limit = w_needed > win ? win : w_needed;
  int h_limit = h_needed > hin ? hin : h_needed;
  int w_even = ((w_limit - 1) >> 1) << 1;
  int h_even = ((h_limit - 1) >> 1) << 1;
  int w_unroll_size = (w_even >> 3) << 3;
  int w_unroll_remain = w_even - w_unroll_size;
  int w_remain = w_needed - w_limit;
  int h_remain = h_needed - h_limit;
  int w_in_2 = win << 1;
2222 2223
  float minval = std::numeric_limits<float>::lowest();
  for (int n = 0; n < num; ++n) {
H
hong19860320 已提交
2224 2225
    float* dout_batch = dout + n * chout * size_channel_out;
    const float* din_batch = din + n * chin * size_channel_in;
2226
#pragma omp parallel for
H
hong19860320 已提交
2227 2228 2229 2230 2231 2232
    for (int c = 0; c < chout; c++) {
      float* dout_ch = dout_batch + c * size_channel_out;
      const float* din_ch = din_batch + c * size_channel_in;
      const float* r0 = din_ch;
      const float* r1 = r0 + win;
      const float* r2 = r1 + win;
2233
      // w = w_in - 8;
H
hong19860320 已提交
2234
      float* dr_out = dout_ch;
2235 2236 2237 2238 2239
      const float* dr0 = r0;
      const float* dr1 = r1;
      const float* dr2 = r2;
      int w = 0;
      int cnt = 0;
H
hong19860320 已提交
2240
      // dout_ch[0] = std::max(std::max(r0[0], r0[1]), std::max(r1[0],
2241 2242 2243 2244 2245
      // r1[1]));
      // first row with zero pad
      // r0 = r1;
      // r1 = r0 + w_in;
      // r2 = r1 + w_in;
H
hong19860320 已提交
2246
      // dout_channel += w_out;
2247 2248 2249 2250 2251 2252
      int h = 0;
      for (; h < h_even; h += 2) {
        // deal with left pad
        float maxr0 = std::max(r0[0], r0[1]);
        float maxr1 = std::max(r1[0], r1[1]);
        float maxr2 = std::max(r2[0], r2[1]);
H
hong19860320 已提交
2253
// dout_ch[0] = std::max(std::max(maxr0, maxr1), maxr2);
2254 2255 2256
#ifdef __aarch64__
        w = 0;
        cnt = 0;
H
hong19860320 已提交
2257
        for (; w < w_unroll_size; w += 8) {
2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284
          float32x4_t vr0_1234 = vld1q_f32(&r0[w]);
          float32x4_t vr0_5678 = vld1q_f32(&r0[w + 4]);
          float32x4_t vr0_9101112 = vld1q_f32(&r0[w + 8]);
          float32x4_t vr1_1234 = vld1q_f32(&r1[w]);
          float32x4_t vr1_5678 = vld1q_f32(&r1[w + 4]);
          float32x4_t vr1_9101112 = vld1q_f32(&r1[w + 8]);
          float32x4_t vr2_1234 = vld1q_f32(&r2[w]);
          float32x4_t vr2_5678 = vld1q_f32(&r2[w + 4]);
          float32x4_t vr2_9101112 = vld1q_f32(&r2[w + 8]);
          float32x4_t vmax_1234 = vmaxq_f32(vr0_1234, vr1_1234);
          vmax_1234 = vmaxq_f32(vmax_1234, vr2_1234);
          float32x4_t vmax_5678 = vmaxq_f32(vr0_5678, vr1_5678);
          vmax_5678 = vmaxq_f32(vmax_5678, vr2_5678);
          float32x4_t vmax_9101112 = vmaxq_f32(vr0_9101112, vr1_9101112);
          vmax_9101112 = vmaxq_f32(vmax_9101112, vr2_9101112);
          float32x4_t vmax_2345 = vextq_f32(vmax_1234, vmax_5678, 1);
          float32x4_t vmax_6789 = vextq_f32(vmax_5678, vmax_9101112, 1);
          float32x2_t vmax_12_34 =
              vpmax_f32(vget_low_f32(vmax_1234), vget_high_f32(vmax_1234));
          float32x2_t vmax_23_45 =
              vpmax_f32(vget_low_f32(vmax_2345), vget_high_f32(vmax_2345));
          float32x2_t vmax_56_78 =
              vpmax_f32(vget_low_f32(vmax_5678), vget_high_f32(vmax_5678));
          float32x2_t vmax_67_89 =
              vpmax_f32(vget_low_f32(vmax_6789), vget_high_f32(vmax_6789));
          float32x2_t vmax_123_345 = vmax_f32(vmax_12_34, vmax_23_45);
          float32x2_t vmax_567_789 = vmax_f32(vmax_56_78, vmax_67_89);
H
hong19860320 已提交
2285 2286
          vst1_f32(&dout_ch[cnt], vmax_123_345);
          vst1_f32(&dout_ch[cnt + 2], vmax_567_789);
2287 2288
          cnt += 4;
        }
H
hong19860320 已提交
2289
        for (; w < w_even; w += 2) {
2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300
          float32x4_t vr0 = vld1q_f32(&r0[w]);
          float32x4_t vr1 = vld1q_f32(&r1[w]);
          float32x4_t vr2 = vld1q_f32(&r2[w]);
          vr0 = vsetq_lane_f32(minval, vr0, 3);
          vr1 = vsetq_lane_f32(minval, vr1, 3);
          vr2 = vsetq_lane_f32(minval, vr2, 3);
          float32x4_t vmax1 = vmaxq_f32(vr0, vr1);
          vmax1 = vmaxq_f32(vmax1, vr2);
          float32x2_t vmax2 =
              vpmax_f32(vget_low_f32(vmax1), vget_high_f32(vmax1));
          float32x2_t vmax = vpmax_f32(vmax2, vmax2);
H
hong19860320 已提交
2301
          dout_ch[cnt] = vget_lane_f32(vmax, 0);
2302 2303 2304
          cnt++;
        }
#else
H
hong19860320 已提交
2305 2306 2307 2308 2309 2310 2311
        dr_out = dout_ch;  // + 1;
        dr0 = r0;          // (r0 + 1);
        dr1 = r1;          // (r1 + 1);
        dr2 = r2;          // (r2 + 1);
        int cnt_num = w_unroll_size >> 3;
        int cnt_num_remain = w_unroll_remain >> 1;
        if (cnt_num > 0 || cnt_num_remain > 0) {
2312
          asm volatile(
H
hong19860320 已提交
2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348
              "cmp       %[cnt_num], #0           @cmp cnt_num,0\n"
              "ble       3f                       @ble exit\n"
              "1:                                 @main loop\n"
              "vld1.f32  {d0-d3}, [%[dr0]]!       @load d0-d5,dr0\n"
              "vld1.f32  {d6-d9}, [%[dr1]]!       @load d4-d7,dr1\n"
              "vld1.f32  {d12-d15}, [%[dr2]]!     @load d4-d7,dr1\n"
              "vld1.f32  {d4}, [%[dr0]]!          @load d0-d5,dr0\n"
              "vld1.f32  {d10}, [%[dr1]]!         @load d4-d7,dr1\n"
              "vld1.f32  {d16}, [%[dr2]]!         @load d4-d7,dr1\n"
              "vmax.f32  q9, q0, q3               @max q0,q0,q2\n"
              "vmax.f32  q10, q1, q4              @max q1,q1,q3\n"
              "vmax.f32  d22, d4, d10             @max q1,q1,q3\n"
              "vmax.f32  q0, q9, q6               @max q0,q0,q2 1234\n"
              "vmax.f32  q3, q10, q7              @max q1,q1,q3 5678\n"
              "vmax.f32  d2, d22, d16             @max q1,q1,q3 9101112\n"
              //"vmov.f32  s7,s6                  @mov s7, s6\n"
              "vext.f32  q4, q0, q3, #1           @vext 2345\n"
              "vext.f32  q2, q3, q1, #1           @vext 6789\n"
              "vpmax.f32 d10, d0, d1              @pmax "
              "d10,vmax_1234,vmax_1234\n"
              "vpmax.f32 d12, d6, d7              @pmax "
              "d12,vmax_5678,vmax_5678\n"
              "vpmax.f32 d11, d8, d9              @pmax "
              "d11,vmax_2345,vmax_2345\n"
              "vpmax.f32 d13, d4, d5              @pmax "
              "d13,vmax_6789,vmax_6789\n"
              "vmax.f32 d0, d10, d11              @pmax "
              "d0,vmax_12_34,vmax_23_45\n"
              "vmax.f32 d1, d12, d13              @pmax "
              "d1,vmax_56_78,vmax_67_89\n"
              "sub       %[dr0], #8               @add w,8\n"
              "sub       %[dr1], #8               @add w,8\n"
              "sub       %[dr2], #8               @add w,8\n"
              "vst1.f32  d0, [%[dr_out]]!         @vst1 d0,dr_out\n"
              "vst1.f32  d1, [%[dr_out]]!         @vst1 d0,dr_out\n"
              "subs      %[cnt_num], #1           @cnt_num--\n"
2349
              "bne       1b                       @bne s3_max_loop_mid\n"
H
hong19860320 已提交
2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370
              "3:                                 @loop\n"
              "cmp       %[cnt_num_remain], #0    @cmp cnt_num_remain,0\n"
              "ble       4f                       @ble exit1\n"
              "2:                                 @mid loop\n"
              "vld1.f32  {d0-d1}, [%[dr0]]!       @load d0-d1,dr0\n"
              "vld1.f32  {d2-d3}, [%[dr1]]!       @load d2-d3,dr1\n"
              "vld1.f32  {d4-d5}, [%[dr2]]!       @load d2-d3,dr1\n"
              "vmov.f32  s3,s2                    @movs3,s2\n"
              "vmov.f32  s7,s6                    @movs7,s6\n"
              "vmov.f32  s11,s10                  @movs11,s10\n"
              "vmax.f32  q0, q0, q1               @max q0,q0,q1\n"
              "vmax.f32  q0, q0, q2               @max q0,q0,q2\n"
              "vpmax.f32 d0, d0, d1               @pmax d0,d0,d1\n"
              "vpmax.f32 d0, d0, d0               @pmax d0,d0,d0\n"
              "vst1.f32  d0[0], [%[dr_out]]!      @vst d0[0],dr_out\n"
              "sub       %[dr0], #8               @add w,6\n"
              "sub       %[dr1], #8               @add w,6\n"
              "sub       %[dr2], #8               @add w,6\n"
              "subs      %[cnt_num_remain], #1    @cnt_num_remain--\n"
              "bne       2b                       @bne s3_max_loop_mid_1\n"
              "4:                                 @exit\n"
2371 2372
              : [dr0] "+r"(dr0), [dr1] "+r"(dr1), [dr2] "+r"(dr2),
                [dr_out] "+r"(dr_out), [cnt_num] "+r"(cnt_num),
H
hong19860320 已提交
2373
                [cnt_num_remain] "+r"(cnt_num_remain)
2374
              : "r"(dr0), "r"(dr1), "r"(dr2), "r"(dr_out), "r"(cnt_num),
H
hong19860320 已提交
2375
                "r"(cnt_num_remain)
2376 2377 2378 2379
              : "cc", "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
                "q8", "q9", "q10", "q11", "q12");
        }
#endif
H
hong19860320 已提交
2380
        if (w_remain > 0) {
2381
          // deal with right pad
H
hong19860320 已提交
2382 2383
          int wstart = (w_even >> 1) * stride - padding;
          int wend = std::min(std::min(wstart + kernel, win + padding), win);
2384 2385 2386 2387 2388
          float tmp = r0[wstart];  // std::numeric_limits<float>::min();
          for (int i = wstart; i < wend; i++) {
            tmp = std::max(tmp, std::max(r0[i], r1[i]));
            tmp = std::max(tmp, r2[i]);
          }
H
hong19860320 已提交
2389
          dout_ch[w_even >> 1] = tmp;
2390 2391 2392
          // cnt ++;
        }
        r0 = r2;
H
hong19860320 已提交
2393 2394 2395
        r1 = r0 + win;
        r2 = r1 + win;
        dout_ch += wout;
2396 2397
      }

H
hong19860320 已提交
2398
      if (h_remain > 0) {
2399 2400 2401
// deal with bottom pad
// first row with zero pad
// int hstart = (h >> 1) * stride_h - pad_h;
H
hong19860320 已提交
2402 2403
// int hend = std::min(std::min(hstart + kernel_h, hin + pad_h), hin);
// dout_ch[0] = std::max(std::max(r0[0], r0[1]), std::max(r1[0],
2404 2405 2406 2407
// r1[1]));
#ifdef __aarch64__
        w = 0;
        cnt = 0;
H
hong19860320 已提交
2408
        for (; w < w_unroll_size; w += 8) {
2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429
          float32x4_t vr0_1234 = vld1q_f32(&r0[w]);
          float32x4_t vr0_5678 = vld1q_f32(&r0[w + 4]);
          float32x4_t vr0_9101112 = vld1q_f32(&r0[w + 8]);
          float32x4_t vr1_1234 = vld1q_f32(&r1[w]);
          float32x4_t vr1_5678 = vld1q_f32(&r1[w + 4]);
          float32x4_t vr1_9101112 = vld1q_f32(&r1[w + 8]);
          float32x4_t vmax_1234 = vmaxq_f32(vr0_1234, vr1_1234);
          float32x4_t vmax_5678 = vmaxq_f32(vr0_5678, vr1_5678);
          float32x4_t vmax_9101112 = vmaxq_f32(vr0_9101112, vr1_9101112);
          float32x4_t vmax_2345 = vextq_f32(vmax_1234, vmax_5678, 1);
          float32x4_t vmax_6789 = vextq_f32(vmax_5678, vmax_9101112, 1);
          float32x2_t vmax_12_34 =
              vpmax_f32(vget_low_f32(vmax_1234), vget_high_f32(vmax_1234));
          float32x2_t vmax_23_45 =
              vpmax_f32(vget_low_f32(vmax_2345), vget_high_f32(vmax_2345));
          float32x2_t vmax_56_78 =
              vpmax_f32(vget_low_f32(vmax_5678), vget_high_f32(vmax_5678));
          float32x2_t vmax_67_89 =
              vpmax_f32(vget_low_f32(vmax_6789), vget_high_f32(vmax_6789));
          float32x2_t vmax_123_345 = vmax_f32(vmax_12_34, vmax_23_45);
          float32x2_t vmax_567_789 = vmax_f32(vmax_56_78, vmax_67_89);
H
hong19860320 已提交
2430 2431
          vst1_f32(&dout_ch[cnt], vmax_123_345);
          vst1_f32(&dout_ch[cnt + 2], vmax_567_789);
2432 2433
          cnt += 4;
        }
H
hong19860320 已提交
2434
        for (; w < w_even; w += 2) {
2435 2436 2437 2438 2439 2440 2441 2442
          float32x4_t vr0 = vld1q_f32(&r0[w]);
          float32x4_t vr1 = vld1q_f32(&r1[w]);
          vr0 = vsetq_lane_f32(minval, vr0, 3);
          vr1 = vsetq_lane_f32(minval, vr1, 3);
          float32x4_t vmax1 = vmaxq_f32(vr0, vr1);
          float32x2_t vmax2 =
              vpmax_f32(vget_low_f32(vmax1), vget_high_f32(vmax1));
          vmax2 = vpmax_f32(vmax2, vmax2);
H
hong19860320 已提交
2443
          dout_ch[cnt] = vget_lane_f32(vmax2, 0);
2444 2445 2446
          cnt++;
        }
#else
H
hong19860320 已提交
2447 2448 2449 2450 2451 2452
        dr_out = dout_ch;  // + 1;
        dr0 = r0;          // (r0 + 1);
        dr1 = r1;          // (r1 + 1);
        int cnt_num = w_unroll_size >> 3;
        int cnt_num_remain = w_unroll_remain >> 1;
        if (cnt_num > 0 || cnt_num_remain > 0) {
2453
          asm volatile(
H
hong19860320 已提交
2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501
              "cmp       %[cnt_num], #0           @cmp cnt_num,0\n"
              "ble       3f                       @ble exit\n"
              "1:                                 @main loop\n"
              "vld1.f32  {d0-d3}, [%[dr0]]!       @load d0-d5,dr0\n"
              "vld1.f32  {d6-d9}, [%[dr1]]!       @load d4-d7,dr1\n"
              "vld1.f32  {d4}, [%[dr0]]!          @load d0-d3,dr0\n"
              "vld1.f32  {d10}, [%[dr1]]!         @load d4-d7,dr1\n"
              "vmax.f32  q6, q0, q3               @max q0,q0,q2 1234\n"
              "vmax.f32  q7, q1, q4               @max q1,q1,q3 5678\n"
              "vmax.f32  d16, d4, d10             @max q1,q1,q3 9101112\n"
              //"vmov.f32  s7,s6                  @mov s7,s6\n"
              "vext.f32  q0, q6, q7, #1           @vext q0,2345\n"
              "vext.f32  q1, q7, q8, #1           @vext q1,6789\n"
              "vpmax.f32 d4, d12, d13             @pmax "
              "d4,vmax_1234,vmax_1234\n"
              "vpmax.f32 d6, d14, d15             @pmax "
              "d6,vmax_5678,vmax_5678\n"
              "vpmax.f32 d5, d0, d1               @pmax "
              "d5,vmax_2345,vmax_2345\n"
              "vpmax.f32 d7, d2, d3               @pmax "
              "d7,vmax_6789,vmax_6789\n"
              "vmax.f32 d8, d4, d5                @max "
              "d2,vmax_12_34,vmax_23_45\n"
              "vmax.f32 d9, d6, d7                @max "
              "d2,vmax_56_78,vmax_67_89\n"
              "sub       %[dr0], #8               @add w,8\n"
              "sub       %[dr1], #8               @add w,8\n"
              "vst1.f32  d8, [%[dr_out]]!         @vst1 d0,dr_out\n"
              "vst1.f32  d9, [%[dr_out]]!         @vst1 d0,dr_out\n"
              "subs      %[cnt_num], #1           @subs cnt_num,#1\n"
              "bne       1b                       @bne s3_max_loop_bot\n"
              "3:                                 @loop \n"
              "cmp       %[cnt_num_remain], #0    @cmp cnt_num_remain,0\n"
              "ble       4f                       @ble exit\n"
              "2:                                 @bot loop\n"
              "vld1.f32  {d0-d1}, [%[dr0]]!       @load d0-d1,dr0\n"
              "vld1.f32  {d2-d3}, [%[dr1]]!       @load d2-d3,dr1\n"
              "vmov.f32  s3,s2                    @movs3,s2\n"
              "vmov.f32  s7,s6                    @movs7,s6\n"
              "vmax.f32  q0, q0, q1               @max q0,q0,q1\n"
              "vpmax.f32 d0, d0, d1               @pmax d0,d0,d1\n"
              "vpmax.f32 d0, d0, d0               @pmax d0,d0,d0\n"
              "vst1.f32  d0[0], [%[dr_out]]!      @vst d0[0],dr_out\n"
              "sub       %[dr0], #8               @add w,6\n"
              "sub       %[dr1], #8               @add w,6\n"
              "subs      %[cnt_num_remain], #1    @cnt_num_remain--\n"
              "bne       2b                       @bne s3_max_loop_bot_1\n"
              "4:                                 @exit\n"
2502
              : [dr0] "+r"(dr0), [dr1] "+r"(dr1), [dr_out] "+r"(dr_out),
H
hong19860320 已提交
2503 2504 2505
                [cnt_num] "+r"(cnt_num), [cnt_num_remain] "+r"(cnt_num_remain)
              : "r"(dr0), "r"(dr1), "r"(dr_out), "r"(cnt_num),
                "r"(cnt_num_remain)
2506 2507 2508 2509
              : "cc", "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7",
                "q8", "q9");
        }
#endif
H
hong19860320 已提交
2510
        if (w_remain > 0) {
2511
          // deal with right pad
H
hong19860320 已提交
2512 2513
          int wstart = (w_even >> 1) * stride - padding;
          int wend = std::min(std::min(wstart + kernel, win + padding), win);
2514 2515 2516 2517
          float tmp = r0[wstart];  // std::numeric_limits<float>::min();
          for (int i = wstart; i < wend; i++) {  // only run 1 or 2 times
            tmp = std::max(tmp, std::max(r0[i], r1[i]));
          }
H
hong19860320 已提交
2518
          dout_ch[w_even >> 1] = tmp;
2519 2520 2521 2522 2523 2524
        }
      }
    }
  }
}

H
hong19860320 已提交
2525
void pooling3x3s2p0_avg(const float* din, float* dout, int num, int chout,
2526
                        int hout, int wout, int chin, int hin, int win,
H
hong19860320 已提交
2527 2528 2529 2530 2531 2532
                        bool exclusive) {
  int kernel = 3;
  int stride = 2;
  int padding = 0;
  int size_channel_out = wout * hout;
  int size_channel_in = win * hin;
2533

H
hong19860320 已提交
2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548
  int w_needed = (wout << 1) + 1;
  int h_needed = (hout << 1) + 1;
  int w_limit = w_needed > win ? win : w_needed;
  int h_limit = h_needed > hin ? hin : h_needed;
  int w_even = ((w_limit - 1) >> 1) << 1;
  int h_even = ((h_limit - 1) >> 1) << 1;
  int w_unroll_size = (w_even >> 3) << 3;
  int w_unroll_remain = w_even - w_unroll_size;
  int w_remain = w_needed - w_limit;
  int h_remain = h_needed - h_limit;
  int w_in_2 = win << 1;
  const float coef = 1.f / 9.f;
  const float coef_6 = exclusive ? 1.f / 6.f : coef;
  float32x4_t vcoef = vdupq_n_f32(coef);
  float32x4_t vcoef_6 = vdupq_n_f32(coef_6);
2549
  for (int n = 0; n < num; ++n) {
H
hong19860320 已提交
2550 2551
    float* dout_batch = dout + n * chout * size_channel_out;
    const float* din_batch = din + n * chin * size_channel_in;
2552
#pragma omp parallel for
H
hong19860320 已提交
2553 2554 2555 2556 2557 2558
    for (int c = 0; c < chout; c++) {
      float* dout_ch = dout_batch + c * size_channel_out;
      const float* din_ch = din_batch + c * size_channel_in;
      const float* r0 = din_ch;
      const float* r1 = r0 + win;
      const float* r2 = r1 + win;
2559
      // w = w_in - 8;
H
hong19860320 已提交
2560
      float* dr_out = dout_ch;
2561 2562 2563 2564 2565 2566 2567 2568
      const float* dr0 = r0;
      const float* dr1 = r1;
      const float* dr2 = r2;

      float32x4_t vzero = vdupq_n_f32(0.f);

      int h = 0;
      for (; h < h_even; h += 2) {
H
hong19860320 已提交
2569 2570 2571 2572 2573
// LOG(INFO) << "h: " << h <<", dr0:" << r0 << ", dr1: " << r1 <<
// ",dr2: " <<r2; deal with left pad float sum0 = r0[0] + r0[1]; float
// sum1 = r1[0] + r1[1]; float sum2 = r2[0] + r2[1]; dout_channel[0] =
// (sum0 + sum1 + sum2) / 9.f;
#ifdef __aarch64__
2574 2575
        int w = 0;
        int cnt = 0;
H
hong19860320 已提交
2576
        for (; w < w_unroll_size; w += 8) {
2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607
          float32x4_t vr0_1234 = vld1q_f32(&r0[w]);
          float32x4_t vr0_5678 = vld1q_f32(&r0[w + 4]);
          float32x4_t vr0_9101112 = vld1q_f32(&r0[w + 8]);
          float32x4_t vr1_1234 = vld1q_f32(&r1[w]);
          float32x4_t vr1_5678 = vld1q_f32(&r1[w + 4]);
          float32x4_t vr1_9101112 = vld1q_f32(&r1[w + 8]);
          float32x4_t vr2_1234 = vld1q_f32(&r2[w]);
          float32x4_t vr2_5678 = vld1q_f32(&r2[w + 4]);
          float32x4_t vr2_9101112 = vld1q_f32(&r2[w + 8]);
          float32x4_t vsum_1234 = vaddq_f32(vr0_1234, vr1_1234);
          float32x4_t vsum_5678 = vaddq_f32(vr0_5678, vr1_5678);
          float32x4_t vsum_9101112 = vaddq_f32(vr0_9101112, vr1_9101112);
          vsum_1234 = vaddq_f32(vsum_1234, vr2_1234);
          vsum_5678 = vaddq_f32(vsum_5678, vr2_5678);
          vsum_9101112 = vaddq_f32(vsum_9101112, vr2_9101112);

          float32x4_t vsum_2345 = vextq_f32(vsum_1234, vsum_5678, 1);
          float32x4_t vsum_3456 = vextq_f32(vsum_1234, vsum_5678, 2);
          float32x4_t vsum_4567 = vextq_f32(vsum_1234, vsum_5678, 3);
          float32x4_t vsum_6789 = vextq_f32(vsum_5678, vsum_9101112, 1);
          float32x4_t vsum_123_345 = vaddq_f32(vsum_1234, vsum_2345);
          vsum_123_345 = vaddq_f32(vsum_123_345, vsum_3456);
          float32x4_t vsum_567_789 = vaddq_f32(vsum_4567, vsum_5678);
          vsum_567_789 = vaddq_f32(vsum_567_789, vsum_6789);
          vsum_123_345 =
              vsetq_lane_f32(vgetq_lane_f32(vsum_123_345, 2), vsum_123_345, 1);
          vsum_123_345 =
              vsetq_lane_f32(vgetq_lane_f32(vsum_567_789, 1), vsum_123_345, 2);
          vsum_123_345 =
              vsetq_lane_f32(vgetq_lane_f32(vsum_567_789, 3), vsum_123_345, 3);
          float32x4_t vrst = vmulq_f32(vsum_123_345, vcoef);
H
hong19860320 已提交
2608
          vst1q_f32(&dout_ch[cnt], vrst);
2609 2610
          cnt += 4;
        }
H
hong19860320 已提交
2611
        for (; w < w_even; w += 2) {
2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622
          float32x4_t vr0 = vld1q_f32(&r0[w]);
          float32x4_t vr1 = vld1q_f32(&r1[w]);
          float32x4_t vr2 = vld1q_f32(&r2[w]);
          vr0 = vsetq_lane_f32(0.f, vr0, 3);
          vr1 = vsetq_lane_f32(0.f, vr1, 3);
          vr2 = vsetq_lane_f32(0.f, vr2, 3);
          float32x4_t vsum1 = vaddq_f32(vr0, vr1);
          vsum1 = vaddq_f32(vsum1, vr2);
          float32x2_t vsum2 =
              vpadd_f32(vget_low_f32(vsum1), vget_high_f32(vsum1));
          float32x2_t vsum = vpadd_f32(vsum2, vsum2);
H
hong19860320 已提交
2623
          dout_ch[cnt] = vget_lane_f32(vsum, 0) * coef;
2624 2625 2626
          cnt++;
        }
#else
H
hong19860320 已提交
2627 2628 2629 2630 2631 2632 2633 2634 2635
        dr_out = dout_ch;  // + 1;
        dr0 = r0;          // (r0 + 1);
        dr1 = r1;          // (r1 + 1);
        dr2 = r2;          // (r2 + 1);
        int cnt_num = w_unroll_size >> 3;
        int cnt_num_remain = w_unroll_remain >> 1;
        // LOG(INFO) << "cnt_num: " << cnt_num << " cnt_num_remain: " <<
        // cnt_num_remain;
        if (cnt_num > 0 || cnt_num_remain > 0) {
2636
          asm volatile(
H
hong19860320 已提交
2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693
              "cmp       %[cnt_num], #0           @cmp cnt_num, 0\n"
              "ble       loop3_ave_p0             @ble exit\n"
              "s3_ave_loop_mid_p0:                @main loop\n"
              "vld1.f32  {d0-d3}, [%[dr0]]!       @load d0-d5, dr0\n"
              "vld1.f32  {d6-d9}, [%[dr1]]!       @load d4-d7, dr1\n"
              "vld1.f32  {d12-d15}, [%[dr2]]!     @load d4-d7, dr2\n"
              "vld1.f32  {d4}, [%[dr0]]!          @load d0-d5, dr0\n"
              "vld1.f32  {d10}, [%[dr1]]!         @load d4-d7, dr1\n"
              "vld1.f32  {d16}, [%[dr2]]!         @load d4-d7, dr2\n"
              "vadd.f32  q9, q0, q3               @max q0,q0,q2\n"
              "vadd.f32  q10, q1, q4              @max q1,q1,q3\n"
              "vadd.f32  d22, d4, d10             @max q1,q1,q3\n"
              "vadd.f32  q6, q9, q6               @max q0,q0,q2 1234\n"
              "vadd.f32  q7, q10, q7              @max q1,q1,q3 5678\n"
              "vadd.f32  d16, d22, d16            @max q1,q1,q3 9101112\n"
              //"vmov.f32  s7,s6                  @mov s7, s6\n"
              "vext.f32  q0, q6, q7, #1           @vext max_2345\n"
              "vext.f32  q1, q6, q7, #3           @vext max_4567\n"
              "vext.f32  q2, q6, q7, #2           @vext max_3456\n"
              "vext.f32  q3, q7, q8, #1           @vext max_6789\n"
              "vadd.f32  q4, q6, q0               @add 1234, 2345\n"
              "vadd.f32  q5, q7, q1               @add 5678, 4567\n"
              "vadd.f32  q4, q4, q2               @add 3456, sum1\n"
              "vadd.f32  q5, q5, q3               @add 6789, sum2\n"
              "vmov.f32  s17, s18                 @mov\n"
              "vmov.f32  s18, s21                 @mov\n"
              "vmov.f32  s19, s23                 @mov\n"
              "vmul.f32  q4, q4, %q[vcoef]        @mul\n"
              "sub       %[dr0], #8               @add w,8\n"
              "sub       %[dr1], #8               @add w,8\n"
              "sub       %[dr2], #8               @add w,8\n"
              "subs      %[cnt_num], #1           @cnt_num--\n"
              "vst1.f32  d8, [%[dr_out]]!         @vst1 d0,dr_out\n"
              "vst1.f32  d9, [%[dr_out]]!         @vst1 d0,dr_out\n"
              "bne       s3_ave_loop_mid_p0       @bne s3_max_loop_mid\n"
              "loop3_ave_p0:                      @loop\n"
              "cmp       %[cnt_num_remain], #0    @cmp cnt_num_remain,0\n"
              "ble       exit1_ave_p0             @ble exit1\n"
              "s3_ave_loop_mid_1_p0:              @mid loop\n"
              "vld1.f32  {d0-d1}, [%[dr0]]!       @load d0-d1,dr0\n"
              "vld1.f32  {d2-d3}, [%[dr1]]!       @load d2-d3,dr1\n"
              "vld1.f32  {d4-d5}, [%[dr2]]!       @load d2-d3,dr1\n"
              "vext.f32  q0, %q[vzero], q0, #3    @ext v0_0123\n"
              "vext.f32  q1, %q[vzero], q1, #3    @ext v1_0123\n"
              "vext.f32  q2, %q[vzero], q2, #3    @ext v1_0123\n"
              "vadd.f32  q0, q0, q1               @add q0,q0,q1\n"
              "vadd.f32  q0, q0, q2               @add q0,q0,q1\n"
              "vpadd.f32 d0, d0, d1               @padd d0,d0,d1\n"
              "vpadd.f32 d0, d0, d0               @padd d0,d0,d0\n"
              "vmul.f32  d0, d0, %e[vcoef]        @mul\n"
              "sub       %[dr0], #8               @add w,6\n"
              "sub       %[dr1], #8               @add w,6\n"
              "sub       %[dr2], #8               @add w,6\n"
              "subs      %[cnt_num_remain], #1    @cnt_num_remain--\n"
              "vst1.f32  d0[0], [%[dr_out]]!      @vst d0[0],dr_out\n"
              "bne       s3_ave_loop_mid_1_p0     @bne s3_max_loop_mid_1\n"
              "exit1_ave_p0:                      @exit\n"
2694 2695
              : [dr0] "+r"(dr0), [dr1] "+r"(dr1), [dr2] "+r"(dr2),
                [dr_out] "+r"(dr_out), [cnt_num] "+r"(cnt_num),
H
hong19860320 已提交
2696
                [cnt_num_remain] "+r"(cnt_num_remain), [vcoef] "+w"(vcoef),
2697 2698
                [vzero] "+w"(vzero)
              : "r"(dr0), "r"(dr1), "r"(dr2), "r"(dr_out), "r"(cnt_num),
H
hong19860320 已提交
2699
                "r"(cnt_num_remain)
2700 2701 2702 2703
              : "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8", "q9",
                "q10", "q11", "q12");
        }
#endif
H
hong19860320 已提交
2704
        if (w_remain > 0) {
2705
          // deal with right pad
H
hong19860320 已提交
2706 2707 2708 2709
          int wstart = (w_even >> 1) * stride - padding;
          int wend = std::min(std::min(wstart + kernel, win + padding), win);
          float tmp1 = 0.f;
          float tmp2 = exclusive ? 1.0f / (3.f * (wend - wstart)) : coef;
2710
          for (int i = wstart; i < wend; i++) {
H
hong19860320 已提交
2711
            tmp1 += (r0[i] + r1[i] + r2[i]);
2712
          }
H
hong19860320 已提交
2713
          dout_ch[w_even >> 1] = tmp1 * tmp2;
2714 2715 2716
          // cnt ++;
        }
        r0 = r2;
H
hong19860320 已提交
2717 2718 2719
        r1 = r0 + win;
        r2 = r1 + win;
        dout_ch += wout;
2720 2721
      }

H
hong19860320 已提交
2722
      if (h_remain > 0) {
2723 2724 2725
// deal with bottom pad
// first row with zero pad
// int hstart = (h >> 1) * stride_h - pad_h;
H
hong19860320 已提交
2726 2727 2728 2729
// int hend = std::min(std::min(hstart + kernel_h, hin + padding_h),
// hin); data_out_channel[0] =(r0[0] + r0[1] + r0[2] + r1[0] + r1[1] +
// r1[2]) / 9.f;
#ifdef __aarch64__
2730 2731
        int w = 0;
        int cnt = 0;
H
hong19860320 已提交
2732
        for (; w < w_unroll_size; w += 8) {
2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756
          float32x4_t vr0_1234 = vld1q_f32(&r0[w]);
          float32x4_t vr0_5678 = vld1q_f32(&r0[w + 4]);
          float32x4_t vr0_9101112 = vld1q_f32(&r0[w + 8]);
          float32x4_t vr1_1234 = vld1q_f32(&r1[w]);
          float32x4_t vr1_5678 = vld1q_f32(&r1[w + 4]);
          float32x4_t vr1_9101112 = vld1q_f32(&r1[w + 8]);

          float32x4_t vsum_1234 = vaddq_f32(vr0_1234, vr1_1234);
          float32x4_t vsum_5678 = vaddq_f32(vr0_5678, vr1_5678);
          float32x4_t vsum_9101112 = vaddq_f32(vr0_9101112, vr1_9101112);
          float32x4_t vsum_2345 = vextq_f32(vsum_1234, vsum_5678, 1);
          float32x4_t vsum_3456 = vextq_f32(vsum_1234, vsum_5678, 2);
          float32x4_t vsum_4567 = vextq_f32(vsum_1234, vsum_5678, 3);
          float32x4_t vsum_6789 = vextq_f32(vsum_5678, vsum_9101112, 1);
          float32x4_t vsum_123_345 = vaddq_f32(vsum_1234, vsum_2345);
          vsum_123_345 = vaddq_f32(vsum_123_345, vsum_3456);
          float32x4_t vsum_567_789 = vaddq_f32(vsum_4567, vsum_5678);
          vsum_567_789 = vaddq_f32(vsum_567_789, vsum_6789);
          vsum_123_345 =
              vsetq_lane_f32(vgetq_lane_f32(vsum_123_345, 2), vsum_123_345, 1);
          vsum_123_345 =
              vsetq_lane_f32(vgetq_lane_f32(vsum_567_789, 1), vsum_123_345, 2);
          vsum_123_345 =
              vsetq_lane_f32(vgetq_lane_f32(vsum_567_789, 3), vsum_123_345, 3);
H
hong19860320 已提交
2757 2758
          float32x4_t vrst = vmulq_f32(vsum_123_345, vcoef_6);
          vst1q_f32(&dout_ch[cnt], vrst);
2759 2760
          cnt += 4;
        }
H
hong19860320 已提交
2761
        for (; w < w_even; w += 2) {
2762 2763 2764 2765 2766 2767 2768 2769
          float32x4_t vr0 = vld1q_f32(&r0[w]);
          float32x4_t vr1 = vld1q_f32(&r1[w]);
          vr0 = vsetq_lane_f32(0.f, vr0, 3);
          vr1 = vsetq_lane_f32(0.f, vr1, 3);
          float32x4_t vsum1 = vaddq_f32(vr0, vr1);
          float32x2_t vsum2 =
              vpadd_f32(vget_low_f32(vsum1), vget_high_f32(vsum1));
          vsum2 = vpadd_f32(vsum2, vsum2);
H
hong19860320 已提交
2770 2771
          float32x2_t vrst = vmul_f32(vsum2, vget_low_f32(vcoef_6));
          dout_ch[cnt] = vget_lane_f32(vrst, 0);
2772 2773 2774
          cnt++;
        }
#else
H
hong19860320 已提交
2775 2776 2777 2778 2779 2780 2781 2782
        dr_out = dout_ch;  // + 1;
        dr0 = r0;          // (r0 + 1);
        dr1 = r1;          // (r1 + 1);
        int cnt_num = w_unroll_size >> 3;
        int cnt_num_remain = w_unroll_remain >> 1;
        // LOG(INFO) << "cnt_num: " << cnt_num << " cnt_num_remain: " <<
        // cnt_num_remain;
        if (cnt_num > 0 || cnt_num_remain > 0) {
2783
          asm volatile(
H
hong19860320 已提交
2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830
              "cmp       %[cnt_num], #0           @cmp cnt_num,0\n"
              "ble       2f                       @ble exit\n"
              "1:                                 @main loop\n"
              "vld1.f32  {d0-d3}, [%[dr0]]!       @load d0-d5,dr0\n"
              "vld1.f32  {d6-d9}, [%[dr1]]!       @load d4-d7,dr1\n"
              "vld1.f32  {d4}, [%[dr0]]!          @load d0-d3,dr0\n"
              "vld1.f32  {d10}, [%[dr1]]!         @load d4-d7,dr1\n"
              "vadd.f32  q6, q0, q3               @max q0,q0,q2 1234\n"
              "vadd.f32  q7, q1, q4               @max q1,q1,q3 5678\n"
              "vadd.f32  d16, d4, d10             @max q1,q1,q3 9101112\n"
              //"vmov.f32  s7,s6                  @mov s7, s6\n"
              "vext.f32  q0, q6, q7, #1           @vext max_2345\n"
              "vext.f32  q1, q6, q7, #3           @vext max_4567\n"
              "vext.f32  q2, q6, q7, #2           @vext max_3456\n"
              "vext.f32  q3, q7, q8, #1           @vext max_6789\n"
              "vadd.f32  q4, q6, q0               @add 1234,2345\n"
              "vadd.f32  q5, q7, q1               @add 5678,4567\n"
              "vadd.f32  q4, q4, q2               @add 3456,sum1\n"
              "vadd.f32  q5, q5, q3               @add 6789,sum2\n"
              "vmov.f32  s17, s18                 @mov\n"
              "vmov.f32  s18, s21                 @mov\n"
              "vmov.f32  s19, s23                 @mov\n"
              "vmul.f32  q4, q4, %q[vcoef_6]      @mul\n"
              "sub       %[dr0], #8               @add w,8\n"
              "sub       %[dr1], #8               @add w,8\n"
              "subs      %[cnt_num], #1           @cnt_num--\n"
              "vst1.f32  d8, [%[dr_out]]!         @vst1 d0,dr_out\n"
              "vst1.f32  d9, [%[dr_out]]!         @vst1 d0,dr_out\n"
              "bne       1b                       @bne s3_max_loop_bot\n"
              "2:                                 @loop\n"
              "cmp       %[cnt_num_remain], #0    @cmp cnt_num_remain, 0\n"
              "ble       3f                       @ble exit\n"
              "4:                                 @bot loop\n"
              "vld1.f32  {d0-d1}, [%[dr0]]!       @load d0-d1,dr0\n"
              "vld1.f32  {d2-d3}, [%[dr1]]!       @load d2-d3,dr1\n"
              "vext.f32  q0, %q[vzero], q0, #3    @ext v0_0123\n"
              "vext.f32  q1, %q[vzero], q1, #3    @ext v1_0123\n"
              "vadd.f32  q0, q0, q1               @add q0,q0,q1\n"
              "vpadd.f32 d0, d0, d1               @padd d0,d0,d1\n"
              "vpadd.f32 d0, d0, d0               @padd d0,d0,d0\n"
              "vmul.f32  d0, d0, %e[vcoef_6]      @mul\n"
              "sub       %[dr0], #8               @add w,6\n"
              "sub       %[dr1], #8               @add w,6\n"
              "subs      %[cnt_num_remain], #1    @cnt_num_remain--\n"
              "vst1.f32  d0[0], [%[dr_out]]!      @vst d0[0],dr_out\n"
              "bne       4b                       @bne s3_max_loop_bot_1\n"
              "3:                                 @exit\n"
2831
              : [dr0] "+r"(dr0), [dr1] "+r"(dr1), [dr_out] "+r"(dr_out),
H
hong19860320 已提交
2832 2833 2834 2835
                [cnt_num] "+r"(cnt_num), [cnt_num_remain] "+r"(cnt_num_remain),
                [vcoef_6] "+w"(vcoef_6), [vzero] "+w"(vzero)
              : "r"(dr0), "r"(dr1), "r"(dr_out), "r"(cnt_num),
                "r"(cnt_num_remain)
2836 2837 2838 2839
              : "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8", "q9");
        }

#endif
H
hong19860320 已提交
2840
        if (w_remain > 0) {
2841
          // deal with right pad
H
hong19860320 已提交
2842 2843 2844 2845
          int wstart = (w_even >> 1) * stride - padding;
          int wend = std::min(std::min(wstart + kernel, win + padding), win);
          float tmp1 = 0.f;
          float tmp2 = exclusive ? 1.0f / (2.f * (wend - wstart)) : coef;
2846
          for (int i = wstart; i < wend; i++) {  // only run 1 or 2 times
H
hong19860320 已提交
2847
            tmp1 += (r0[i] + r1[i]);
2848
          }
H
hong19860320 已提交
2849
          dout_ch[w_even >> 1] = tmp1 * tmp2;
2850 2851 2852 2853 2854 2855 2856 2857 2858 2859
        }
      }
    }
  }
}

}  // namespace math
}  // namespace arm
}  // namespace lite
}  // namespace paddle