depthwise_conv3x3_int8.cpp 76.6 KB
Newer Older
H
hjchen2 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2018 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. */

H
update  
hjchen2 已提交
15
#if defined(__ARM_NEON__) || defined(__ARM_NEON)
H
hjchen2 已提交
16 17

#include <arm_neon.h>
H
hjchen2 已提交
18
#include "operators/math/depthwise_conv3x3.h"
H
hjchen2 已提交
19 20 21 22 23

namespace paddle_mobile {
namespace operators {
namespace math {

H
hjchen2 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#define DEPTHWISE_CONV_NORMAL_BORDER(start, end)                         \
  for (int w = start; w < end; ++w) {                                    \
    const int w_in_start = -padding_w + w * Stride_w;                    \
    const int w_in_end = w_in_start + 3;                                 \
    const int w_start = w_in_start > 0 ? w_in_start : 0;                 \
    const int w_end = w_in_end < input_w ? w_in_end : input_w;           \
    int32_t value = 0;                                                   \
    for (int h_in = h_start; h_in < h_end; ++h_in) {                     \
      for (int w_in = w_start; w_in < w_end; ++w_in) {                   \
        value += filter[(h_in - h_in_start) * 3 + (w_in - w_in_start)] * \
                 input[h_in * input_w + w_in];                           \
      }                                                                  \
    }                                                                    \
    output_ptr[w] = value;                                               \
  }

H
hjchen2 已提交
40 41 42 43 44
template <int Stride = 1>
inline void Depth3x3NormalRowLoadInput(const int8_t *input, int16x8_t *y) {
  y[0] = vmovl_s8(vld1_s8(input));
  y[1] = vextq_s16(y[0], y[0], 1);
  y[2] = vextq_s16(y[1], y[1], 1);
H
hjchen2 已提交
45 46 47
}

template <>
H
hjchen2 已提交
48
inline void Depth3x3NormalRowLoadInput<2>(const int8_t *input, int16x8_t *y) {
H
hjchen2 已提交
49
  int8x8x2_t x0 = vld2_s8(input);
H
hjchen2 已提交
50 51 52
  y[0] = vmovl_s8(x0.val[0]);
  y[1] = vmovl_s8(x0.val[1]);
  y[2] = vextq_s16(y[0], y[0], 1);
H
hjchen2 已提交
53 54 55 56 57 58 59
}

template <int Stride_h, int Stride_w>
inline void DepthwiseConv3x3NormalRow(const int8_t *input, const int8_t *filter,
                                      const int h_output, const int input_h,
                                      const int input_w, const int padding_h,
                                      const int padding_w, const int output_w,
H
hjchen2 已提交
60
                                      int32_t *output, int16x4_t *ker) {
H
hjchen2 已提交
61 62 63 64 65
  const int h_in_start = -padding_h + h_output * Stride_h;
  const int h_in_end = h_in_start + 3;
  const int h_start = h_in_start > 0 ? h_in_start : 0;
  const int h_end = h_in_end < input_h ? h_in_end : input_h;

H
hjchen2 已提交
66 67
  const int valid_w_start = (padding_w + Stride_w - 1) / Stride_w;
  const int valid_w_end = (input_w + padding_w - 3) / Stride_w + 1;
H
hjchen2 已提交
68 69 70 71 72
  int32_t *output_ptr = output + h_output * output_w;
  // border left
  DEPTHWISE_CONV_NORMAL_BORDER(0, valid_w_start)
  // middle
  int output_tiles = (valid_w_end - valid_w_start) / 6;
73
  int remain_start = valid_w_start + output_tiles * 6;
H
hjchen2 已提交
74
  int32x4_t _sum0, _sum1;
H
hjchen2 已提交
75
  int16x8_t _y[3];
H
hjchen2 已提交
76 77 78 79 80 81 82 83
  for (int w = 0; w < output_tiles * 6; w += 6) {
    _sum0 = veorq_s32(_sum0, _sum0);
    _sum1 = veorq_s32(_sum1, _sum1);
    int output_offset = valid_w_start + w;
    int input_w_offset = output_offset * Stride_w - padding_w;
    for (int h_in = h_start; h_in < h_end; ++h_in) {
      int index = h_in - h_in_start;
      Depth3x3NormalRowLoadInput<Stride_w>(
H
hjchen2 已提交
84 85 86 87 88 89 90
          input + h_in * input_w + input_w_offset, _y);
      _sum0 = vmlal_lane_s16(_sum0, vget_low_s16(_y[0]), ker[index], 0);
      _sum0 = vmlal_lane_s16(_sum0, vget_low_s16(_y[1]), ker[index], 1);
      _sum0 = vmlal_lane_s16(_sum0, vget_low_s16(_y[2]), ker[index], 2);
      _sum1 = vmlal_lane_s16(_sum1, vget_high_s16(_y[0]), ker[index], 0);
      _sum1 = vmlal_lane_s16(_sum1, vget_high_s16(_y[1]), ker[index], 1);
      _sum1 = vmlal_lane_s16(_sum1, vget_high_s16(_y[2]), ker[index], 2);
H
hjchen2 已提交
91 92
    }
    vst1q_s32(output_ptr + output_offset, _sum0);
H
hjchen2 已提交
93
    vst1_s32(output_ptr + output_offset + 4, vget_low_s32(_sum1));
H
hjchen2 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
  }
  for (int w = remain_start; w < valid_w_end; ++w) {
    int32_t value = 0;
    int input_start = -padding_w + w * Stride_w;
    for (int h_in = h_start; h_in < h_end; ++h_in) {
      for (int j = 0; j < 3; ++j) {
        value += filter[(h_in - h_in_start) * 3 + j] *
                 input[h_in * input_w + j + input_start];
      }
    }
    output_ptr[w] = value;
  }
  // border right
  DEPTHWISE_CONV_NORMAL_BORDER(valid_w_end, output_w)
}

H
hjchen2 已提交
110
template <>
H
hjchen2 已提交
111
void DepthwiseConv3x3S1<int8_t, int32_t>(const framework::Tensor &input,
H
hjchen2 已提交
112
                                         const framework::Tensor &filter,
113
                                         const std::vector<int> &paddings,
H
hjchen2 已提交
114
                                         framework::Tensor *output) {
H
hjchen2 已提交
115 116 117 118 119 120 121
  const int8_t *input_data = input.data<int8_t>();
  const int8_t *filter_data = filter.data<int8_t>();
  int32_t *out_data = output->mutable_data<int32_t>();
  int input_h = input.dims()[2];
  int input_w = input.dims()[3];
  int output_h = output->dims()[2];
  int output_w = output->dims()[3];
H
hjchen2 已提交
122 123
  int padding_h = paddings[0];
  int padding_w = paddings[1];
H
hjchen2 已提交
124 125
  int image_size = input_h * input_w;
  int out_image_size = output_h * output_w;
H
hjchen2 已提交
126 127 128 129 130 131 132
  int valid_h_start = padding_h;
  int valid_h_end = output_h - valid_h_start;
  int valid_h = valid_h_end - valid_h_start;
  int valid_w_start = padding_w;
  int valid_w_end = output_w - valid_w_start;
  int valid_w = valid_w_end - valid_w_start;

H
hjchen2 已提交
133
  #pragma omp parallel for
H
hjchen2 已提交
134 135 136 137
  for (int g = 0; g < input.dims()[1]; ++g) {
    const int8_t *input_ptr = input_data + g * image_size;
    const int8_t *filter_ptr = filter_data + g * 9;
    int32_t *output_ptr = out_data + g * out_image_size;
H
hjchen2 已提交
138 139 140 141 142 143 144 145 146 147 148

    const int8_t *filter_ptr0 = filter_ptr;
    const int8_t *filter_ptr1 = filter_ptr0 + 3;
    const int8_t *filter_ptr2 = filter_ptr1 + 3;
    int16x4_t _k0 = vget_low_s16(vmovl_s8(vld1_s8(filter_ptr0)));
    int16x4_t _k1 = vget_low_s16(vmovl_s8(vld1_s8(filter_ptr1)));
    int16x4_t _k2 = vget_low_s16(vmovl_s8(vld1_s8(filter_ptr2)));
    int16x8_t _ker0 = vcombine_s16(_k0, _k1);
    int16x8_t _ker1 = vcombine_s16(_k2, _k2);
    int16x4_t zero = vdup_n_s16(0);
    int16x4_t _ker[3] = {_k0, _k1, _k2};
H
hjchen2 已提交
149 150 151 152
    // top
    for (int h = 0; h < valid_h_start; ++h) {
      DepthwiseConv3x3NormalRow<1, 1>(input_ptr, filter_ptr, h, input_h,
                                      input_w, padding_h, padding_w, output_w,
H
hjchen2 已提交
153
                                      output_ptr, _ker);
H
hjchen2 已提交
154 155 156 157 158 159 160 161 162 163 164
    }
    // valid
    int output_w_tiles = valid_w / 6;
    int output_w_remain = valid_w - output_w_tiles * 6;
    for (int h = valid_h_start; h < valid_h_end - 3; h += 4) {
      const int8_t *input_ptr0 = input_ptr + (h - padding_h) * input_w;
      const int8_t *input_ptr1 = input_ptr0 + input_w;
      const int8_t *input_ptr2 = input_ptr1 + input_w;
      const int8_t *input_ptr3 = input_ptr2 + input_w;
      const int8_t *input_ptr4 = input_ptr3 + input_w;
      const int8_t *input_ptr5 = input_ptr4 + input_w;
H
hjchen2 已提交
165
      int32_t *output_ptr0 = output_ptr + h * output_w;
H
hjchen2 已提交
166 167 168
      int32_t *output_ptr1 = output_ptr0 + output_w;
      int32_t *output_ptr2 = output_ptr1 + output_w;
      int32_t *output_ptr3 = output_ptr2 + output_w;
H
hjchen2 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
      // pad left
      if (padding_w) {
        int16x4_t row0 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr0)));
        int16x4_t row1 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr1)));
        int16x4_t row2 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr2)));
        int16x4_t row3 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr3)));
        int16x4_t row4 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr4)));
        int16x4_t row5 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr5)));
        int32x4_t acc;
        for (int w = valid_w_start - 1; w >= 0; --w) {
          int padding = padding_w - w;
          if (padding >= 3) {
            output_ptr0[w] = 0;
            output_ptr1[w] = 0;
            output_ptr2[w] = 0;
            output_ptr3[w] = 0;
          } else {
            row0 = vext_s16(zero, row0, 3);
            row1 = vext_s16(zero, row1, 3);
            row2 = vext_s16(zero, row2, 3);
            row3 = vext_s16(zero, row3, 3);
            row4 = vext_s16(zero, row4, 3);
            row5 = vext_s16(zero, row5, 3);
            acc = vmull_s16(row0, _ker[0]);
            acc = vmlal_s16(acc, row1, _ker[1]);
            acc = vmlal_s16(acc, row2, _ker[2]);
            output_ptr0[w] = vgetq_lane_s32(acc, 1) + vgetq_lane_s32(acc, 2);
            acc = vmull_s16(row1, _ker[0]);
            acc = vmlal_s16(acc, row2, _ker[1]);
            acc = vmlal_s16(acc, row3, _ker[2]);
            output_ptr1[w] = vgetq_lane_s32(acc, 1) + vgetq_lane_s32(acc, 2);
            acc = vmull_s16(row2, _ker[0]);
            acc = vmlal_s16(acc, row3, _ker[1]);
            acc = vmlal_s16(acc, row4, _ker[2]);
            output_ptr2[w] = vgetq_lane_s32(acc, 1) + vgetq_lane_s32(acc, 2);
            acc = vmull_s16(row3, _ker[0]);
            acc = vmlal_s16(acc, row4, _ker[1]);
            acc = vmlal_s16(acc, row5, _ker[2]);
            output_ptr3[w] = vgetq_lane_s32(acc, 1) + vgetq_lane_s32(acc, 2);
          }
        }
        output_ptr0 += valid_w_start;
        output_ptr1 += valid_w_start;
        output_ptr2 += valid_w_start;
        output_ptr3 += valid_w_start;
      }
H
update  
hjchen2 已提交
215 216
#if __aarch64__
#else
H
hjchen2 已提交
217
      // valid
H
hjchen2 已提交
218
      int loop = output_w_tiles;
H
hjchen2 已提交
219
      asm volatile(
H
hjchen2 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
          "cmp        %[loop], #0                  \n"
          "ble        start_remain_%=              \n"
          "mov        r0, #6                       \n"
          // loop 6 width
          "loop_4h6w_%=:                           \n"
          "vld1.32    {d9}, [%[input_ptr0]], r0    \n"
          "vld1.32    {d10}, [%[input_ptr1]], r0   \n"
          "vld1.32    {d11}, [%[input_ptr2]], r0   \n"
          "vext.s8    d12, d9, d9, #1              \n"
          "vext.s8    d13, d9, d9, #2              \n"
          "vmovl.s8   q7, d9                       \n"
          "vmovl.s8   q8, d12                      \n"
          "vmovl.s8   q9, d13                      \n"
          "vmull.s16  q10, d14, %e[ker0][0]        \n"
          "vmlal.s16  q10, d16, %e[ker0][1]        \n"
          "vmlal.s16  q10, d18, %e[ker0][2]        \n"
          "vmull.s16  q11, d15, %e[ker0][0]        \n"
          "vmlal.s16  q11, d17, %e[ker0][1]        \n"
          "vmlal.s16  q11, d19, %e[ker0][2]        \n"

          "vext.s8    d12, d10, d10, #1            \n"
          "vext.s8    d13, d10, d10, #2            \n"
          "vmovl.s8   q7, d10                      \n"
          "vmovl.s8   q8, d12                      \n"
          "vmovl.s8   q9, d13                      \n"
          "vmlal.s16  q10, d14, %f[ker0][0]        \n"
          "vmlal.s16  q10, d16, %f[ker0][1]        \n"
          "vmlal.s16  q10, d18, %f[ker0][2]        \n"
          "vmlal.s16  q11, d15, %f[ker0][0]        \n"
          "vmlal.s16  q11, d17, %f[ker0][1]        \n"
          "vmlal.s16  q11, d19, %f[ker0][2]        \n"

          "vmull.s16  q12, d14, %e[ker0][0]        \n"
          "vmlal.s16  q12, d16, %e[ker0][1]        \n"
          "vmlal.s16  q12, d18, %e[ker0][2]        \n"
          "vmull.s16  q13, d15, %e[ker0][0]        \n"
          "vmlal.s16  q13, d17, %e[ker0][1]        \n"
          "vmlal.s16  q13, d19, %e[ker0][2]        \n"

          "vext.s8    d12, d11, d11, #1            \n"
          "vext.s8    d13, d11, d11, #2            \n"
          "vmovl.s8   q7, d11                      \n"
          "vmovl.s8   q8, d12                      \n"
          "vmovl.s8   q9, d13                      \n"
          "vmlal.s16  q10, d14, %e[ker1][0]        \n"
          "vmlal.s16  q10, d16, %e[ker1][1]        \n"
          "vmlal.s16  q10, d18, %e[ker1][2]        \n"
          "vmlal.s16  q11, d15, %e[ker1][0]        \n"
          "vmlal.s16  q11, d17, %e[ker1][1]        \n"
          "vmlal.s16  q11, d19, %e[ker1][2]        \n"
H
hjchen2 已提交
270 271 272
          // store row 0, reuse q10/q11
          "vst1.32    {d20-d22}, [%[output_ptr0]]! \n"

H
hjchen2 已提交
273 274 275 276 277 278
          "vmlal.s16  q12, d14, %f[ker0][0]        \n"
          "vmlal.s16  q12, d16, %f[ker0][1]        \n"
          "vmlal.s16  q12, d18, %f[ker0][2]        \n"
          "vmlal.s16  q13, d15, %f[ker0][0]        \n"
          "vmlal.s16  q13, d17, %f[ker0][1]        \n"
          "vmlal.s16  q13, d19, %f[ker0][2]        \n"
H
hjchen2 已提交
279

H
hjchen2 已提交
280 281 282 283 284 285
          "vmull.s16  q14, d14, %e[ker0][0]        \n"
          "vmlal.s16  q14, d16, %e[ker0][1]        \n"
          "vmlal.s16  q14, d18, %e[ker0][2]        \n"
          "vmull.s16  q15, d15, %e[ker0][0]        \n"
          "vmlal.s16  q15, d17, %e[ker0][1]        \n"
          "vmlal.s16  q15, d19, %e[ker0][2]        \n"
H
hjchen2 已提交
286

H
hjchen2 已提交
287 288 289
          "vld1.32    {d9}, [%[input_ptr3]], r0    \n"
          "vld1.32    {d10}, [%[input_ptr4]], r0   \n"
          "vld1.32    {d11}, [%[input_ptr5]], r0   \n"
H
hjchen2 已提交
290 291 292 293 294 295 296 297 298 299 300
          "vext.s8    d12, d9, d9, #1              \n"
          "vext.s8    d13, d9, d9, #2              \n"
          "vmovl.s8   q7, d9                       \n"
          "vmovl.s8   q8, d12                      \n"
          "vmovl.s8   q9, d13                      \n"
          "vmlal.s16  q12, d14, %e[ker1][0]        \n"
          "vmlal.s16  q12, d16, %e[ker1][1]        \n"
          "vmlal.s16  q12, d18, %e[ker1][2]        \n"
          "vmlal.s16  q13, d15, %e[ker1][0]        \n"
          "vmlal.s16  q13, d17, %e[ker1][1]        \n"
          "vmlal.s16  q13, d19, %e[ker1][2]        \n"
H
hjchen2 已提交
301 302 303
          // store row 1
          "vst1.32    {d24-d26}, [%[output_ptr1]]! \n"

H
hjchen2 已提交
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
          "vmlal.s16  q14, d14, %f[ker0][0]        \n"
          "vmlal.s16  q14, d16, %f[ker0][1]        \n"
          "vmlal.s16  q14, d18, %f[ker0][2]        \n"
          "vmlal.s16  q15, d15, %f[ker0][0]        \n"
          "vmlal.s16  q15, d17, %f[ker0][1]        \n"
          "vmlal.s16  q15, d19, %f[ker0][2]        \n"

          "vmull.s16  q10, d14, %e[ker0][0]        \n"
          "vmlal.s16  q10, d16, %e[ker0][1]        \n"
          "vmlal.s16  q10, d18, %e[ker0][2]        \n"
          "vmull.s16  q11, d15, %e[ker0][0]        \n"
          "vmlal.s16  q11, d17, %e[ker0][1]        \n"
          "vmlal.s16  q11, d19, %e[ker0][2]        \n"

          "vext.s8    d12, d10, d10, #1            \n"
          "vext.s8    d13, d10, d10, #2            \n"
          "vmovl.s8   q7, d10                      \n"
          "vmovl.s8   q8, d12                      \n"
          "vmovl.s8   q9, d13                      \n"
          "vmlal.s16  q14, d14, %e[ker1][0]        \n"
          "vmlal.s16  q14, d16, %e[ker1][1]        \n"
          "vmlal.s16  q14, d18, %e[ker1][2]        \n"
          "vmlal.s16  q15, d15, %e[ker1][0]        \n"
          "vmlal.s16  q15, d17, %e[ker1][1]        \n"
          "vmlal.s16  q15, d19, %e[ker1][2]        \n"
H
hjchen2 已提交
329
          // store row 2
H
hjchen2 已提交
330
          "vst1.32    {d28-d30}, [%[output_ptr2]]! \n"
H
hjchen2 已提交
331

H
hjchen2 已提交
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
          "vmlal.s16  q10, d14, %f[ker0][0]        \n"
          "vmlal.s16  q10, d16, %f[ker0][1]        \n"
          "vmlal.s16  q10, d18, %f[ker0][2]        \n"
          "vmlal.s16  q11, d15, %f[ker0][0]        \n"
          "vmlal.s16  q11, d17, %f[ker0][1]        \n"
          "vmlal.s16  q11, d19, %f[ker0][2]        \n"

          "vext.s8    d12, d11, d11, #1            \n"
          "vext.s8    d13, d11, d11, #2            \n"
          "vmovl.s8   q7, d11                      \n"
          "vmovl.s8   q8, d12                      \n"
          "vmovl.s8   q9, d13                      \n"
          "vmlal.s16  q10, d14, %e[ker1][0]        \n"
          "vmlal.s16  q10, d16, %e[ker1][1]        \n"
          "vmlal.s16  q10, d18, %e[ker1][2]        \n"
          "vmlal.s16  q11, d15, %e[ker1][0]        \n"
          "vmlal.s16  q11, d17, %e[ker1][1]        \n"
          "vmlal.s16  q11, d19, %e[ker1][2]        \n"
H
hjchen2 已提交
350 351 352
          // store row 3
          "vst1.32    {d20-d22}, [%[output_ptr3]]! \n"

H
hjchen2 已提交
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
          "subs       %[loop], #1                  \n"
          "bne        loop_4h6w_%=                 \n"

          "start_remain_%=:                        \n"
          "cmp        %[remain], #0                \n"
          "ble        end_%=                       \n"

          "mov        r0, %[remain]                \n"
          "vld1.32    {d9}, [%[input_ptr0]], r0    \n"
          "vmovl.s8   q7, d9                       \n"
          "vext.s8    d9, d9, d9, #1               \n"
          "vmovl.s8   q8, d9                       \n"
          "vext.s8    d9, d9, d9, #1               \n"
          "vmovl.s8   q9, d9                       \n"
          "vmull.s16  q10, d14, %e[ker0][0]        \n"
          "vmlal.s16  q10, d16, %e[ker0][1]        \n"
          "vmlal.s16  q10, d18, %e[ker0][2]        \n"
          "vld1.32    {d9}, [%[input_ptr1]], r0    \n"
          "vmull.s16  q11, d15, %e[ker0][0]        \n"
          "vmlal.s16  q11, d17, %e[ker0][1]        \n"
          "vmlal.s16  q11, d19, %e[ker0][2]        \n"

          "vmovl.s8   q7, d9                       \n"
          "vext.s8    d9, d9, d9, #1               \n"
          "vmovl.s8   q8, d9                       \n"
          "vext.s8    d9, d9, d9, #1               \n"
          "vmovl.s8   q9, d9                       \n"
          "vmlal.s16  q10, d14, %f[ker0][0]        \n"
          "vmlal.s16  q10, d16, %f[ker0][1]        \n"
          "vmlal.s16  q10, d18, %f[ker0][2]        \n"
          "vmlal.s16  q11, d15, %f[ker0][0]        \n"
          "vmlal.s16  q11, d17, %f[ker0][1]        \n"
          "vmlal.s16  q11, d19, %f[ker0][2]        \n"

          "vmull.s16  q12, d14, %e[ker0][0]        \n"
          "vmlal.s16  q12, d16, %e[ker0][1]        \n"
          "vmlal.s16  q12, d18, %e[ker0][2]        \n"
          "vld1.32    {d9}, [%[input_ptr2]], r0    \n"
          "vmull.s16  q13, d15, %e[ker0][0]        \n"
          "vmlal.s16  q13, d17, %e[ker0][1]        \n"
          "vmlal.s16  q13, d19, %e[ker0][2]        \n"

          "vmovl.s8   q7, d9                       \n"
          "vext.s8    d9, d9, d9, #1               \n"
          "vmovl.s8   q8, d9                       \n"
          "vext.s8    d9, d9, d9, #1               \n"
          "vmovl.s8   q9, d9                       \n"
          "vmlal.s16  q10, d14, %e[ker1][0]        \n"
          "vmlal.s16  q10, d16, %e[ker1][1]        \n"
          "vmlal.s16  q10, d18, %e[ker1][2]        \n"
          "vmlal.s16  q11, d15, %e[ker1][0]        \n"
          "vmlal.s16  q11, d17, %e[ker1][1]        \n"
          "vmlal.s16  q11, d19, %e[ker1][2]        \n"

          "vmlal.s16  q12, d14, %f[ker0][0]        \n"
          "vmlal.s16  q12, d16, %f[ker0][1]        \n"
          "vmlal.s16  q12, d18, %f[ker0][2]        \n"
          "vmlal.s16  q13, d15, %f[ker0][0]        \n"
          "vmlal.s16  q13, d17, %f[ker0][1]        \n"
          "vmlal.s16  q13, d19, %f[ker0][2]        \n"

          "vmull.s16  q14, d14, %e[ker0][0]        \n"
          "vmlal.s16  q14, d16, %e[ker0][1]        \n"
          "vmlal.s16  q14, d18, %e[ker0][2]        \n"
          "vld1.32    {d9}, [%[input_ptr3]], r0    \n"
          "vmull.s16  q15, d15, %e[ker0][0]        \n"
          "vmlal.s16  q15, d17, %e[ker0][1]        \n"
          "vmlal.s16  q15, d19, %e[ker0][2]        \n"

          "vmovl.s8   q7, d9                       \n"
          "vext.s8    d9, d9, d9, #1               \n"
          "vmovl.s8   q8, d9                       \n"
          "vext.s8    d9, d9, d9, #1               \n"
          "vmovl.s8   q9, d9                       \n"
          "vmlal.s16  q12, d14, %e[ker1][0]        \n"
          "vmlal.s16  q12, d16, %e[ker1][1]        \n"
          "vmlal.s16  q12, d18, %e[ker1][2]        \n"
          "vmlal.s16  q13, d15, %e[ker1][0]        \n"
          "vmlal.s16  q13, d17, %e[ker1][1]        \n"
          "vmlal.s16  q13, d19, %e[ker1][2]        \n"

          "vmlal.s16  q14, d14, %f[ker0][0]        \n"
          "vmlal.s16  q14, d16, %f[ker0][1]        \n"
          "vmlal.s16  q14, d18, %f[ker0][2]        \n"
          "vmlal.s16  q15, d15, %f[ker0][0]        \n"
          "vmlal.s16  q15, d17, %f[ker0][1]        \n"
          "vmlal.s16  q15, d19, %f[ker0][2]        \n"

          "vmull.s16  q5, d14, %e[ker0][0]         \n"
          "vmlal.s16  q5, d16, %e[ker0][1]         \n"
          "vmlal.s16  q5, d18, %e[ker0][2]         \n"
          "vld1.32    {d9}, [%[input_ptr4]], r0    \n"
          "vmull.s16  q6, d15, %e[ker0][0]         \n"
          "vmlal.s16  q6, d17, %e[ker0][1]         \n"
          "vmlal.s16  q6, d19, %e[ker0][2]         \n"

          "vmovl.s8   q7, d9                       \n"
          "vext.s8    d9, d9, d9, #1               \n"
          "vmovl.s8   q8, d9                       \n"
          "vext.s8    d9, d9, d9, #1               \n"
          "vmovl.s8   q9, d9                       \n"
          "vmlal.s16  q14, d14, %e[ker1][0]        \n"
          "vmlal.s16  q14, d16, %e[ker1][1]        \n"
          "vmlal.s16  q14, d18, %e[ker1][2]        \n"
          "vmlal.s16  q15, d15, %e[ker1][0]        \n"
          "vmlal.s16  q15, d17, %e[ker1][1]        \n"
          "vmlal.s16  q15, d19, %e[ker1][2]        \n"

          "vmlal.s16  q5, d14, %f[ker0][0]         \n"
          "vmlal.s16  q5, d16, %f[ker0][1]         \n"
          "vmlal.s16  q5, d18, %f[ker0][2]         \n"
          "vld1.32    {d9}, [%[input_ptr5]], r0    \n"
          "vmlal.s16  q6, d15, %f[ker0][0]         \n"
          "vmlal.s16  q6, d17, %f[ker0][1]         \n"
          "vmlal.s16  q6, d19, %f[ker0][2]         \n"

          "vmovl.s8   q7, d9                       \n"
          "vext.s8    d9, d9, d9, #1               \n"
          "vmovl.s8   q8, d9                       \n"
          "vext.s8    d9, d9, d9, #1               \n"
          "vmovl.s8   q9, d9                       \n"
          "vmlal.s16  q5, d14, %e[ker1][0]         \n"
          "vmlal.s16  q5, d16, %e[ker1][1]         \n"
          "vmlal.s16  q5, d18, %e[ker1][2]         \n"
          "vmlal.s16  q6, d15, %e[ker1][0]         \n"
          "vmlal.s16  q6, d17, %e[ker1][1]         \n"
          "vmlal.s16  q6, d19, %e[ker1][2]         \n"

          "cmp        %[remain], #4                \n"
          "blt        store_4h2w_%=                \n"
          "vst1.32    {q10}, [%[output_ptr0]]!     \n"
          "vst1.32    {q12}, [%[output_ptr1]]!     \n"
          "vst1.32    {q14}, [%[output_ptr2]]!     \n"
          "vst1.32    {q5}, [%[output_ptr3]]!      \n"
          "cmp        %[remain], #5                \n"
          "blt        end_%=                       \n"
          "vst1.32    {d22[0]}, [%[output_ptr0]]!  \n"
          "vst1.32    {d26[0]}, [%[output_ptr1]]!  \n"
          "vst1.32    {d30[0]}, [%[output_ptr2]]!  \n"
          "vst1.32    {d12[0]}, [%[output_ptr3]]!  \n"
          "b          end_%=                       \n"

          "store_4h2w_%=:                          \n"
          "cmp        %[remain], #2                \n"
          "blt        store_4h1w_%=                \n"
          "vst1.32    {d20}, [%[output_ptr0]]!     \n"
          "vst1.32    {d24}, [%[output_ptr1]]!     \n"
          "vst1.32    {d28}, [%[output_ptr2]]!     \n"
          "vst1.32    {d10}, [%[output_ptr3]]!     \n"
          "cmp        %[remain], #3                \n"
          "blt        end_%=                       \n"
          "vst1.32    {d21[0]}, [%[output_ptr0]]!  \n"
          "vst1.32    {d25[0]}, [%[output_ptr1]]!  \n"
          "vst1.32    {d29[0]}, [%[output_ptr2]]!  \n"
          "vst1.32    {d11[0]}, [%[output_ptr3]]!  \n"
          "b          end_%=                       \n"

          "store_4h1w_%=:                          \n"
          "cmp        %[remain], #1                \n"
          "blt        end_%=                       \n"
          "vst1.32    {d20[0]}, [%[output_ptr0]]!  \n"
          "vst1.32    {d24[0]}, [%[output_ptr1]]!  \n"
          "vst1.32    {d28[0]}, [%[output_ptr2]]!  \n"
          "vst1.32    {d10[0]}, [%[output_ptr3]]!  \n"
          "end_%=:                                 \n"
H
hjchen2 已提交
518 519 520 521
          : [output_ptr0] "+r"(output_ptr0), [output_ptr1] "+r"(output_ptr1),
            [output_ptr2] "+r"(output_ptr2), [output_ptr3] "+r"(output_ptr3),
            [input_ptr0] "+r"(input_ptr0), [input_ptr1] "+r"(input_ptr1),
            [input_ptr2] "+r"(input_ptr2), [input_ptr3] "+r"(input_ptr3),
522 523
            [input_ptr4] "+r"(input_ptr4), [input_ptr5] "+r"(input_ptr5),
            [loop] "+r"(loop)
H
hjchen2 已提交
524 525 526
          : [remain] "r"(output_w_remain), [ker0] "w"(_ker0), [ker1] "w"(_ker1)
          : "cc", "memory", "q4", "q5", "q6", "q7", "q8", "q9", "q10", "q11",
            "q12", "q13", "q14", "q15", "r0");
H
update  
hjchen2 已提交
527
#endif  // __aarch64__
H
hjchen2 已提交
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
      // pad right
      if (padding_w) {
        int16x4_t row0 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr0 - 2)));
        int16x4_t row1 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr1 - 2)));
        int16x4_t row2 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr2 - 2)));
        int16x4_t row3 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr3 - 2)));
        int16x4_t row4 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr4 - 2)));
        int16x4_t row5 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr5 - 2)));
        row0 = vext_s16(row0, zero, 2);
        row1 = vext_s16(row1, zero, 2);
        row2 = vext_s16(row2, zero, 2);
        row3 = vext_s16(row3, zero, 2);
        row4 = vext_s16(row4, zero, 2);
        row5 = vext_s16(row5, zero, 2);
        int32x4_t acc;
        for (int w = valid_w_end; w < output_w; ++w) {
          int padding = w + 3 - (padding_w + input_w);
          if (padding >= 3) {
            *output_ptr0 = 0;
            *output_ptr1 = 0;
            *output_ptr2 = 0;
            *output_ptr3 = 0;
          } else {
            acc = vmull_s16(row0, _ker[0]);
            acc = vmlal_s16(acc, row1, _ker[1]);
            acc = vmlal_s16(acc, row2, _ker[2]);
            *output_ptr0 = vgetq_lane_s32(acc, 0) + vgetq_lane_s32(acc, 1);
            acc = vmull_s16(row1, _ker[0]);
            acc = vmlal_s16(acc, row2, _ker[1]);
            acc = vmlal_s16(acc, row3, _ker[2]);
            *output_ptr1 = vgetq_lane_s32(acc, 0) + vgetq_lane_s32(acc, 1);
            acc = vmull_s16(row2, _ker[0]);
            acc = vmlal_s16(acc, row3, _ker[1]);
            acc = vmlal_s16(acc, row4, _ker[2]);
            *output_ptr2 = vgetq_lane_s32(acc, 0) + vgetq_lane_s32(acc, 1);
            acc = vmull_s16(row3, _ker[0]);
            acc = vmlal_s16(acc, row4, _ker[1]);
            acc = vmlal_s16(acc, row5, _ker[2]);
            *output_ptr3 = vgetq_lane_s32(acc, 0) + vgetq_lane_s32(acc, 1);

            row0 = vext_s16(row0, zero, 1);
            row1 = vext_s16(row1, zero, 1);
            row2 = vext_s16(row2, zero, 1);
            row3 = vext_s16(row3, zero, 1);
            row4 = vext_s16(row4, zero, 1);
            row5 = vext_s16(row5, zero, 1);
          }
          output_ptr0++;
          output_ptr1++;
          output_ptr2++;
          output_ptr3++;
        }
      }
H
hjchen2 已提交
581 582
    }
    // remain height
H
hjchen2 已提交
583 584 585 586 587 588
    int start_h = valid_h_start + (valid_h & 0xFFFC);
    for (int h = start_h; h < valid_h_end - 1; h += 2) {
      const int8_t *input_ptr0 = input_ptr + (h - padding_h) * input_w;
      const int8_t *input_ptr1 = input_ptr0 + input_w;
      const int8_t *input_ptr2 = input_ptr1 + input_w;
      const int8_t *input_ptr3 = input_ptr2 + input_w;
H
hjchen2 已提交
589
      int32_t *output_ptr0 = output_ptr + h * output_w;
H
hjchen2 已提交
590
      int32_t *output_ptr1 = output_ptr0 + output_w;
H
hjchen2 已提交
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
      // pad left
      if (padding_w) {
        int16x4_t row0 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr0)));
        int16x4_t row1 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr1)));
        int16x4_t row2 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr2)));
        int16x4_t row3 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr3)));
        int32x4_t acc;
        for (int w = valid_w_start - 1; w >= 0; --w) {
          int padding = padding_w - w;
          if (padding >= 3) {
            output_ptr0[w] = 0;
            output_ptr1[w] = 0;
          } else {
            row0 = vext_s16(zero, row0, 3);
            row1 = vext_s16(zero, row1, 3);
            row2 = vext_s16(zero, row2, 3);
            row3 = vext_s16(zero, row3, 3);
            acc = vmull_s16(row0, _ker[0]);
            acc = vmlal_s16(acc, row1, _ker[1]);
            acc = vmlal_s16(acc, row2, _ker[2]);
            output_ptr0[w] = vgetq_lane_s32(acc, 1) + vgetq_lane_s32(acc, 2);
            acc = vmull_s16(row1, _ker[0]);
            acc = vmlal_s16(acc, row2, _ker[1]);
            acc = vmlal_s16(acc, row3, _ker[2]);
            output_ptr1[w] = vgetq_lane_s32(acc, 1) + vgetq_lane_s32(acc, 2);
          }
        }
        output_ptr0 += valid_w_start;
        output_ptr1 += valid_w_start;
      }
H
update  
hjchen2 已提交
621 622 623
        // valid
#if __aarch64__
#else
H
hjchen2 已提交
624
      int loop = output_w_tiles;
H
hjchen2 已提交
625
      asm volatile(
H
hjchen2 已提交
626 627 628
          "cmp        %[loop], #0                  \n"
          "ble        start_remain_%=              \n"
          "mov        r0, #6                       \n"
H
hjchen2 已提交
629
          // loop 6 widths
H
hjchen2 已提交
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
          "loop_2h6w_%=:                           \n"
          "vld1.32    {d9}, [%[input_ptr0]], r0    \n"
          "vld1.32    {d10}, [%[input_ptr1]], r0   \n"
          "vld1.32    {d11}, [%[input_ptr2]], r0   \n"
          "vext.s8    d12, d9, d9, #1              \n"
          "vext.s8    d13, d9, d9, #2              \n"
          "vmovl.s8   q7, d9                       \n"
          "vmovl.s8   q8, d12                      \n"
          "vmovl.s8   q9, d13                      \n"
          "vmull.s16  q10, d14, %e[ker0][0]        \n"
          "vmlal.s16  q10, d16, %e[ker0][1]        \n"
          "vmlal.s16  q10, d18, %e[ker0][2]        \n"
          "vmull.s16  q11, d15, %e[ker0][0]        \n"
          "vmlal.s16  q11, d17, %e[ker0][1]        \n"
          "vmlal.s16  q11, d19, %e[ker0][2]        \n"

          "vext.s8    d12, d10, d10, #1            \n"
          "vext.s8    d13, d10, d10, #2            \n"
          "vmovl.s8   q7, d10                      \n"
          "vmovl.s8   q8, d12                      \n"
          "vmovl.s8   q9, d13                      \n"
          "vmlal.s16  q10, d14, %f[ker0][0]        \n"
          "vmlal.s16  q10, d16, %f[ker0][1]        \n"
          "vmlal.s16  q10, d18, %f[ker0][2]        \n"
          "vmlal.s16  q11, d15, %f[ker0][0]        \n"
          "vmlal.s16  q11, d17, %f[ker0][1]        \n"
          "vmlal.s16  q11, d19, %f[ker0][2]        \n"

          "vmull.s16  q12, d14, %e[ker0][0]        \n"
          "vmlal.s16  q12, d16, %e[ker0][1]        \n"
          "vmlal.s16  q12, d18, %e[ker0][2]        \n"
          "vmull.s16  q13, d15, %e[ker0][0]        \n"
          "vmlal.s16  q13, d17, %e[ker0][1]        \n"
          "vmlal.s16  q13, d19, %e[ker0][2]        \n"

          "vext.s8    d12, d11, d11, #1            \n"
          "vext.s8    d13, d11, d11, #2            \n"
          "vmovl.s8   q7, d11                      \n"
          "vmovl.s8   q8, d12                      \n"
          "vmovl.s8   q9, d13                      \n"
          "vmlal.s16  q10, d14, %e[ker1][0]        \n"
          "vmlal.s16  q10, d16, %e[ker1][1]        \n"
          "vmlal.s16  q10, d18, %e[ker1][2]        \n"
          "vmlal.s16  q11, d15, %e[ker1][0]        \n"
          "vmlal.s16  q11, d17, %e[ker1][1]        \n"
          "vmlal.s16  q11, d19, %e[ker1][2]        \n"
H
hjchen2 已提交
676 677 678
          // store row 0, reuse q10/q11
          "vst1.32    {d20-d22}, [%[output_ptr0]]! \n"

H
hjchen2 已提交
679 680 681 682 683 684
          "vmlal.s16  q12, d14, %f[ker0][0]        \n"
          "vmlal.s16  q12, d16, %f[ker0][1]        \n"
          "vmlal.s16  q12, d18, %f[ker0][2]        \n"
          "vmlal.s16  q13, d15, %f[ker0][0]        \n"
          "vmlal.s16  q13, d17, %f[ker0][1]        \n"
          "vmlal.s16  q13, d19, %f[ker0][2]        \n"
H
hjchen2 已提交
685 686

          "vld1.32    {d9}, [%[input_ptr3]], r0    \n"
H
hjchen2 已提交
687 688 689 690 691 692 693 694 695 696 697
          "vext.s8    d12, d9, d9, #1              \n"
          "vext.s8    d13, d9, d9, #2              \n"
          "vmovl.s8   q7, d9                       \n"
          "vmovl.s8   q8, d12                      \n"
          "vmovl.s8   q9, d13                      \n"
          "vmlal.s16  q12, d14, %e[ker1][0]        \n"
          "vmlal.s16  q12, d16, %e[ker1][1]        \n"
          "vmlal.s16  q12, d18, %e[ker1][2]        \n"
          "vmlal.s16  q13, d15, %e[ker1][0]        \n"
          "vmlal.s16  q13, d17, %e[ker1][1]        \n"
          "vmlal.s16  q13, d19, %e[ker1][2]        \n"
H
hjchen2 已提交
698 699 700
          // store row 1
          "vst1.32    {d24-d26}, [%[output_ptr1]]! \n"

H
hjchen2 已提交
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
          "subs       %[loop], #1                  \n"
          "bne        loop_2h6w_%=                 \n"

          "start_remain_%=:                        \n"
          "cmp        %[remain], #0                \n"
          "ble        end_%=                       \n"

          "mov        r0, %[remain]                \n"
          "vld1.32    {d9}, [%[input_ptr0]], r0    \n"
          "vld1.32    {d10}, [%[input_ptr1]], r0   \n"
          "vld1.32    {d11}, [%[input_ptr2]], r0   \n"
          "vext.s8    d12, d9, d9, #1              \n"
          "vext.s8    d13, d9, d9, #2              \n"
          "vmovl.s8   q7, d9                       \n"
          "vmovl.s8   q8, d12                      \n"
          "vmovl.s8   q9, d13                      \n"
          "vmull.s16  q10, d14, %e[ker0][0]        \n"
          "vmlal.s16  q10, d16, %e[ker0][1]        \n"
          "vmlal.s16  q10, d18, %e[ker0][2]        \n"
          "vmull.s16  q11, d15, %e[ker0][0]        \n"
          "vmlal.s16  q11, d17, %e[ker0][1]        \n"
          "vmlal.s16  q11, d19, %e[ker0][2]        \n"

          "vext.s8    d12, d10, d10, #1            \n"
          "vext.s8    d13, d10, d10, #2            \n"
          "vmovl.s8   q7, d10                      \n"
          "vmovl.s8   q8, d12                      \n"
          "vmovl.s8   q9, d13                      \n"
          "vmlal.s16  q10, d14, %f[ker0][0]        \n"
          "vmlal.s16  q10, d16, %f[ker0][1]        \n"
          "vmlal.s16  q10, d18, %f[ker0][2]        \n"
          "vmlal.s16  q11, d15, %f[ker0][0]        \n"
          "vmlal.s16  q11, d17, %f[ker0][1]        \n"
          "vmlal.s16  q11, d19, %f[ker0][2]        \n"

          "vmull.s16  q12, d14, %e[ker0][0]        \n"
          "vmlal.s16  q12, d16, %e[ker0][1]        \n"
          "vmlal.s16  q12, d18, %e[ker0][2]        \n"
          "vmull.s16  q13, d15, %e[ker0][0]        \n"
          "vmlal.s16  q13, d17, %e[ker0][1]        \n"
          "vmlal.s16  q13, d19, %e[ker0][2]        \n"

          "vext.s8    d12, d11, d11, #1            \n"
          "vext.s8    d13, d11, d11, #2            \n"
          "vmovl.s8   q7, d11                      \n"
          "vmovl.s8   q8, d12                      \n"
          "vmovl.s8   q9, d13                      \n"
          "vmlal.s16  q10, d14, %e[ker1][0]        \n"
          "vmlal.s16  q10, d16, %e[ker1][1]        \n"
          "vmlal.s16  q10, d18, %e[ker1][2]        \n"
          "vmlal.s16  q11, d15, %e[ker1][0]        \n"
          "vmlal.s16  q11, d17, %e[ker1][1]        \n"
          "vmlal.s16  q11, d19, %e[ker1][2]        \n"

          "vmlal.s16  q12, d14, %f[ker0][0]        \n"
          "vmlal.s16  q12, d16, %f[ker0][1]        \n"
          "vmlal.s16  q12, d18, %f[ker0][2]        \n"
          "vmlal.s16  q13, d15, %f[ker0][0]        \n"
          "vmlal.s16  q13, d17, %f[ker0][1]        \n"
          "vmlal.s16  q13, d19, %f[ker0][2]        \n"

          "vld1.32    {d9}, [%[input_ptr3]], r0    \n"
          "vext.s8    d12, d9, d9, #1              \n"
          "vext.s8    d13, d9, d9, #2              \n"
          "vmovl.s8   q7, d9                       \n"
          "vmovl.s8   q8, d12                      \n"
          "vmovl.s8   q9, d13                      \n"
          "vmlal.s16  q12, d14, %e[ker1][0]        \n"
          "vmlal.s16  q12, d16, %e[ker1][1]        \n"
          "vmlal.s16  q12, d18, %e[ker1][2]        \n"
          "vmlal.s16  q13, d15, %e[ker1][0]        \n"
          "vmlal.s16  q13, d17, %e[ker1][1]        \n"
          "vmlal.s16  q13, d19, %e[ker1][2]        \n"

          "cmp        %[remain], #4                \n"
          "blt        store_2h2w_%=                \n"
          "vst1.32    {q10}, [%[output_ptr0]]!     \n"
          "vst1.32    {q12}, [%[output_ptr1]]!     \n"
          "cmp        %[remain], #5                \n"
          "blt        end_%=                       \n"
          "vst1.32    {d22[0]}, [%[output_ptr0]]!  \n"
          "vst1.32    {d26[0]}, [%[output_ptr1]]!  \n"
          "b          end_%=                       \n"

          "store_2h2w_%=:                          \n"
          "cmp        %[remain], #2                \n"
          "blt        store_2h1w_%=                \n"
          "vst1.32    {d20}, [%[output_ptr0]]!     \n"
          "vst1.32    {d24}, [%[output_ptr1]]!     \n"
          "cmp        %[remain], #3                \n"
          "blt        end_%=                       \n"
          "vst1.32    {d21[0]}, [%[output_ptr0]]!  \n"
          "vst1.32    {d25[0]}, [%[output_ptr1]]!  \n"
          "b          end_%=                       \n"

          "store_2h1w_%=:                          \n"
          "cmp        %[remain], #1                \n"
          "blt        end_%=                       \n"
          "vst1.32    {d20[0]}, [%[output_ptr0]]!  \n"
          "vst1.32    {d24[0]}, [%[output_ptr1]]!  \n"
          "end_%=:                                 \n"
H
hjchen2 已提交
802 803
          : [output_ptr0] "+r"(output_ptr0), [output_ptr1] "+r"(output_ptr1),
            [input_ptr0] "+r"(input_ptr0), [input_ptr1] "+r"(input_ptr1),
804 805
            [input_ptr2] "+r"(input_ptr2), [input_ptr3] "+r"(input_ptr3),
            [loop] "+r"(loop)
H
hjchen2 已提交
806 807 808
          : [remain] "r"(output_w_remain), [ker0] "w"(_ker0), [ker1] "w"(_ker1)
          : "cc", "memory", "q4", "q5", "q6", "q7", "q8", "q9", "q10", "q11",
            "q12", "q13", "q14", "q15", "r0");
H
update  
hjchen2 已提交
809
#endif  // __aarch64__
H
hjchen2 已提交
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
      // pad right
      if (padding_w) {
        int16x4_t row0 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr0 - 2)));
        int16x4_t row1 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr1 - 2)));
        int16x4_t row2 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr2 - 2)));
        int16x4_t row3 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr3 - 2)));
        row0 = vext_s16(row0, zero, 2);
        row1 = vext_s16(row1, zero, 2);
        row2 = vext_s16(row2, zero, 2);
        row3 = vext_s16(row3, zero, 2);
        int32x4_t acc;
        for (int w = valid_w_end; w < output_w; ++w) {
          int padding = w + 3 - (padding_w + input_w);
          if (padding >= 3) {
            *output_ptr0 = 0;
            *output_ptr1 = 0;
          } else {
            acc = vmull_s16(row0, _ker[0]);
            acc = vmlal_s16(acc, row1, _ker[1]);
            acc = vmlal_s16(acc, row2, _ker[2]);
            *output_ptr0 = vgetq_lane_s32(acc, 0) + vgetq_lane_s32(acc, 1);
            acc = vmull_s16(row1, _ker[0]);
            acc = vmlal_s16(acc, row2, _ker[1]);
            acc = vmlal_s16(acc, row3, _ker[2]);
            *output_ptr1 = vgetq_lane_s32(acc, 0) + vgetq_lane_s32(acc, 1);

            row0 = vext_s16(row0, zero, 1);
            row1 = vext_s16(row1, zero, 1);
            row2 = vext_s16(row2, zero, 1);
            row3 = vext_s16(row3, zero, 1);
          }
          output_ptr0++;
          output_ptr1++;
        }
      }
H
hjchen2 已提交
845 846
    }

H
hjchen2 已提交
847 848 849 850 851
    start_h = valid_h_start + (valid_h & 0xFFFE);
    if (start_h < valid_h_end) {
      const int8_t *input_ptr0 = input_ptr + (start_h - padding_h) * input_w;
      const int8_t *input_ptr1 = input_ptr0 + input_w;
      const int8_t *input_ptr2 = input_ptr1 + input_w;
H
hjchen2 已提交
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
      int32_t *output_ptr0 = output_ptr + start_h * output_w;
      // pad left
      if (padding_w) {
        int16x4_t row0 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr0)));
        int16x4_t row1 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr1)));
        int16x4_t row2 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr2)));
        int32x4_t acc;
        for (int w = valid_w_start - 1; w >= 0; --w) {
          int padding = padding_w - w;
          if (padding >= 3) {
            output_ptr0[w] = 0;
          } else {
            row0 = vext_s16(zero, row0, 3);
            row1 = vext_s16(zero, row1, 3);
            row2 = vext_s16(zero, row2, 3);
            acc = vmull_s16(row0, _ker[0]);
            acc = vmlal_s16(acc, row1, _ker[1]);
            acc = vmlal_s16(acc, row2, _ker[2]);
            output_ptr0[w] = vgetq_lane_s32(acc, 1) + vgetq_lane_s32(acc, 2);
          }
        }
        output_ptr0 += valid_w_start;
      }
H
update  
hjchen2 已提交
875 876 877
        // valid
#if __aarch64__
#else
H
hjchen2 已提交
878
      int loop = output_w_tiles;
H
hjchen2 已提交
879
      asm volatile(
H
hjchen2 已提交
880 881 882
          "cmp        %[loop], #0                  \n"
          "ble        start_remain_%=              \n"
          "mov        r0, #6                       \n"
H
hjchen2 已提交
883
          // loop 6 widths
H
hjchen2 已提交
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
          "loop_1h6w_%=:                           \n"
          "vld1.32    {d9}, [%[input_ptr0]], r0    \n"
          "vld1.32    {d10}, [%[input_ptr1]], r0   \n"
          "vld1.32    {d11}, [%[input_ptr2]], r0   \n"
          "vext.s8    d12, d9, d9, #1              \n"
          "vext.s8    d13, d9, d9, #2              \n"
          "vmovl.s8   q7, d9                       \n"
          "vmovl.s8   q8, d12                      \n"
          "vmovl.s8   q9, d13                      \n"
          "vmull.s16  q10, d14, %e[ker0][0]        \n"
          "vmlal.s16  q10, d16, %e[ker0][1]        \n"
          "vmlal.s16  q10, d18, %e[ker0][2]        \n"
          "vmull.s16  q11, d15, %e[ker0][0]        \n"
          "vmlal.s16  q11, d17, %e[ker0][1]        \n"
          "vmlal.s16  q11, d19, %e[ker0][2]        \n"

          "vext.s8    d12, d10, d10, #1            \n"
          "vext.s8    d13, d10, d10, #2            \n"
          "vmovl.s8   q7, d10                      \n"
          "vmovl.s8   q8, d12                      \n"
          "vmovl.s8   q9, d13                      \n"
          "vmlal.s16  q10, d14, %f[ker0][0]        \n"
          "vmlal.s16  q10, d16, %f[ker0][1]        \n"
          "vmlal.s16  q10, d18, %f[ker0][2]        \n"
          "vmlal.s16  q11, d15, %f[ker0][0]        \n"
          "vmlal.s16  q11, d17, %f[ker0][1]        \n"
          "vmlal.s16  q11, d19, %f[ker0][2]        \n"

          "vext.s8    d12, d11, d11, #1            \n"
          "vext.s8    d13, d11, d11, #2            \n"
          "vmovl.s8   q7, d11                      \n"
          "vmovl.s8   q8, d12                      \n"
          "vmovl.s8   q9, d13                      \n"
          "vmlal.s16  q10, d14, %e[ker1][0]        \n"
          "vmlal.s16  q10, d16, %e[ker1][1]        \n"
          "vmlal.s16  q10, d18, %e[ker1][2]        \n"
          "vmlal.s16  q11, d15, %e[ker1][0]        \n"
          "vmlal.s16  q11, d17, %e[ker1][1]        \n"
          "vmlal.s16  q11, d19, %e[ker1][2]        \n"
H
hjchen2 已提交
923 924 925
          // store row 0, reuse q10/q11
          "vst1.32    {d20-d22}, [%[output_ptr0]]! \n"

H
hjchen2 已提交
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
          "subs       %[loop], #1                  \n"
          "bne        loop_1h6w_%=                 \n"

          "start_remain_%=:                        \n"
          "cmp        %[remain], #0                \n"
          "ble        end_%=                       \n"
          "mov        r0, %[remain]                \n"

          "vld1.32    {d9}, [%[input_ptr0]], r0    \n"
          "vld1.32    {d10}, [%[input_ptr1]], r0   \n"
          "vld1.32    {d11}, [%[input_ptr2]], r0   \n"
          "vext.s8    d12, d9, d9, #1              \n"
          "vext.s8    d13, d9, d9, #2              \n"
          "vmovl.s8   q7, d9                       \n"
          "vmovl.s8   q8, d12                      \n"
          "vmovl.s8   q9, d13                      \n"
          "vmull.s16  q10, d14, %e[ker0][0]        \n"
          "vmlal.s16  q10, d16, %e[ker0][1]        \n"
          "vmlal.s16  q10, d18, %e[ker0][2]        \n"
          "vmull.s16  q11, d15, %e[ker0][0]        \n"
          "vmlal.s16  q11, d17, %e[ker0][1]        \n"
          "vmlal.s16  q11, d19, %e[ker0][2]        \n"

          "vext.s8    d12, d10, d10, #1            \n"
          "vext.s8    d13, d10, d10, #2            \n"
          "vmovl.s8   q7, d10                      \n"
          "vmovl.s8   q8, d12                      \n"
          "vmovl.s8   q9, d13                      \n"
          "vmlal.s16  q10, d14, %f[ker0][0]        \n"
          "vmlal.s16  q10, d16, %f[ker0][1]        \n"
          "vmlal.s16  q10, d18, %f[ker0][2]        \n"
          "vmlal.s16  q11, d15, %f[ker0][0]        \n"
          "vmlal.s16  q11, d17, %f[ker0][1]        \n"
          "vmlal.s16  q11, d19, %f[ker0][2]        \n"

          "vext.s8    d12, d11, d11, #1            \n"
          "vext.s8    d13, d11, d11, #2            \n"
          "vmovl.s8   q7, d11                      \n"
          "vmovl.s8   q8, d12                      \n"
          "vmovl.s8   q9, d13                      \n"
          "vmlal.s16  q10, d14, %e[ker1][0]        \n"
          "vmlal.s16  q10, d16, %e[ker1][1]        \n"
          "vmlal.s16  q10, d18, %e[ker1][2]        \n"
          "vmlal.s16  q11, d15, %e[ker1][0]        \n"
          "vmlal.s16  q11, d17, %e[ker1][1]        \n"
          "vmlal.s16  q11, d19, %e[ker1][2]        \n"

          "cmp        %[remain], #4                \n"
          "blt        store_1h2w_%=                \n"
          "vst1.32    {q10}, [%[output_ptr0]]!     \n"
          "cmp        %[remain], #5                \n"
          "blt        end_%=                       \n"
          "vst1.32    {d22[0]}, [%[output_ptr0]]!  \n"
          "b          end_%=                       \n"

          "store_1h2w_%=:                          \n"
          "cmp        %[remain], #2                \n"
          "blt        store_1h1w_%=                \n"
          "vst1.32    {d20}, [%[output_ptr0]]!     \n"
          "cmp        %[remain], #3                \n"
          "blt        end_%=                       \n"
          "vst1.32    {d21[0]}, [%[output_ptr0]]!  \n"
          "b          end_%=                       \n"

          "store_1h1w_%=:                          \n"
          "cmp        %[remain], #1                \n"
          "blt        end_%=                       \n"
          "vst1.32    {d20[0]}, [%[output_ptr0]]!  \n"
          "end_%=:                                 \n"
H
hjchen2 已提交
995
          : [output_ptr0] "+r"(output_ptr0), [input_ptr0] "+r"(input_ptr0),
996 997
            [input_ptr1] "+r"(input_ptr1), [input_ptr2] "+r"(input_ptr2),
            [loop] "+r"(loop)
H
hjchen2 已提交
998 999 1000
          : [remain] "r"(output_w_remain), [ker0] "w"(_ker0), [ker1] "w"(_ker1)
          : "cc", "memory", "q4", "q5", "q6", "q7", "q8", "q9", "q10", "q11",
            "q12", "q13", "q14", "q15", "r0");
H
update  
hjchen2 已提交
1001
#endif  // __aarch64__
H
hjchen2 已提交
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
      // pad right
      if (padding_w) {
        int16x4_t row0 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr0 - 2)));
        int16x4_t row1 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr1 - 2)));
        int16x4_t row2 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr2 - 2)));
        row0 = vext_s16(row0, zero, 2);
        row1 = vext_s16(row1, zero, 2);
        row2 = vext_s16(row2, zero, 2);
        int32x4_t acc;
        for (int w = valid_w_end; w < output_w; ++w) {
          int padding = w + 3 - (padding_w + input_w);
          if (padding >= 3) {
            *output_ptr0 = 0;
          } else {
            acc = vmull_s16(row0, _ker[0]);
            acc = vmlal_s16(acc, row1, _ker[1]);
            acc = vmlal_s16(acc, row2, _ker[2]);
            *output_ptr0 = vgetq_lane_s32(acc, 0) + vgetq_lane_s32(acc, 1);

            row0 = vext_s16(row0, zero, 1);
            row1 = vext_s16(row1, zero, 1);
            row2 = vext_s16(row2, zero, 1);
          }
          output_ptr0++;
        }
      }
    }
    // bottom
    for (int h = valid_h_end; h < output_h; ++h) {
      DepthwiseConv3x3NormalRow<1, 1>(input_ptr, filter_ptr, h, input_h,
                                      input_w, padding_h, padding_w, output_w,
                                      output_ptr, _ker);
H
hjchen2 已提交
1034 1035 1036 1037 1038
    }
  }
}

template <>
H
hjchen2 已提交
1039
void DepthwiseConv3x3S2<int8_t, int32_t>(const framework::Tensor &input,
H
hjchen2 已提交
1040
                                         const framework::Tensor &filter,
1041
                                         const std::vector<int> &paddings,
H
hjchen2 已提交
1042 1043 1044 1045 1046 1047 1048 1049
                                         framework::Tensor *output) {
  const int8_t *input_data = input.data<int8_t>();
  const int8_t *filter_data = filter.data<int8_t>();
  int32_t *out_data = output->mutable_data<int32_t>();
  int input_h = input.dims()[2];
  int input_w = input.dims()[3];
  int output_h = output->dims()[2];
  int output_w = output->dims()[3];
H
hjchen2 已提交
1050 1051
  int padding_h = paddings[0];
  int padding_w = paddings[1];
H
hjchen2 已提交
1052 1053
  int image_size = input_h * input_w;
  int out_image_size = output_h * output_w;
H
hjchen2 已提交
1054
  int valid_h_start = (padding_h + 1) / 2;
H
hjchen2 已提交
1055
  int valid_h_end = (input_h + padding_h - 1) / 2;
H
hjchen2 已提交
1056 1057
  int valid_h = valid_h_end - valid_h_start;
  int valid_w_start = (padding_w + 1) / 2;
H
hjchen2 已提交
1058
  int valid_w_end = (input_w + padding_w - 1) / 2;
H
hjchen2 已提交
1059
  int valid_w = valid_w_end - valid_w_start;
H
hjchen2 已提交
1060 1061
  // for pad left
  int valid_input_w_start = (valid_w_start << 1) - padding_w;
H
hjchen2 已提交
1062 1063 1064 1065 1066 1067

  //  DLOG << "valid_h_start: " << valid_h_start;
  //  DLOG << "valid_h_end: " << valid_h_end;
  //  DLOG << "valid_w_start: " << valid_w_start;
  //  DLOG << "valid_w_end: " << valid_w_end;

H
hjchen2 已提交
1068
  #pragma omp parallel for
H
hjchen2 已提交
1069 1070 1071 1072
  for (int g = 0; g < input.dims()[1]; ++g) {
    const int8_t *input_ptr = input_data + g * image_size;
    const int8_t *filter_ptr = filter_data + g * 9;
    int32_t *output_ptr = out_data + g * out_image_size;
H
hjchen2 已提交
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083

    const int8_t *filter_ptr0 = filter_ptr;
    const int8_t *filter_ptr1 = filter_ptr0 + 3;
    const int8_t *filter_ptr2 = filter_ptr1 + 3;
    int16x4_t _k0 = vget_low_s16(vmovl_s8(vld1_s8(filter_ptr0)));
    int16x4_t _k1 = vget_low_s16(vmovl_s8(vld1_s8(filter_ptr1)));
    int16x4_t _k2 = vget_low_s16(vmovl_s8(vld1_s8(filter_ptr2)));
    int16x8_t _ker0 = vcombine_s16(_k0, _k1);
    int16x8_t _ker1 = vcombine_s16(_k2, _k2);
    int16x4_t _ker[3] = {_k0, _k1, _k2};

H
hjchen2 已提交
1084 1085 1086 1087
    // top
    for (int h = 0; h < valid_h_start; ++h) {
      DepthwiseConv3x3NormalRow<2, 2>(input_ptr, filter_ptr, h, input_h,
                                      input_w, padding_h, padding_w, output_w,
H
hjchen2 已提交
1088
                                      output_ptr, _ker);
H
hjchen2 已提交
1089 1090 1091 1092 1093 1094
    }
    // valid
    int input_w_start = 2 * valid_w_start - padding_w;
    int output_w_tiles = valid_w / 6;
    int output_w_remain = valid_w - output_w_tiles * 6;
    for (int h = valid_h_start; h < valid_h_end - 2; h += 3) {
H
hjchen2 已提交
1095
      const int8_t *input_ptr0 = input_ptr + (2 * h - padding_h) * input_w;
H
hjchen2 已提交
1096 1097 1098 1099 1100 1101
      const int8_t *input_ptr1 = input_ptr0 + input_w;
      const int8_t *input_ptr2 = input_ptr1 + input_w;
      const int8_t *input_ptr3 = input_ptr2 + input_w;
      const int8_t *input_ptr4 = input_ptr3 + input_w;
      const int8_t *input_ptr5 = input_ptr4 + input_w;
      const int8_t *input_ptr6 = input_ptr5 + input_w;
H
hjchen2 已提交
1102
      int32_t *output_ptr0 = output_ptr + h * output_w;
H
hjchen2 已提交
1103 1104
      int32_t *output_ptr1 = output_ptr0 + output_w;
      int32_t *output_ptr2 = output_ptr1 + output_w;
H
hjchen2 已提交
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
      // pad left
      if (padding_w) {
        for (int w = valid_w_start - 1; w >= 0; --w) {
          int padding = padding_w - (w << 1);
          if (padding >= 3) {
            output_ptr0[w] = 0;
            output_ptr1[w] = 0;
            output_ptr2[w] = 0;
          } else {
            int16x4_t row0 =
                vget_low_s16(vmovl_s8(vld1_s8(input_ptr0 - padding)));
            int16x4_t row1 =
                vget_low_s16(vmovl_s8(vld1_s8(input_ptr1 - padding)));
            int16x4_t row2 =
                vget_low_s16(vmovl_s8(vld1_s8(input_ptr2 - padding)));
            int16x4_t row3 =
                vget_low_s16(vmovl_s8(vld1_s8(input_ptr3 - padding)));
            int16x4_t row4 =
                vget_low_s16(vmovl_s8(vld1_s8(input_ptr4 - padding)));
            int16x4_t row5 =
                vget_low_s16(vmovl_s8(vld1_s8(input_ptr5 - padding)));
            int16x4_t row6 =
                vget_low_s16(vmovl_s8(vld1_s8(input_ptr6 - padding)));
            int32x4_t acc0 = vmull_s16(row0, _ker[0]);
            acc0 = vmlal_s16(acc0, row1, _ker[1]);
            acc0 = vmlal_s16(acc0, row2, _ker[2]);
            int32x4_t acc1 = vmull_s16(row2, _ker[0]);
            acc1 = vmlal_s16(acc1, row3, _ker[1]);
            acc1 = vmlal_s16(acc1, row4, _ker[2]);
            int32x4_t acc2 = vmull_s16(row4, _ker[0]);
            acc2 = vmlal_s16(acc2, row5, _ker[1]);
            acc2 = vmlal_s16(acc2, row6, _ker[2]);
            int32_t sum0 = vgetq_lane_s32(acc0, 2);
            int32_t sum1 = vgetq_lane_s32(acc1, 2);
            int32_t sum2 = vgetq_lane_s32(acc2, 2);
            if (padding == 1) {
              sum0 += vgetq_lane_s32(acc0, 1);
              sum1 += vgetq_lane_s32(acc1, 1);
              sum2 += vgetq_lane_s32(acc2, 1);
            }
            output_ptr0[w] = sum0;
            output_ptr1[w] = sum1;
            output_ptr2[w] = sum2;
          }
        }
        input_ptr0 += valid_input_w_start;
        input_ptr1 += valid_input_w_start;
        input_ptr2 += valid_input_w_start;
        input_ptr3 += valid_input_w_start;
        input_ptr4 += valid_input_w_start;
        input_ptr5 += valid_input_w_start;
        input_ptr6 += valid_input_w_start;
        output_ptr0 += valid_w_start;
        output_ptr1 += valid_w_start;
        output_ptr2 += valid_w_start;
      }
H
update  
hjchen2 已提交
1161 1162 1163
        // valid
#if __aarch64__
#else
H
hjchen2 已提交
1164
      int loop = output_w_tiles;
H
hjchen2 已提交
1165
      asm volatile(
H
hjchen2 已提交
1166 1167 1168
          "cmp        %[loop], #0                     \n"
          "ble        start_remain_%=                 \n"
          "mov        r0, #12                         \n"
H
hjchen2 已提交
1169
          // loop 6 widths
H
hjchen2 已提交
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
          "loop_3h6w_%=:                              \n"
          "vld2.8     {d10-d11}, [%[input_ptr0]], r0  \n"
          "vld2.8     {d12-d13}, [%[input_ptr1]], r0  \n"
          "vld2.8     {d14-d15}, [%[input_ptr2]], r0  \n"
          "vext.s8    d9, d10, d10, #1                \n"
          "vmovl.s8   q10, d9                         \n"
          "vmovl.s8   q8, d10                         \n"
          "vmovl.s8   q9, d11                         \n"
          "vmull.s16  q11, d16, %e[ker0][0]           \n"
          "vmlal.s16  q11, d18, %e[ker0][1]           \n"
          "vmlal.s16  q11, d20, %e[ker0][2]           \n"
          "vmull.s16  q12, d17, %e[ker0][0]           \n"
          "vmlal.s16  q12, d19, %e[ker0][1]           \n"
          "vmlal.s16  q12, d21, %e[ker0][2]           \n"

          "vext.s8    d9, d12, d12, #1                \n"
          "vmovl.s8   q10, d9                         \n"
          "vmovl.s8   q8, d12                         \n"
          "vmovl.s8   q9, d13                         \n"
          "vmlal.s16  q11, d16, %f[ker0][0]           \n"
          "vmlal.s16  q11, d18, %f[ker0][1]           \n"
          "vmlal.s16  q11, d20, %f[ker0][2]           \n"
          "vmlal.s16  q12, d17, %f[ker0][0]           \n"
          "vmlal.s16  q12, d19, %f[ker0][1]           \n"
          "vmlal.s16  q12, d21, %f[ker0][2]           \n"

          "vext.s8    d9, d14, d14, #1                \n"
          "vmovl.s8   q10, d9                         \n"
          "vmovl.s8   q8, d14                         \n"
          "vmovl.s8   q9, d15                         \n"
          "vmlal.s16  q11, d16, %e[ker1][0]           \n"
          "vmlal.s16  q11, d18, %e[ker1][1]           \n"
          "vmlal.s16  q11, d20, %e[ker1][2]           \n"
          "vmlal.s16  q12, d17, %e[ker1][0]           \n"
          "vmlal.s16  q12, d19, %e[ker1][1]           \n"
          "vmlal.s16  q12, d21, %e[ker1][2]           \n"
1206
          // store row 0, reuse q11/q12
H
hjchen2 已提交
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
          "vst1.32    {d22-d24}, [%[output_ptr0]]!    \n"

          "vmull.s16  q13, d16, %e[ker0][0]           \n"
          "vmlal.s16  q13, d18, %e[ker0][1]           \n"
          "vmlal.s16  q13, d20, %e[ker0][2]           \n"
          "vmull.s16  q14, d17, %e[ker0][0]           \n"
          "vmlal.s16  q14, d19, %e[ker0][1]           \n"
          "vmlal.s16  q14, d21, %e[ker0][2]           \n"

          "vld2.8     {d10-d11}, [%[input_ptr3]], r0  \n"
          "vld2.8     {d12-d13}, [%[input_ptr4]], r0  \n"
          "vld2.8     {d14-d15}, [%[input_ptr5]], r0  \n"
          "vext.s8    d9, d10, d10, #1                \n"
          "vmovl.s8   q10, d9                         \n"
          "vmovl.s8   q8, d10                         \n"
          "vmovl.s8   q9, d11                         \n"
          "vmlal.s16  q13, d16, %f[ker0][0]           \n"
          "vmlal.s16  q13, d18, %f[ker0][1]           \n"
          "vmlal.s16  q13, d20, %f[ker0][2]           \n"
          "vmlal.s16  q14, d17, %f[ker0][0]           \n"
          "vmlal.s16  q14, d19, %f[ker0][1]           \n"
          "vmlal.s16  q14, d21, %f[ker0][2]           \n"

          "vext.s8    d9, d12, d12, #1                \n"
          "vmovl.s8   q10, d9                         \n"
          "vmovl.s8   q8, d12                         \n"
          "vmovl.s8   q9, d13                         \n"
          "vmlal.s16  q13, d16, %e[ker1][0]           \n"
          "vmlal.s16  q13, d18, %e[ker1][1]           \n"
          "vmlal.s16  q13, d20, %e[ker1][2]           \n"
          "vmlal.s16  q14, d17, %e[ker1][0]           \n"
          "vmlal.s16  q14, d19, %e[ker1][1]           \n"
          "vmlal.s16  q14, d21, %e[ker1][2]           \n"
H
hjchen2 已提交
1240
          // store row 1
H
hjchen2 已提交
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
          "vst1.32    {d26-d28}, [%[output_ptr1]]!    \n"

          "vmull.s16  q11, d16, %e[ker0][0]           \n"
          "vmlal.s16  q11, d18, %e[ker0][1]           \n"
          "vmlal.s16  q11, d20, %e[ker0][2]           \n"
          "vmull.s16  q12, d17, %e[ker0][0]           \n"
          "vmlal.s16  q12, d19, %e[ker0][1]           \n"
          "vmlal.s16  q12, d21, %e[ker0][2]           \n"

          "vext.s8    d9, d14, d14, #1                \n"
          "vmovl.s8   q10, d9                         \n"
          "vmovl.s8   q8, d14                         \n"
          "vmovl.s8   q9, d15                         \n"
          "vmlal.s16  q11, d16, %f[ker0][0]           \n"
          "vmlal.s16  q11, d18, %f[ker0][1]           \n"
          "vmlal.s16  q11, d20, %f[ker0][2]           \n"
          "vmlal.s16  q12, d17, %f[ker0][0]           \n"
          "vmlal.s16  q12, d19, %f[ker0][1]           \n"
          "vmlal.s16  q12, d21, %f[ker0][2]           \n"

          "vld2.8     {d10-d11}, [%[input_ptr6]], r0  \n"
          "vext.s8    d9, d10, d10, #1                \n"
          "vmovl.s8   q10, d9                         \n"
          "vmovl.s8   q8, d10                         \n"
          "vmovl.s8   q9, d11                         \n"
          "vmlal.s16  q11, d16, %e[ker1][0]           \n"
          "vmlal.s16  q11, d18, %e[ker1][1]           \n"
          "vmlal.s16  q11, d20, %e[ker1][2]           \n"
          "vmlal.s16  q12, d17, %e[ker1][0]           \n"
          "vmlal.s16  q12, d19, %e[ker1][1]           \n"
          "vmlal.s16  q12, d21, %e[ker1][2]           \n"
1272
          // store row 2
H
hjchen2 已提交
1273 1274 1275 1276 1277 1278 1279 1280 1281 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 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 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 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412
          "vst1.32    {d22-d24}, [%[output_ptr2]]!    \n"

          "subs       %[loop], #1                     \n"
          "bne        loop_3h6w_%=                    \n"

          "start_remain_%=:                           \n"
          "cmp        %[remain], #0                   \n"
          "ble        end_%=                          \n"
          "mov        r0, %[remain], lsl #1           \n"

          "vld2.8     {d10-d11}, [%[input_ptr0]], r0  \n"
          "vld2.8     {d12-d13}, [%[input_ptr1]], r0  \n"
          "vext.s8    d9, d10, d10, #1                \n"
          "vmovl.s8   q9, d9                          \n"
          "vmovl.s8   q7, d10                         \n"
          "vmovl.s8   q8, d11                         \n"
          "vmull.s16  q10, d14, %e[ker0][0]           \n"
          "vmlal.s16  q10, d16, %e[ker0][1]           \n"
          "vmlal.s16  q10, d18, %e[ker0][2]           \n"
          "vmull.s16  q11, d15, %e[ker0][0]           \n"
          "vmlal.s16  q11, d17, %e[ker0][1]           \n"
          "vmlal.s16  q11, d19, %e[ker0][2]           \n"

          "vext.s8    d9, d12, d12, #1                \n"
          "vmovl.s8   q9, d9                          \n"
          "vmovl.s8   q7, d12                         \n"
          "vmovl.s8   q8, d13                         \n"
          "vmlal.s16  q10, d14, %f[ker0][0]           \n"
          "vmlal.s16  q10, d16, %f[ker0][1]           \n"
          "vmlal.s16  q10, d18, %f[ker0][2]           \n"
          "vmlal.s16  q11, d15, %f[ker0][0]           \n"
          "vmlal.s16  q11, d17, %f[ker0][1]           \n"
          "vmlal.s16  q11, d19, %f[ker0][2]           \n"

          "vld2.8     {d10-d11}, [%[input_ptr2]], r0  \n"
          "vld2.8     {d12-d13}, [%[input_ptr3]], r0  \n"
          "vext.s8    d9, d10, d10, #1                \n"
          "vmovl.s8   q9, d9                          \n"
          "vmovl.s8   q7, d10                         \n"
          "vmovl.s8   q8, d11                         \n"
          "vmlal.s16  q10, d14, %e[ker1][0]           \n"
          "vmlal.s16  q10, d16, %e[ker1][1]           \n"
          "vmlal.s16  q10, d18, %e[ker1][2]           \n"
          "vmlal.s16  q11, d15, %e[ker1][0]           \n"
          "vmlal.s16  q11, d17, %e[ker1][1]           \n"
          "vmlal.s16  q11, d19, %e[ker1][2]           \n"

          "vmull.s16  q12, d14, %e[ker0][0]           \n"
          "vmlal.s16  q12, d16, %e[ker0][1]           \n"
          "vmlal.s16  q12, d18, %e[ker0][2]           \n"
          "vmull.s16  q13, d15, %e[ker0][0]           \n"
          "vmlal.s16  q13, d17, %e[ker0][1]           \n"
          "vmlal.s16  q13, d19, %e[ker0][2]           \n"

          "vext.s8    d9, d12, d12, #1                \n"
          "vmovl.s8   q9, d9                          \n"
          "vmovl.s8   q7, d12                         \n"
          "vmovl.s8   q8, d13                         \n"
          "vmlal.s16  q12, d14, %f[ker0][0]           \n"
          "vmlal.s16  q12, d16, %f[ker0][1]           \n"
          "vmlal.s16  q12, d18, %f[ker0][2]           \n"
          "vmlal.s16  q13, d15, %f[ker0][0]           \n"
          "vmlal.s16  q13, d17, %f[ker0][1]           \n"
          "vmlal.s16  q13, d19, %f[ker0][2]           \n"

          "vld2.8     {d10-d11}, [%[input_ptr4]], r0  \n"
          "vld2.8     {d12-d13}, [%[input_ptr5]], r0  \n"
          "vext.s8    d9, d10, d10, #1                \n"
          "vmovl.s8   q9, d9                          \n"
          "vmovl.s8   q7, d10                         \n"
          "vmovl.s8   q8, d11                         \n"
          "vmlal.s16  q12, d14, %e[ker1][0]           \n"
          "vmlal.s16  q12, d16, %e[ker1][1]           \n"
          "vmlal.s16  q12, d18, %e[ker1][2]           \n"
          "vmlal.s16  q13, d15, %e[ker1][0]           \n"
          "vmlal.s16  q13, d17, %e[ker1][1]           \n"
          "vmlal.s16  q13, d19, %e[ker1][2]           \n"

          "vmull.s16  q14, d14, %e[ker0][0]           \n"
          "vmlal.s16  q14, d16, %e[ker0][1]           \n"
          "vmlal.s16  q14, d18, %e[ker0][2]           \n"
          "vmull.s16  q15, d15, %e[ker0][0]           \n"
          "vmlal.s16  q15, d17, %e[ker0][1]           \n"
          "vmlal.s16  q15, d19, %e[ker0][2]           \n"

          "vext.s8    d9, d12, d12, #1                \n"
          "vmovl.s8   q9, d9                          \n"
          "vmovl.s8   q7, d12                         \n"
          "vmovl.s8   q8, d13                         \n"
          "vmlal.s16  q14, d14, %f[ker0][0]           \n"
          "vmlal.s16  q14, d16, %f[ker0][1]           \n"
          "vmlal.s16  q14, d18, %f[ker0][2]           \n"
          "vmlal.s16  q15, d15, %f[ker0][0]           \n"
          "vmlal.s16  q15, d17, %f[ker0][1]           \n"
          "vmlal.s16  q15, d19, %f[ker0][2]           \n"

          "vld2.8     {d10-d11}, [%[input_ptr6]], r0  \n"
          "vext.s8    d9, d10, d10, #1                \n"
          "vmovl.s8   q9, d9                          \n"
          "vmovl.s8   q7, d10                         \n"
          "vmovl.s8   q8, d11                         \n"
          "vmlal.s16  q14, d14, %e[ker1][0]           \n"
          "vmlal.s16  q14, d16, %e[ker1][1]           \n"
          "vmlal.s16  q14, d18, %e[ker1][2]           \n"
          "vmlal.s16  q15, d15, %e[ker1][0]           \n"
          "vmlal.s16  q15, d17, %e[ker1][1]           \n"
          "vmlal.s16  q15, d19, %e[ker1][2]           \n"

          "cmp        %[remain], #4                   \n"
          "blt        store_3h2w_%=                   \n"
          "vst1.32    {q10}, [%[output_ptr0]]!        \n"
          "vst1.32    {q12}, [%[output_ptr1]]!        \n"
          "vst1.32    {q14}, [%[output_ptr2]]!        \n"
          "cmp        %[remain], #5                   \n"
          "blt        end_%=                          \n"
          "vst1.32    {d22[0]}, [%[output_ptr0]]!     \n"
          "vst1.32    {d26[0]}, [%[output_ptr1]]!     \n"
          "vst1.32    {d30[0]}, [%[output_ptr2]]!     \n"
          "b          end_%=                          \n"

          "store_3h2w_%=:                             \n"
          "cmp        %[remain], #2                   \n"
          "blt        store_3h1w_%=                   \n"
          "vst1.32    {d20}, [%[output_ptr0]]!        \n"
          "vst1.32    {d24}, [%[output_ptr1]]!        \n"
          "vst1.32    {d28}, [%[output_ptr2]]!        \n"
          "cmp        %[remain], #3                   \n"
          "blt        end_%=                          \n"
          "vst1.32    {d21[0]}, [%[output_ptr0]]!     \n"
          "vst1.32    {d25[0]}, [%[output_ptr1]]!     \n"
          "vst1.32    {d29[0]}, [%[output_ptr2]]!     \n"
          "b          end_%=                          \n"

          "store_3h1w_%=:                             \n"
          "cmp        %[remain], #1                   \n"
          "blt        end_%=                          \n"
          "vst1.32    {d20[0]}, [%[output_ptr0]]!     \n"
          "vst1.32    {d24[0]}, [%[output_ptr1]]!     \n"
          "vst1.32    {d28[0]}, [%[output_ptr2]]!     \n"
          "end_%=:                                    \n"
H
hjchen2 已提交
1413
          : [output_ptr0] "+r"(output_ptr0), [output_ptr1] "+r"(output_ptr1),
1414
            [output_ptr2] "+r"(output_ptr2), [input_ptr6] "+r"(input_ptr6),
H
hjchen2 已提交
1415 1416
            [input_ptr0] "+r"(input_ptr0), [input_ptr1] "+r"(input_ptr1),
            [input_ptr2] "+r"(input_ptr2), [input_ptr3] "+r"(input_ptr3),
1417 1418
            [input_ptr4] "+r"(input_ptr4), [input_ptr5] "+r"(input_ptr5),
            [loop] "+r"(loop)
H
hjchen2 已提交
1419 1420 1421
          : [remain] "r"(output_w_remain), [ker0] "w"(_ker0), [ker1] "w"(_ker1)
          : "cc", "memory", "q4", "q5", "q6", "q7", "q8", "q9", "q10", "q11",
            "q12", "q13", "q14", "q15", "r0");
H
update  
hjchen2 已提交
1422
#endif  // __aarch64__
H
hjchen2 已提交
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
      // pad right
      if (padding_w > 0) {
        int16x4_t row0 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr0)));
        int16x4_t row1 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr1)));
        int16x4_t row2 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr2)));
        int16x4_t row3 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr3)));
        int16x4_t row4 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr4)));
        int16x4_t row5 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr5)));
        int16x4_t row6 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr6)));
        int32x4_t acc0, acc1, acc2;
        for (int w = valid_w_end; w < output_w; ++w) {
          int padding = 2 * w + 3 - (padding_w + input_w);
          if (padding >= 3) {
            *output_ptr0 = 0;
            *output_ptr1 = 0;
            *output_ptr2 = 0;
          } else {
            acc0 = vmull_s16(row0, _ker[0]);
            acc0 = vmlal_s16(acc0, row1, _ker[1]);
            acc0 = vmlal_s16(acc0, row2, _ker[2]);
            acc1 = vmull_s16(row2, _ker[0]);
            acc1 = vmlal_s16(acc1, row3, _ker[1]);
            acc1 = vmlal_s16(acc1, row4, _ker[2]);
            acc2 = vmull_s16(row4, _ker[0]);
            acc2 = vmlal_s16(acc2, row5, _ker[1]);
            acc2 = vmlal_s16(acc2, row6, _ker[2]);
            int32_t sum0 = vgetq_lane_s32(acc0, 0);
            int32_t sum1 = vgetq_lane_s32(acc1, 0);
            int32_t sum2 = vgetq_lane_s32(acc2, 0);
            if (padding == 1) {
              sum0 += vgetq_lane_s32(acc0, 1);
              sum1 += vgetq_lane_s32(acc1, 1);
              sum2 += vgetq_lane_s32(acc2, 1);
            }
            *output_ptr0 = sum0;
            *output_ptr1 = sum1;
            *output_ptr2 = sum2;
          }
          output_ptr0++;
          output_ptr1++;
          output_ptr2++;
        }
      }
H
hjchen2 已提交
1466
    }
H
hjchen2 已提交
1467
    // remain height
H
hjchen2 已提交
1468 1469
    int start_h = valid_h_start + valid_h / 3 * 3;
    for (int h = start_h; h < valid_h_end; ++h) {
H
hjchen2 已提交
1470
      const int8_t *input_ptr0 = input_ptr + (2 * h - padding_h) * input_w;
H
hjchen2 已提交
1471 1472
      const int8_t *input_ptr1 = input_ptr0 + input_w;
      const int8_t *input_ptr2 = input_ptr1 + input_w;
H
hjchen2 已提交
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501
      int32_t *output_ptr0 = output_ptr + h * output_w;
      // pad left
      if (padding_w) {
        for (int w = valid_w_start - 1; w >= 0; --w) {
          int padding = padding_w - (w << 1);
          if (padding >= 3) {
            output_ptr0[w] = 0;
          } else {
            int16x4_t row0 =
                vget_low_s16(vmovl_s8(vld1_s8(input_ptr0 - padding)));
            int16x4_t row1 =
                vget_low_s16(vmovl_s8(vld1_s8(input_ptr1 - padding)));
            int16x4_t row2 =
                vget_low_s16(vmovl_s8(vld1_s8(input_ptr2 - padding)));
            int32x4_t acc = vmull_s16(row0, _ker[0]);
            acc = vmlal_s16(acc, row1, _ker[1]);
            acc = vmlal_s16(acc, row2, _ker[2]);
            int32_t sum0 = vgetq_lane_s32(acc, 2);
            if (padding == 1) {
              sum0 += vgetq_lane_s32(acc, 1);
            }
            output_ptr0[w] = sum0;
          }
        }
        input_ptr0 += valid_input_w_start;
        input_ptr1 += valid_input_w_start;
        input_ptr2 += valid_input_w_start;
        output_ptr0 += valid_w_start;
      }
H
update  
hjchen2 已提交
1502 1503 1504
        // valid
#if __aarch64__
#else
H
hjchen2 已提交
1505
      int loop = output_w_tiles;
H
hjchen2 已提交
1506
      asm volatile(
H
hjchen2 已提交
1507 1508 1509
          "cmp        %[loop], #0                      \n"
          "ble        start_remain_%=                  \n"
          "mov        r0, #12                          \n"
H
hjchen2 已提交
1510
          // loop 6 widths
1511 1512 1513 1514
          "loop_1h6w_%=:                               \n"
          "vld2.8     {d10, d11}, [%[input_ptr0]], r0  \n"
          "vld2.8     {d12, d13}, [%[input_ptr1]], r0  \n"
          "vld2.8     {d14, d15}, [%[input_ptr2]], r0  \n"
H
hjchen2 已提交
1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
          "vext.s8    d9, d10, d10, #1                 \n"
          "vmovl.s8   q10, d9                          \n"
          "vmovl.s8   q8, d10                          \n"
          "vmovl.s8   q9, d11                          \n"
          "vmull.s16  q11, d16, %e[ker0][0]            \n"
          "vmlal.s16  q11, d18, %e[ker0][1]            \n"
          "vmlal.s16  q11, d20, %e[ker0][2]            \n"
          "vmull.s16  q12, d17, %e[ker0][0]            \n"
          "vmlal.s16  q12, d19, %e[ker0][1]            \n"
          "vmlal.s16  q12, d21, %e[ker0][2]            \n"

          "vext.s8    d9, d12, d12, #1                 \n"
          "vmovl.s8   q10, d9                          \n"
          "vmovl.s8   q8, d12                          \n"
          "vmovl.s8   q9, d13                          \n"
          "vmlal.s16  q11, d16, %f[ker0][0]            \n"
          "vmlal.s16  q11, d18, %f[ker0][1]            \n"
          "vmlal.s16  q11, d20, %f[ker0][2]            \n"
          "vmlal.s16  q12, d17, %f[ker0][0]            \n"
          "vmlal.s16  q12, d19, %f[ker0][1]            \n"
          "vmlal.s16  q12, d21, %f[ker0][2]            \n"

          "vext.s8    d9, d14, d14, #1                 \n"
          "vmovl.s8   q10, d9                          \n"
          "vmovl.s8   q8, d14                          \n"
          "vmovl.s8   q9, d15                          \n"
          "vmlal.s16  q11, d16, %e[ker1][0]            \n"
          "vmlal.s16  q11, d18, %e[ker1][1]            \n"
          "vmlal.s16  q11, d20, %e[ker1][2]            \n"
          "vmlal.s16  q12, d17, %e[ker1][0]            \n"
          "vmlal.s16  q12, d19, %e[ker1][1]            \n"
          "vmlal.s16  q12, d21, %e[ker1][2]            \n"
1547
          // store row 0
H
hjchen2 已提交
1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 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 1613 1614 1615
          "vst1.32    {d22-d24}, [%[output_ptr0]]!     \n"

          "subs       %[loop], #1                      \n"
          "bne        loop_1h6w_%=                     \n"

          "start_remain_%=:                            \n"
          "cmp        %[remain], #0                    \n"
          "ble        end_%=                           \n"
          "mov        r0, %[remain], lsl #1            \n"

          "vld2.8     {d10, d11}, [%[input_ptr0]], r0  \n"
          "vld2.8     {d12, d13}, [%[input_ptr1]], r0  \n"
          "vld2.8     {d14, d15}, [%[input_ptr2]], r0  \n"
          "vext.s8    d9, d10, d10, #1                 \n"
          "vmovl.s8   q10, d9                          \n"
          "vmovl.s8   q8, d10                          \n"
          "vmovl.s8   q9, d11                          \n"
          "vmull.s16  q11, d16, %e[ker0][0]            \n"
          "vmlal.s16  q11, d18, %e[ker0][1]            \n"
          "vmlal.s16  q11, d20, %e[ker0][2]            \n"
          "vmull.s16  q12, d17, %e[ker0][0]            \n"
          "vmlal.s16  q12, d19, %e[ker0][1]            \n"
          "vmlal.s16  q12, d21, %e[ker0][2]            \n"

          "vext.s8    d9, d12, d12, #1                 \n"
          "vmovl.s8   q10, d9                          \n"
          "vmovl.s8   q8, d12                          \n"
          "vmovl.s8   q9, d13                          \n"
          "vmlal.s16  q11, d16, %f[ker0][0]            \n"
          "vmlal.s16  q11, d18, %f[ker0][1]            \n"
          "vmlal.s16  q11, d20, %f[ker0][2]            \n"
          "vmlal.s16  q12, d17, %f[ker0][0]            \n"
          "vmlal.s16  q12, d19, %f[ker0][1]            \n"
          "vmlal.s16  q12, d21, %f[ker0][2]            \n"

          "vext.s8    d9, d14, d14, #1                 \n"
          "vmovl.s8   q10, d9                          \n"
          "vmovl.s8   q8, d14                          \n"
          "vmovl.s8   q9, d15                          \n"
          "vmlal.s16  q11, d16, %e[ker1][0]            \n"
          "vmlal.s16  q11, d18, %e[ker1][1]            \n"
          "vmlal.s16  q11, d20, %e[ker1][2]            \n"
          "vmlal.s16  q12, d17, %e[ker1][0]            \n"
          "vmlal.s16  q12, d19, %e[ker1][1]            \n"
          "vmlal.s16  q12, d21, %e[ker1][2]            \n"

          "cmp        %[remain], #4                    \n"
          "blt        store_1h2w_%=                    \n"
          "vst1.32    {q11}, [%[output_ptr0]]!         \n"
          "cmp        %[remain], #5                    \n"
          "blt        end_%=                           \n"
          "vst1.32    {d24[0]}, [%[output_ptr0]]!      \n"
          "b          end_%=                           \n"

          "store_1h2w_%=:                              \n"
          "cmp        %[remain], #2                    \n"
          "blt        store_1h1w_%=                    \n"
          "vst1.32    {d22}, [%[output_ptr0]]!         \n"
          "cmp        %[remain], #3                    \n"
          "blt        end_%=                           \n"
          "vst1.32    {d23[0]}, [%[output_ptr0]]!      \n"
          "b          end_%=                           \n"

          "store_1h1w_%=:                              \n"
          "cmp        %[remain], #1                    \n"
          "blt        end_%=                           \n"
          "vst1.32    {d22[0]}, [%[output_ptr0]]!      \n"
          "end_%=:                                     \n"
H
hjchen2 已提交
1616
          : [output_ptr0] "+r"(output_ptr0), [input_ptr0] "+r"(input_ptr0),
1617 1618
            [input_ptr1] "+r"(input_ptr1), [input_ptr2] "+r"(input_ptr2),
            [loop] "+r"(loop)
H
hjchen2 已提交
1619 1620 1621
          : [remain] "r"(output_w_remain), [ker0] "w"(_ker0), [ker1] "w"(_ker1)
          : "cc", "memory", "q4", "q5", "q6", "q7", "q8", "q9", "q10", "q11",
            "q12", "q13", "q14", "q15", "r0");
H
update  
hjchen2 已提交
1622
#endif  // __aarch64__
H
hjchen2 已提交
1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651
      // pad right
      if (padding_w > 0) {
        int16x4_t row0 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr0)));
        int16x4_t row1 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr1)));
        int16x4_t row2 = vget_low_s16(vmovl_s8(vld1_s8(input_ptr2)));
        int32x4_t acc;
        for (int w = valid_w_end; w < output_w; ++w) {
          int padding = 2 * w + 3 - (padding_w + input_w);
          if (padding >= 3) {
            *output_ptr0 = 0;
          } else {
            acc = vmull_s16(row0, _ker[0]);
            acc = vmlal_s16(acc, row1, _ker[1]);
            acc = vmlal_s16(acc, row2, _ker[2]);
            int32_t sum0 = vgetq_lane_s32(acc, 0);
            if (padding == 1) {
              sum0 += vgetq_lane_s32(acc, 1);
            }
            *output_ptr0 = sum0;
          }
          output_ptr0++;
        }
      }
    }
    // bottom
    for (int h = valid_h_end; h < output_h; ++h) {
      DepthwiseConv3x3NormalRow<2, 2>(input_ptr, filter_ptr, h, input_h,
                                      input_w, padding_h, padding_w, output_w,
                                      output_ptr, _ker);
H
hjchen2 已提交
1652 1653 1654 1655 1656 1657 1658
    }
  }
}

}  // namespace math
}  // namespace operators
}  // namespace paddle_mobile
H
hjchen2 已提交
1659

H
update  
hjchen2 已提交
1660
#endif  // __ARM_NEON__