conv_block_utils.h 162.8 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// 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.

#pragma once
#include <arm_neon.h>
#include <cmath>
18
#include "lite/backends/arm/math/gemm_s8.h"
19
#include "lite/backends/arm/math/saturate.h"
20
#include "lite/backends/arm/math/sgemm.h"
21
#include "lite/backends/arm/math/type_trans.h"
Y
Yan Chunwei 已提交
22
#include "lite/core/target_wrapper.h"
23
#include "lite/operators/op_params.h"
Y
Yan Chunwei 已提交
24 25 26 27 28 29 30 31
#include "lite/utils/cp_logging.h"

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

#define LITEMAX(a, b) ((a) > (b) ? (a) : (b))
32
#define LITEMIN(a, b) ((a) < (b) ? (a) : (b))
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
#define ROUNDUP(a, b) ((((a) + (b)-1) / (b)) * (b))

template <PrecisionType Ptype>
inline void trans_gemm_weights(const Tensor& tin,
                               Tensor& tout,  // NOLINT
                               int group,
                               ARMContext* ctx);

template <>
inline void trans_gemm_weights<PRECISION(kFloat)>(const Tensor& tin,
                                                  Tensor& tout,  // NOLINT
                                                  int group,
                                                  ARMContext* ctx) {
  CHECK_EQ(tin.dims().size(), 4) << "conv weights dims size must = 4";
  int m = tin.dims()[0] / group;
  int k = tin.dims().count(1, 4);
  int hblock = lite::arm::math::get_hblock(ctx);
  int m_roundup = hblock * ((m + hblock - 1) / hblock);
  int group_size_round_up = ((m_roundup * k + 15) / 16) * 16;
  float* w_trans_ptr = nullptr;
  tout.Resize({group_size_round_up * group});
  w_trans_ptr = tout.mutable_data<float>();
  const auto* w_data = tin.data<float>();
  for (int g = 0; g < group; ++g) {
    const float* weights_group = w_data + g * m * k;
    float* weights_trans_ptr = w_trans_ptr + g * group_size_round_up;
    lite::arm::math::prepackA(
        weights_trans_ptr, weights_group, 1.f, k, 0, m, 0, k, false, ctx);
  }
}

template <>
inline void trans_gemm_weights<PRECISION(kInt8)>(const Tensor& tin,
                                                 Tensor& tout,  // NOLINT
                                                 int group,
                                                 ARMContext* ctx) {
  CHECK_EQ(tin.dims().size(), 4) << "conv weights dims size must = 4";
  int m = tin.dims()[0] / group;
  int k = tin.dims().count(1, 4);
  prepackA_int8(&tout, tin, m, k, group, false, ctx);
}
Y
Yan Chunwei 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 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 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

inline void fill_packed_biasc4(float* dout, const float* bias, int size) {
  float32x4_t vb = vld1q_f32(bias);
  int cnt = size / 4;
  for (int i = 0; i < cnt; ++i) {
    vst1q_f32(dout, vb);
    dout += 4;
  }
}

/*preprocessing weights
* input weights: [chout, chin/ group, kh, kw] --> outputs weights: [chout / n,
* chin/ group, kh, kw, n]
*/
template <typename dtype>
static bool conv_trans_weights_numc(const dtype* din,
                                    dtype* dout,
                                    int chout,
                                    int chin,
                                    int n,
                                    int kernel_size) {
  if (n <= 0) {
    LOG(ERROR) << "ch_n and hei_n are more than zero";
    return false;
  }
  int c_loop = chout / n;
  int chout_round = (chout + n - 1) / n;
  int win_stride = chin * kernel_size;
  int wout_stride = n * win_stride;
  int co = 0;
  for (; co < c_loop; ++co) {
    dtype* dout_c = dout + co * wout_stride;
    const dtype* din_array[n];
    din_array[0] = din + co * wout_stride;
    for (int i = 1; i < n; i++) {
      din_array[i] = din_array[i - 1] + win_stride;
    }
    for (int ci = 0; ci < chin; ++ci) {
      for (int k = 0; k < kernel_size; ++k) {
        for (int i = 0; i < n; i++) {
          *(dout_c++) = *(din_array[i]++);
        }
      }
    }
  }
  // pad final chout
  if (chout_round > c_loop) {
    dtype* dout_c = dout + c_loop * wout_stride;
    const dtype* din_array[n];
    din_array[0] = din + c_loop * wout_stride;
    for (int i = 1; i < n; i++) {
      din_array[i] = din_array[i - 1] + win_stride;
    }
    // deal remain
    int cremain = chout_round * n - chout;
    for (int i = 1; i <= cremain; i++) {
      din_array[n - i] = din_array[0];
    }
    for (int ci = 0; ci < chin; ++ci) {
      for (int k = 0; k < kernel_size; ++k) {
        for (int i = 0; i < n; i++) {
          *(dout_c++) = *(din_array[i]++);
        }
      }
    }
  }
  return true;
}
/*preprocessing inputs
* input din: [1, chin, he-hs, we - ws] --> outputs dout: [n, chin, 1, we - ws]
* n = he - hs
*/
template <typename dtype>
static bool prepack_input_nxw(const dtype* din,
                              dtype* dout,
                              int cs,
                              int ce,
                              int hs,
                              int he,
                              int ws,
                              int we,
                              int channel,
                              int width,
                              int height,
                              dtype* zero_ptr) {
  int n = he - hs;
  if (n <= 0) {
    LOG(ERROR) << "hei_n is more than zero";
    return false;
  }
  int w0 = ws < 0 ? 0 : ws;
  int w1 = we > width ? width : we;

  int size_w = we - ws;
  int size_wc_len = size_w * channel;
  int size_c = width * height;

  int valid_w = w1 - w0;
  size_t valid_w_byte = valid_w * sizeof(dtype);

  dtype* out_array[n];
  out_array[0] = dout;
  for (int i = 1; i < n; i++) {
    out_array[i] = out_array[i - 1] + size_wc_len;
  }

  for (int c = 0; c < channel; ++c) {
    int j = 0;
    // valid height
    for (int i = hs; i < he; i++) {
      // get address
      const dtype* in_array;
      if (i < 0 || i >= height) {
        in_array = zero_ptr;
      } else {
        in_array = din + i * width;
      }

      for (int w = ws; w < w0; ++w) {
        *(out_array[j]++) = 0.f;
      }
      memcpy(out_array[j], in_array, valid_w_byte);
      out_array[j] += valid_w;
      for (int w = w1; w < we; ++w) {
        *(out_array[j]++) = 0.f;
      }
      j++;
    }
    din += size_c;
  }
  return true;
}

207 208 209 210 211 212 213 214 215 216 217 218 219 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
inline void transpose_4x4(float32x4_t v0,
                          float32x4_t v1,
                          float32x4_t v2,
                          float32x4_t v3,
                          float* dout) {
#ifdef __aarch64__
  asm volatile(
      "trn1   v0.4s, %[v0].4s, %[v1].4s\n" /* trans q0, q1, a0b0a2b2*/
      "trn2   v1.4s, %[v0].4s, %[v1].4s\n" /* trans q0, q1, a1b1a3b3*/
      "trn1   v2.4s, %[v2].4s, %[v3].4s\n" /* trans q2, q3, c0d0c2d2*/
      "trn2   v3.4s, %[v2].4s, %[v3].4s\n" /* trans q2, q3, c1d1c3d3*/
      "trn1   v4.2d, v0.2d, v2.2d\n"       /* trans q0, q2, a0b0c0d0*/
      "trn2   v6.2d, v0.2d, v2.2d\n"       /* trans q0, q2, a2b2c2d2*/
      "trn1   v5.2d, v1.2d, v3.2d\n"       /* trans q1, q3, a1b1c1d1*/
      "trn2   v7.2d, v1.2d, v3.2d\n"       /* trans q1, q3, a3b3c3d3*/
      "stp  q4, q5, [%[dout]], #32\n"
      "stp  q6, q7, [%[dout]]\n"
      : [dout] "+r"(dout)
      : [v0] "w"(v0), [v1] "w"(v1), [v2] "w"(v2), [v3] "w"(v3)
      : "cc", "memory", "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7");
#else
  asm volatile(
      "vtrn.32 %q[v0], %q[v1]\n" /* trans q0, q1, a0b0a2b2, a1b1a3b3*/
      "vtrn.32 %q[v2], %q[v3]\n" /* trans q2, q3, c0d0c2d2, c1d1c3d3*/
      "vswp   %f[v0], %e[v2]\n"  /* trans q0, q2, a0b0c0d0, a2b2c2d2*/
      "vswp   %f[v1], %e[v3]\n"  /* trans q1, q3, a1b1c1d1, a3b3c3d3*/
      "vst1.32  {%e[v0], %f[v0]}, [%[dout]]!\n"
      "vst1.32  {%e[v1], %f[v1]}, [%[dout]]!\n"
      "vst1.32  {%e[v2], %f[v2]}, [%[dout]]!\n"
      "vst1.32  {%e[v3], %f[v3]}, [%[dout]]\n"
      : [dout] "+r"(dout)
      : [v0] "w"(v0), [v1] "w"(v1), [v2] "w"(v2), [v3] "w"(v3)
      :);
#endif
}

inline void prepack_input_nxwc4_dw(const float* din,
                                   float* dout,
                                   int cs,
                                   int hs,
                                   int he,
                                   int ws,
                                   int we,
                                   int channel,
                                   int width,
                                   int height,
                                   float* zero_ptr) {
  int n = he - hs;
  if (n <= 0) {
    LOG(FATAL) << "prepack_dw_input, valid height must > zero";
  }
  float32x4_t vzero = vdupq_n_f32(0.f);
T
TianXiaogang 已提交
259
  auto out_data = dout;
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274

  int size_w = we - ws;
  int w0 = ws < 0 ? 0 : ws;
  int w1 = we > width ? width : we;
  int valid_w = w1 - w0;

  int mask[4] = {0, 1, 2, 3};

  int pad_l = ws < 0 ? -ws : 0;
  int pad_r = we > width ? we - width : 0;
  int cnt_l = pad_l / 4;
  int left_remain = pad_l - cnt_l * 4;

  bool flag_ext_l = left_remain > 0;
  int left_sl = 4 - left_remain;
T
TianXiaogang 已提交
275
  int left_valid_sl = left_sl > width ? width : left_sl;
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
  uint32x4_t vmask_padl;
  bool flag_mask_l = false;
  if (flag_ext_l) {
    if (valid_w < 3) {
      flag_mask_l = true;
      vmask_padl = vcltq_s32(vld1q_s32(mask), vdupq_n_s32(valid_w));
    }
    valid_w -= left_sl;
    valid_w = valid_w > 0 ? valid_w : 0;
  }
  int cnt_valid = valid_w / 4;
  int valid_sl = valid_w - cnt_valid * 4;
  bool flag_mask_valid = valid_sl > 0;
  uint32x4_t vmask_valid;
  if (flag_mask_valid) {
    vmask_valid = vcltq_s32(vld1q_s32(mask), vdupq_n_s32(valid_sl));
    pad_r -= 4 - valid_sl;
    pad_r = pad_r > 0 ? pad_r : 0;
  }
  int size_c = width * height;
  for (int h = hs; h < he; ++h) {
T
TianXiaogang 已提交
297
    dout = out_data + (h - hs) * 4 * size_w;
298 299 300 301 302 303 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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
    auto ptr_c0 = din + cs * size_c + h * width;
    auto ptr_c1 = ptr_c0 + size_c;
    auto ptr_c2 = ptr_c1 + size_c;
    auto ptr_c3 = ptr_c2 + size_c;
    if (h < 0 || h >= height) {
      memset(dout, 0, sizeof(float) * size_w * 4);
      dout += size_w * 4;
      continue;
    } else if (cs + 4 > channel) {
      switch (cs + 4 - channel) {
        case 3:
          ptr_c1 = zero_ptr;
        case 2:
          ptr_c2 = zero_ptr;
        case 1:
          ptr_c3 = zero_ptr;
        default:
          break;
      }
    }
    /// left padding
    if (cnt_l > 0) {
      memset(dout, 0, sizeof(float) * 16 * cnt_l);
      dout += 16 * cnt_l;
    }
    /// left mask
    if (flag_ext_l) {
      float32x4_t vc0 = vld1q_f32(ptr_c0);
      float32x4_t vc1 = vld1q_f32(ptr_c1);
      float32x4_t vc2 = vld1q_f32(ptr_c2);
      float32x4_t vc3 = vld1q_f32(ptr_c3);
      if (flag_mask_l) {
        vc0 = vbslq_f32(vmask_padl, vc0, vzero);
        vc1 = vbslq_f32(vmask_padl, vc1, vzero);
        vc2 = vbslq_f32(vmask_padl, vc2, vzero);
        vc3 = vbslq_f32(vmask_padl, vc3, vzero);
      }
      switch (left_sl) {
        case 1:
          vc0 = vextq_f32(vzero, vc0, 1);
          vc1 = vextq_f32(vzero, vc1, 1);
          vc2 = vextq_f32(vzero, vc2, 1);
          vc3 = vextq_f32(vzero, vc3, 1);
          break;
        case 2:
          vc0 = vextq_f32(vzero, vc0, 2);
          vc1 = vextq_f32(vzero, vc1, 2);
          vc2 = vextq_f32(vzero, vc2, 2);
          vc3 = vextq_f32(vzero, vc3, 2);
          break;
        case 3:
          vc0 = vextq_f32(vzero, vc0, 3);
          vc1 = vextq_f32(vzero, vc1, 3);
          vc2 = vextq_f32(vzero, vc2, 3);
          vc3 = vextq_f32(vzero, vc3, 3);
          break;
        default:
          break;
      }
      transpose_4x4(vc0, vc1, vc2, vc3, dout);
      dout += 16;
T
TianXiaogang 已提交
359 360 361 362
      ptr_c0 += left_valid_sl;
      ptr_c1 += left_valid_sl;
      ptr_c2 += left_valid_sl;
      ptr_c3 += left_valid_sl;
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
    }
    /// valid
    for (int i = 0; i < cnt_valid; ++i) {
      float32x4_t vc0 = vld1q_f32(ptr_c0);
      float32x4_t vc1 = vld1q_f32(ptr_c1);
      float32x4_t vc2 = vld1q_f32(ptr_c2);
      float32x4_t vc3 = vld1q_f32(ptr_c3);
      transpose_4x4(vc0, vc1, vc2, vc3, dout);
      dout += 16;
      ptr_c0 += 4;
      ptr_c1 += 4;
      ptr_c2 += 4;
      ptr_c3 += 4;
    }
    if (flag_mask_valid) {
      float32x4_t vc0 = vld1q_f32(ptr_c0);
      float32x4_t vc1 = vld1q_f32(ptr_c1);
      float32x4_t vc2 = vld1q_f32(ptr_c2);
      float32x4_t vc3 = vld1q_f32(ptr_c3);
      vc0 = vbslq_f32(vmask_valid, vc0, vzero);
      vc1 = vbslq_f32(vmask_valid, vc1, vzero);
      vc2 = vbslq_f32(vmask_valid, vc2, vzero);
      vc3 = vbslq_f32(vmask_valid, vc3, vzero);
      transpose_4x4(vc0, vc1, vc2, vc3, dout);
      dout += 16;
    }
    /// right padding
    if (pad_r > 0) {
      memset(dout, 0, sizeof(float) * 4 * pad_r);
      dout += 4 * pad_r;
    }
  }
}

Y
yiicy 已提交
397 398 399 400 401 402 403 404 405 406
inline void prepack_input_nxwc8_int8_dw(const int8_t* din,
                                        int8_t* dout,
                                        int cs,
                                        int hs,
                                        int he,
                                        int ws,
                                        int we,
                                        int channel,
                                        int width,
                                        int height) {
407 408
  int n = he - hs;
  if (n <= 0) {
Y
yiicy 已提交
409
    LOG(FATAL) << "prepack_dw_input_int8, valid height must > zero";
410
  }
Y
yiicy 已提交
411
  int size_w = we - ws;
412 413 414
  int w0 = ws < 0 ? 0 : ws;
  int w1 = we > width ? width : we;
  int valid_w = w1 - w0;
Y
yiicy 已提交
415 416 417
  int pad_l = ws < 0 ? -ws : 0;
  int pad_r = we > width ? we - width : 0;
  int size_c = width * height;
418

Y
yiicy 已提交
419 420
  int valid_cnt = valid_w >> 3;
  int remain = valid_w & 7;
421 422 423 424

  int8_t zero_ptr[size_w * 2];  // NOLINT
  memset(zero_ptr, 0, size_w * 2);

Y
yiicy 已提交
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
  for (int h = hs; h < he; ++h) {
    const int8_t* ptr_c0 = din + h * width + cs * size_c;
    const int8_t* ptr_c1 = ptr_c0 + size_c;
    const int8_t* ptr_c2 = ptr_c1 + size_c;
    const int8_t* ptr_c3 = ptr_c2 + size_c;
    const int8_t* ptr_c4 = ptr_c3 + size_c;
    const int8_t* ptr_c5 = ptr_c4 + size_c;
    const int8_t* ptr_c6 = ptr_c5 + size_c;
    const int8_t* ptr_c7 = ptr_c6 + size_c;
    if (h < 0 || h >= height) {
      memset(dout, 0, 8 * size_w * sizeof(int8_t));
      dout += size_w * 8;
      continue;
    } else if (cs + 8 > channel) {
      switch (cs + 8 - channel) {
440
        case 7:
Y
yiicy 已提交
441
          ptr_c1 = zero_ptr;
442
        case 6:
Y
yiicy 已提交
443
          ptr_c2 = zero_ptr;
444
        case 5:
Y
yiicy 已提交
445
          ptr_c3 = zero_ptr;
446
        case 4:
Y
yiicy 已提交
447
          ptr_c4 = zero_ptr;
448
        case 3:
Y
yiicy 已提交
449
          ptr_c5 = zero_ptr;
450
        case 2:
Y
yiicy 已提交
451
          ptr_c6 = zero_ptr;
452
        case 1:
Y
yiicy 已提交
453
          ptr_c7 = zero_ptr;
454 455 456 457
        default:
          break;
      }
    }
Y
yiicy 已提交
458 459 460 461 462 463
    if (pad_l) {
      memset(dout, 0, pad_l * 8 * sizeof(int8_t));
      dout += pad_l * 8;
    }
    if (valid_cnt) {
      int cnt = valid_cnt;
464
#ifdef __aarch64__
Y
yiicy 已提交
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 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
      asm volatile(
          /* main loop */
          "1:\n"
          "ldr d0,    [%[r0]], #8\n"
          "ldr d1,    [%[r1]], #8\n"
          "ldr d2,    [%[r2]], #8\n"
          "ldr d3,    [%[r3]], #8\n"
          "ldr d4,    [%[r4]], #8\n"
          "ldr d5,    [%[r5]], #8\n"
          "ldr d6,    [%[r6]], #8\n"
          "ldr d7,    [%[r7]], #8\n"
          "trn1 v8.8b,  v0.8b, v1.8b\n"
          "trn2 v9.8b,  v0.8b, v1.8b\n"
          "trn1 v10.8b, v2.8b, v3.8b\n"
          "trn2 v11.8b, v2.8b, v3.8b\n"
          "trn1 v12.8b, v4.8b, v5.8b\n"
          "trn2 v13.8b, v4.8b, v5.8b\n"
          "trn1 v14.8b, v6.8b, v7.8b\n"
          "trn2 v15.8b, v6.8b, v7.8b\n"
          "trn1 v0.4h,  v8.4h, v10.4h\n"
          "trn2 v1.4h,  v8.4h, v10.4h\n"
          "trn1 v2.4h,  v9.4h, v11.4h\n"
          "trn2 v3.4h,  v9.4h, v11.4h\n"
          "trn1 v4.4h,  v12.4h, v14.4h\n"
          "trn2 v5.4h,  v12.4h, v14.4h\n"
          "trn1 v6.4h,  v13.4h, v15.4h\n"
          "trn2 v7.4h,  v13.4h, v15.4h\n"
          "trn1 v8.2s,  v0.2s, v4.2s\n"
          "trn1 v9.2s,  v2.2s, v6.2s\n"
          "trn1 v10.2s, v1.2s, v5.2s\n"
          "trn1 v11.2s, v3.2s, v7.2s\n"
          "stp d8, d9, [%[ptr_out]], #16\n"
          "trn2 v12.2s, v0.2s, v4.2s\n"
          "trn2 v13.2s, v2.2s, v6.2s\n"
          "stp d10, d11, [%[ptr_out]], #16\n"
          "trn2 v14.2s, v1.2s, v5.2s\n"
          "trn2 v15.2s, v3.2s, v7.2s\n"
          "subs %w[cnt], %w[cnt], #1\n"
          "stp d12, d13, [%[ptr_out]], #16\n"
          "stp d14, d15, [%[ptr_out]], #16\n"
          "bne    1b\n"
          : [cnt] "+r"(cnt),
            [r0] "+r"(ptr_c0),
            [r1] "+r"(ptr_c1),
            [r2] "+r"(ptr_c2),
            [r3] "+r"(ptr_c3),
            [r4] "+r"(ptr_c4),
            [r5] "+r"(ptr_c5),
            [r6] "+r"(ptr_c6),
            [r7] "+r"(ptr_c7),
            [ptr_out] "+r"(dout)
          :
          : "cc",
            "memory",
            "v0",
            "v1",
            "v2",
            "v3",
            "v4",
            "v5",
            "v6",
            "v7",
            "v8",
            "v9",
            "v10",
            "v11",
            "v12",
            "v13",
            "v14",
            "v15");
535
#else
Y
yiicy 已提交
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 581 582 583 584 585 586 587 588 589 590
      asm volatile(
          /* main loop */
          "1:\n"
          "vld1.32 {d0},  [%[r0]]!\n"
          "vld1.32 {d1},  [%[r1]]!\n"
          "vld1.32 {d2},  [%[r2]]!\n"
          "vld1.32 {d3},  [%[r3]]!\n"
          "vld1.32 {d4},  [%[r4]]!\n"
          "vld1.32 {d5},  [%[r5]]!\n"
          "vld1.32 {d6},  [%[r6]]!\n"
          "vld1.32 {d7},  [%[r7]]!\n"
          "vtrn.8   d0, d1\n"
          "vtrn.8   d2, d3\n"
          "vtrn.8   d4, d5\n"
          "vtrn.8   d6, d7\n"
          "vtrn.16  d0, d2\n"
          "vtrn.16  d1, d3\n"
          "vtrn.16  d4, d6\n"
          "vtrn.16  d5, d7\n"
          "vtrn.32  d0, d4\n"
          "vtrn.32  d2, d6\n"
          "vtrn.32  d1, d5\n"
          "vtrn.32  d3, d7\n"
          "subs %[cnt], #1\n"
          "vst1.32 {d0-d3}, [%[ptr_out]]!\n"
          "vst1.32 {d4-d7}, [%[ptr_out]]!\n"
          "bne    1b\n"
          : [cnt] "+r"(cnt),
            [r0] "+r"(ptr_c0),
            [r1] "+r"(ptr_c1),
            [r2] "+r"(ptr_c2),
            [r3] "+r"(ptr_c3),
            [r4] "+r"(ptr_c4),
            [r5] "+r"(ptr_c5),
            [r6] "+r"(ptr_c6),
            [r7] "+r"(ptr_c7),
            [ptr_out] "+r"(dout)
          :
          : "cc", "memory", "q0", "q1", "q2", "q3");
#endif  // __aarch64__
    }
    for (int i = 0; i < remain; ++i) {
      dout[0] = *(ptr_c0++);
      dout[1] = *(ptr_c1++);
      dout[2] = *(ptr_c2++);
      dout[3] = *(ptr_c3++);
      dout[4] = *(ptr_c4++);
      dout[5] = *(ptr_c5++);
      dout[6] = *(ptr_c6++);
      dout[7] = *(ptr_c7++);
      dout += 8;
    }
    if (pad_r) {
      memset(dout, 0, pad_r * 8 * sizeof(int8_t));
      dout += pad_r * 8;
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
// clang-format off
#ifdef __aarch64__
#define NCHWC1_TRANS_FP32_COMPUTE                                      \
  "ldr q0, [%[ptr_din]], #16      \n" /* load data, c0r0, c1r0, c0r1*/ \
  "ldr q1, [%[ptr_din]], #16      \n" /* load data, c0r0, c1r0, c0r1*/ \
  "ldr q2, [%[ptr_din]], #16      \n" /* load data, c0r0, c1r0, c0r1*/ \
  "ldr q3, [%[ptr_din]], #16      \n" /* load data, c0r0, c1r0, c0r1*/ \
  "movi v20.4s, #0                \n" /* for relu */                   \
  "1:                             \n" /* main loop*/

#define NCHWC1_TRANS_FP32_RELU                 \
  "fmax   v0.4s, v0.4s, v20.4s    \n" /*relu*/ \
  "fmax   v1.4s, v1.4s, v20.4s    \n" /*relu*/ \
  "fmax   v2.4s, v2.4s, v20.4s    \n" /*relu*/ \
  "fmax   v3.4s, v3.4s, v20.4s    \n" /*relu*/

#define NCHWC1_TRANS_FP32_RELU6                    \
  "fmin   v0.4s, v0.4s, %[six].4s  \n" /* relu6 */ \
  "fmin   v1.4s, v1.4s, %[six].4s  \n" /* relu6 */ \
  "fmin   v2.4s, v2.4s, %[six].4s  \n" /* relu6 */ \
  "fmin   v3.4s, v3.4s, %[six].4s  \n" /* relu6 */

#define NCHWC1_TRANS_FP32_LEAKY_RELU                   \
617 618 619 620 621 622
  "fcmge v4.4s, v0.4s, v20.4s \n"      /* vcgeq_f32 */ \
  "fcmge v5.4s, v1.4s, v20.4s \n"      /* vcgeq_f32 */ \
  "fcmge v6.4s, v2.4s, v20.4s \n"      /* vcgeq_f32 */ \
  "fcmge v7.4s, v3.4s, v20.4s \n"      /* vcgeq_f32 */ \
  "fmul v8.4s, v0.4s, %[scale].4s  \n" /* mul */       \
  "fmul v9.4s, v1.4s, %[scale].4s  \n" /* mul */       \
623 624
  "fmul v10.4s, v2.4s, %[scale].4s \n" /* mul */       \
  "fmul v11.4s, v3.4s, %[scale].4s \n" /* mul */       \
625 626
  "bif  v0.16b, v8.16b, v4.16b  \n"    /* choose*/     \
  "bif  v1.16b, v9.16b, v5.16b  \n"    /* choose*/     \
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
  "bif  v2.16b, v10.16b, v6.16b \n"    /* choose*/     \
  "bif  v3.16b, v11.16b, v7.16b \n"    /* choose*/

#define NCHWC1_TRANS_FP32_STORE                                        \
  "subs   %w[cnt], %w[cnt], #1    \n" /* loop count -1*/               \
                                                                       \
  "str    q0, [%[doutc0r0]], #16  \n" /* store c0r0*/                  \
  "str    q1, [%[doutc0r0]], #16  \n" /* store c0r0*/                  \
  "ldr q0, [%[ptr_din]], #16      \n" /* load data, c0r0, c1r0, c0r1*/ \
  "ldr q1, [%[ptr_din]], #16      \n" /* load data, c0r0, c1r0, c0r1*/ \
  "str    q2, [%[doutc0r0]], #16  \n" /* store c0r0*/                  \
  "str    q3, [%[doutc0r0]], #16  \n" /* store c2r0*/                  \
  "ldr q2, [%[ptr_din]], #16      \n" /* load data, c0r0, c1r0, c0r1*/ \
  "ldr q3, [%[ptr_din]], #16      \n" /* load data, c0r0, c1r0, c0r1*/ \
                                                                       \
  "bne    1b                      \n" /* jump to main loop*/
#else
#define NCHWC1_TRANS_FP32_COMPUTE                                       \
  "vld1.32 {d0-d3}, [%[ptr_din]]!                 @ load data, c0r0 \n" \
  "vld1.32 {d4-d7}, [%[ptr_din]]!                 @ load data, c0r0 \n" \
  "vmov.u32 q15, #0                       @ dump zero\n"                \
  "1:                                     @ main loop\n"
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 676
#define NCHWC1_TRANS_FP32_RELU                      \
  "vmax.f32   q0, q0, q15                 @ relu\n" \
  "vmax.f32   q1, q1, q15                 @ relu\n" \
  "vmax.f32   q2, q2, q15                 @ relu\n" \
  "vmax.f32   q3, q3, q15                 @ relu\n"

#define NCHWC1_TRANS_FP32_RELU6                  \
  "vmin.f32   q0, q0, %q[six]        @ relu6 \n" \
  "vmin.f32   q1, q1, %q[six]        @ relu6 \n" \
  "vmin.f32   q2, q2, %q[six]        @ relu6 \n" \
  "vmin.f32   q3, q3, %q[six]        @ relu6 \n"

#define NCHWC1_TRANS_FP32_LEAKY_RELU          \
  "vcge.f32   q5, q0, q15        @ q0 > 0 \n" \
  "vcge.f32   q6, q1, q15        @ q0 > 0 \n" \
  "vcge.f32   q7, q2, q15        @ q0 > 0 \n" \
  "vcge.f32   q8, q3, q15        @ q0 > 0 \n" \
  "vmul.f32 q9, q0, %q[scale] \n"             \
  "vmul.f32 q10, q1, %q[scale] \n"            \
  "vmul.f32 q11, q2, %q[scale] \n"            \
  "vmul.f32 q12, q3, %q[scale] \n"            \
  "vbif q0, q9, q5 @ choose \n"               \
  "vbif q1, q10, q6 @ choose \n"              \
  "vbif q2, q11, q7 @ choose \n"              \
  "vbif q3, q12, q8 @ choose \n"

#define NCHWC1_TRANS_FP32_STORE                                 \
677 678
  "vst1.32  {d0-d1}, [%[doutc0r0]]!       @ store result  \n"   \
  "vst1.32  {d2-d3}, [%[doutc0r0]]!       @ store result, \n"   \
679 680
  "subs   %[cnt], %[cnt], #1              @ loop count - 1\n"   \
                                                                \
681 682
  "vld1.32 {d0-d3}, [%[ptr_din]]!         @ load data      \n"  \
  "vst1.32  {d4-d5}, [%[doutc0r0]]!       @ store result   \n"  \
683 684
  "vst1.32  {d6-d7}, [%[doutc0r0]]!       @ store result,  \n"  \
                                                                \
685
  "vld1.32 {d4-d7}, [%[ptr_din]]!         @ load data     \n"   \
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
                                                                \
  "bne    1b                              @ jump to main loop\n"
#endif
// clang-format on
inline void act_switch_c1_fp32(const float* din_ptr,
                               float* doutc0_ptr,
                               int cnt_loop,
                               const operators::ActivationParam* act_param) {
  if (act_param != nullptr && act_param->has_active) {
    float32x4_t six = vdupq_n_f32(act_param->Relu_clipped_coef);
    float32x4_t scale = vdupq_n_f32(act_param->Leaky_relu_alpha);
    switch (act_param->active_type) {
      case lite_api::ActivationType::kRelu:
#ifdef __aarch64__
        asm volatile(NCHWC1_TRANS_FP32_COMPUTE NCHWC1_TRANS_FP32_RELU
                         NCHWC1_TRANS_FP32_STORE
                     : [doutc0r0] "+r"(doutc0_ptr),
                       [cnt] "+r"(cnt_loop),
                       [ptr_din] "+r"(din_ptr)
                     :
706 707 708
                     : "cc",
                       "memory",
                       "v0",
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
                       "v1",
                       "v2",
                       "v3",
                       "v4",
                       "v5",
                       "v6",
                       "v7",
                       "v8",
                       "v9",
                       "v10",
                       "v20");
#else
        asm volatile(NCHWC1_TRANS_FP32_COMPUTE NCHWC1_TRANS_FP32_RELU
                         NCHWC1_TRANS_FP32_STORE
                     : [doutc0r0] "+r"(doutc0_ptr),
                       [ptr_din] "+r"(din_ptr),
                       [cnt] "+r"(cnt_loop)
                     :
727
                     : "cc", "memory", "q0", "q1", "q2", "q3", "q15");
728 729 730 731 732 733 734 735 736 737 738
#endif
        break;
      case lite_api::ActivationType::kRelu6:
/* 0 <= din <= 6 */
#ifdef __aarch64__
        asm volatile(NCHWC1_TRANS_FP32_COMPUTE NCHWC1_TRANS_FP32_RELU
                         NCHWC1_TRANS_FP32_RELU6 NCHWC1_TRANS_FP32_STORE
                     : [doutc0r0] "+r"(doutc0_ptr),
                       [cnt] "+r"(cnt_loop),
                       [ptr_din] "+r"(din_ptr)
                     : [six] "w"(six)
739 740 741
                     : "cc",
                       "memory",
                       "v0",
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
                       "v1",
                       "v2",
                       "v3",
                       "v4",
                       "v5",
                       "v6",
                       "v7",
                       "v8",
                       "v9",
                       "v10",
                       "v20");
#else
        asm volatile(NCHWC1_TRANS_FP32_COMPUTE NCHWC1_TRANS_FP32_RELU
                         NCHWC1_TRANS_FP32_RELU6 NCHWC1_TRANS_FP32_STORE
                     : [doutc0r0] "+r"(doutc0_ptr),
                       [ptr_din] "+r"(din_ptr),
                       [cnt] "+r"(cnt_loop)
                     : [six] "w"(six)
760
                     : "cc", "memory", "q0", "q1", "q2", "q3", "q15");
761 762 763 764 765 766 767 768 769 770 771
#endif
        break;
      case lite_api::ActivationType::kLeakyRelu:
/*din = din >= 0 ? din : din * scale*/
#ifdef __aarch64__
        asm volatile(NCHWC1_TRANS_FP32_COMPUTE NCHWC1_TRANS_FP32_LEAKY_RELU
                         NCHWC1_TRANS_FP32_STORE
                     : [doutc0r0] "+r"(doutc0_ptr),
                       [cnt] "+r"(cnt_loop),
                       [ptr_din] "+r"(din_ptr)
                     : [scale] "w"(scale)
772 773 774
                     : "cc",
                       "memory",
                       "v0",
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
                       "v1",
                       "v2",
                       "v3",
                       "v4",
                       "v5",
                       "v6",
                       "v7",
                       "v8",
                       "v9",
                       "v10",
                       "v11",
                       "v20");
#else
        asm volatile(NCHWC1_TRANS_FP32_COMPUTE NCHWC1_TRANS_FP32_LEAKY_RELU
                         NCHWC1_TRANS_FP32_STORE
                     : [doutc0r0] "+r"(doutc0_ptr),
                       [ptr_din] "+r"(din_ptr),
                       [cnt] "+r"(cnt_loop)
                     : [scale] "w"(scale)
794 795 796
                     : "cc",
                       "memory",
                       "q0",
797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
                       "q1",
                       "q2",
                       "q3",
                       "q5",
                       "q6",
                       "q7",
                       "q8",
                       "q9",
                       "q10",
                       "q11",
                       "q12",
                       "q15");
#endif
        break;
      default:
        LOG(FATAL) << "this act_type: "
                   << static_cast<int>(act_param->active_type)
                   << " fuse not support";
    }
  } else {
#ifdef __aarch64__
    asm volatile(NCHWC1_TRANS_FP32_COMPUTE NCHWC1_TRANS_FP32_STORE
                 : [doutc0r0] "+r"(doutc0_ptr),
                   [cnt] "+r"(cnt_loop),
                   [ptr_din] "+r"(din_ptr)
                 :
823
                 : "cc", "memory", "v0", "v1", "v2", "v3", "v20");
824 825 826 827 828 829
#else
    asm volatile(NCHWC1_TRANS_FP32_COMPUTE NCHWC1_TRANS_FP32_STORE
                 : [doutc0r0] "+r"(doutc0_ptr),
                   [ptr_din] "+r"(din_ptr),
                   [cnt] "+r"(cnt_loop)
                 :
830
                 : "cc", "memory", "q0", "q1", "q2", "q3", "q15");
831 832 833
#endif
  }
}
Y
Yan Chunwei 已提交
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
/*wirte result in outputs
* input din: [n, c, h, w], output dout: [n, c, h, w]
*/
inline bool write_to_output_c1_fp32(const float* din,
                                    float* dout,
                                    int cs,
                                    int ce,
                                    int hs,
                                    int he,
                                    int ws,
                                    int we,
                                    int channel,
                                    int height,
                                    int width,
                                    bool flag_relu,
849 850
                                    float* trash_ptr,
                                    operators::ActivationParam* act_param) {
Y
Yan Chunwei 已提交
851 852 853 854 855
  if (cs > channel) {
    return true;
  }

  const int c1 = 1;
856
  const int w4 = 16;
Y
Yan Chunwei 已提交
857 858 859 860 861 862 863 864 865 866 867

  int size_c_out = width * height;

  float* doutc0r0 = dout + cs * size_c_out + hs * width + ws;

  const float* ptr_din = din;

  int size_h = (he > height ? height : he) - hs;  // size_h == hei_n

  int w_round = we - ws;
  int cnt = (width - ws) / w4;
868
  int remain = (width - ws) % w4;
Y
Yan Chunwei 已提交
869 870 871 872 873 874
  for (int i = 0; i < size_h; i++) {
    int size_w = i * width;
    float* doutc0_ptr = doutc0r0 + size_w;  // doutc0r0 + width;
    const float* din_hei_ptr = ptr_din + i * w_round * c1;
    if (cnt > 0) {
      int cnt_loop = cnt;
875
      act_switch_c1_fp32(din_hei_ptr, doutc0_ptr, cnt_loop, act_param);
Y
Yan Chunwei 已提交
876
    }
877
    if (remain > 0) {
Y
Yan Chunwei 已提交
878 879
      int offset = i * w_round * c1 + c1 * w4 * cnt;
      din_hei_ptr = ptr_din + offset;
880 881 882 883 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
      doutc0_ptr += w4 * cnt;
      int j = w4 * cnt;
      if (act_param != nullptr && act_param->has_active) {
        float six = act_param->Relu_clipped_coef;
        float scale = act_param->Leaky_relu_alpha;
        switch (act_param->active_type) {
          case lite_api::ActivationType::kRelu:
            for (; j < width; ++j) {
              *(doutc0_ptr++) = LITEMAX(din_hei_ptr[0], 0.f);
              din_hei_ptr++;
            }
            break;
          case lite_api::ActivationType::kRelu6:
            /* 0 <= din <= 6 */
            for (; j < width; ++j) {
              float tmp = LITEMAX(din_hei_ptr[0], 0.f);
              *(doutc0_ptr++) = LITEMIN(tmp, six);
              din_hei_ptr++;
            }
            break;
          case lite_api::ActivationType::kLeakyRelu:
            /*din = din >= 0 ? din : din * scale*/
            for (; j < width; ++j) {
              if (din_hei_ptr[0] >= 0) {
                *(doutc0_ptr++) = din_hei_ptr[0];
              } else {
                *(doutc0_ptr++) = din_hei_ptr[0] * scale;
              }
              din_hei_ptr++;
            }
            break;
          default:
            LOG(FATAL) << "this act_type: "
                       << static_cast<int>(act_param->active_type)
                       << " fuse not support";
Y
Yan Chunwei 已提交
915 916 917 918 919 920 921 922 923 924
        }
      } else {
        for (; j < width; ++j) {
          *(doutc0_ptr++) = *(din_hei_ptr++);
        }
      }
    }
  }
  return true;
}
925
// clang-format off
H
HappyAngel 已提交
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
#ifdef __aarch64__
#define NCHWC2_TRANS_FP32_COMPUTE                                      \
  "ldp q0, q1, [%[ptr_din]], #32  \n" /* load data, c0r0, c1r0, c0r1*/ \
  "movi v20.4s, #0                \n" /* for relu */                   \
  "1:                             \n" /* main loop*/                   \
  "trn1   v2.4s, v0.4s, v1.4s     \n" /* trans q0, q1*/                \
  "trn2   v3.4s, v0.4s, v1.4s     \n" /* trans q0, q1*/                \
  "ldp q0, q1, [%[ptr_din]], #32  \n" /* load data, c0r0, c1r0, c0r1*/ \
  "trn1   v4.2d, v2.2d, v3.2d     \n" /* trans q8, q10*/               \
  "trn2   v5.2d, v2.2d, v3.2d     \n" /* trans q8, q10*/

#define NCHWC2_TRANS_FP32_RELU                 \
  "fmax   v2.4s, v4.4s, v20.4s    \n" /*relu*/ \
  "fmax   v3.4s, v5.4s, v20.4s    \n" /*relu*/

941 942 943 944
#define NCHWC2_TRANS_FP32_RELU6                    \
  "fmin   v2.4s, v2.4s, %[six].4s  \n" /* relu6 */ \
  "fmin   v3.4s, v3.4s, %[six].4s  \n" /* relu6 */

945 946 947 948 949 950
#define NCHWC2_TRANS_FP32_LEAKY_RELU                   \
  "fcmge v6.4s, v2.4s, v20.4s \n"      /* vcgeq_f32 */ \
  "fcmge v7.4s, v3.4s, v20.4s \n"      /* vcgeq_f32 */ \
  "fmul v4.4s, v2.4s, %[scale].4s \n" /* mul */        \
  "fmul v5.4s, v3.4s, %[scale].4s \n" /* mul */        \
  "bif  v2.16b, v4.16b, v6.16b \n"    /* choose*/      \
951 952
  "bif  v3.16b, v5.16b, v7.16b \n"    /* choose*/

H
HappyAngel 已提交
953 954 955 956 957 958 959 960 961
#define NCHWC2_TRANS_FP32_STORE                          \
  "subs   %w[cnt], %w[cnt], #1    \n" /* loop count -1*/ \
                                                         \
  "str    q2, [%[doutc0r0]], #16  \n" /* store c0r0*/    \
  "str    q3, [%[doutc1r0]], #16  \n" /* store c2r0*/    \
                                                         \
  "bne    1b                      \n" /* jump to main loop*/
#else
#define NCHWC2_TRANS_FP32_COMPUTE                                      \
962
  "vld1.32 {d0-d3}, [%[ptr_din]]!         @ load data, c0r0, c1r0 \n"  \
H
HappyAngel 已提交
963 964 965 966 967 968 969 970 971 972 973 974 975
  "vmov.u32 q15, #0                       @ dump zero\n"               \
  "1:                                     @ main loop\n"               \
  "vtrn.32 d0, d1                         @ trans data:c0r0, c0r1, "   \
  "c1r0, c1r1 \n"                                                      \
  "vtrn.32 d2, d3                         @ trans data:c0r2, c0r3, "   \
  "c1r2, c1r3 \n"                                                      \
                                                                       \
  "vswp  d1, d2                           @ swap data\n"

#define NCHWC2_TRANS_FP32_RELU                      \
  "vmax.f32   q0, q0, q15                 @ relu\n" \
  "vmax.f32   q1, q1, q15                 @ relu\n"

976 977 978 979 980 981 982 983 984 985 986 987
#define NCHWC2_TRANS_FP32_RELU6                  \
  "vmin.f32   q0, q0, %q[six]        @ relu6 \n" \
  "vmin.f32   q1, q1, %q[six]        @ relu6 \n"

#define NCHWC2_TRANS_FP32_LEAKY_RELU          \
  "vcge.f32   q5, q0, q15        @ q0 > 0 \n" \
  "vcge.f32   q6, q1, q15        @ q0 > 0 \n" \
  "vmul.f32 q9, q0, %q[scale] \n"             \
  "vmul.f32 q10, q1, %q[scale] \n"            \
  "vbif q0, q9, q5 @ choose \n"               \
  "vbif q1, q10, q6 @ choose \n"

H
HappyAngel 已提交
988
#define NCHWC2_TRANS_FP32_STORE                                 \
989 990
  "vst1.32  {d0-d1}, [%[doutc0r0]]!       @ store result, add pointer\n"   \
  "vst1.32  {d2-d3}, [%[doutc1r0]]!       @ store result, add pointer\n"   \
H
HappyAngel 已提交
991 992 993 994 995 996 997
                                                                \
  "subs   %[cnt], %[cnt], #1              @ loop count - 1\n"   \
                                                                \
  "vld1.32 {d0-d3}, [%[ptr_din]]!         @ load data \n"       \
                                                                \
  "bne    1b                              @ jump to main loop\n"
#endif
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
// clang-format on
inline void act_switch_c2_fp32(const float* din_ptr,
                               float* doutc0_ptr,
                               float* doutc1_ptr,
                               int cnt_loop,
                               const operators::ActivationParam* act_param) {
  if (act_param != nullptr && act_param->has_active) {
    float32x4_t six = vdupq_n_f32(act_param->Relu_clipped_coef);
    float32x4_t scale = vdupq_n_f32(act_param->Leaky_relu_alpha);
    switch (act_param->active_type) {
      case lite_api::ActivationType::kRelu:
#ifdef __aarch64__
        asm volatile(NCHWC2_TRANS_FP32_COMPUTE NCHWC2_TRANS_FP32_RELU
                         NCHWC2_TRANS_FP32_STORE
                     : [doutc0r0] "+r"(doutc0_ptr),
                       [doutc1r0] "+r"(doutc1_ptr),
                       [cnt] "+r"(cnt_loop),
                       [ptr_din] "+r"(din_ptr)
                     :
1017 1018 1019
                     : "cc",
                       "memory",
                       "v0",
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
                       "v1",
                       "v2",
                       "v3",
                       "v4",
                       "v5",
                       "v6",
                       "v7",
                       "v8",
                       "v9",
                       "v10",
                       "v20");
#else
        asm volatile(NCHWC2_TRANS_FP32_COMPUTE NCHWC2_TRANS_FP32_RELU
                         NCHWC2_TRANS_FP32_STORE
                     : [doutc0r0] "+r"(doutc0_ptr),
                       [doutc1r0] "+r"(doutc1_ptr),
                       [ptr_din] "+r"(din_ptr),
                       [cnt] "+r"(cnt_loop)
                     :
1039
                     : "cc", "memory", "q0", "q1", "q2", "q3", "q15");
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
#endif
        break;
      case lite_api::ActivationType::kRelu6:
/* 0 <= din <= 6 */
#ifdef __aarch64__
        asm volatile(NCHWC2_TRANS_FP32_COMPUTE NCHWC2_TRANS_FP32_RELU
                         NCHWC2_TRANS_FP32_RELU6 NCHWC2_TRANS_FP32_STORE
                     : [doutc0r0] "+r"(doutc0_ptr),
                       [doutc1r0] "+r"(doutc1_ptr),
                       [cnt] "+r"(cnt_loop),
                       [ptr_din] "+r"(din_ptr)
                     : [six] "w"(six)
1052 1053 1054
                     : "cc",
                       "memory",
                       "v0",
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
                       "v1",
                       "v2",
                       "v3",
                       "v4",
                       "v5",
                       "v6",
                       "v7",
                       "v8",
                       "v9",
                       "v10",
                       "v20");
#else
        asm volatile(NCHWC2_TRANS_FP32_COMPUTE NCHWC2_TRANS_FP32_RELU
                         NCHWC2_TRANS_FP32_RELU6 NCHWC2_TRANS_FP32_STORE
                     : [doutc0r0] "+r"(doutc0_ptr),
                       [doutc1r0] "+r"(doutc1_ptr),
                       [ptr_din] "+r"(din_ptr),
                       [cnt] "+r"(cnt_loop)
                     : [six] "w"(six)
1074
                     : "cc", "memory", "q0", "q1", "q2", "q3", "q15");
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
#endif
        break;
      case lite_api::ActivationType::kLeakyRelu:
/*din = din >= 0 ? din : din * scale*/
#ifdef __aarch64__
        asm volatile(NCHWC2_TRANS_FP32_COMPUTE NCHWC2_TRANS_FP32_LEAKY_RELU
                         NCHWC2_TRANS_FP32_STORE
                     : [doutc0r0] "+r"(doutc0_ptr),
                       [doutc1r0] "+r"(doutc1_ptr),
                       [cnt] "+r"(cnt_loop),
                       [ptr_din] "+r"(din_ptr)
                     : [scale] "w"(scale)
1087 1088 1089
                     : "cc",
                       "memory",
                       "v0",
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
                       "v1",
                       "v2",
                       "v3",
                       "v4",
                       "v5",
                       "v6",
                       "v7",
                       "v8",
                       "v9",
                       "v10",
                       "v20");
#else
        asm volatile(NCHWC2_TRANS_FP32_COMPUTE NCHWC2_TRANS_FP32_LEAKY_RELU
                         NCHWC2_TRANS_FP32_STORE
                     : [doutc0r0] "+r"(doutc0_ptr),
                       [doutc1r0] "+r"(doutc1_ptr),
                       [ptr_din] "+r"(din_ptr),
                       [cnt] "+r"(cnt_loop)
                     : [scale] "w"(scale)
1109 1110 1111
                     : "cc",
                       "memory",
                       "q0",
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
                       "q1",
                       "q2",
                       "q3",
                       "q5",
                       "q6",
                       "q7",
                       "q8",
                       "q9",
                       "q10",
                       "q11",
                       "q12",
                       "q15");
#endif
        break;
      default:
        LOG(FATAL) << "this act_type: "
                   << static_cast<int>(act_param->active_type)
                   << " fuse not support";
    }
  } else {
#ifdef __aarch64__
    asm volatile(NCHWC2_TRANS_FP32_COMPUTE NCHWC2_TRANS_FP32_STORE
                 : [doutc0r0] "+r"(doutc0_ptr),
                   [doutc1r0] "+r"(doutc1_ptr),
                   [cnt] "+r"(cnt_loop),
                   [ptr_din] "+r"(din_ptr)
                 :
1139
                 : "cc", "memory", "v0", "v1", "v2", "v3", "v4", "v5", "v20");
1140 1141 1142 1143 1144 1145 1146
#else
    asm volatile(NCHWC2_TRANS_FP32_COMPUTE NCHWC2_TRANS_FP32_STORE
                 : [doutc0r0] "+r"(doutc0_ptr),
                   [doutc1r0] "+r"(doutc1_ptr),
                   [ptr_din] "+r"(din_ptr),
                   [cnt] "+r"(cnt_loop)
                 :
1147
                 : "cc", "memory", "q0", "q1", "q2", "q3", "q15");
1148 1149 1150
#endif
  }
}
Y
Yan Chunwei 已提交
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
/*wirte result in outputs
* input din: [n, c / 4, h, w * 4], output dout: [n, c, h, w]
*/
inline bool write_to_output_c2_fp32(const float* din,
                                    float* dout,
                                    int cs,
                                    int ce,
                                    int hs,
                                    int he,
                                    int ws,
                                    int we,
                                    int channel,
                                    int height,
                                    int width,
                                    bool flag_relu,
1166 1167
                                    float* trash_ptr,
                                    operators::ActivationParam* act_param) {
Y
Yan Chunwei 已提交
1168 1169 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
  if (cs > channel) {
    return true;
  }
  const int c2 = 2;
  const int w4 = 4;

  //    float trash_ptr[width];

  int size_c_out = width * height;

  float* doutc0r0 = dout + cs * size_c_out + hs * width + ws;
  float* doutc1r0 = doutc0r0 + size_c_out;

  const float* ptr_din = din;

  int size_h = (he > height ? height : he) - hs;  // size_h == hei_n

  int w_round = we - ws;
  int cnt = (width - ws) / w4;

  for (int i = 0; i < size_h; i++) {
    int size_w = i * width;
    float* doutc0_ptr = doutc0r0 + size_w;  // doutc0r0 + width;
    float* doutc1_ptr = doutc1r0 + size_w;
    if (ce > channel) {
      switch (ce - channel) {
        case 1:
          doutc1_ptr = trash_ptr;
        default:
          break;
      }
    }
    const float* din_hei_ptr = ptr_din + i * w_round * c2;
    if (cnt > 0) {
      int cnt_loop = cnt;
1203 1204
      act_switch_c2_fp32(
          din_hei_ptr, doutc0_ptr, doutc1_ptr, cnt_loop, act_param);
Y
Yan Chunwei 已提交
1205 1206 1207 1208
    }
    if (we > width) {
      int offset = i * w_round * c2 + c2 * w4 * cnt;
      din_hei_ptr = ptr_din + offset;
1209 1210
      doutc0_ptr += w4 * cnt;
      doutc1_ptr += w4 * cnt;
Y
Yan Chunwei 已提交
1211
      int j = we - w4;
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 1247 1248 1249 1250 1251 1252
      if (act_param != nullptr && act_param->has_active) {
        float six = act_param->Relu_clipped_coef;
        float scale = act_param->Leaky_relu_alpha;
        switch (act_param->active_type) {
          case lite_api::ActivationType::kRelu:
            for (; j < width; ++j) {
              *(doutc0_ptr++) = LITEMAX(din_hei_ptr[0], 0.f);
              *(doutc1_ptr++) = LITEMAX(din_hei_ptr[1], 0.f);
              din_hei_ptr += 2;
            }
            break;
          case lite_api::ActivationType::kRelu6:
            /* 0 <= din <= 6 */
            for (; j < width; ++j) {
              float tmp1 = LITEMAX(din_hei_ptr[0], 0.f);
              float tmp2 = LITEMAX(din_hei_ptr[1], 0.f);
              *(doutc0_ptr++) = LITEMIN(tmp1, six);
              *(doutc1_ptr++) = LITEMIN(tmp2, six);
              din_hei_ptr += 2;
            }
            break;
          case lite_api::ActivationType::kLeakyRelu:
            /*din = din >= 0 ? din : din * scale*/
            for (; j < width; ++j) {
              if (din_hei_ptr[0] >= 0) {
                *(doutc0_ptr++) = din_hei_ptr[0];
              } else {
                *(doutc0_ptr++) = din_hei_ptr[0] * scale;
              }
              if (din_hei_ptr[1] >= 0) {
                *(doutc1_ptr++) = din_hei_ptr[1];
              } else {
                *(doutc1_ptr++) = din_hei_ptr[1] * scale;
              }
              din_hei_ptr += 2;
            }
            break;
          default:
            LOG(FATAL) << "this act_type: "
                       << static_cast<int>(act_param->active_type)
                       << " fuse not support";
Y
Yan Chunwei 已提交
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
        }
      } else {
        for (; j < width; ++j) {
          *(doutc0_ptr++) = *(din_hei_ptr++);
          *(doutc1_ptr++) = *(din_hei_ptr++);
        }
      }
    }
  }
  return true;
}
1264
// clang-format off
H
HappyAngel 已提交
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
#ifdef __aarch64__
#define NCHWC4_TRANS_FP32_COMPUTE                                   \
  "ldp q0, q1, [%[ptr_din]], #32  \n" /* load r00, r01 to q0, q1 */ \
  "ldp q2, q3, [%[ptr_din]], #32  \n" /* load r02, r03 to q2, q3 */ \
  "movi v20.4s, #0                \n" /* for relu */                \
  "1:                             \n" /* main loop*/                \
  "trn1   v8.4s, v0.4s, v1.4s     \n" /* trans q0, q1*/             \
  "trn2   v9.4s, v0.4s, v1.4s     \n" /* trans q0, q1*/             \
  "ldp q0, q1, [%[ptr_din]], #32  \n" /* load r00, r01 to q0, q1 */ \
  "trn1   v10.4s, v2.4s, v3.4s    \n" /* trans q2, q3*/             \
  "trn2   v11.4s, v2.4s, v3.4s    \n" /* trans q2, q3*/             \
  "ldp q2, q3, [%[ptr_din]], #32  \n" /* load r02, r03 to q2, q3 */ \
  "trn1   v16.2d, v8.2d, v10.2d   \n" /* trans q8, q10*/            \
  "trn2   v17.2d, v8.2d, v10.2d   \n" /* trans q8, q10*/            \
  "trn1   v18.2d, v9.2d, v11.2d   \n" /* trans q9, q11*/            \
  "trn2   v19.2d, v9.2d, v11.2d   \n" /* trans q9, q11*/

#define NCHWC4_TRANS_FP32_RELU                 \
  "fmax   v16.4s, v16.4s, v20.4s  \n" /*relu*/ \
  "fmax   v17.4s, v17.4s, v20.4s  \n" /*relu*/ \
  "fmax   v18.4s, v18.4s, v20.4s  \n" /*relu*/ \
  "fmax   v19.4s, v19.4s, v20.4s  \n" /*relu*/

1288 1289 1290 1291 1292 1293
#define NCHWC4_TRANS_FP32_RELU6                      \
  "fmin   v16.4s, v16.4s, %[six].4s  \n" /* relu6 */ \
  "fmin   v17.4s, v17.4s, %[six].4s  \n" /* relu6 */ \
  "fmin   v18.4s, v18.4s, %[six].4s  \n" /* relu6 */ \
  "fmin   v19.4s, v19.4s, %[six].4s  \n" /* relu6 */

1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
#define NCHWC4_TRANS_FP32_LEAKY_RELU                    \
  "fcmge v8.4s, v16.4s, v20.4s  \n"     /* vcgeq_f32 */ \
  "fcmge v9.4s, v17.4s, v20.4s  \n"     /* vcgeq_f32 */ \
  "fcmge v10.4s, v18.4s, v20.4s \n"     /* vcgeq_f32 */ \
  "fcmge v11.4s, v19.4s, v20.4s \n"     /* vcgeq_f32 */ \
  "fmul v4.4s, v16.4s, %[scale].4s \n"  /* mul */       \
  "fmul v5.4s, v17.4s, %[scale].4s \n"  /* mul */       \
  "fmul v6.4s, v18.4s, %[scale].4s \n"  /* mul */       \
  "fmul v7.4s, v19.4s, %[scale].4s \n"  /* mul */       \
  "bif  v16.16b, v4.16b, v8.16b  \n"    /* choose*/     \
  "bif  v17.16b, v5.16b, v9.16b  \n"    /* choose*/     \
  "bif  v18.16b, v6.16b, v10.16b \n"    /* choose*/     \
  "bif  v19.16b, v7.16b, v11.16b \n"    /* choose*/
1307

H
HappyAngel 已提交
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
#define NCHWC4_TRANS_FP32_STORE                          \
  "str    q16, [%[doutc0r0]], #16 \n" /* store c0r0*/    \
  "str    q17, [%[doutc2r0]], #16 \n" /* store c2r0*/    \
  "str    q18, [%[doutc1r0]], #16 \n" /* store c1r0*/    \
  "str    q19, [%[doutc3r0]], #16 \n" /* store c3r0*/    \
                                                         \
  "subs   %w[cnt], %w[cnt], #1    \n" /* loop count -1*/ \
  "bne    1b                      \n" /* jump to main loop*/
#else
#define NCHWC4_TRANS_FP32_COMPUTE                                     \
  "vld1.32 {d0-d3}, [%[ptr_din]]!                 @load data \n"      \
  "vld1.32 {d4-d7}, [%[ptr_din]]!         @load data \n"              \
  "vmov.u32 q15, #0                       @ dump zero\n"              \
  "1:                                     @ main loop\n"              \
  "vtrn.32 q0, q1                         @ trans data:c00c01c20c21 " \
  "\n"                                                                \
  "vtrn.32 q2, q3                         @ trans data:c02c03c22c23 " \
  "\n"                                                                \
                                                                      \
  "vswp   d1, d4                          @ swap data\n"              \
  "vswp   d3, d6                          @ swap data\n"

#define NCHWC4_TRANS_FP32_RELU             \
  "vmax.f32   q0, q0, q15        @ relu\n" \
  "vmax.f32   q1, q1, q15        @ relu\n" \
  "vmax.f32   q2, q2, q15        @ relu\n" \
  "vmax.f32   q3, q3, q15        @ relu\n"

1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
#define NCHWC4_TRANS_FP32_RELU6                  \
  "vmin.f32   q0, q0, %q[six]        @ relu6 \n" \
  "vmin.f32   q1, q1, %q[six]        @ relu6 \n" \
  "vmin.f32   q2, q2, %q[six]        @ relu6 \n" \
  "vmin.f32   q3, q3, %q[six]        @ relu6 \n"

#define NCHWC4_TRANS_FP32_LEAKY_RELU          \
  "vcge.f32   q5, q0, q15        @ q0 > 0 \n" \
  "vcge.f32   q6, q1, q15        @ q0 > 0 \n" \
  "vcge.f32   q7, q2, q15        @ q0 > 0 \n" \
  "vcge.f32   q8, q3, q15        @ q0 > 0 \n" \
  "vmul.f32 q9, q0, %q[scale] \n"             \
  "vmul.f32 q10, q1, %q[scale] \n"            \
  "vmul.f32 q11, q2, %q[scale] \n"            \
  "vmul.f32 q12, q3, %q[scale] \n"            \
  "vbif q0, q9, q5 @ choose \n"               \
  "vbif q1, q10, q6 @ choose \n"              \
  "vbif q2, q11, q7 @ choose \n"              \
  "vbif q3, q12, q8 @ choose \n"

H
HappyAngel 已提交
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
#define NCHWC4_TRANS_FP32_STORE                                        \
  "vst1.32  {d0-d1}, [%[doutc0r0]]!     @ store result, add pointer\n" \
  "vst1.32  {d2-d3}, [%[doutc1r0]]!     @ store result, add pointer\n" \
  "vst1.32  {d4-d5}, [%[doutc2r0]]!     @ store result, add pointer\n" \
  "vst1.32  {d6-d7}, [%[doutc3r0]]!     @ store result, add pointer\n" \
                                                                       \
  "subs   %[cnt], %[cnt], #1    @ loop count - 1\n"                    \
                                                                       \
  "vld1.32 {d0-d3}, [%[ptr_din]]!        @load data \n"                \
  "vld1.32 {d4-d7}, [%[ptr_din]]!        @load data \n"                \
                                                                       \
  "bne    1b                            @ jump to main loop\n"
#endif
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
// clang-format on
inline void act_switch_c4_fp32(const float* din_ptr,
                               float* doutc0_ptr,
                               float* doutc1_ptr,
                               float* doutc2_ptr,
                               float* doutc3_ptr,
                               int cnt_loop,
                               const operators::ActivationParam* act_param) {
  if (act_param != nullptr && act_param->has_active) {
    float32x4_t six = vdupq_n_f32(act_param->Relu_clipped_coef);
    float32x4_t scale = vdupq_n_f32(act_param->Leaky_relu_alpha);
    switch (act_param->active_type) {
      case lite_api::ActivationType::kRelu:
Y
Yan Chunwei 已提交
1382
#ifdef __aarch64__
H
HappyAngel 已提交
1383 1384 1385 1386 1387 1388 1389
        asm volatile(NCHWC4_TRANS_FP32_COMPUTE NCHWC4_TRANS_FP32_RELU
                         NCHWC4_TRANS_FP32_STORE
                     : [doutc0r0] "+r"(doutc0_ptr),
                       [doutc1r0] "+r"(doutc1_ptr),
                       [doutc2r0] "+r"(doutc2_ptr),
                       [doutc3r0] "+r"(doutc3_ptr),
                       [cnt] "+r"(cnt_loop),
1390
                       [ptr_din] "+r"(din_ptr)
H
HappyAngel 已提交
1391
                     :
1392 1393 1394
                     : "cc",
                       "memory",
                       "v0",
H
HappyAngel 已提交
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
                       "v1",
                       "v2",
                       "v3",
                       "v4",
                       "v5",
                       "v6",
                       "v7",
                       "v8",
                       "v9",
                       "v10",
                       "v11",
                       "v12",
                       "v13",
                       "v14",
                       "v16",
                       "v17",
                       "v18",
                       "v19",
                       "v20");
Y
Yan Chunwei 已提交
1414
#else
H
HappyAngel 已提交
1415 1416 1417 1418 1419 1420
        asm volatile(NCHWC4_TRANS_FP32_COMPUTE NCHWC4_TRANS_FP32_RELU
                         NCHWC4_TRANS_FP32_STORE
                     : [doutc0r0] "+r"(doutc0_ptr),
                       [doutc1r0] "+r"(doutc1_ptr),
                       [doutc2r0] "+r"(doutc2_ptr),
                       [doutc3r0] "+r"(doutc3_ptr),
1421
                       [ptr_din] "+r"(din_ptr),
H
HappyAngel 已提交
1422 1423
                       [cnt] "+r"(cnt_loop)
                     :
1424
                     : "cc", "memory", "q0", "q1", "q2", "q3", "q15");
Y
Yan Chunwei 已提交
1425
#endif
1426 1427 1428
        break;
      case lite_api::ActivationType::kRelu6:
/* 0 <= din <= 6 */
Y
Yan Chunwei 已提交
1429
#ifdef __aarch64__
1430 1431
        asm volatile(NCHWC4_TRANS_FP32_COMPUTE NCHWC4_TRANS_FP32_RELU
                         NCHWC4_TRANS_FP32_RELU6 NCHWC4_TRANS_FP32_STORE
H
HappyAngel 已提交
1432 1433 1434 1435 1436
                     : [doutc0r0] "+r"(doutc0_ptr),
                       [doutc1r0] "+r"(doutc1_ptr),
                       [doutc2r0] "+r"(doutc2_ptr),
                       [doutc3r0] "+r"(doutc3_ptr),
                       [cnt] "+r"(cnt_loop),
1437 1438
                       [ptr_din] "+r"(din_ptr)
                     : [six] "w"(six)
1439 1440 1441
                     : "cc",
                       "memory",
                       "v0",
1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470
                       "v1",
                       "v2",
                       "v3",
                       "v4",
                       "v5",
                       "v6",
                       "v7",
                       "v8",
                       "v9",
                       "v10",
                       "v11",
                       "v12",
                       "v13",
                       "v14",
                       "v16",
                       "v17",
                       "v18",
                       "v19",
                       "v20");
#else
        asm volatile(NCHWC4_TRANS_FP32_COMPUTE NCHWC4_TRANS_FP32_RELU
                         NCHWC4_TRANS_FP32_RELU6 NCHWC4_TRANS_FP32_STORE
                     : [doutc0r0] "+r"(doutc0_ptr),
                       [doutc1r0] "+r"(doutc1_ptr),
                       [doutc2r0] "+r"(doutc2_ptr),
                       [doutc3r0] "+r"(doutc3_ptr),
                       [ptr_din] "+r"(din_ptr),
                       [cnt] "+r"(cnt_loop)
                     : [six] "w"(six)
1471
                     : "cc", "memory", "q0", "q1", "q2", "q3", "q15");
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
#endif
        break;
      case lite_api::ActivationType::kLeakyRelu:
/*din = din >= 0 ? din : din * scale*/
#ifdef __aarch64__
        asm volatile(NCHWC4_TRANS_FP32_COMPUTE NCHWC4_TRANS_FP32_LEAKY_RELU
                         NCHWC4_TRANS_FP32_STORE
                     : [doutc0r0] "+r"(doutc0_ptr),
                       [doutc1r0] "+r"(doutc1_ptr),
                       [doutc2r0] "+r"(doutc2_ptr),
                       [doutc3r0] "+r"(doutc3_ptr),
                       [cnt] "+r"(cnt_loop),
                       [ptr_din] "+r"(din_ptr)
                     : [scale] "w"(scale)
1486 1487 1488
                     : "cc",
                       "memory",
                       "v0",
H
HappyAngel 已提交
1489 1490 1491
                       "v1",
                       "v2",
                       "v3",
1492 1493 1494 1495
                       "v4",
                       "v5",
                       "v6",
                       "v7",
H
HappyAngel 已提交
1496 1497 1498 1499
                       "v8",
                       "v9",
                       "v10",
                       "v11",
1500 1501 1502
                       "v12",
                       "v13",
                       "v14",
H
HappyAngel 已提交
1503 1504 1505
                       "v16",
                       "v17",
                       "v18",
1506 1507
                       "v19",
                       "v20");
Y
Yan Chunwei 已提交
1508
#else
1509 1510
        asm volatile(NCHWC4_TRANS_FP32_COMPUTE NCHWC4_TRANS_FP32_LEAKY_RELU
                         NCHWC4_TRANS_FP32_STORE
H
HappyAngel 已提交
1511 1512 1513 1514
                     : [doutc0r0] "+r"(doutc0_ptr),
                       [doutc1r0] "+r"(doutc1_ptr),
                       [doutc2r0] "+r"(doutc2_ptr),
                       [doutc3r0] "+r"(doutc3_ptr),
1515
                       [ptr_din] "+r"(din_ptr),
H
HappyAngel 已提交
1516
                       [cnt] "+r"(cnt_loop)
1517
                     : [scale] "w"(scale)
1518 1519 1520
                     : "cc",
                       "memory",
                       "q0",
1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532
                       "q1",
                       "q2",
                       "q3",
                       "q5",
                       "q6",
                       "q7",
                       "q8",
                       "q9",
                       "q10",
                       "q11",
                       "q12",
                       "q15");
Y
Yan Chunwei 已提交
1533
#endif
1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549
        break;
      default:
        LOG(FATAL) << "this act_type: "
                   << static_cast<int>(act_param->active_type)
                   << " fuse not support";
    }
  } else {
#ifdef __aarch64__
    asm volatile(NCHWC4_TRANS_FP32_COMPUTE NCHWC4_TRANS_FP32_STORE
                 : [doutc0r0] "+r"(doutc0_ptr),
                   [doutc1r0] "+r"(doutc1_ptr),
                   [doutc2r0] "+r"(doutc2_ptr),
                   [doutc3r0] "+r"(doutc3_ptr),
                   [cnt] "+r"(cnt_loop),
                   [ptr_din] "+r"(din_ptr)
                 :
1550 1551 1552
                 : "cc",
                   "memory",
                   "v0",
1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572
                   "v1",
                   "v2",
                   "v3",
                   "v8",
                   "v9",
                   "v10",
                   "v11",
                   "v16",
                   "v17",
                   "v18",
                   "v19");
#else
    asm volatile(NCHWC4_TRANS_FP32_COMPUTE NCHWC4_TRANS_FP32_STORE
                 : [doutc0r0] "+r"(doutc0_ptr),
                   [doutc1r0] "+r"(doutc1_ptr),
                   [doutc2r0] "+r"(doutc2_ptr),
                   [doutc3r0] "+r"(doutc3_ptr),
                   [ptr_din] "+r"(din_ptr),
                   [cnt] "+r"(cnt_loop)
                 :
1573
                 : "cc", "memory", "q0", "q1", "q2", "q3", "q15");
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 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
#endif
  }
}
/*wirte result in outputs
* input din: [n, c / 4, h, w * 4], output dout: [n, c, h, w]
*/
inline bool write_to_output_c4_fp32(const float* din,
                                    float* dout,
                                    int cs,
                                    int ce,
                                    int hs,
                                    int he,
                                    int ws,
                                    int we,
                                    int channel,
                                    int height,
                                    int width,
                                    bool flag_relu,
                                    float* trash_ptr,
                                    operators::ActivationParam* act_param) {
  const int c4 = 4;
  const int w4 = 4;
  const int w_round = we - ws;
  const int ch_n = ce - cs;

  if (ch_n != 4) {
    LOG(ERROR) << "write_to_output_c4_fp32 ch_n must be equal 4 and hei_n is "
                  "more than zero";
    return false;
  }
  int size_c_out = width * height;

  float* doutc0r0 = dout + cs * size_c_out + hs * width + ws;
  float* doutc1r0 = doutc0r0 + size_c_out;
  float* doutc2r0 = doutc1r0 + size_c_out;
  float* doutc3r0 = doutc2r0 + size_c_out;

  const float* ptr_din = din;

  int size_h = (he > height ? height : he) - hs;  // size_h == hei_n

  int valid_we = we > width ? width : we;
  int cnt = (valid_we - ws) / w4;
  int remain = valid_we - ws - cnt * w4;

  for (int i = 0; i < size_h; i++) {
    int size_w = i * width;
    float* doutc0_ptr = doutc0r0 + size_w;  // doutc0r0 + width;
    float* doutc1_ptr = doutc1r0 + size_w;
    float* doutc2_ptr = doutc2r0 + size_w;
    float* doutc3_ptr = doutc3r0 + size_w;
    if (ce > channel) {
      switch (ce - channel) {
        case 3:
          doutc1_ptr = trash_ptr;
        case 2:
          doutc2_ptr = trash_ptr;
        case 1:
          doutc3_ptr = trash_ptr;
        default:
          break;
Y
Yan Chunwei 已提交
1635 1636
      }
    }
1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
    const float* din_hei_ptr = ptr_din + i * w_round * ch_n;
    if (cnt > 0) {
      int cnt_loop = cnt;
      act_switch_c4_fp32(din_hei_ptr,
                         doutc0_ptr,
                         doutc1_ptr,
                         doutc2_ptr,
                         doutc3_ptr,
                         cnt_loop,
                         act_param);
    }
T
TianXiaogang 已提交
1648
    if (remain > 0) {
Y
Yan Chunwei 已提交
1649 1650
      int offset = i * w_round * c4 + c4 * w4 * cnt;
      din_hei_ptr = ptr_din + offset;
1651 1652 1653 1654
      doutc0_ptr += w4 * cnt;
      doutc1_ptr += w4 * cnt;
      doutc2_ptr += w4 * cnt;
      doutc3_ptr += w4 * cnt;
T
TianXiaogang 已提交
1655
      int j = 0;
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712
      if (act_param != nullptr && act_param->has_active) {
        float six = act_param->Relu_clipped_coef;
        float scale = act_param->Leaky_relu_alpha;
        switch (act_param->active_type) {
          case lite_api::ActivationType::kRelu:
            for (; j < remain; ++j) {
              *(doutc0_ptr++) = LITEMAX(din_hei_ptr[0], 0.f);
              *(doutc1_ptr++) = LITEMAX(din_hei_ptr[1], 0.f);
              *(doutc2_ptr++) = LITEMAX(din_hei_ptr[2], 0.f);
              *(doutc3_ptr++) = LITEMAX(din_hei_ptr[3], 0.f);
              din_hei_ptr += 4;
            }
            break;
          case lite_api::ActivationType::kRelu6:
            /* 0 <= din <= 6 */
            for (; j < remain; ++j) {
              float tmp1 = LITEMAX(din_hei_ptr[0], 0.f);
              float tmp2 = LITEMAX(din_hei_ptr[1], 0.f);
              float tmp3 = LITEMAX(din_hei_ptr[2], 0.f);
              float tmp4 = LITEMAX(din_hei_ptr[3], 0.f);
              *(doutc0_ptr++) = LITEMIN(tmp1, six);
              *(doutc1_ptr++) = LITEMIN(tmp2, six);
              *(doutc2_ptr++) = LITEMIN(tmp3, six);
              *(doutc3_ptr++) = LITEMIN(tmp4, six);
              din_hei_ptr += 4;
            }
            break;
          case lite_api::ActivationType::kLeakyRelu:
            /*din = din >= 0 ? din : din * scale*/
            for (; j < remain; ++j) {
              if (din_hei_ptr[0] >= 0) {
                *(doutc0_ptr++) = din_hei_ptr[0];
              } else {
                *(doutc0_ptr++) = din_hei_ptr[0] * scale;
              }
              if (din_hei_ptr[1] >= 0) {
                *(doutc1_ptr++) = din_hei_ptr[1];
              } else {
                *(doutc1_ptr++) = din_hei_ptr[1] * scale;
              }
              if (din_hei_ptr[2] >= 0) {
                *(doutc2_ptr++) = din_hei_ptr[2];
              } else {
                *(doutc2_ptr++) = din_hei_ptr[2] * scale;
              }
              if (din_hei_ptr[3] >= 0) {
                *(doutc3_ptr++) = din_hei_ptr[3];
              } else {
                *(doutc3_ptr++) = din_hei_ptr[3] * scale;
              }
              din_hei_ptr += 4;
            }
            break;
          default:
            LOG(FATAL) << "this act_type: "
                       << static_cast<int>(act_param->active_type)
                       << " fuse not support";
Y
Yan Chunwei 已提交
1713 1714
        }
      } else {
T
TianXiaogang 已提交
1715
        for (; j < remain; ++j) {
Y
Yan Chunwei 已提交
1716 1717 1718 1719
          *(doutc0_ptr++) = din_hei_ptr[0];
          *(doutc1_ptr++) = din_hei_ptr[1];
          *(doutc2_ptr++) = din_hei_ptr[2];
          *(doutc3_ptr++) = din_hei_ptr[3];
1720
          din_hei_ptr += 4;
Y
Yan Chunwei 已提交
1721 1722 1723 1724 1725 1726
        }
      }
    }
  }
  return true;
}
1727
// clang-format off
H
HappyAngel 已提交
1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 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
#ifdef __aarch64__
#define NCHWC8_TRANS_FP32_COMPUTE                                    \
  "ldp q0, q1, [%[ptr_din]], #32  \n" /* load r00, r01 to q0, q1 */  \
  "ldp q2, q3, [%[ptr_din]], #32  \n" /* load r02, r03 to q2, q3 */  \
  "ldp q4, q5, [%[ptr_din]], #32  \n" /* load r00, r01 to q0, q1 */  \
  "ldp q6, q7, [%[ptr_din]], #32  \n" /* load r02, r03 to q2, q3 */  \
  "movi v20.4s, #0                \n" /* for relu */                 \
  "1:                             \n" /* main loop*/                 \
  "trn1   v8.4s, v0.4s, v2.4s     \n" /* trans q0, q1*/              \
  "trn2   v9.4s, v0.4s, v2.4s     \n" /* trans q0, q1*/              \
  "trn1   v10.4s, v1.4s, v3.4s    \n" /* trans q2, q3*/              \
  "trn2   v11.4s, v1.4s, v3.4s    \n" /* trans q2, q3*/              \
  "ldp q0, q1, [%[ptr_din]], #32  \n" /* load r00, r01 to q0, q1 */  \
                                                                     \
  "trn1   v12.4s, v4.4s, v6.4s    \n" /* trans q0, q1*/              \
  "trn2   v13.4s, v4.4s, v6.4s    \n" /* trans q0, q1*/              \
  "trn1   v14.4s, v5.4s, v7.4s    \n" /* trans q2, q3*/              \
  "trn2   v15.4s, v5.4s, v7.4s    \n" /* trans q2, q3*/              \
  "ldp q2, q3, [%[ptr_din]], #32  \n" /* load r02, r03 to q2, q3 */  \
                                                                     \
  "trn1   v16.2d, v8.2d, v12.2d   \n" /* trans q8, q10 00 01 02 03*/ \
  "trn2   v17.2d, v8.2d, v12.2d   \n" /* trans q8, q10 20 21 22 23*/ \
  "trn1   v18.2d, v9.2d, v13.2d   \n" /* trans q9, q11 10 11 12 13*/ \
  "trn2   v19.2d, v9.2d, v13.2d   \n" /* trans q9, q11 30 31 32 33*/ \
  "ldp q4, q5, [%[ptr_din]], #32  \n" /* load r00, r01 to q0, q1 */  \
                                                                     \
  "trn1   v8.2d, v10.2d, v14.2d   \n" /* trans q8, q10 40 41 42 43*/ \
  "trn2   v9.2d, v10.2d, v14.2d   \n" /* trans q8, q10 60 61 62 63*/ \
  "trn1   v12.2d, v11.2d, v15.2d  \n" /* trans q9, q11 50 51 52 53*/ \
  "trn2   v13.2d, v11.2d, v15.2d  \n" /* trans q9, q11 70 71 72 73*/ \
  "ldp q6, q7, [%[ptr_din]], #32  \n" /* load r02, r03 to q2, q3 */

#define NCHWC8_TRANS_FP32_RELU                 \
  "fmax   v16.4s, v16.4s, v20.4s  \n" /*relu*/ \
  "fmax   v17.4s, v17.4s, v20.4s  \n" /*relu*/ \
  "fmax   v18.4s, v18.4s, v20.4s  \n" /*relu*/ \
  "fmax   v19.4s, v19.4s, v20.4s  \n" /*relu*/ \
                                               \
  "fmax   v8.4s,  v8.4s,  v20.4s  \n" /*relu*/ \
  "fmax   v9.4s,  v9.4s,  v20.4s  \n" /*relu*/ \
  "fmax   v12.4s, v12.4s, v20.4s  \n" /*relu*/ \
  "fmax   v13.4s, v13.4s, v20.4s  \n" /*relu*/

1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782
#define NCHWC8_TRANS_FP32_RELU6                    \
  "fmin   v16.4s, v16.4s, %[six].4s  \n" /*relu6*/ \
  "fmin   v17.4s, v17.4s, %[six].4s  \n" /*relu6*/ \
  "fmin   v18.4s, v18.4s, %[six].4s  \n" /*relu6*/ \
  "fmin   v19.4s, v19.4s, %[six].4s  \n" /*relu6*/ \
                                                   \
  "fmin   v8.4s,  v8.4s,  %[six].4s  \n" /*relu6*/ \
  "fmin   v9.4s,  v9.4s,  %[six].4s  \n" /*relu6*/ \
  "fmin   v12.4s, v12.4s, %[six].4s  \n" /*relu6*/ \
  "fmin   v13.4s, v13.4s, %[six].4s  \n" /*relu6*/

#define NCHWC8_TRANS_FP32_LEAKY_RELU                \
1783 1784 1785 1786
  "fcmge v10.4s, v16.4s, v20.4s \n" /* vcgeq_u32 */ \
  "fcmge v11.4s, v17.4s, v20.4s \n" /* vcgeq_u32 */ \
  "fcmge v14.4s, v18.4s, v20.4s \n" /* vcgeq_u32 */ \
  "fcmge v15.4s, v19.4s, v20.4s \n" /* vcgeq_u32 */ \
1787
                                                    \
1788 1789 1790 1791
  "fcmge v21.4s, v8.4s, v20.4s \n"  /* vcgeq_u32 */ \
  "fcmge v22.4s, v9.4s, v20.4s \n"  /* vcgeq_u32 */ \
  "fcmge v23.4s, v12.4s, v20.4s \n" /* vcgeq_u32 */ \
  "fcmge v24.4s, v13.4s, v20.4s \n" /* vcgeq_u32 */ \
1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812
                                                    \
  "fmul v25.4s, v16.4s, %[scale].4s \n" /* mul */   \
  "fmul v26.4s, v17.4s, %[scale].4s \n" /* mul */   \
  "fmul v27.4s, v18.4s, %[scale].4s \n" /* mul */   \
  "fmul v28.4s, v19.4s, %[scale].4s \n" /* mul */   \
                                                    \
  "fmul v29.4s, v8.4s, %[scale].4s \n"  /* mul */   \
  "fmul v30.4s, v9.4s, %[scale].4s \n"  /* mul */   \
  "fmul v31.4s, v12.4s, %[scale].4s \n" /* mul */   \
                                                    \
  "bif  v16.16b, v25.16b, v10.16b \n"   /* choose*/ \
  "bif  v17.16b, v26.16b, v11.16b \n"   /* choose*/ \
  "bif  v18.16b, v27.16b, v14.16b \n"   /* choose*/ \
  "bif  v19.16b, v28.16b, v15.16b \n"   /* choose*/ \
  "fmul v25.4s, v13.4s, %[scale].4s \n" /* mul */   \
                                                    \
  "bif  v8.16b, v29.16b, v21.16b \n"  /* choose*/   \
  "bif  v9.16b, v30.16b, v22.16b \n"  /* choose*/   \
  "bif  v12.16b, v31.16b, v23.16b \n" /* choose*/   \
  "bif  v13.16b, v25.16b, v24.16b \n" /* choose*/

H
HappyAngel 已提交
1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825
#define NCHWC8_TRANS_FP32_STORE                          \
  "str    q16, [%[doutc0r0]], #16 \n" /* store c0r0*/    \
  "str    q17, [%[doutc2r0]], #16 \n" /* store c2r0*/    \
  "str    q18, [%[doutc1r0]], #16 \n" /* store c1r0*/    \
  "str    q19, [%[doutc3r0]], #16 \n" /* store c3r0*/    \
                                                         \
  "subs   %w[cnt], %w[cnt], #1    \n" /* loop count -1*/ \
  "str    q8,  [%[doutc4r0]], #16 \n" /* store c0r0*/    \
  "str    q9,  [%[doutc6r0]], #16 \n" /* store c2r0*/    \
  "str    q12, [%[doutc5r0]], #16 \n" /* store c1r0*/    \
  "str    q13, [%[doutc7r0]], #16 \n" /* store c3r0*/    \
                                                         \
  "bne    1b                      \n" /* jump to main loop*/
1826

H
HappyAngel 已提交
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
#else
#define NCHWC8_TRANS_FP32_COMPUTE                           \
  "vld1.32 {d0-d3}, [%[ptr_din]]!        @load data \n"     \
  "vld1.32 {d4-d7}, [%[ptr_din]]!        @load data \n"     \
  "vld1.32 {d8-d11}, [%[ptr_din]]!       @load data \n"     \
  "vld1.32 {d12-d15}, [%[ptr_din]]!      @load data \n"     \
  "vmov.u32 q15, #0                      @ dump zero\n"     \
  "1:                                    @ main loop\n"     \
  "vtrn.32   q0, q2                      @ trans q0, q2 \n" \
  "vtrn.32   q4, q6                      @ trans q4, q6 \n" \
  "vswp.32   d1, d8                      @ swap  d1, d8 \n" \
  "vswp.32   d5, d12                     @ swap  d5, d12\n" \
                                                            \
  "vtrn.32   q1, q3                      @ trans q1, q3 \n" \
  "vtrn.32   q5, q7                      @ trans q5, q7 \n" \
  "vswp.32   d3, d10                     @ swap  d3, d10\n" \
  "vswp.32   d7, d14                     @ swap  d7, d14\n"

#define NCHWC8_TRANS_FP32_RELU                     \
  "vmax.f32  q0, q0, q15                 @ relu\n" \
  "vmax.f32  q1, q1, q15                 @ relu\n" \
  "vmax.f32  q2, q2, q15                 @ relu\n" \
  "vmax.f32  q3, q3, q15                 @ relu\n" \
                                                   \
  "vmax.f32  q4, q4, q15                 @ relu\n" \
  "vmax.f32  q5, q5, q15                 @ relu\n" \
  "vmax.f32  q6, q6, q15                 @ relu\n" \
  "vmax.f32  q7, q7, q15                 @ relu\n"

1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867
#define NCHWC8_TRANS_FP32_RELU6                         \
  "vmin.f32  q0, q0, %q[six]                 @ relu6\n" \
  "vmin.f32  q1, q1, %q[six]                 @ relu6\n" \
  "vmin.f32  q2, q2, %q[six]                 @ relu6\n" \
  "vmin.f32  q3, q3, %q[six]                 @ relu6\n" \
                                                        \
  "vmin.f32  q4, q4, %q[six]                 @ relu6\n" \
  "vmin.f32  q5, q5, %q[six]                 @ relu6\n" \
  "vmin.f32  q6, q6, %q[six]                 @ relu6\n" \
  "vmin.f32  q7, q7, %q[six]                 @ relu6\n"

#define NCHWC8_TRANS_FP32_LEAKY_RELU           \
1868
  "vcge.f32   q9, q0,  q15        @ q0 > 0 \n" \
1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897
  "vcge.f32   q10, q1, q15        @ q0 > 0 \n" \
  "vcge.f32   q11, q2, q15        @ q0 > 0 \n" \
  "vcge.f32   q12, q3, q15        @ q0 > 0 \n" \
  "vmul.f32 q13, q0, %q[scale] \n"             \
  "vmul.f32 q14, q1, %q[scale] \n"             \
  "vmul.f32 q15, q2, %q[scale] \n"             \
                                               \
  "vbif q0, q13, q9 @ choose \n"               \
  "vmul.f32 q9, q3, %q[scale] \n"              \
                                               \
  "vbif q1, q14, q10 @ choose \n"              \
  "vbif q2, q15, q11 @ choose \n"              \
  "vbif q3, q9, q12 @ choose \n"               \
                                               \
  "vcge.f32   q9, q4, q15        @ q0 > 0 \n"  \
  "vcge.f32   q10, q5, q15        @ q0 > 0 \n" \
  "vcge.f32   q11, q6, q15        @ q0 > 0 \n" \
  "vcge.f32   q12, q7, q15        @ q0 > 0 \n" \
  "vmul.f32 q13, q4, %q[scale] \n"             \
  "vmul.f32 q14, q5, %q[scale] \n"             \
  "vmul.f32 q15, q6, %q[scale] \n"             \
                                               \
  "vbif q4, q13, q9 @ choose \n"               \
  "vmul.f32 q9, q7, %q[scale] \n"              \
                                               \
  "vbif q5, q14, q10 @ choose \n"              \
  "vbif q6, q15, q11 @ choose \n"              \
  "vbif q7, q9, q12 @ choose \n"

H
HappyAngel 已提交
1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926
#define NCHWC8_TRANS_FP32_STORE                                \
  "subs   %[cnt], %[cnt], #1             @ loop count - 1\n"   \
  "vst1.32   {d0-d1}, [%[doutc0r0]]!     @ store result, add " \
  "pointer\n"                                                  \
  "vst1.32   {d2-d3}, [%[doutc4r0]]!     @ store result, add " \
  "pointer\n"                                                  \
  "vst1.32   {d4-d5}, [%[doutc1r0]]!     @ store result, add " \
  "pointer\n"                                                  \
  "vst1.32   {d6-d7}, [%[doutc5r0]]!     @ store result, add " \
  "pointer\n"                                                  \
                                                               \
  "vld1.32   {d0-d3}, [%[ptr_din]]!      @load data \n"        \
  "vld1.32   {d4-d7}, [%[ptr_din]]!      @load data \n"        \
                                                               \
  "vst1.32   {d8-d9},   [%[doutc2r0]]!   @ store result, add " \
  "pointer\n"                                                  \
  "vst1.32   {d10-d11}, [%[doutc6r0]]!   @ store result, add " \
  "pointer\n"                                                  \
  "vst1.32   {d12-d13}, [%[doutc3r0]]!   @ store result, add " \
  "pointer\n"                                                  \
  "vst1.32   {d14-d15}, [%[doutc7r0]]!   @ store result, add " \
  "pointer\n"                                                  \
                                                               \
  "vld1.32 {d8-d11}, [%[ptr_din]]!       @load data \n"        \
  "vld1.32 {d12-d15}, [%[ptr_din]]!      @load data \n"        \
                                                               \
  "bne    1b                             @ jump to main loop\n"

#endif
1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943
// clang-format on
inline void act_switch_c8_fp32(const float* din_ptr,
                               float* doutc0_ptr,
                               float* doutc1_ptr,
                               float* doutc2_ptr,
                               float* doutc3_ptr,
                               float* doutc4_ptr,
                               float* doutc5_ptr,
                               float* doutc6_ptr,
                               float* doutc7_ptr,
                               int cnt_loop,
                               const operators::ActivationParam* act_param) {
  if (act_param != nullptr && act_param->has_active) {
    float32x4_t six = vdupq_n_f32(act_param->Relu_clipped_coef);
    float32x4_t scale = vdupq_n_f32(act_param->Leaky_relu_alpha);
    switch (act_param->active_type) {
      case lite_api::ActivationType::kRelu:
Y
Yan Chunwei 已提交
1944
#ifdef __aarch64__
H
HappyAngel 已提交
1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955
        asm volatile(NCHWC8_TRANS_FP32_COMPUTE NCHWC8_TRANS_FP32_RELU
                         NCHWC8_TRANS_FP32_STORE
                     : [doutc0r0] "+r"(doutc0_ptr),
                       [doutc1r0] "+r"(doutc1_ptr),
                       [doutc2r0] "+r"(doutc2_ptr),
                       [doutc3r0] "+r"(doutc3_ptr),
                       [doutc4r0] "+r"(doutc4_ptr),
                       [doutc5r0] "+r"(doutc5_ptr),
                       [doutc6r0] "+r"(doutc6_ptr),
                       [doutc7r0] "+r"(doutc7_ptr),
                       [cnt] "+r"(cnt_loop),
1956
                       [ptr_din] "+r"(din_ptr)
H
HappyAngel 已提交
1957
                     :
1958 1959 1960
                     : "cc",
                       "memory",
                       "v0",
1961
                       "v1",
H
HappyAngel 已提交
1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979
                       "v2",
                       "v3",
                       "v4",
                       "v5",
                       "v6",
                       "v7",
                       "v8",
                       "v9",
                       "v10",
                       "v11",
                       "v12",
                       "v13",
                       "v14",
                       "v16",
                       "v17",
                       "v18",
                       "v19",
                       "v20");
Y
Yan Chunwei 已提交
1980
#else
H
HappyAngel 已提交
1981 1982 1983 1984 1985 1986 1987 1988 1989 1990
        asm volatile(NCHWC8_TRANS_FP32_COMPUTE NCHWC8_TRANS_FP32_RELU
                         NCHWC8_TRANS_FP32_STORE
                     : [doutc0r0] "+r"(doutc0_ptr),
                       [doutc1r0] "+r"(doutc1_ptr),
                       [doutc2r0] "+r"(doutc2_ptr),
                       [doutc3r0] "+r"(doutc3_ptr),
                       [doutc4r0] "+r"(doutc4_ptr),
                       [doutc5r0] "+r"(doutc5_ptr),
                       [doutc6r0] "+r"(doutc6_ptr),
                       [doutc7r0] "+r"(doutc7_ptr),
1991
                       [ptr_din] "+r"(din_ptr),
H
HappyAngel 已提交
1992 1993
                       [cnt] "+r"(cnt_loop)
                     :
1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004
                     : "cc",
                       "memory",
                       "q0",
                       "q1",
                       "q2",
                       "q3",
                       "q4",
                       "q5",
                       "q6",
                       "q7",
                       "q15");
Y
Yan Chunwei 已提交
2005
#endif
2006 2007 2008
        break;
      case lite_api::ActivationType::kRelu6:
/* 0 <= din <= 6 */
Y
Yan Chunwei 已提交
2009
#ifdef __aarch64__
2010 2011
        asm volatile(NCHWC8_TRANS_FP32_COMPUTE NCHWC8_TRANS_FP32_RELU6
                         NCHWC8_TRANS_FP32_STORE
H
HappyAngel 已提交
2012 2013 2014 2015 2016 2017 2018 2019 2020
                     : [doutc0r0] "+r"(doutc0_ptr),
                       [doutc1r0] "+r"(doutc1_ptr),
                       [doutc2r0] "+r"(doutc2_ptr),
                       [doutc3r0] "+r"(doutc3_ptr),
                       [doutc4r0] "+r"(doutc4_ptr),
                       [doutc5r0] "+r"(doutc5_ptr),
                       [doutc6r0] "+r"(doutc6_ptr),
                       [doutc7r0] "+r"(doutc7_ptr),
                       [cnt] "+r"(cnt_loop),
2021 2022
                       [ptr_din] "+r"(din_ptr)
                     : [six] "w"(six)
2023 2024 2025
                     : "cc",
                       "memory",
                       "v0",
H
HappyAngel 已提交
2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044
                       "v1",
                       "v2",
                       "v3",
                       "v4",
                       "v5",
                       "v6",
                       "v7",
                       "v8",
                       "v9",
                       "v10",
                       "v11",
                       "v12",
                       "v13",
                       "v14",
                       "v16",
                       "v17",
                       "v18",
                       "v19",
                       "v20");
Y
Yan Chunwei 已提交
2045
#else
2046 2047 2048 2049 2050 2051 2052 2053 2054
        asm volatile(NCHWC4_TRANS_FP32_COMPUTE NCHWC4_TRANS_FP32_RELU
                         NCHWC4_TRANS_FP32_RELU6 NCHWC4_TRANS_FP32_STORE
                     : [doutc0r0] "+r"(doutc0_ptr),
                       [doutc1r0] "+r"(doutc1_ptr),
                       [doutc2r0] "+r"(doutc2_ptr),
                       [doutc3r0] "+r"(doutc3_ptr),
                       [ptr_din] "+r"(din_ptr),
                       [cnt] "+r"(cnt_loop)
                     : [six] "w"(six)
2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065
                     : "cc",
                       "memory",
                       "q0",
                       "q1",
                       "q2",
                       "q3",
                       "q4",
                       "q5",
                       "q6",
                       "q7",
                       "q15");
2066 2067 2068 2069 2070 2071 2072
#endif
        break;
      case lite_api::ActivationType::kLeakyRelu:
/*din = din >= 0 ? din : din * scale*/
#ifdef __aarch64__
        asm volatile(NCHWC8_TRANS_FP32_COMPUTE NCHWC8_TRANS_FP32_LEAKY_RELU
                         NCHWC8_TRANS_FP32_STORE
H
HappyAngel 已提交
2073 2074 2075 2076 2077 2078 2079 2080
                     : [doutc0r0] "+r"(doutc0_ptr),
                       [doutc1r0] "+r"(doutc1_ptr),
                       [doutc2r0] "+r"(doutc2_ptr),
                       [doutc3r0] "+r"(doutc3_ptr),
                       [doutc4r0] "+r"(doutc4_ptr),
                       [doutc5r0] "+r"(doutc5_ptr),
                       [doutc6r0] "+r"(doutc6_ptr),
                       [doutc7r0] "+r"(doutc7_ptr),
2081 2082 2083
                       [cnt] "+r"(cnt_loop),
                       [ptr_din] "+r"(din_ptr)
                     : [scale] "w"(scale)
2084 2085 2086
                     : "cc",
                       "memory",
                       "v0",
2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128
                       "v1",
                       "v2",
                       "v3",
                       "v4",
                       "v5",
                       "v6",
                       "v7",
                       "v8",
                       "v9",
                       "v10",
                       "v11",
                       "v12",
                       "v13",
                       "v14",
                       "v16",
                       "v17",
                       "v18",
                       "v19",
                       "v20",
                       "v21",
                       "v22",
                       "v23",
                       "v24",
                       "v25",
                       "v26",
                       "v27",
                       "v28",
                       "v29",
                       "v30",
                       "v31");
#else
        asm volatile(NCHWC8_TRANS_FP32_COMPUTE NCHWC8_TRANS_FP32_LEAKY_RELU
                         NCHWC8_TRANS_FP32_STORE
                     : [doutc0r0] "+r"(doutc0_ptr),
                       [doutc1r0] "+r"(doutc1_ptr),
                       [doutc2r0] "+r"(doutc2_ptr),
                       [doutc3r0] "+r"(doutc3_ptr),
                       [doutc4r0] "+r"(doutc4_ptr),
                       [doutc5r0] "+r"(doutc5_ptr),
                       [doutc6r0] "+r"(doutc6_ptr),
                       [doutc7r0] "+r"(doutc7_ptr),
                       [ptr_din] "+r"(din_ptr),
H
HappyAngel 已提交
2129
                       [cnt] "+r"(cnt_loop)
2130
                     : [scale] "w"(scale)
2131 2132 2133
                     : "cc",
                       "memory",
                       "q0",
2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147
                       "q1",
                       "q2",
                       "q3",
                       "q4",
                       "q5",
                       "q6",
                       "q7",
                       "q9",
                       "q10",
                       "q11",
                       "q12",
                       "q13",
                       "q14",
                       "q15");
Y
Yan Chunwei 已提交
2148
#endif
2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168
        break;
      default:
        LOG(FATAL) << "this act_type: "
                   << static_cast<int>(act_param->active_type)
                   << " fuse not support";
    }
  } else {
#ifdef __aarch64__
    asm volatile(NCHWC8_TRANS_FP32_COMPUTE NCHWC8_TRANS_FP32_STORE
                 : [doutc0r0] "+r"(doutc0_ptr),
                   [doutc1r0] "+r"(doutc1_ptr),
                   [doutc2r0] "+r"(doutc2_ptr),
                   [doutc3r0] "+r"(doutc3_ptr),
                   [doutc4r0] "+r"(doutc4_ptr),
                   [doutc5r0] "+r"(doutc5_ptr),
                   [doutc6r0] "+r"(doutc6_ptr),
                   [doutc7r0] "+r"(doutc7_ptr),
                   [cnt] "+r"(cnt_loop),
                   [ptr_din] "+r"(din_ptr)
                 :
2169 2170 2171
                 : "cc",
                   "memory",
                   "v0",
2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204
                   "v1",
                   "v2",
                   "v3",
                   "v4",
                   "v5",
                   "v6",
                   "v7",
                   "v8",
                   "v9",
                   "v10",
                   "v11",
                   "v12",
                   "v13",
                   "v14",
                   "v15",
                   "v16",
                   "v17",
                   "v18",
                   "v19",
                   "v20");
#else
    asm volatile(NCHWC8_TRANS_FP32_COMPUTE NCHWC8_TRANS_FP32_STORE
                 : [doutc0r0] "+r"(doutc0_ptr),
                   [doutc1r0] "+r"(doutc1_ptr),
                   [doutc2r0] "+r"(doutc2_ptr),
                   [doutc3r0] "+r"(doutc3_ptr),
                   [doutc4r0] "+r"(doutc4_ptr),
                   [doutc5r0] "+r"(doutc5_ptr),
                   [doutc6r0] "+r"(doutc6_ptr),
                   [doutc7r0] "+r"(doutc7_ptr),
                   [ptr_din] "+r"(din_ptr),
                   [cnt] "+r"(cnt_loop)
                 :
2205
                 : "cc",
2206
                   "memory",
2207 2208 2209 2210 2211 2212 2213 2214 2215
                   "q0",
                   "q1",
                   "q2",
                   "q3",
                   "q4",
                   "q5",
                   "q6",
                   "q7",
                   "q15");
2216 2217 2218 2219
#endif
  }
}

2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236
#ifdef __aarch64__
#define LOAD_DATA                                               \
  "1:                               \n"                         \
  "ld1 {v0.4s}, [%[din_ptr]], #16   \n" /*vld1q_f32(din_ptr0)*/ \
  "ld1 {v1.4s}, [%[din_ptr]], #16   \n" /*vld1q_f32(din_ptr0)*/ \
  "ld1 {v2.4s}, [%[din_ptr]], #16   \n" /*vld1q_f32(din_ptr0)*/ \
  "ld1 {v3.4s}, [%[din_ptr]], #16   \n" /*vld1q_f32(din_ptr0)*/
#define DO_RELU                                           \
  "fmax v0.4s, v0.4s, %[vzero].4s   \n" /* vmaxq_f32() */ \
  "fmax v1.4s, v1.4s, %[vzero].4s   \n" /* vmaxq_f32() */ \
  "fmax v2.4s, v2.4s, %[vzero].4s   \n" /* vmaxq_f32() */ \
  "fmax v3.4s, v3.4s, %[vzero].4s   \n" /* vmaxq_f32() */
#define DO_RELU6                                         \
  "fmin v0.4s, v0.4s, %[vsix].4s   \n" /* vmaxq_f32() */ \
  "fmin v1.4s, v1.4s, %[vsix].4s   \n" /* vmaxq_f32() */ \
  "fmin v2.4s, v2.4s, %[vsix].4s   \n" /* vmaxq_f32() */ \
  "fmin v3.4s, v3.4s, %[vsix].4s   \n" /* vmaxq_f32() */
2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249
#define DO_LEAKY_RELU                                    \
  "fcmge v4.4s, v0.4s,  %[vzero].4s  \n" /* vcgeq_f32 */ \
  "fmul v5.4s, v0.4s, %[vscale].4s   \n" /* vmulq_f32 */ \
  "fcmge v6.4s, v1.4s,  %[vzero].4s  \n" /* vcgeq_f32 */ \
  "fmul v7.4s, v1.4s, %[vscale].4s   \n" /* vmulq_f32 */ \
  "fcmge v8.4s, v2.4s,  %[vzero].4s  \n" /* vcgeq_f32 */ \
  "fmul v9.4s, v2.4s, %[vscale].4s   \n" /* vmulq_f32 */ \
  "fcmge v10.4s, v3.4s,  %[vzero].4s \n" /* vcgeq_f32 */ \
  "fmul v11.4s, v3.4s, %[vscale].4s  \n" /* vmulq_f32 */ \
  "bif v0.16b, v5.16b, v4.16b        \n" /* choose*/     \
  "bif v1.16b, v7.16b, v6.16b        \n" /* choose*/     \
  "bif v2.16b, v9.16b, v8.16b        \n" /* choose*/     \
  "bif v3.16b, v11.16b, v10.16b      \n" /* choose*/
2250 2251 2252 2253 2254 2255 2256 2257 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 2285
#define DO_STORE                                         \
  "subs %w[cnt], %w[cnt], #1                    \n"      \
  "st1 {v0.4s}, [%[dout_ptr]], #16 \n" /* vst1q_f32() */ \
  "st1 {v1.4s}, [%[dout_ptr]], #16 \n" /* vst1q_f32() */ \
  "st1 {v2.4s}, [%[dout_ptr]], #16 \n" /* vst1q_f32() */ \
  "st1 {v3.4s}, [%[dout_ptr]], #16 \n" /* vst1q_f32() */ \
  "bne  1b                                    \n"
#else
#define LOAD_DATA                                            \
  "1:                               \n"                      \
  "vld1.32 {d6-d7}, [%[din_ptr]]!   @ vld1q_f32(din_ptr) \n" \
  "vld1.32 {d8-d9}, [%[din_ptr]]!   @ vld1q_f32(din_ptr) \n" \
  "vld1.32 {d10-d11}, [%[din_ptr]]! @ vld1q_f32(din_ptr) \n" \
  "vld1.32 {d12-d13}, [%[din_ptr]]! @ vld1q_f32(din_ptr) \n"
#define DO_RELU                                 \
  "vmax.f32 q3, q3, %q[vzero] @ vmaxq_f32() \n" \
  "vmax.f32 q4, q4, %q[vzero] @ vmaxq_f32() \n" \
  "vmax.f32 q5, q5, %q[vzero] @ vmaxq_f32() \n" \
  "vmax.f32 q6, q6, %q[vzero] @ vmaxq_f32() \n"
#define DO_RELU6                               \
  "vmin.f32 q3, q3, %q[vsix] @ vminq_f32() \n" \
  "vmin.f32 q4, q4, %q[vsix] @ vmaxq_f32() \n" \
  "vmin.f32 q5, q5, %q[vsix] @ vmaxq_f32() \n" \
  "vmin.f32 q6, q6, %q[vsix] @ vmaxq_f32() \n"
#define DO_LEAKY_RELU                            \
  "vcge.f32 q7, q3, %q[vzero]   @ vcgeq_u32 \n"  \
  "vmul.f32 q8, q3, %q[vscale]  @ vmulq_f32 \n"  \
  "vcge.f32 q9, q4, %q[vzero]   @ vcgeq_u32 \n"  \
  "vmul.f32 q10, q4, %q[vscale]  @ vmulq_f32 \n" \
  "vcge.f32 q11, q5, %q[vzero]   @ vcgeq_u32 \n" \
  "vmul.f32 q12, q5, %q[vscale]  @ vmulq_f32 \n" \
  "vcge.f32 q13, q6, %q[vzero]   @ vcgeq_u32 \n" \
  "vmul.f32 q14, q6, %q[vscale]  @ vmulq_f32 \n" \
  "vbif q3, q8, q7               @ choose \n"    \
  "vbif q4, q10, q9              @ choose \n"    \
  "vbif q5, q12, q11             @ choose \n"    \
2286
  "vbif q6, q14, q13             @ choose \n"
2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305
#define DO_STORE                                            \
  "subs %[cnt], #1                                \n"       \
  "vst1.32 {d6-d7}, [%[dout_ptr]]!       @ vst1q_f32()  \n" \
  "vst1.32 {d8-d9}, [%[dout_ptr]]!       @ vst1q_f32()  \n" \
  "vst1.32 {d10-d11}, [%[dout_ptr]]!     @ vst1q_f32()  \n" \
  "vst1.32 {d12-d13}, [%[dout_ptr]]!     @ vst1q_f32()  \n" \
  "bne  1b                                    \n"
#endif
/*
* Data do activation process
* Now support relu relu6 leakyrelu act
*/
inline void act_switch_process(float* src,
                               float* dst,
                               int size,
                               const operators::ActivationParam* act_param) {
  int cnt = size >> 4;
  int remain = size % 16;
  float32x4_t vzero = vdupq_n_f32(0.f);
2306
  if (act_param != nullptr) {
2307 2308 2309 2310 2311 2312 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 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395
    float32x4_t vsix = vdupq_n_f32(act_param->Relu_clipped_coef);
    float32x4_t vscale = vdupq_n_f32(act_param->Leaky_relu_alpha);
    if (cnt > 0) {
      switch (act_param->active_type) {
        case lite_api::ActivationType::kRelu:
#ifdef __aarch64__
          asm volatile(
              LOAD_DATA DO_RELU DO_STORE
              : [din_ptr] "+r"(src), [dout_ptr] "+r"(dst), [cnt] "+r"(cnt)
              : [vzero] "w"(vzero)
              : "memory", "cc", "v0", "v1", "v2", "v3");
#else
          asm volatile(
              LOAD_DATA DO_RELU DO_STORE
              : [din_ptr] "+r"(src), [dout_ptr] "+r"(dst), [cnt] "+r"(cnt)
              : [vzero] "w"(vzero)
              : "memory", "cc", "q3", "q4", "q5", "q6");
#endif
          break;
        case lite_api::ActivationType::kRelu6:
#ifdef __aarch64__
          asm volatile(
              LOAD_DATA DO_RELU DO_RELU6 DO_STORE
              : [din_ptr] "+r"(src), [dout_ptr] "+r"(dst), [cnt] "+r"(cnt)
              : [vzero] "w"(vzero), [vsix] "w"(vsix)
              : "memory", "cc", "v0", "v1", "v2", "v3");
#else
          asm volatile(
              LOAD_DATA DO_RELU DO_RELU6 DO_STORE
              : [din_ptr] "+r"(src), [dout_ptr] "+r"(dst), [cnt] "+r"(cnt)
              : [vzero] "w"(vzero), [vsix] "w"(vsix)
              : "memory", "cc", "q3", "q4", "q5", "q6");
#endif
          break;
        case lite_api::ActivationType::kLeakyRelu:
#ifdef __aarch64__
          asm volatile(
              LOAD_DATA DO_LEAKY_RELU DO_STORE
              : [din_ptr] "+r"(src), [dout_ptr] "+r"(dst), [cnt] "+r"(cnt)
              : [vzero] "w"(vzero), [vscale] "w"(vscale)
              : "memory",
                "cc",
                "v0",
                "v1",
                "v2",
                "v3",
                "v4",
                "v5",
                "v6",
                "v7",
                "v8",
                "v9",
                "v10",
                "v11");
#else
          asm volatile(
              LOAD_DATA DO_LEAKY_RELU DO_STORE
              : [din_ptr] "+r"(src), [dout_ptr] "+r"(dst), [cnt] "+r"(cnt)
              : [vzero] "w"(vzero), [vscale] "w"(vscale)
              : "memory",
                "cc",
                "q3",
                "q4",
                "q5",
                "q6",
                "q7",
                "q8",
                "q9",
                "q10",
                "q11",
                "q12",
                "q13",
                "q14");
#endif
          break;
        default:
          LOG(FATAL) << "this act_type: "
                     << static_cast<int>(act_param->active_type)
                     << " fuse not support";
      }
    }
    // remain
    switch (act_param->active_type) {
      case lite_api::ActivationType::kRelu:
        for (int i = 0; i < remain; i++) {
          *dst = *src >= 0.f ? *src : 0.f;
          src++;
          dst++;
        }
2396
        break;
2397 2398 2399 2400 2401 2402 2403 2404 2405
      case lite_api::ActivationType::kRelu6:
        for (int i = 0; i < remain; i++) {
          float tmp = *src >= 0.f ? *src : 0.f;
          *dst = tmp <= act_param->Relu_clipped_coef
                     ? tmp
                     : act_param->Relu_clipped_coef;
          src++;
          dst++;
        }
2406
        break;
2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425
      case lite_api::ActivationType::kLeakyRelu:
        for (int i = 0; i < remain; i++) {
          if (*src >= 0.f) {
            *dst = *src;
          } else {
            *dst = *src * act_param->Leaky_relu_alpha;
          }
          src++;
          dst++;
        }
        break;
      default:
        LOG(FATAL) << "this act_type: "
                   << static_cast<int>(act_param->active_type)
                   << " fuse not support";
    }
  }
}

2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 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
/*wirte result in outputs
* input din: [n, c / 8, h, w * 8], output dout: [n, c, h, w]
*/
inline bool write_to_output_c8_fp32(const float* din,
                                    float* dout,
                                    int ch_n,
                                    int hei_n,
                                    int cs,
                                    int ce,
                                    int hs,
                                    int he,
                                    int ws,
                                    int we,
                                    int channel,
                                    int height,
                                    int width,
                                    bool flag_relu,
                                    float* trash_ptr,
                                    operators::ActivationParam* act_param) {
  if (ch_n != 8 || hei_n <= 0) {
    LOG(ERROR) << "ch_n must be equal 8 and hei_n is more than zero";
    return false;
  }
  int size_c_out = width * height;

  float* doutc0r0 = dout + cs * size_c_out + hs * width + ws;
  float* doutc1r0 = doutc0r0 + size_c_out;
  float* doutc2r0 = doutc1r0 + size_c_out;
  float* doutc3r0 = doutc2r0 + size_c_out;
  float* doutc4r0 = doutc3r0 + size_c_out;
  float* doutc5r0 = doutc4r0 + size_c_out;
  float* doutc6r0 = doutc5r0 + size_c_out;
  float* doutc7r0 = doutc6r0 + size_c_out;

  const float* ptr_din = din;

  int size_h = (he > height ? height : he) - hs;  // size_h == hei_n

  int valid_w = we - ws;
  int w4 = 4;
  int cnt = valid_w / 4;

  if (we > width) {
    cnt--;
  }
  for (int i = 0; i < size_h; i++) {
    int size_w = i * width;
    float* doutc0_ptr = doutc0r0 + size_w;  // doutc0r0 + width;
    float* doutc1_ptr = doutc1r0 + size_w;
    float* doutc2_ptr = doutc2r0 + size_w;
    float* doutc3_ptr = doutc3r0 + size_w;
    float* doutc4_ptr = doutc4r0 + size_w;
    float* doutc5_ptr = doutc5r0 + size_w;
    float* doutc6_ptr = doutc6r0 + size_w;
    float* doutc7_ptr = doutc7r0 + size_w;
    if (ce > channel) {
      switch (ce - channel) {
        case 7:
          doutc1_ptr = trash_ptr;
        case 6:
          doutc2_ptr = trash_ptr;
        case 5:
          doutc3_ptr = trash_ptr;
        case 4:
          doutc4_ptr = trash_ptr;
        case 3:
          doutc5_ptr = trash_ptr;
        case 2:
          doutc6_ptr = trash_ptr;
        case 1:
          doutc7_ptr = trash_ptr;
        default:
          break;
Y
Yan Chunwei 已提交
2499
      }
2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 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 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619
    }
    ptr_din = din + i * valid_w * ch_n;
    const float* din_hei_ptr = ptr_din;
    if (cnt > 0) {
      int cnt_loop = cnt;
      act_switch_c8_fp32(din_hei_ptr,
                         doutc0_ptr,
                         doutc1_ptr,
                         doutc2_ptr,
                         doutc3_ptr,
                         doutc4_ptr,
                         doutc5_ptr,
                         doutc6_ptr,
                         doutc7_ptr,
                         cnt_loop,
                         act_param);
    }
    if (we > width) {
      int offset = 32 * (valid_w / 4 - 1);
      din_hei_ptr = ptr_din + offset;
      doutc0_ptr += w4 * cnt;
      doutc1_ptr += w4 * cnt;
      doutc2_ptr += w4 * cnt;
      doutc3_ptr += w4 * cnt;
      doutc4_ptr += w4 * cnt;
      doutc5_ptr += w4 * cnt;
      doutc6_ptr += w4 * cnt;
      doutc7_ptr += w4 * cnt;
      int i = we - 4;
      if (act_param != nullptr && act_param->has_active) {
        float six = act_param->Relu_clipped_coef;
        float scale = act_param->Leaky_relu_alpha;
        switch (act_param->active_type) {
          case lite_api::ActivationType::kRelu:
            for (; i < width; ++i) {
              *(doutc0_ptr++) = LITEMAX(din_hei_ptr[0], 0.f);
              *(doutc1_ptr++) = LITEMAX(din_hei_ptr[1], 0.f);
              *(doutc2_ptr++) = LITEMAX(din_hei_ptr[2], 0.f);
              *(doutc3_ptr++) = LITEMAX(din_hei_ptr[3], 0.f);
              *(doutc4_ptr++) = LITEMAX(din_hei_ptr[4], 0.f);
              *(doutc5_ptr++) = LITEMAX(din_hei_ptr[5], 0.f);
              *(doutc6_ptr++) = LITEMAX(din_hei_ptr[6], 0.f);
              *(doutc7_ptr++) = LITEMAX(din_hei_ptr[7], 0.f);
              din_hei_ptr += 8;
            }
            break;
          case lite_api::ActivationType::kRelu6:
            /* 0 <= din <= 6 */
            for (; i < width; ++i) {
              float tmp1 = LITEMAX(din_hei_ptr[0], 0.f);
              float tmp2 = LITEMAX(din_hei_ptr[1], 0.f);
              float tmp3 = LITEMAX(din_hei_ptr[2], 0.f);
              float tmp4 = LITEMAX(din_hei_ptr[3], 0.f);
              float tmp5 = LITEMAX(din_hei_ptr[4], 0.f);
              float tmp6 = LITEMAX(din_hei_ptr[5], 0.f);
              float tmp7 = LITEMAX(din_hei_ptr[6], 0.f);
              float tmp8 = LITEMAX(din_hei_ptr[7], 0.f);
              *(doutc0_ptr++) = LITEMIN(tmp1, six);
              *(doutc1_ptr++) = LITEMIN(tmp2, six);
              *(doutc2_ptr++) = LITEMIN(tmp3, six);
              *(doutc3_ptr++) = LITEMIN(tmp4, six);
              *(doutc4_ptr++) = LITEMIN(tmp5, six);
              *(doutc5_ptr++) = LITEMIN(tmp6, six);
              *(doutc6_ptr++) = LITEMIN(tmp7, six);
              *(doutc7_ptr++) = LITEMIN(tmp8, six);
              din_hei_ptr += 8;
            }
            break;
          case lite_api::ActivationType::kLeakyRelu:
            /*din = din >= 0 ? din : din * scale*/
            for (; i < width; ++i) {
              if (din_hei_ptr[0] >= 0) {
                *(doutc0_ptr++) = din_hei_ptr[0];
              } else {
                *(doutc0_ptr++) = din_hei_ptr[0] * scale;
              }
              if (din_hei_ptr[1] >= 0) {
                *(doutc1_ptr++) = din_hei_ptr[1];
              } else {
                *(doutc1_ptr++) = din_hei_ptr[1] * scale;
              }
              if (din_hei_ptr[2] >= 0) {
                *(doutc2_ptr++) = din_hei_ptr[2];
              } else {
                *(doutc2_ptr++) = din_hei_ptr[2] * scale;
              }
              if (din_hei_ptr[3] >= 0) {
                *(doutc3_ptr++) = din_hei_ptr[3];
              } else {
                *(doutc3_ptr++) = din_hei_ptr[3] * scale;
              }
              if (din_hei_ptr[4] >= 0) {
                *(doutc4_ptr++) = din_hei_ptr[4];
              } else {
                *(doutc4_ptr++) = din_hei_ptr[4] * scale;
              }
              if (din_hei_ptr[4] >= 0) {
                *(doutc5_ptr++) = din_hei_ptr[5];
              } else {
                *(doutc5_ptr++) = din_hei_ptr[5] * scale;
              }
              if (din_hei_ptr[6] >= 0) {
                *(doutc6_ptr++) = din_hei_ptr[6];
              } else {
                *(doutc6_ptr++) = din_hei_ptr[6] * scale;
              }
              if (din_hei_ptr[7] >= 0) {
                *(doutc7_ptr++) = din_hei_ptr[7];
              } else {
                *(doutc7_ptr++) = din_hei_ptr[7] * scale;
              }
              din_hei_ptr += 8;
            }
            break;
          default:
            LOG(FATAL) << "this act_type: "
                       << static_cast<int>(act_param->active_type)
                       << " fuse not support";
        }
      } else {
Y
Yan Chunwei 已提交
2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636
        for (; i < width; ++i) {
          *(doutc0_ptr++) = din_hei_ptr[0];
          *(doutc1_ptr++) = din_hei_ptr[1];
          *(doutc2_ptr++) = din_hei_ptr[2];
          *(doutc3_ptr++) = din_hei_ptr[3];
          *(doutc4_ptr++) = din_hei_ptr[4];
          *(doutc5_ptr++) = din_hei_ptr[5];
          *(doutc6_ptr++) = din_hei_ptr[6];
          *(doutc7_ptr++) = din_hei_ptr[7];
          din_hei_ptr += 8;
        }
      }
    }
  }
  return true;
}

2637 2638 2639 2640 2641 2642 2643 2644 2645
template <typename Dtype>
inline void int32_nchwc4_kernel(Dtype*& dout0,        // NOLINT
                                Dtype*& dout1,        // NOLINT
                                Dtype*& dout2,        // NOLINT
                                Dtype*& dout3,        // NOLINT
                                const int32_t*& din,  // NOLINT
                                int cnt,
                                float32x4_t scale,
                                float32x4_t bias,
2646 2647
                                int flag_act,
                                float* alpha);
2648 2649

#ifdef __aarch64__
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 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715
#define NCHWC4_TRANS_INT32                                      \
  "ldp q0, q1, [%[ptr_din]], #32\n"                             \
  "ldp q2, q3, [%[ptr_din]], #32\n"                             \
  "1:\n"                                                        \
  "trn1   v8.4s, v0.4s, v1.4s\n"                                \
  "trn2   v9.4s, v0.4s, v1.4s\n"                                \
  "ldp q0, q1, [%[ptr_din]], #32\n"                             \
  "trn1   v10.4s, v2.4s, v3.4s\n"                               \
  "trn2   v11.4s, v2.4s, v3.4s\n"                               \
  "ldp q2, q3, [%[ptr_din]], #32\n"                             \
  "trn1   v16.2d, v8.2d, v10.2d\n"                              \
  "trn2   v17.2d, v8.2d, v10.2d\n"                              \
  "trn1   v18.2d, v9.2d, v11.2d\n"                              \
  "trn2   v19.2d, v9.2d, v11.2d\n" /* int32 --> fp32 */         \
  "scvtf   v4.4s, v16.4s\n"                                     \
  "scvtf   v5.4s, v17.4s\n"                                     \
  "scvtf   v6.4s, v18.4s\n"                                     \
  "scvtf   v7.4s, v19.4s\n" /* add bias */                      \
  "dup    v16.4s, %[bias].s[0]\n"                               \
  "dup    v17.4s, %[bias].s[2]\n"                               \
  "dup    v18.4s, %[bias].s[1]\n"                               \
  "dup    v19.4s, %[bias].s[3]\n" /* mul scale */               \
  "fmla    v16.4s, v4.4s, %[scale].s[0]\n"                      \
  "fmla    v17.4s, v5.4s, %[scale].s[2]\n"                      \
  "fmla    v18.4s, v6.4s, %[scale].s[1]\n"                      \
  "fmla    v19.4s, v7.4s, %[scale].s[3]\n"                      \
  "cmp    %w[flag_act],   #1\n"                                 \
  "bne    12f                     \n"                           \
  "movi   v20.4s,  #0             \n" /* for relu*/             \
  "fmax   v16.4s, v16.4s, v20.4s  \n"                           \
  "fmax   v17.4s, v17.4s, v20.4s  \n"                           \
  "fmax   v18.4s, v18.4s, v20.4s  \n"                           \
  "fmax   v19.4s, v19.4s, v20.4s  \n"                           \
  "b      2f                      \n"   /* relu end */          \
  "12:                            \n"   /* no relu */           \
  "cmp    %w[flag_act],  #0       \n"   /* check no act */      \
  "beq    2f                      \n"   /* no act end */        \
  "cmp    %w[flag_act],  #2       \n"   /* check relu6 */       \
  "bne    13f                     \n"   /* jump no relu6*/      \
  "movi   v8.4s, #0               \n"   /* for relu6 */         \
  "ld1    {v9.4s}, [%[alpha]]     \n"   /* relu6 alpha */       \
  "fmax   v16.4s, v16.4s, v8.4s  \n"    /* relu6 */             \
  "fmax   v17.4s, v17.4s, v8.4s  \n"    /* relu6 */             \
  "fmax   v18.4s, v18.4s, v8.4s  \n"    /* relu6 */             \
  "fmax   v19.4s, v19.4s, v8.4s  \n"    /* relu6 */             \
  "fmin   v16.4s, v16.4s, v9.4s  \n"    /* relu6 */             \
  "fmin   v17.4s, v17.4s, v9.4s  \n"    /* relu6 */             \
  "fmin   v18.4s, v18.4s, v9.4s  \n"    /* relu6 */             \
  "fmin   v19.4s, v19.4s, v9.4s  \n"    /* relu6 */             \
  "b      2f                     \n"    /* relu6 end */         \
  "13:                              \n" /* leakey relu */       \
  "movi   v12.4s,   #0              \n" /* for leakey relu */   \
  "ld1    {v13.4s}, [%[alpha]]      \n" /* leakey relu alpha */ \
  "fcmge  v4.4s,   v16.4s,  v12.4s  \n" /* vcgeq_f32 */         \
  "fmul   v5.4s,   v16.4s,  v13.4s  \n" /* vmulq_f32 */         \
  "fcmge  v6.4s,   v17.4s,  v12.4s  \n" /* vcgeq_f32 */         \
  "fmul   v7.4s,   v17.4s,  v13.4s  \n" /* vmulq_f32 */         \
  "fcmge  v8.4s,   v18.4s,  v12.4s  \n" /* vcgeq_f32 */         \
  "fmul   v9.4s,   v18.4s,  v13.4s  \n" /* vmulq_f32 */         \
  "fcmge  v10.4s,  v19.4s,  v12.4s  \n" /* vcgeq_f32 */         \
  "fmul   v11.4s,  v19.4s,  v13.4s  \n" /* vmulq_f32 */         \
  "bif    v16.16b, v5.16b,  v4.16b  \n" /* choose*/             \
  "bif    v17.16b, v7.16b,  v6.16b  \n" /* choose*/             \
  "bif    v18.16b, v9.16b,  v8.16b  \n" /* choose*/             \
  "bif    v19.16b, v11.16b, v10.16b \n" /* choose*/             \
  "2:                               \n" /* act end */
2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736

#else
#define NCHWC4_TRANS_INT32                        \
  "vld1.32    {d4-d7}, [%[ptr_din]]!\n"           \
  "vld1.32    {d8-d11}, [%[ptr_din]]!\n"          \
  "1:\n" /* transpose */                          \
  "vtrn.32    q2, q3\n"                           \
  "vtrn.32    q4, q5\n"                           \
  "vswp.32    d5, d8\n"                           \
  "vswp.32    d7, d10\n" /* int32-> fp32 */       \
  "vcvt.f32.s32   q6, q2\n"                       \
  "vcvt.f32.s32   q7, q3\n"                       \
  "vcvt.f32.s32   q8, q4\n"                       \
  "vcvt.f32.s32   q9, q5\n" /* add bias */        \
  "vdup.32    q10, %e[bias][0]\n"                 \
  "vdup.32    q11, %e[bias][1]\n"                 \
  "vdup.32    q12, %f[bias][0]\n"                 \
  "vdup.32    q13, %f[bias][1]\n" /* mul scale */ \
  "vmla.f32  q10, q6, %e[scale][0]\n"             \
  "vmla.f32  q11, q7, %e[scale][1]\n"             \
  "vmla.f32  q12, q8, %f[scale][0]\n"             \
2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774
  "vmla.f32  q13, q9, %f[scale][1]\n"             \
  "vmov.u32   q15, #0              \n"            \
  "cmp    %[flag_act],   #1        \n"            \
  "bne    12f                      \n"            \
  "vmax.f32   q10, q10, q15        \n"            \
  "vmax.f32   q11, q11, q15        \n"            \
  "vmax.f32   q12, q12, q15        \n"            \
  "vmax.f32   q13, q13, q15        \n"            \
  "b      2f                       \n"            \
  "12:                             \n"            \
  "cmp    %[flag_act],  #0         \n"            \
  "beq    2f                       \n"            \
  "cmp    %[flag_act],  #2         \n"            \
  "bne    13f                      \n"            \
  "vld1.f32  {d14-d15}, [%[alpha]] \n"            \
  "vmax.f32   q10, q10, q15        \n"            \
  "vmax.f32   q11, q11, q15        \n"            \
  "vmax.f32   q12, q12, q15        \n"            \
  "vmax.f32   q13, q13, q15        \n"            \
  "vmin.f32   q10, q10, q7         \n"            \
  "vmin.f32   q11, q11, q7         \n"            \
  "vmin.f32   q12, q12, q7         \n"            \
  "vmin.f32   q13, q13, q7         \n"            \
  "b      2f                       \n"            \
  "13:                             \n"            \
  "vld1.f32  {d6-d7}, [%[alpha]]   \n"            \
  "vcge.f32  q6,  q10, q15         \n"            \
  "vmul.f32  q7,  q10, q3          \n"            \
  "vcge.f32  q8,  q11, q15         \n"            \
  "vmul.f32  q9,  q11, q3          \n"            \
  "vbif      q10, q7,  q6          \n"            \
  "vbif      q11, q9,  q8          \n"            \
  "vcge.f32  q6,  q12, q15         \n"            \
  "vmul.f32  q7,  q12, q3          \n"            \
  "vcge.f32  q8,  q13, q15         \n"            \
  "vmul.f32  q9,  q13, q3          \n"            \
  "vbif      q12, q7,  q6          \n"            \
  "vbif      q13, q9,  q8          \n"            \
2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787
  "2:\n"

#endif

template <>
inline void int32_nchwc4_kernel(float*& dout0,        // NOLINT
                                float*& dout1,        // NOLINT
                                float*& dout2,        // NOLINT
                                float*& dout3,        // NOLINT
                                const int32_t*& din,  // NOLINT
                                int cnt,
                                float32x4_t scale,
                                float32x4_t bias,
2788 2789
                                int flag_act,
                                float* alpha) {
2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804
#ifdef __aarch64__
  asm volatile(NCHWC4_TRANS_INT32
               "subs   %w[cnt], %w[cnt], #1\n"
               /* store result */
               "str    q16, [%[doutc0r0]], #16\n"
               "str    q17, [%[doutc2r0]], #16\n"
               "str    q18, [%[doutc1r0]], #16\n"
               "str    q19, [%[doutc3r0]], #16\n"
               "bne    1b\n"
               : [doutc0r0] "+r"(dout0),
                 [doutc1r0] "+r"(dout1),
                 [doutc2r0] "+r"(dout2),
                 [doutc3r0] "+r"(dout3),
                 [ptr_din] "+r"(din),
                 [cnt] "+r"(cnt)
2805 2806 2807 2808
               : [scale] "w"(scale),
                 [bias] "w"(bias),
                 [flag_act] "r"(flag_act),
                 [alpha] "r"(alpha)
2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849
               : "cc",
                 "memory",
                 "v0",
                 "v1",
                 "v2",
                 "v3",
                 "v4",
                 "v5",
                 "v6",
                 "v7",
                 "v8",
                 "v9",
                 "v10",
                 "v11",
                 "v12",
                 "v13",
                 "v14",
                 "v15",
                 "v16",
                 "v17",
                 "v18",
                 "v19",
                 "v20",
                 "v31");
#else
  asm volatile(NCHWC4_TRANS_INT32
               "subs   %[cnt], %[cnt], #1\n"
               /* store result */
               "vld1.32 {d4-d7}, [%[ptr_din]]!\n"
               "vst1.32  {d20-d21}, [%[doutc0r0]]!\n"
               "vst1.32  {d22-d23}, [%[doutc1r0]]!\n"
               "vld1.32 {d8-d11}, [%[ptr_din]]!\n"
               "vst1.32  {d24-d25}, [%[doutc2r0]]!\n"
               "vst1.32  {d26-d27}, [%[doutc3r0]]!\n"
               "bne    1b\n"
               : [doutc0r0] "+r"(dout0),
                 [doutc1r0] "+r"(dout1),
                 [doutc2r0] "+r"(dout2),
                 [doutc3r0] "+r"(dout3),
                 [ptr_din] "+r"(din),
                 [cnt] "+r"(cnt)
2850 2851 2852 2853
               : [scale] "w"(scale),
                 [bias] "w"(bias),
                 [flag_act] "r"(flag_act),
                 [alpha] "r"(alpha)
2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881
               : "cc",
                 "memory",
                 "q2",
                 "q3",
                 "q4",
                 "q5",
                 "q6",
                 "q7",
                 "q8",
                 "q9",
                 "q10",
                 "q11",
                 "q12",
                 "q13",
                 "q14",
                 "q15");
#endif
}

template <>
inline void int32_nchwc4_kernel(int8_t*& dout0,       // NOLINT
                                int8_t*& dout1,       // NOLINT
                                int8_t*& dout2,       // NOLINT
                                int8_t*& dout3,       // NOLINT
                                const int32_t*& din,  // NOLINT
                                int cnt,
                                float32x4_t scale,
                                float32x4_t bias,
2882 2883
                                int flag_act,
                                float* alpha) {
2884
#ifdef __aarch64__
2885
  float32x4_t vmax = vdupq_n_f32(-127.f);
2886 2887
  asm volatile(NCHWC4_TRANS_INT32
               "subs   %w[cnt], %w[cnt], #1\n"
2888 2889 2890 2891 2892 2893 2894 2895 2896
               /* data >= -127 */
               "fcmge v4.4s, v16.4s, %[vmax].4s             \n"
               "fcmge v5.4s, v18.4s, %[vmax].4s             \n"
               "fcmge v6.4s, v17.4s, %[vmax].4s            \n"
               "fcmge v7.4s, v19.4s, %[vmax].4s            \n"
               "bif v16.16b, %[vmax].16b, v4.16b            \n"
               "bif v18.16b, %[vmax].16b, v5.16b            \n"
               "bif v17.16b, %[vmax].16b, v6.16b            \n"
               "bif v19.16b, %[vmax].16b, v7.16b            \n"
2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923
               /* fp32-int32 */
               "fcvtas  v4.4s, v16.4s\n"
               "fcvtas  v5.4s, v18.4s\n"
               "fcvtas  v6.4s, v17.4s\n"
               "fcvtas  v7.4s, v19.4s\n"
               /* int32-int16 */
               "sqxtn   v8.4h, v4.4s\n"
               "sqxtn   v9.4h, v5.4s\n"
               "sqxtn   v10.4h, v6.4s\n"
               "sqxtn   v11.4h, v7.4s\n"
               /* int16-int8 */
               "sqxtn  v16.8b, v8.8h\n"
               "sqxtn  v17.8b, v9.8h\n"
               "sqxtn  v18.8b, v10.8h\n"
               "sqxtn  v19.8b, v11.8h\n"
               /* store result */
               "str     s16, [%[doutc0r0]], #4\n"
               "str     s17, [%[doutc1r0]], #4\n"
               "str     s18, [%[doutc2r0]], #4\n"
               "str     s19, [%[doutc3r0]], #4\n"
               "bne    1b\n"
               : [doutc0r0] "+r"(dout0),
                 [doutc1r0] "+r"(dout1),
                 [doutc2r0] "+r"(dout2),
                 [doutc3r0] "+r"(dout3),
                 [ptr_din] "+r"(din),
                 [cnt] "+r"(cnt)
2924 2925 2926
               : [scale] "w"(scale),
                 [vmax] "w"(vmax),
                 [bias] "w"(bias),
2927 2928
                 [flag_act] "r"(flag_act),
                 [alpha] "r"(alpha)
2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953
               : "cc",
                 "memory",
                 "v0",
                 "v1",
                 "v2",
                 "v3",
                 "v4",
                 "v5",
                 "v6",
                 "v7",
                 "v8",
                 "v9",
                 "v10",
                 "v11",
                 "v12",
                 "v13",
                 "v14",
                 "v15",
                 "v16",
                 "v17",
                 "v18",
                 "v19",
                 "v20",
                 "v31");
#else
2954
  float vmax[4] = {-127.f, -127.f, -127.f, -127.f};
2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970
  asm volatile(NCHWC4_TRANS_INT32
               /* set 0.5 offset */
               "vmov.f32 q2, #0.5\n"
               "vmov.f32 q14, #-0.5\n"
               "vand.i32   q3, q2, q2    @ set offset, 0.5\n"
               "vand.i32   q4, q2, q2    @ set offset, 0.5\n"
               "vand.i32   q5, q2, q2    @ set offset, 0.5\n"
               "vcgt.f32   q6, q10, q15  @ get mask > 0, in0\n"
               "vcgt.f32   q7, q11, q15  @ get mask > 0, in1\n"
               "vcgt.f32   q8, q12, q15  @ get mask > 0, in2\n"
               "vcgt.f32   q9, q13, q15  @ get mask > 0, in3\n"
               /* set 0.5 offset */
               "vbif.f32   q2, q14, q6   @ get right offset\n"
               "vbif.f32   q3, q14, q7   @ get right offset\n"
               "vbif.f32   q4, q14, q8   @ get right offset\n"
               "vbif.f32   q5, q14, q9   @ get right offset\n"
2971
               "vld1.32 {d28-d29}, [%[vmax]] \n"
2972 2973 2974 2975 2976
               /* add offset */
               "vadd.f32   q10, q2, q10\n"
               "vadd.f32   q11, q3, q11\n"
               "vadd.f32   q12, q4, q12\n"
               "vadd.f32   q13, q5, q13\n"
2977 2978 2979 2980 2981 2982 2983 2984 2985
               /* data >= -127 */
               "vcge.f32 q6, q10, q14     @ q10 >= vmax \n"
               "vcge.f32 q7, q11, q14     @ q11 >= vmax \n"
               "vcge.f32 q8, q12, q14     @ q12 >= vmax \n"
               "vcge.f32 q9, q13, q14     @ q13 >= vmax \n"
               "vbif q10, q14, q6         @ choose \n"
               "vbif q11, q14, q7         @ choose \n"
               "vbif q12, q14, q8         @ choose \n"
               "vbif q13, q14, q9         @ choose \n"
2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001
               /* fp32 to int32 */
               "vcvt.s32.f32  q6, q10    @ cvt to int32\n"
               "vcvt.s32.f32  q7, q11    @ cvt to int32\n"
               "vcvt.s32.f32  q8, q12    @ cvt to int32\n"
               "vcvt.s32.f32  q9, q13    @ cvt to int32\n"
               /* int32 to int16 */
               "vqmovn.s32 d20, q6       @ cnt to int16\n"
               "vqmovn.s32 d22, q7       @ cnt to int16\n"
               "vqmovn.s32 d24, q8       @ cnt to int16\n"
               "vqmovn.s32 d26, q9       @ cnt to int16\n"
               /* int16 to int8 */
               "vqmovn.s16 d12, q10       @ cnt to int8\n"
               "vqmovn.s16 d13, q11       @ cnt to int8\n"
               "vqmovn.s16 d14, q12      @ cnt to int8\n"
               "vqmovn.s16 d15, q13      @ cnt to int8\n"
               "subs   %[cnt], %[cnt], #1\n"
3002
               /* store data*/
3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015
               "vld1.32 {d4-d7}, [%[ptr_din]]!\n"
               "vst1.32 {d12[0]},    [%[doutc0r0]]!\n"
               "vst1.32 {d13[0]},    [%[doutc1r0]]!\n"
               "vld1.32 {d8-d11}, [%[ptr_din]]!\n"
               "vst1.32 {d14[0]},    [%[doutc2r0]]!\n"
               "vst1.32 {d15[0]},    [%[doutc3r0]]!\n"
               "bne    1b                @ jump to main loop\n"
               : [doutc0r0] "+r"(dout0),
                 [doutc1r0] "+r"(dout1),
                 [doutc2r0] "+r"(dout2),
                 [doutc3r0] "+r"(dout3),
                 [ptr_din] "+r"(din),
                 [cnt] "+r"(cnt)
3016 3017
               : [scale] "w"(scale),
                 [bias] "w"(bias),
3018 3019 3020
                 [vmax] "r"(vmax),
                 [flag_act] "r"(flag_act),
                 [alpha] "r"(alpha)
3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040
               : "cc",
                 "memory",
                 "q2",
                 "q3",
                 "q4",
                 "q5",
                 "q6",
                 "q7",
                 "q8",
                 "q9",
                 "q10",
                 "q11",
                 "q12",
                 "q13",
                 "q14",
                 "q15");
#endif
}

template <typename Dtype>
3041 3042
inline Dtype cvt_kernel(
    int din, float scale, float bias, int flag_act, float alpha);
3043 3044

template <>
3045 3046 3047
inline float cvt_kernel(
    int din, float scale, float bias, int flag_act, float alpha) {
  if (flag_act == 1) {
3048
    return LITEMAX(din * scale + bias, 0);
3049 3050 3051 3052 3053 3054 3055 3056
  } else if (flag_act == 0) {
    return din * scale + bias;
  } else if (flag_act == 2) {
    float max = LITEMAX(din * scale + bias, 0);
    return LITEMIN(max, alpha);
  } else {
    float result = din * scale + bias;
    return result > 0 ? result : alpha * result;
Y
Yan Chunwei 已提交
3057
  }
3058
}
Y
Yan Chunwei 已提交
3059

3060
template <>
3061 3062 3063 3064 3065 3066
inline int8_t cvt_kernel(
    int din, float scale, float bias, int flag_act, float alpha) {
  if (flag_act == 1) {
    auto tmp = saturate_cast<int8_t>(round(LITEMAX(din * scale + bias, 0)));
    return tmp < -127 ? -127 : tmp;
  } else if (flag_act == 0) {
3067 3068
    auto tmp = saturate_cast<int8_t>(round(din * scale + bias));
    return tmp < -127 ? -127 : tmp;
3069 3070 3071 3072 3073 3074 3075 3076 3077 3078
  } else if (flag_act == 2) {
    float max = LITEMAX(din * scale + bias, 0);
    float relu6_result = LITEMIN(max, alpha);
    auto tmp = saturate_cast<int8_t>(round(relu6_result));
    return tmp < -127 ? -127 : tmp;
  } else {
    float result = din * scale + bias;
    float leaky_result = result > 0 ? result : alpha * result;
    auto tmp = saturate_cast<int8_t>(round(leaky_result));
    return tmp < -127 ? -127 : tmp;
3079 3080
  }
}
Y
Yan Chunwei 已提交
3081

3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093
template <typename Dtype>
inline void write_int32_nchwc4_to_nchw(const int* din,
                                       Dtype* dout,
                                       int cs,
                                       int ce,
                                       int hs,
                                       int he,
                                       int ws,
                                       int we,
                                       int channel,
                                       int height,
                                       int width,
3094 3095
                                       int flag_act,
                                       float* alpha,
3096 3097 3098 3099 3100 3101 3102 3103 3104 3105
                                       float* bias,
                                       bool flag_bias,
                                       Dtype* trash_ptr,
                                       const float* scale) {
  int size_c_out = width * height;

  Dtype* doutc0r0 = dout + cs * size_c_out + hs * width + ws;
  Dtype* doutc1r0 = doutc0r0 + size_c_out;
  Dtype* doutc2r0 = doutc1r0 + size_c_out;
  Dtype* doutc3r0 = doutc2r0 + size_c_out;
Y
Yan Chunwei 已提交
3106 3107 3108 3109 3110 3111

  int size_h = (he > height ? height : he) - hs;  // size_h == hei_n

  int valid_w = we - ws;
  int cnt = valid_w / 4;

3112 3113 3114
  float32x4_t w_scale = vld1q_f32(scale);
  float32x4_t w_bias = flag_bias ? vld1q_f32(bias) : vdupq_n_f32(0.f);

Y
Yan Chunwei 已提交
3115 3116 3117
  if (we > width) {
    cnt--;
  }
3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133
  for (int i = 0; i < size_h; i++) {
    int size_w = i * width;
    Dtype* doutc0_ptr = doutc0r0 + size_w;
    Dtype* doutc1_ptr = doutc1r0 + size_w;
    Dtype* doutc2_ptr = doutc2r0 + size_w;
    Dtype* doutc3_ptr = doutc3r0 + size_w;
    if (ce > channel) {
      switch (ce - channel) {
        case 3:
          doutc1_ptr = trash_ptr;
        case 2:
          doutc2_ptr = trash_ptr;
        case 1:
          doutc3_ptr = trash_ptr;
        default:
          break;
Y
Yan Chunwei 已提交
3134
      }
3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146
    }
    int index = i * valid_w * 4;
    const int* din_hei_ptr = din + index;
    if (cnt > 0) {
      int32_nchwc4_kernel<Dtype>(doutc0_ptr,
                                 doutc1_ptr,
                                 doutc2_ptr,
                                 doutc3_ptr,
                                 din_hei_ptr,
                                 cnt,
                                 w_scale,
                                 w_bias,
3147 3148
                                 flag_act,
                                 alpha);
3149 3150 3151 3152 3153 3154
    }
    if (we > width) {
      int offset = 16 * (valid_w / 4 - 1);
      din_hei_ptr = din + index + offset;
      int j = we - 4;
      for (; j < width; ++j) {
3155 3156 3157 3158 3159 3160 3161 3162
        *(doutc0_ptr++) = cvt_kernel<Dtype>(
            din_hei_ptr[0], scale[0], bias[0], flag_act, alpha[0]);
        *(doutc1_ptr++) = cvt_kernel<Dtype>(
            din_hei_ptr[1], scale[1], bias[1], flag_act, alpha[0]);
        *(doutc2_ptr++) = cvt_kernel<Dtype>(
            din_hei_ptr[2], scale[2], bias[2], flag_act, alpha[0]);
        *(doutc3_ptr++) = cvt_kernel<Dtype>(
            din_hei_ptr[3], scale[3], bias[3], flag_act, alpha[0]);
3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183
        din_hei_ptr += 4;
      }
    }
  }
}

template <typename Dtype>
inline void int32_nchwc8_kernel(Dtype*& dout0,        // NOLINT
                                Dtype*& dout1,        // NOLINT
                                Dtype*& dout2,        // NOLINT
                                Dtype*& dout3,        // NOLINT
                                Dtype*& dout4,        // NOLINT
                                Dtype*& dout5,        // NOLINT
                                Dtype*& dout6,        // NOLINT
                                Dtype*& dout7,        // NOLINT
                                const int32_t*& din,  // NOLINT
                                int cnt,
                                float32x4_t scale0,
                                float32x4_t scale1,
                                float32x4_t bias0,
                                float32x4_t bias1,
3184 3185
                                int flag_act,
                                float* alpha);
3186 3187

// clang-format off
Y
Yan Chunwei 已提交
3188
#ifdef __aarch64__
3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243
#define INT32_NCHWC8_TO_NCHW_FP32                                  \
  "ldp q0, q1, [%[ptr_din]], #32\n" /* load r00, r01 to q0, q1 */  \
  "ldp q2, q3, [%[ptr_din]], #32\n" /* load r02, r03 to q2, q3 */  \
  "ldp q4, q5, [%[ptr_din]], #32\n" /* load r00, r01 to q0, q1 */  \
  "ldp q6, q7, [%[ptr_din]], #32\n" /* load r02, r03 to q2, q3 */  \
  "1:\n"                                                           \
  "trn1   v8.4s, v0.4s, v2.4s\n"    /* trans q0, q1*/              \
  "trn2   v9.4s, v0.4s, v2.4s\n"    /* trans q0, q1*/              \
  "trn1   v10.4s, v1.4s, v3.4s\n"   /* trans q2, q3*/              \
  "trn2   v11.4s, v1.4s, v3.4s\n"   /* trans q2, q3*/              \
  "ldp q0, q1, [%[ptr_din]], #32\n" /* load r00, r01 to q0, q1 */  \
  "trn1   v12.4s, v4.4s, v6.4s\n"   /* trans q0, q1*/              \
  "trn2   v13.4s, v4.4s, v6.4s\n"   /* trans q0, q1*/              \
  "trn1   v14.4s, v5.4s, v7.4s\n"   /* trans q2, q3*/              \
  "trn2   v15.4s, v5.4s, v7.4s\n"   /* trans q2, q3*/              \
  "ldp q2, q3, [%[ptr_din]], #32\n" /* load r02, r03 to q2, q3 */  \
  "trn1   v16.2d, v8.2d, v12.2d\n"  /* trans q8, q10 00 01 02 03*/ \
  "trn2   v17.2d, v8.2d, v12.2d\n"  /* trans q8, q10 20 21 22 23*/ \
  "trn1   v18.2d, v9.2d, v13.2d\n"  /* trans q9, q11 10 11 12 13*/ \
  "trn2   v19.2d, v9.2d, v13.2d\n"  /* trans q9, q11 30 31 32 33*/ \
  "ldp q4, q5, [%[ptr_din]], #32\n" /* load r00, r01 to q0, q1 */  \
  "trn1   v8.2d, v10.2d, v14.2d\n"  /* trans q8, q10 40 41 42 43*/ \
  "trn2   v9.2d, v10.2d, v14.2d\n"  /* trans q8, q10 60 61 62 63*/ \
  "trn1   v12.2d, v11.2d, v15.2d\n" /* trans q9, q11 50 51 52 53*/ \
  "trn2   v13.2d, v11.2d, v15.2d\n" /* trans q9, q11 70 71 72 73*/ \
  "ldp q6, q7, [%[ptr_din]], #32\n" /* load r02, r03 to q2, q3 */  \
  /* int32->fp32 */                                                \
  "scvtf   v10.4s, v16.4s\n"                                       \
  "scvtf   v11.4s, v17.4s\n"                                       \
  "scvtf   v14.4s, v18.4s\n"                                       \
  "scvtf   v15.4s, v19.4s\n"                                       \
  /* add bias */                                                   \
  "dup    v16.4s, %[bias0].s[0]\n"                                 \
  "dup    v17.4s, %[bias0].s[2]\n"                                 \
  "dup    v18.4s, %[bias0].s[1]\n"                                 \
  "dup    v19.4s, %[bias0].s[3]\n"                                 \
  /* mul scale */                                                  \
  "fmla    v16.4s, v10.4s, %[scale0].s[0]\n"                       \
  "fmla    v17.4s, v11.4s, %[scale0].s[2]\n"                       \
  "fmla    v18.4s, v14.4s, %[scale0].s[1]\n"                       \
  "fmla    v19.4s, v15.4s, %[scale0].s[3]\n"                       \
  "scvtf   v10.4s, v8.4s\n"                                        \
  "scvtf   v11.4s, v9.4s\n"                                        \
  "scvtf   v14.4s, v12.4s\n"                                       \
  "scvtf   v15.4s, v13.4s\n"                                       \
  /* add bias */                                                   \
  "dup    v8.4s, %[bias1].s[0]\n"                                  \
  "dup    v9.4s, %[bias1].s[2]\n"                                  \
  "dup    v12.4s, %[bias1].s[1]\n"                                 \
  "dup    v13.4s, %[bias1].s[3]\n"                                 \
  /* mul scale */                                                  \
  "fmla    v8.4s, v10.4s, %[scale1].s[0]\n"                        \
  "fmla    v9.4s, v11.4s, %[scale1].s[2]\n"                        \
  "fmla    v12.4s, v14.4s, %[scale1].s[1]\n"                       \
  "fmla    v13.4s, v15.4s, %[scale1].s[3]\n"                       \
3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308
  /* activation */                                                 \
  "cmp    %w[flag_act],   #1\n"                                    \
  "bne    12f                     \n"                              \
  "movi   v31.4s,  #0             \n" /* for relu*/                \
  "fmax   v16.4s, v16.4s, v31.4s  \n" /*relu*/                     \
  "fmax   v17.4s, v17.4s, v31.4s  \n" /*relu*/                     \
  "fmax   v18.4s, v18.4s, v31.4s  \n" /*relu*/                     \
  "fmax   v19.4s, v19.4s, v31.4s  \n" /*relu*/                     \
  "fmax   v8.4s,  v8.4s,  v31.4s  \n" /*relu*/                     \
  "fmax   v9.4s,  v9.4s,  v31.4s  \n" /*relu*/                     \
  "fmax   v12.4s, v12.4s, v31.4s  \n" /*relu*/                     \
  "fmax   v13.4s, v13.4s, v31.4s  \n" /*relu*/                     \
  "b      2f                      \n" /* relu end */               \
  "12:                            \n" /* no relu */                \
  "cmp    %w[flag_act],  #0       \n" /* check no act */           \
  "beq    2f                      \n" /* no act end */             \
  "cmp    %w[flag_act],  #2       \n" /* check relu6 */            \
  "bne    13f                     \n" /* jump no relu6*/           \
  "movi   v20.4s, #0              \n" /* for relu6 */              \
  "ld1    {v21.4s}, [%[alpha]]    \n" /* relu6 alpha */            \
  "fmax   v16.4s, v16.4s, v20.4s  \n" /* relu6 */                  \
  "fmax   v17.4s, v17.4s, v20.4s  \n" /* relu6 */                  \
  "fmax   v18.4s, v18.4s, v20.4s  \n" /* relu6 */                  \
  "fmax   v19.4s, v19.4s, v20.4s  \n" /* relu6 */                  \
  "fmax   v8.4s,  v8.4s,  v20.4s  \n" /* relu6 */                  \
  "fmax   v9.4s,  v9.4s,  v20.4s  \n" /* relu6 */                  \
  "fmax   v12.4s, v12.4s, v20.4s  \n" /* relu6 */                  \
  "fmax   v13.4s, v13.4s, v20.4s  \n" /* relu6 */                  \
  "fmin   v16.4s, v16.4s, v21.4s  \n" /* relu6 */                  \
  "fmin   v17.4s, v17.4s, v21.4s  \n" /* relu6 */                  \
  "fmin   v18.4s, v18.4s, v21.4s  \n" /* relu6 */                  \
  "fmin   v19.4s, v19.4s, v21.4s  \n" /* relu6 */                  \
  "fmin   v8.4s,  v8.4s,  v21.4s  \n" /* relu6 */                  \
  "fmin   v9.4s,  v9.4s,  v21.4s  \n" /* relu6 */                  \
  "fmin   v12.4s, v12.4s, v21.4s  \n" /* relu6 */                  \
  "fmin   v13.4s, v13.4s, v21.4s  \n" /* relu6 */                  \
  "b      2f                      \n" /* relu6 end */              \
  "13:                               \n" /* leakey relu */         \
  "movi   v20.4s,   #0               \n" /* for leakey relu */     \
  "ld1    {v21.4s}, [%[alpha]]       \n" /* leakey relu alpha */   \
  "fcmge  v10.4s,   v16.4s,  v20.4s  \n" /* vcgeq_f32 */           \
  "fmul   v11.4s,   v16.4s,  v21.4s  \n" /* vmulq_f32 */           \
  "fcmge  v14.4s,   v17.4s,  v20.4s  \n" /* vcgeq_f32 */           \
  "fmul   v15.4s,   v17.4s,  v21.4s  \n" /* vmulq_f32 */           \
  "fcmge  v22.4s,   v18.4s,  v20.4s  \n" /* vcgeq_f32 */           \
  "fmul   v23.4s,   v18.4s,  v21.4s  \n" /* vmulq_f32 */           \
  "fcmge  v24.4s,   v19.4s,  v20.4s  \n" /* vcgeq_f32 */           \
  "fmul   v25.4s,   v19.4s,  v21.4s  \n" /* vmulq_f32 */           \
  "bif    v16.16b, v11.16b,  v10.16b \n" /* choose*/               \
  "bif    v17.16b, v15.16b,  v14.16b \n" /* choose*/               \
  "bif    v18.16b, v23.16b,  v22.16b \n" /* choose*/               \
  "bif    v19.16b, v25.16b,  v24.16b \n" /* choose*/               \
  "fcmge  v10.4s,   v8.4s,   v20.4s  \n" /* vcgeq_f32 */           \
  "fmul   v11.4s,   v8.4s,   v21.4s  \n" /* vmulq_f32 */           \
  "fcmge  v14.4s,   v9.4s,   v20.4s  \n" /* vcgeq_f32 */           \
  "fmul   v15.4s,   v9.4s,   v21.4s  \n" /* vmulq_f32 */           \
  "fcmge  v22.4s,   v12.4s,  v20.4s  \n" /* vcgeq_f32 */           \
  "fmul   v23.4s,   v12.4s,  v21.4s  \n" /* vmulq_f32 */           \
  "fcmge  v24.4s,   v13.4s,  v20.4s  \n" /* vcgeq_f32 */           \
  "fmul   v25.4s,   v13.4s,  v21.4s  \n" /* vmulq_f32 */           \
  "bif    v8.16b,  v11.16b,  v10.16b \n" /* choose*/               \
  "bif    v9.16b,  v15.16b,  v14.16b \n" /* choose*/               \
  "bif    v12.16b, v23.16b,  v22.16b \n" /* choose*/               \
  "bif    v13.16b, v25.16b,  v24.16b \n" /* choose*/               \
  "2:                                \n" /* act end */
Y
Yan Chunwei 已提交
3309

3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353
#else
#define INT32_NCHWC8_TO_NCHW_FP32                                 \
  "1:                                 @ main loop\n"              \
  "vld1.32 {d0-d3},   [%[ptr_din]]!   @load data \n"              \
  "vld1.32 {d4-d7},   [%[ptr_din]]!   @load data \n"              \
  "vld1.32 {d8-d11},  [%[ptr_din]]!   @load data \n"              \
  "vld1.32 {d12-d15}, [%[ptr_din]]!   @load data \n"              \
  /* int32-> fp32 */                                              \
  "vcvt.f32.s32   q8, q0\n"                                       \
  "vcvt.f32.s32   q9, q1\n"                                       \
  "vcvt.f32.s32   q10, q2\n"                                      \
  "vcvt.f32.s32   q11, q3\n"                                      \
  "vand.32   q0, %q[bias0], %q[bias0]\n"                          \
  "vand.32   q1, %q[bias1], %q[bias1]\n"                          \
  "vand.32   q2, %q[bias0], %q[bias0]\n"                          \
  "vand.32   q3, %q[bias1], %q[bias1]\n"                          \
  /* mul scale */                                                 \
  "vmla.f32  q0, q8, %q[scale0]\n"                                \
  "vmla.f32  q1, q9, %q[scale1]\n"                                \
  "vmla.f32  q2, q10, %q[scale0]\n"                               \
  "vmla.f32  q3, q11, %q[scale1]\n"                               \
  /* int32-> fp32 */                                              \
  "vcvt.f32.s32   q8, q4\n"                                       \
  "vcvt.f32.s32   q9, q5\n"                                       \
  "vcvt.f32.s32   q10, q6\n"                                      \
  "vcvt.f32.s32   q11, q7\n"                                      \
  "vand.32   q4, %q[bias0], %q[bias0]\n"                          \
  "vand.32   q5, %q[bias1], %q[bias1]\n"                          \
  "vand.32   q6, %q[bias0], %q[bias0]\n"                          \
  "vand.32   q7, %q[bias1], %q[bias1]\n"                          \
  /* mul scale */                                                 \
  "vmla.f32  q4, q8, %q[scale0]\n"                                \
  "vmla.f32  q5, q9, %q[scale1]\n"                                \
  "vmla.f32  q6, q10, %q[scale0]\n"                               \
  "vmla.f32  q7, q11, %q[scale1]\n"                               \
  /* transpose */                                                 \
  "vtrn.32    q0, q2\n"                                           \
  "vtrn.32    q1, q3\n"                                           \
  "vtrn.32    q4, q6\n"                                           \
  "vtrn.32    q5, q7\n"                                           \
  "vswp    d1, d8\n"  /* q0: a0-a3, q4: c0-c3 */                  \
  "vswp    d5, d12\n" /* q2: b0-b3, q6: d0-d3 */                  \
  "vswp    d3, d10\n" /* q1: e0-e3, q5: g0-g3 */                  \
  "vswp    d7, d14\n" /* q3: f0-f3, q7: h0-h3 */                  \
3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415
  /* activation */                                                \
  "vmov.u32   q8, #0                \n"                           \
  "cmp    %[flag_act],   #1         \n"                           \
  "bne    12f                       \n"                           \
  "vmax.f32 q0, q0, q8              \n" /*relu*/                  \
  "vmax.f32 q2, q2, q8              \n" /*relu*/                  \
  "vmax.f32 q4, q4, q8              \n" /*relu*/                  \
  "vmax.f32 q6, q6, q8              \n" /*relu*/                  \
  "vmax.f32 q1, q1, q8              \n" /*relu*/                  \
  "vmax.f32 q3, q3, q8              \n" /*relu*/                  \
  "vmax.f32 q5, q5, q8              \n" /*relu*/                  \
  "vmax.f32 q7, q7, q8              \n" /*relu*/                  \
  "b      2f                        \n"                           \
  "12:                              \n"                           \
  "cmp    %[flag_act],  #0          \n"                           \
  "beq    2f                        \n"                           \
  "cmp    %[flag_act],  #2          \n"                           \
  "bne    13f                       \n"                           \
  "vld1.f32  {d18-d19}, [%[alpha]]  \n"                           \
  "vmax.f32   q0, q0, q8            \n"                           \
  "vmax.f32   q2, q2, q8            \n"                           \
  "vmax.f32   q4, q4, q8            \n"                           \
  "vmax.f32   q6, q6, q8            \n"                           \
  "vmax.f32   q1, q1, q8            \n"                           \
  "vmax.f32   q3, q3, q8            \n"                           \
  "vmax.f32   q5, q5, q8            \n"                           \
  "vmax.f32   q7, q7, q8            \n"                           \
  "vmin.f32   q0, q0, q9            \n"                           \
  "vmin.f32   q2, q2, q9            \n"                           \
  "vmin.f32   q4, q4, q9            \n"                           \
  "vmin.f32   q6, q6, q9            \n"                           \
  "vmin.f32   q1, q1, q9            \n"                           \
  "vmin.f32   q3, q3, q9            \n"                           \
  "vmin.f32   q5, q5, q9            \n"                           \
  "vmin.f32   q7, q7, q9            \n"                           \
  "b      2f                        \n"                           \
  "13:                              \n"                           \
  "vld1.f32  {d18-d19}, [%[alpha]]  \n"                           \
  "vcge.f32  q10,  q0,  q8          \n"                           \
  "vmul.f32  q11,  q0,  q9          \n"                           \
  "vbif      q0,   q11, q10         \n"                           \
  "vcge.f32  q10,  q2,  q8          \n"                           \
  "vmul.f32  q11,  q2,  q9          \n"                           \
  "vbif      q2,   q11, q10         \n"                           \
  "vcge.f32  q10,  q4,  q8          \n"                           \
  "vmul.f32  q11,  q4,  q9          \n"                           \
  "vbif      q4,   q11, q10         \n"                           \
  "vcge.f32  q10,  q6,  q8          \n"                           \
  "vmul.f32  q11,  q6,  q9          \n"                           \
  "vbif      q6,   q11, q10         \n"                           \
  "vcge.f32  q10,  q1,  q8          \n"                           \
  "vmul.f32  q11,  q1,  q9          \n"                           \
  "vbif      q1,   q11, q10         \n"                           \
  "vcge.f32  q10,  q3,  q8          \n"                           \
  "vmul.f32  q11,  q3,  q9          \n"                           \
  "vbif      q3,   q11, q10         \n"                           \
  "vcge.f32  q10,  q5,  q8          \n"                           \
  "vmul.f32  q11,  q5,  q9          \n"                           \
  "vbif      q5,   q11, q10         \n"                           \
  "vcge.f32  q10,  q7,  q8          \n"                           \
  "vmul.f32  q11,  q7,  q9          \n"                           \
  "vbif      q7,   q11, q10         \n"                           \
3416
  "2:\n"
Y
Yan Chunwei 已提交
3417

3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435
#endif
// clang-format on

template <>
inline void int32_nchwc8_kernel(float*& dout0,        // NOLINT
                                float*& dout1,        // NOLINT
                                float*& dout2,        // NOLINT
                                float*& dout3,        // NOLINT
                                float*& dout4,        // NOLINT
                                float*& dout5,        // NOLINT
                                float*& dout6,        // NOLINT
                                float*& dout7,        // NOLINT
                                const int32_t*& din,  // NOLINT
                                int cnt,
                                float32x4_t scale0,
                                float32x4_t scale1,
                                float32x4_t bias0,
                                float32x4_t bias1,
3436 3437 3438
                                int flag_act,
                                float* alpha) {
// clang-format off
3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464
#ifdef __aarch64__
  asm volatile(INT32_NCHWC8_TO_NCHW_FP32
               "subs   %w[cnt], %w[cnt],  #1\n"   /* loop count -1*/
               "str    q16, [%[doutc0r0]], #16\n" /* store c0r0*/
               "str    q17, [%[doutc2r0]], #16\n" /* store c2r0*/
               "str    q18, [%[doutc1r0]], #16\n" /* store c1r0*/
               "str    q19, [%[doutc3r0]], #16\n" /* store c3r0*/
               "str    q8, [%[doutc4r0]], #16\n"  /* store c4r0*/
               "str    q9, [%[doutc6r0]], #16\n"  /* store c6r0*/
               "str    q12, [%[doutc5r0]], #16\n" /* store c5r0*/
               "str    q13, [%[doutc7r0]], #16\n" /* store c7r0*/
               "bne    1b\n"                      /* jump to main loop*/
               : [doutc0r0] "+r"(dout0),
                 [doutc1r0] "+r"(dout1),
                 [doutc2r0] "+r"(dout2),
                 [doutc3r0] "+r"(dout3),
                 [doutc4r0] "+r"(dout4),
                 [doutc5r0] "+r"(dout5),
                 [doutc6r0] "+r"(dout6),
                 [doutc7r0] "+r"(dout7),
                 [ptr_din] "+r"(din),
                 [cnt] "+r"(cnt)
               : [scale0] "w"(scale0),
                 [scale1] "w"(scale1),
                 [bias0] "w"(bias0),
                 [bias1] "w"(bias1),
3465 3466 3467 3468 3469 3470 3471
                 [flag_act] "r"(flag_act), 
                 [alpha] "r"(alpha)
               : "cc", "memory", "v0", "v1", "v2", "v3", "v4", "v5", "v6",
                 "v7", "v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15",
                 "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23", "v24",
                 "v25", "v31"
               );
Y
Yan Chunwei 已提交
3472
#else
3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497
  asm volatile(INT32_NCHWC8_TO_NCHW_FP32
               "subs    %[cnt],  #1\n"               /* loop count -1*/
               "vst1.32 {d0-d1}, [%[doutc0r0]]!\n"   /* store c0r0*/
               "vst1.32 {d4-d5}, [%[doutc1r0]]!\n"   /* store c0r0*/
               "vst1.32 {d8-d9}, [%[doutc2r0]]!\n"   /* store c0r0*/
               "vst1.32 {d12-d13}, [%[doutc3r0]]!\n" /* store c0r0*/
               "vst1.32 {d2-d3}, [%[doutc4r0]]!\n"   /* store c0r0*/
               "vst1.32 {d6-d7}, [%[doutc5r0]]!\n"   /* store c0r0*/
               "vst1.32 {d10-d11}, [%[doutc6r0]]!\n" /* store c0r0*/
               "vst1.32 {d14-d15}, [%[doutc7r0]]!\n" /* store c0r0*/
               "bne    1b\n"                         /* jump to main loop*/
               : [doutc0r0] "+r"(dout0),
                 [doutc1r0] "+r"(dout1),
                 [doutc2r0] "+r"(dout2),
                 [doutc3r0] "+r"(dout3),
                 [doutc4r0] "+r"(dout4),
                 [doutc5r0] "+r"(dout5),
                 [doutc6r0] "+r"(dout6),
                 [doutc7r0] "+r"(dout7),
                 [ptr_din] "+r"(din),
                 [cnt] "+r"(cnt)
               : [scale0] "w"(scale0),
                 [scale1] "w"(scale1),
                 [bias0] "w"(bias0),
                 [bias1] "w"(bias1),
3498 3499 3500 3501 3502
                 [flag_act] "r"(flag_act), 
                 [alpha] "r"(alpha)
               : "cc", "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6",
                 "q7", "q8", "q9", "q10", "q11"
               );
3503
#endif
3504
  // clang-format on
3505
}
Y
Yan Chunwei 已提交
3506

3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521
template <>
inline void int32_nchwc8_kernel(int8_t*& dout0,       // NOLINT
                                int8_t*& dout1,       // NOLINT
                                int8_t*& dout2,       // NOLINT
                                int8_t*& dout3,       // NOLINT
                                int8_t*& dout4,       // NOLINT
                                int8_t*& dout5,       // NOLINT
                                int8_t*& dout6,       // NOLINT
                                int8_t*& dout7,       // NOLINT
                                const int32_t*& din,  // NOLINT
                                int cnt,
                                float32x4_t scale0,
                                float32x4_t scale1,
                                float32x4_t bias0,
                                float32x4_t bias1,
3522 3523 3524
                                int flag_act,
                                float* alpha) {
// clang-format off
3525
#ifdef __aarch64__
3526
  float32x4_t vmax = vdupq_n_f32(-127.f);
3527
  asm volatile(INT32_NCHWC8_TO_NCHW_FP32 /* fp32-int32 */
3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546
               /* data >= -127 */
               "fcmge v10.4s, v16.4s, %[vmax].4s             \n"
               "fcmge v11.4s, v17.4s, %[vmax].4s             \n"
               "fcmge v14.4s, v18.4s, %[vmax].4s            \n"
               "fcmge v15.4s, v19.4s, %[vmax].4s            \n"
               "fcmge v20.4s, v8.4s, %[vmax].4s             \n"
               "fcmge v21.4s, v9.4s, %[vmax].4s             \n"
               "fcmge v22.4s, v12.4s, %[vmax].4s            \n"
               "fcmge v23.4s, v13.4s, %[vmax].4s            \n"
               /* choose data */
               "bif v16.16b, %[vmax].16b, v10.16b            \n"
               "bif v17.16b, %[vmax].16b, v11.16b            \n"
               "bif v18.16b, %[vmax].16b, v14.16b            \n"
               "bif v19.16b, %[vmax].16b, v15.16b            \n"
               "bif v8.16b, %[vmax].16b, v20.16b            \n"
               "bif v9.16b, %[vmax].16b, v21.16b            \n"
               "bif v12.16b, %[vmax].16b, v22.16b            \n"
               "bif v13.16b, %[vmax].16b, v23.16b            \n"
               /* fp32 - int32 */
3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596
               "fcvtas  v10.4s, v16.4s\n"
               "fcvtas  v11.4s, v17.4s\n"
               "fcvtas  v14.4s, v18.4s\n"
               "fcvtas  v15.4s, v19.4s\n"
               "fcvtas  v20.4s, v8.4s\n"
               "fcvtas  v21.4s, v9.4s\n"
               "fcvtas  v22.4s, v12.4s\n"
               "fcvtas  v23.4s, v13.4s\n"
               /* int32-int16 */
               "sqxtn   v16.4h, v10.4s\n"
               "sqxtn   v17.4h, v11.4s\n"
               "sqxtn   v18.4h, v14.4s\n"
               "sqxtn   v19.4h, v15.4s\n"
               "sqxtn   v8.4h, v20.4s\n"
               "sqxtn   v9.4h, v21.4s\n"
               "sqxtn   v12.4h, v22.4s\n"
               "sqxtn   v13.4h, v23.4s\n"
               /* int16-int8 */
               "sqxtn  v10.8b, v16.8h\n"
               "sqxtn  v11.8b, v17.8h\n"
               "sqxtn  v14.8b, v18.8h\n"
               "sqxtn  v15.8b, v19.8h\n"
               "sqxtn  v20.8b, v8.8h\n"
               "sqxtn  v21.8b, v9.8h\n"
               "sqxtn  v22.8b, v12.8h\n"
               "sqxtn  v23.8b, v13.8h\n"
               "str    s10, [%[doutc0r0]], #4 \n"  /* store c0r0*/
               "str    s11, [%[doutc2r0]], #4 \n"  /* store c2r0*/
               "str    s14, [%[doutc1r0]], #4 \n"  /* store c1r0*/
               "str    s15, [%[doutc3r0]], #4 \n"  /* store c3r0*/
               "subs   %w[cnt], %w[cnt],  #1   \n" /* loop count -1*/
               "str    s20, [%[doutc4r0]], #4  \n" /* store c0r0*/
               "str    s21, [%[doutc6r0]], #4  \n" /* store c2r0*/
               "str    s22, [%[doutc5r0]], #4 \n"  /* store c1r0*/
               "str    s23, [%[doutc7r0]], #4 \n"  /* store c3r0*/
               "bne    1b                      \n" /* jump to main loop*/
               : [doutc0r0] "+r"(dout0),
                 [doutc1r0] "+r"(dout1),
                 [doutc2r0] "+r"(dout2),
                 [doutc3r0] "+r"(dout3),
                 [doutc4r0] "+r"(dout4),
                 [doutc5r0] "+r"(dout5),
                 [doutc6r0] "+r"(dout6),
                 [doutc7r0] "+r"(dout7),
                 [ptr_din] "+r"(din),
                 [cnt] "+r"(cnt)
               : [scale0] "w"(scale0),
                 [scale1] "w"(scale1),
                 [bias0] "w"(bias0),
                 [bias1] "w"(bias1),
3597
                 [vmax] "w"(vmax),
3598 3599 3600 3601 3602 3603 3604
                 [flag_act] "r"(flag_act), 
                 [alpha] "r"(alpha)
               : "cc", "memory", "v0", "v1", "v2", "v3", "v4", "v5", "v6",
                 "v7", "v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15",
                 "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23", "v24",
                 "v25", "v31"
               );
3605
#else
3606
  float vmax[4] = {-127.f, -127.f, -127.f, -127.f};
3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639
  asm volatile(INT32_NCHWC8_TO_NCHW_FP32 /* set +-0.5 offset */
               "vmov.f32 q10, #-0.5\n"
               "vmov.f32 q9, #0.5\n"
               "vcgt.f32   q11, q0, q8   @ get mask > 0, in0\n"
               "vbif.f32   q9, q10, q11   @ get right offset\n"
               "vadd.f32   q0, q0, q9\n"
               "vmov.f32 q9, #0.5\n"
               "vcgt.f32   q11, q2, q8   @ get mask > 0, in0\n"
               "vbif.f32   q9, q10, q11   @ get right offset\n"
               "vadd.f32   q2, q2, q9\n"
               "vmov.f32 q9, #0.5\n"
               "vcgt.f32   q11, q4, q8   @ get mask > 0, in0\n"
               "vbif.f32   q9, q10, q11   @ get right offset\n"
               "vadd.f32   q4, q4, q9\n"
               "vmov.f32 q9, #0.5\n"
               "vcgt.f32   q11, q6, q8   @ get mask > 0, in0\n"
               "vbif.f32   q9, q10, q11   @ get right offset\n"
               "vadd.f32   q6, q6, q9\n"
               "vmov.f32 q9, #0.5\n"
               "vcgt.f32   q11, q1, q8   @ get mask > 0, in0\n"
               "vbif.f32   q9, q10, q11   @ get right offset\n"
               "vadd.f32   q1, q1, q9\n"
               "vmov.f32 q9, #0.5\n"
               "vcgt.f32   q11, q3, q8   @ get mask > 0, in0\n"
               "vbif.f32   q9, q10, q11   @ get right offset\n"
               "vadd.f32   q3, q3, q9\n"
               "vmov.f32 q9, #0.5\n"
               "vcgt.f32   q11, q5, q8   @ get mask > 0, in0\n"
               "vbif.f32   q9, q10, q11   @ get right offset\n"
               "vadd.f32   q5, q5, q9\n"
               "vmov.f32 q9, #0.5\n"
               "vcgt.f32   q11, q7, q8   @ get mask > 0, in0\n"
               "vbif.f32   q9, q10, q11   @ get right offset\n"
3640
               "vld1.32 {d22-d23}, [%[vmax]] \n"
3641
               "vadd.f32   q7, q7, q9\n"
3642 3643 3644 3645 3646 3647 3648 3649 3650 3651
               /* data >= -127 */
               "vcge.f32 q8, q0, q11     @ q10 >= vmax \n"
               "vcge.f32 q9, q2, q11     @ q10 >= vmax \n"
               "vcge.f32 q10, q4, q11     @ q10 >= vmax \n"
               /* choose data */
               "vbif q0, q11, q8    @ choose \n"
               "vcge.f32 q8, q6, q11     @ q10 >= vmax \n"
               "vbif q2, q11, q9    @ choose \n"
               "vbif q4, q11, q10    @ choose \n"
               "vbif q6, q11, q8    @ choose \n"
3652 3653 3654 3655 3656 3657 3658 3659 3660 3661
               /* fp32 to int32 */
               "vcvt.s32.f32  q8, q0    @ cvt to int32\n"
               "vcvt.s32.f32  q9, q2    @ cvt to int32\n"
               "vcvt.s32.f32  q10, q4   @ cvt to int32\n"
               "vcvt.s32.f32  q11, q6   @ cvt to int32\n"
               /* int32 to int16 */
               "vqmovn.s32 d0, q8       @ cnt to int16\n"
               "vqmovn.s32 d4, q9       @ cnt to int16\n"
               "vqmovn.s32 d8, q10      @ cnt to int16\n"
               "vqmovn.s32 d12, q11      @ cnt to int16\n"
3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672
               /* data >= -127 */
               "vld1.32 {d22-d23}, [%[vmax]] \n"
               "vcge.f32 q8, q1, q11     @ q10 >= vmax \n"
               "vcge.f32 q9, q3, q11     @ q10 >= vmax \n"
               "vcge.f32 q10, q5, q11     @ q10 >= vmax \n"
               /* choose data */
               "vbif q1, q11, q8    @ choose \n"
               "vcge.f32 q8, q7, q11     @ q10 >= vmax \n"
               "vbif q3, q11, q9    @ choose \n"
               "vbif q5, q11, q10    @ choose \n"
               "vbif q7, q11, q8    @ choose \n"
3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715
               /* fp32 to int32 */
               "vcvt.s32.f32  q8, q1    @ cvt to int32\n"
               "vcvt.s32.f32  q9, q3    @ cvt to int32\n"
               "vcvt.s32.f32  q10, q5   @ cvt to int32\n"
               "vcvt.s32.f32  q11, q7   @ cvt to int32\n"
               /* int32 to int16 */
               "vqmovn.s32 d2, q8       @ cnt to int16\n"
               "vqmovn.s32 d6, q9       @ cnt to int16\n"
               "vqmovn.s32 d10, q10     @ cnt to int16\n"
               "vqmovn.s32 d14, q11     @ cnt to int16\n"
               /* int16 to int8 */
               "vqmovn.s16 d16, q0      @ cnt to int8\n"
               "vqmovn.s16 d17, q2      @ cnt to int8\n"
               "vqmovn.s16 d18, q4      @ cnt to int8\n"
               "vqmovn.s16 d19, q6      @ cnt to int8\n"
               "vst1.32    {d16[0]}, [%[doutc0r0]]!\n"
               "vqmovn.s16 d20, q1      @ cnt to int8\n"
               "vst1.32    {d17[0]}, [%[doutc1r0]]!\n"
               "vqmovn.s16 d21, q3      @ cnt to int8\n"
               "vst1.32    {d18[0]}, [%[doutc2r0]]!\n"
               "vqmovn.s16 d22, q5      @ cnt to int8\n"
               "vst1.32    {d19[0]}, [%[doutc3r0]]!\n"
               "vqmovn.s16 d23, q7      @ cnt to int8\n"
               "subs   %[cnt], #1\n"
               "vst1.32    {d20[0]}, [%[doutc4r0]]!\n"
               "vst1.32    {d21[0]}, [%[doutc5r0]]!\n"
               "vst1.32    {d22[0]}, [%[doutc6r0]]!\n"
               "vst1.32    {d23[0]}, [%[doutc7r0]]!\n"
               "bne    1b\n" /* jump to main loop*/
               : [doutc0r0] "+r"(dout0),
                 [doutc1r0] "+r"(dout1),
                 [doutc2r0] "+r"(dout2),
                 [doutc3r0] "+r"(dout3),
                 [doutc4r0] "+r"(dout4),
                 [doutc5r0] "+r"(dout5),
                 [doutc6r0] "+r"(dout6),
                 [doutc7r0] "+r"(dout7),
                 [ptr_din] "+r"(din),
                 [cnt] "+r"(cnt)
               : [scale0] "w"(scale0),
                 [scale1] "w"(scale1),
                 [bias0] "w"(bias0),
                 [bias1] "w"(bias1),
3716
                 [vmax] "r"(vmax),
3717 3718 3719 3720 3721
                 [flag_act] "r"(flag_act), 
                 [alpha] "r"(alpha)
               : "cc", "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6",
                 "q7", "q8", "q9", "q10", "q11"
               );
3722
#endif
3723
  // clang-format on
3724
}
Y
Yan Chunwei 已提交
3725

3726
/*wirte result in outputs
Y
Yan Chunwei 已提交
3727 3728
* input din: [n, c / 8, h, w * 8], output dout: [n, c, h, w]
*/
3729 3730 3731
template <typename Dtype>
inline void write_int32_nchwc8_to_nchw(const int* din,
                                       Dtype* dout,
Y
Yan Chunwei 已提交
3732 3733 3734 3735 3736 3737 3738 3739 3740
                                       int cs,
                                       int ce,
                                       int hs,
                                       int he,
                                       int ws,
                                       int we,
                                       int channel,
                                       int height,
                                       int width,
3741 3742
                                       int flag_act,
                                       float* alpha,
3743 3744 3745 3746
                                       float* bias,
                                       bool flag_bias,
                                       Dtype* trash_ptr,
                                       const float* scale) {
Y
Yan Chunwei 已提交
3747 3748
  int size_c_out = width * height;

3749 3750 3751 3752 3753 3754 3755 3756
  Dtype* doutc0r0 = dout + cs * size_c_out + hs * width + ws;
  Dtype* doutc1r0 = doutc0r0 + size_c_out;
  Dtype* doutc2r0 = doutc1r0 + size_c_out;
  Dtype* doutc3r0 = doutc2r0 + size_c_out;
  Dtype* doutc4r0 = doutc3r0 + size_c_out;
  Dtype* doutc5r0 = doutc4r0 + size_c_out;
  Dtype* doutc6r0 = doutc5r0 + size_c_out;
  Dtype* doutc7r0 = doutc6r0 + size_c_out;
Y
Yan Chunwei 已提交
3757 3758 3759 3760 3761

  const int* ptr_din = din;

  int size_h = (he > height ? height : he) - hs;  // size_h == hei_n

3762 3763
  int w_stride = we - ws;
  int valid_w = (we > width ? width : we) - ws;
Y
Yan Chunwei 已提交
3764 3765 3766 3767
  int cnt = valid_w / 4;

  float32x4_t w_scale0 = vld1q_f32(scale);
  float32x4_t w_scale1 = vld1q_f32(scale + 4);
3768 3769
  float32x4_t w_bias0 = flag_bias ? vld1q_f32(bias) : vdupq_n_f32(0.f);
  float32x4_t w_bias1 = flag_bias ? vld1q_f32(bias + 4) : vdupq_n_f32(0.f);
Y
Yan Chunwei 已提交
3770

3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798
  for (int i = 0; i < size_h; i++) {
    int size_w = i * width;
    Dtype* doutc0_ptr = doutc0r0 + size_w;  // doutc0r0 + width;
    Dtype* doutc1_ptr = doutc1r0 + size_w;
    Dtype* doutc2_ptr = doutc2r0 + size_w;
    Dtype* doutc3_ptr = doutc3r0 + size_w;
    Dtype* doutc4_ptr = doutc4r0 + size_w;
    Dtype* doutc5_ptr = doutc5r0 + size_w;
    Dtype* doutc6_ptr = doutc6r0 + size_w;
    Dtype* doutc7_ptr = doutc7r0 + size_w;
    if (ce > channel) {
      switch (ce - channel) {
        case 7:
          doutc1_ptr = trash_ptr;
        case 6:
          doutc2_ptr = trash_ptr;
        case 5:
          doutc3_ptr = trash_ptr;
        case 4:
          doutc4_ptr = trash_ptr;
        case 3:
          doutc5_ptr = trash_ptr;
        case 2:
          doutc6_ptr = trash_ptr;
        case 1:
          doutc7_ptr = trash_ptr;
        default:
          break;
Y
Yan Chunwei 已提交
3799 3800
      }
    }
3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817
    ptr_din = din + i * w_stride * 8;
    const int* din_hei_ptr = ptr_din;
    if (cnt > 0) {
      int32_nchwc8_kernel(doutc0_ptr,
                          doutc1_ptr,
                          doutc2_ptr,
                          doutc3_ptr,
                          doutc4_ptr,
                          doutc5_ptr,
                          doutc6_ptr,
                          doutc7_ptr,
                          din_hei_ptr,
                          cnt,
                          w_scale0,
                          w_scale1,
                          w_bias0,
                          w_bias1,
3818 3819
                          flag_act,
                          alpha);
3820 3821 3822 3823 3824 3825
    }
    if (we > width) {
      int offset = 32 * cnt;
      din_hei_ptr = ptr_din + offset;
      for (int j = ws + cnt * 4; j < width; ++j) {
        if (flag_bias) {
3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841
          *(doutc0_ptr++) = cvt_kernel<Dtype>(
              din_hei_ptr[0], scale[0], bias[0], flag_act, alpha[0]);
          *(doutc1_ptr++) = cvt_kernel<Dtype>(
              din_hei_ptr[1], scale[1], bias[1], flag_act, alpha[0]);
          *(doutc2_ptr++) = cvt_kernel<Dtype>(
              din_hei_ptr[2], scale[2], bias[2], flag_act, alpha[0]);
          *(doutc3_ptr++) = cvt_kernel<Dtype>(
              din_hei_ptr[3], scale[3], bias[3], flag_act, alpha[0]);
          *(doutc4_ptr++) = cvt_kernel<Dtype>(
              din_hei_ptr[4], scale[4], bias[4], flag_act, alpha[0]);
          *(doutc5_ptr++) = cvt_kernel<Dtype>(
              din_hei_ptr[5], scale[5], bias[5], flag_act, alpha[0]);
          *(doutc6_ptr++) = cvt_kernel<Dtype>(
              din_hei_ptr[6], scale[6], bias[6], flag_act, alpha[0]);
          *(doutc7_ptr++) = cvt_kernel<Dtype>(
              din_hei_ptr[7], scale[7], bias[7], flag_act, alpha[0]);
3842
        } else {
3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858
          *(doutc0_ptr++) = cvt_kernel<Dtype>(
              din_hei_ptr[0], scale[0], 0.f, flag_act, alpha[0]);
          *(doutc1_ptr++) = cvt_kernel<Dtype>(
              din_hei_ptr[1], scale[1], 0.f, flag_act, alpha[0]);
          *(doutc2_ptr++) = cvt_kernel<Dtype>(
              din_hei_ptr[2], scale[2], 0.f, flag_act, alpha[0]);
          *(doutc3_ptr++) = cvt_kernel<Dtype>(
              din_hei_ptr[3], scale[3], 0.f, flag_act, alpha[0]);
          *(doutc4_ptr++) = cvt_kernel<Dtype>(
              din_hei_ptr[4], scale[4], 0.f, flag_act, alpha[0]);
          *(doutc5_ptr++) = cvt_kernel<Dtype>(
              din_hei_ptr[5], scale[5], 0.f, flag_act, alpha[0]);
          *(doutc6_ptr++) = cvt_kernel<Dtype>(
              din_hei_ptr[6], scale[6], 0.f, flag_act, alpha[0]);
          *(doutc7_ptr++) = cvt_kernel<Dtype>(
              din_hei_ptr[7], scale[7], 0.f, flag_act, alpha[0]);
Y
Yan Chunwei 已提交
3859
        }
3860
        din_hei_ptr += 8;
Y
Yan Chunwei 已提交
3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083
      }
    }
  }
}

/*
* din [n, hei_n, ch_n, w]
* dout [n, ch_n, hei_n, w]
*/
template <typename dtype>
static bool write_to_output_numc(const dtype* din,
                                 dtype* dout,
                                 int ch_n,
                                 int hei_n,
                                 int cs,
                                 int ce,
                                 int hs,
                                 int he,
                                 int ws,
                                 int we,
                                 int channel,
                                 int height,
                                 int width,
                                 bool flag_relu,
                                 dtype* trash_ptr) {
  if (ch_n <= 0 || hei_n <= 0) {
    LOG(ERROR) << "ch_n and hei_n are more than zero";
    return false;
  }
  int size_c_out = width * height;

  dtype* out_array[ch_n];
  out_array[0] = dout + cs * size_c_out + hs * width + ws;

  for (int i = 1; i < ch_n; i++) {
    out_array[i] = out_array[i - 1] + size_c_out;
  }

  const dtype* ptr_din = din;

  int cremain = ce - channel;
  for (int i = 1; i <= cremain; i++) {
    out_array[ch_n - i] = trash_ptr;
  }

  int size_h = (he > height ? height : he) - hs;  // size_h == hei_n

  int size_w = we - ws;

  int size_c_in = ch_n * size_w;

  size_t valid_w_byte = width * sizeof(dtype);

  if (flag_relu) {
    for (int h = 0; h < size_h; h++) {
      const dtype* din_ptr = din + h * size_c_in;
      for (int i = 0; i < ch_n; i++) {
        dtype* dout_ptr = out_array[i] + h * width;
        for (int k = 0; k < width; k++) {
          *(dout_ptr++) = LITEMAX(din_ptr[k], 0);
        }
        din_ptr += size_w;
      }
    }
  } else {
    for (int h = 0; h < size_h; h++) {
      const dtype* din_ptr = din + h * size_c_in;
      for (int i = 0; i < ch_n; i++) {
        dtype* dout_ptr = out_array[i] + h * width;
        memcpy(dout_ptr, din_ptr, valid_w_byte);
        din_ptr += size_w;
      }
    }
  }
  return true;
}

/// ch_n == ce - cs ??
/// hei_n == he - hs ??
/// channel height width ? -> output
template <typename ditype, typename dotype>
static bool write2_to_output_numc(const ditype* din,
                                  dotype* dout,
                                  int ch_n,
                                  int hei_n,
                                  int cs,
                                  int ce,
                                  int hs,
                                  int he,
                                  int ws,
                                  int we,
                                  int channel,
                                  int height,
                                  int width,
                                  bool flag_relu,
                                  dotype* trash_ptr,
                                  float const* scales) {
  // static_assert(std::is_same<dotype, float>::value, "just support float");

  if (ch_n <= 0 || hei_n <= 0) {
    LOG(ERROR) << "ch_n and hei_n are more than zero";
    return false;
  }

  int size_c_out = width * height;

  dotype* out_array[ch_n];
  out_array[0] = dout + cs * size_c_out + hs * width + ws;

  for (int i = 1; i < ch_n; i++) {
    out_array[i] = out_array[i - 1] + size_c_out;
  }

  const ditype* ptr_din = din;

  int cremain = ce - channel;
  for (int i = 1; i <= cremain; i++) {
    out_array[ch_n - i] = trash_ptr;
  }

  int size_h = (he > height ? height : he) - hs;  // size_h == hei_n

  int size_w = we - ws;

  int size_c_in = ch_n * size_w;

  size_t valid_w_byte = width * sizeof(ditype);

  if (flag_relu) {
    for (int h = 0; h < size_h; h++) {
      ditype const* din_ptr = din + h * size_c_in;
      for (int i = 0; i < ch_n; i++) {
        float const ws = scales[(i + cs) % ch_n];
        dotype* dout_ptr = out_array[i] + h * width;
        for (int k = 0; k < width; k++) {
          *(dout_ptr++) = LITEMAX(din_ptr[k] * ws, 0);
        }
        din_ptr += size_w;
      }
    }
  } else {
    for (int h = 0; h < size_h; h++) {
      ditype const* din_ptr = din + h * size_c_in;
      for (int i = 0; i < ch_n; i++) {
        dotype* dout_ptr = out_array[i] + h * width;

        float const* ws = &scales[(i + cs) % ch_n];
        int32_to_dtype(din_ptr, dout_ptr, ws, 1, 1, width);

        din_ptr += size_w;
      }
    }
  }
  return true;
}
/**
* innput din: nchwc(num)
*/
inline bool fill_packed_bias_nxmw_fp32(
    const float* bias, float* dout, int ch_n, int hei_n, int wround) {
  if (ch_n <= 0 || hei_n <= 0) {
    LOG(ERROR) << "ch_n and hei_n are more than zero";
    return false;
  }
  int cnt_ch = ch_n / 4;
  int size = wround * ch_n;
  for (int h = 0; h < hei_n; h++) {
    float* dout_ptr = dout + h * size;
    for (int i = 0; i < wround; i++) {
      const float* bias_ptr = bias;
      int j = 0;
      for (; j < cnt_ch; j++) {
        float32x4_t vb = vld1q_f32(bias_ptr);
        bias_ptr += 4;

        vst1q_f32(dout_ptr, vb);
        dout_ptr += 4;
      }
      j = j * 4;
      for (; j < ch_n; j++) {
        *dout_ptr = *bias_ptr;
        dout_ptr++;
        bias_ptr++;
      }
    }
  }
}

inline bool fill_packed_bias_nxmw_int8(
    const int* bias, int* dout, int ch_n, int hei_n, int wround) {
  if (ch_n <= 0 || hei_n <= 0) {
    LOG(ERROR) << "ch_n and hei_n are more than zero";
    return false;
  }
  int cnt_ch = ch_n / 4;
  int size = wround * ch_n;
  for (int h = 0; h < hei_n; h++) {
    int* dout_ptr = dout + h * size;
    for (int i = 0; i < wround; i++) {
      const int* bias_ptr = bias;
      int j = 0;
      for (; j < cnt_ch; j++) {
        int32x4_t vb = vld1q_s32(bias_ptr);
        bias_ptr += 4;

        vst1q_s32(dout_ptr, vb);
        dout_ptr += 4;
      }
      j = j * 4;
      for (; j < ch_n; j++) {
        *dout_ptr = *bias_ptr;
        dout_ptr++;
        bias_ptr++;
      }
    }
  }
  return true;
}

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