gemm.cpp 30.7 KB
Newer Older
W
wangliu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
Z
zhaojiaying01 已提交
14

Z
zhaojiaying01 已提交
15
#include "operators/math/gemm.h"
16 17
#include "common/log.h"
#include "memory/t_malloc.h"
Z
zhaojiaying01 已提交
18 19 20
#ifndef X86
#include <arm_neon.h>
#endif
Z
zhaojiaying01 已提交
21 22 23 24

namespace paddle_mobile {
namespace operators {
namespace math {
Z
zhaojiaying01 已提交
25 26 27
alignas(64) float packedA[MC * KC];
alignas(64) float packedB[KC * NC];
alignas(64) float ab[MR * NR];
Z
zhaojiaying01 已提交
28
// 将A矩阵分块复制到连续内存(ColMajor)
Z
zhaojiaying01 已提交
29 30 31
void PackMatrixA(int m, int k, int m_tail, const float *A, int lda,
                 float *buffer) {
  int i, j;
Z
zhaojiaying01 已提交
32
  const float *Aij;
Z
zhaojiaying01 已提交
33 34
  for (i = 0; i < m - m_tail; i += MR) {
    for (j = 0; j < k; ++j) {
Z
zhaojiaying01 已提交
35 36 37 38 39 40 41
      Aij = &A(i, j);
      *buffer++ = *Aij;
      *buffer++ = *(Aij + 1);
      *buffer++ = *(Aij + 2);
      *buffer++ = *(Aij + 3);
    }
  }
Z
zhaojiaying01 已提交
42
  if (m_tail != 0) {
Z
zhaojiaying01 已提交
43
    for (j = 0; j < k; ++j) {
Z
zhaojiaying01 已提交
44 45
      Aij = &A(m - m_tail, j);
      for (i = 0; i < m_tail; ++i) {
Z
zhaojiaying01 已提交
46 47
        *buffer++ = *(Aij + i);
      }
Z
zhaojiaying01 已提交
48
      for (i = m_tail; i < MR; ++i) {
Z
zhaojiaying01 已提交
49 50 51 52 53 54
        *buffer++ = 0;
      }
    }
  }
}

Z
zhaojiaying01 已提交
55
// 将A矩阵分块复制到连续内存(RowMajor)
Z
zhaojiaying01 已提交
56 57 58
void PackMatrixA_(int m, int k, int m_tail, const float *A, int lda,
                  float *buffer) {
  int i, j;
Z
zhaojiaying01 已提交
59
  const float *Ai, *Ai1, *Ai2, *Ai3;
Z
zhaojiaying01 已提交
60
  for (i = 0; i < m - m_tail; i += MR) {
Z
zhaojiaying01 已提交
61 62 63 64 65 66 67 68 69 70 71
    Ai = &A(i, 0);
    Ai1 = &A(i + 1, 0);
    Ai2 = &A(i + 2, 0);
    Ai3 = &A(i + 3, 0);
    for (int j = 0; j < k; ++j) {
      *buffer++ = *Ai++;
      *buffer++ = *Ai1++;
      *buffer++ = *Ai2++;
      *buffer++ = *Ai3++;
    }
  }
Z
zhaojiaying01 已提交
72
  if (m_tail != 0) {
Z
zhaojiaying01 已提交
73
    for (j = 0; j < k; ++j) {
Z
zhaojiaying01 已提交
74
      for (i = m - m_tail; i < m; ++i) {
Z
zhaojiaying01 已提交
75 76
        *buffer++ = A(i, j);
      }
Z
zhaojiaying01 已提交
77
      for (i = m; i < m + (MR - m_tail); ++i) {
Z
zhaojiaying01 已提交
78 79 80 81 82 83 84
        *buffer++ = 0;
      }
    }
  }
}

// 将B矩阵分块复制到连续内存(ColMajor)
Z
zhaojiaying01 已提交
85 86 87
void PackMatrixB(int k, int n, int n_tail, const float *B, int ldb,
                 float *buffer) {
  int i, j;
Z
zhaojiaying01 已提交
88
  const float *Bj, *Bj1, *Bj2, *Bj3;
Z
zhaojiaying01 已提交
89
  for (j = 0; j < n - n_tail; j += NR) {
Z
zhaojiaying01 已提交
90 91 92 93
    Bj = &B(0, j);
    Bj1 = &B(0, j + 1);
    Bj2 = &B(0, j + 2);
    Bj3 = &B(0, j + 3);
Z
zhaojiaying01 已提交
94 95 96 97 98 99 100
    for (i = 0; i < k; ++i) {
      *buffer++ = *Bj++;
      *buffer++ = *Bj1++;
      *buffer++ = *Bj2++;
      *buffer++ = *Bj3++;
    }
  }
Z
zhaojiaying01 已提交
101
  if (n_tail != 0) {
Z
zhaojiaying01 已提交
102
    for (i = 0; i < k; ++i) {
Z
zhaojiaying01 已提交
103
      for (int j = n - n_tail; j < n; ++j) {
Z
zhaojiaying01 已提交
104 105
        *buffer++ = B(i, j);
      }
Z
zhaojiaying01 已提交
106
      for (int j = n; j < n + (NR - n_tail); ++j) {
Z
zhaojiaying01 已提交
107 108 109 110 111 112 113
        *buffer++ = 0;
      }
    }
  }
}

// 将B矩阵分块复制到连续内存(RowMajor)
Z
zhaojiaying01 已提交
114 115 116
void PackMatrixB_(int k, int n, int n_tail, const float *B, int ldb,
                  float *buffer) {
  int i, j;
Z
zhaojiaying01 已提交
117
  const float *Bij;
Z
zhaojiaying01 已提交
118
  for (j = 0; j < n - n_tail; j += NR) {
119 120
#ifdef ARMV7

Z
zhaojiaying01 已提交
121 122
    for (i = 0; i < k; ++i) {
      Bij = &B(i, j);
Z
zhaojiaying01 已提交
123 124 125 126 127 128
      asm volatile(
          "vld1.32    {q0}, [%[Bij]]        \n\t"
          "vst1.32    {q0}, [%[buffer]]!    \n\t"
          : [buffer] "+r"(buffer)
          : [Bij] "r"(Bij)
          : "memory", "q0");
Z
zhaojiaying01 已提交
129
    }
130 131
#else
    for (i = 0; i < k; ++i) {
L
liuruilong 已提交
132 133 134 135 136 137
      Bij = &B(i, j);
      *buffer++ = *Bij;
      *buffer++ = *(Bij + 1);
      *buffer++ = *(Bij + 2);
      *buffer++ = *(Bij + 3);
    }
138
#endif
Z
zhaojiaying01 已提交
139
  }
Z
zhaojiaying01 已提交
140
  if (n_tail != 0) {
Z
zhaojiaying01 已提交
141
    for (i = 0; i < k; ++i) {
Z
zhaojiaying01 已提交
142 143
      Bij = &B(i, n - n_tail);
      for (int j = n - n_tail; j < n; ++j) {
Z
zhaojiaying01 已提交
144 145
        *buffer++ = *Bij++;
      }
Z
zhaojiaying01 已提交
146
      for (int j = n; j < n + (NR - n_tail); ++j) {
Z
zhaojiaying01 已提交
147 148 149 150 151 152 153
        *buffer++ = 0;
      }
    }
  }
}

// 分块矩阵乘法
154 155 156
void InnerKernel(int m, int n, int k, float alpha, const float *A, int lda,
                 const float *B, int ldb, float beta, float *C, int ldc,
                 int first_time) {
Z
zhaojiaying01 已提交
157 158
  int m_block = (m + MR - 1) / MR * MR;
  int n_block = (n + NR - 1) / NR * NR;
Z
zhaojiaying01 已提交
159

Z
zhaojiaying01 已提交
160 161
  int m_tail = m % MR;
  int n_tail = n % NR;
Z
zhaojiaying01 已提交
162 163

  if (first_time) {
Z
zhaojiaying01 已提交
164
    PackMatrixB_(k, n, n_tail, B, ldb, packedB);
Z
zhaojiaying01 已提交
165
  }
Z
zhaojiaying01 已提交
166
  PackMatrixA_(m, k, m_tail, A, lda, packedA);
Z
zhaojiaying01 已提交
167 168 169 170

  int i, j, mc, nc;

  // B 取 4 列, 打包预热
Z
zhaojiaying01 已提交
171 172
  for (j = 0; j < n_block; j += NR) {
    nc = (n - j) < NR ? n_tail : NR;
Z
zhaojiaying01 已提交
173
    // A 取 4 行,打包预热
Z
zhaojiaying01 已提交
174 175
    for (i = 0; i < m_block; i += MR) {
      mc = (m - i) < MR ? m_tail : MR;
176 177
      AddDot4x4(k, alpha, &packedA[i * k], 4, &packedB[j * k], k, beta,
                &C(i, j), ldc, mc, nc);
Z
zhaojiaying01 已提交
178 179 180 181
    }
  }
}

L
liuruilong 已提交
182 183
// 分块矩阵乘法
void InnerKernel_relu(int m, int n, int k, float alpha, const float *A, int lda,
L
liuruilong 已提交
184 185
                      const float *B, int ldb, float beta, float *C, int ldc,
                      int first_time, bool relu = false) {
Z
zhaojiaying01 已提交
186 187
  int m_block = (m + MR - 1) / MR * MR;
  int n_block = (n + NR - 1) / NR * NR;
L
liuruilong 已提交
188

Z
zhaojiaying01 已提交
189 190
  int m_tail = m % MR;
  int n_tail = n % NR;
L
liuruilong 已提交
191 192

  if (first_time) {
Z
zhaojiaying01 已提交
193
    PackMatrixB_(k, n, n_tail, B, ldb, packedB);
L
liuruilong 已提交
194
  }
Z
zhaojiaying01 已提交
195
  PackMatrixA_(m, k, m_tail, A, lda, packedA);
L
liuruilong 已提交
196 197 198 199

  int i, j, mc, nc;

  // B 取 4 列, 打包预热
Z
zhaojiaying01 已提交
200 201
  for (j = 0; j < n_block; j += NR) {
    nc = (n - j) < NR ? n_tail : NR;
L
liuruilong 已提交
202
    // A 取 4 行,打包预热
Z
zhaojiaying01 已提交
203 204
    for (i = 0; i < m_block; i += MR) {
      mc = (m - i) < MR ? m_tail : MR;
L
liuruilong 已提交
205
      AddDot4x4_relu(k, alpha, &packedA[i * k], 4, &packedB[j * k], k, beta,
L
liuruilong 已提交
206
                     &C(i, j), ldc, mc, nc, relu);
L
liuruilong 已提交
207 208 209 210
    }
  }
}

211
// 计算一个更小的 4 * 4 的 C 矩阵分块
Z
zhaojiaying01 已提交
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 259 260 261
#if defined(IOS)
void AddDot4x4(int k, float alpha, const float *a, int lda, const float *b,
               int ldb, float beta, float *C, int ldc, int mc, int nc) {
  // init C
  float32x4_t cv0 = vdupq_n_f32(0.0);
  float32x4_t cv1 = vdupq_n_f32(0.0);
  float32x4_t cv2 = vdupq_n_f32(0.0);
  float32x4_t cv3 = vdupq_n_f32(0.0);

  float32x4_t av;
  float32x4_t bv;

  float32x2_t av01;
  float32x2_t av23;

  for (int p = 0; p < k; p += 1) {
    av = vld1q_f32(a);
    bv = vld1q_f32(b);

    av01 = vget_low_f32(av);
    cv0 = vmlaq_lane_f32(cv0, bv, av01, 0);
    cv1 = vmlaq_lane_f32(cv1, bv, av01, 1);
    av23 = vget_high_f32(av);
    cv2 = vmlaq_lane_f32(cv2, bv, av23, 0);
    cv3 = vmlaq_lane_f32(cv3, bv, av23, 1);

    a += MR;
    b += NR;
  }
  float32x4x4_t cv = {cv0, cv1, cv2, cv3};
  int i, j;
  for (i = 0; i < mc; ++i) {
    for (j = 0; j < nc; ++j) {
      if (beta == 0.0) {
        C(i, j) = 0.0;
      } else if (beta != 1.0) {
        C(i, j) *= beta;
      }
      if (j == 0) {
        C(i, j) += alpha * vgetq_lane_f32(cv.val[i], 0);
      } else if (j == 1) {
        C(i, j) += alpha * vgetq_lane_f32(cv.val[i], 1);
      } else if (j == 2) {
        C(i, j) += alpha * vgetq_lane_f32(cv.val[i], 2);
      } else if (j == 3) {
        C(i, j) += alpha * vgetq_lane_f32(cv.val[i], 3);
      }
    }
  }
}
L
liuruilong 已提交
262 263

void AddDot4x4_relu(int k, float alpha, const float *a, int lda, const float *b,
L
liuruilong 已提交
264 265 266
                    int ldb, float beta, float *C, int ldc, int mc, int nc,
                    bool relu = false) {
  // init C
L
liuruilong 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
  float32x4_t cv0 = vdupq_n_f32(0.0);
  float32x4_t cv1 = vdupq_n_f32(0.0);
  float32x4_t cv2 = vdupq_n_f32(0.0);
  float32x4_t cv3 = vdupq_n_f32(0.0);

  float32x4_t av;
  float32x4_t bv;

  float32x2_t av01;
  float32x2_t av23;

  for (int p = 0; p < k; p += 1) {
    av = vld1q_f32(a);
    bv = vld1q_f32(b);

    av01 = vget_low_f32(av);
    cv0 = vmlaq_lane_f32(cv0, bv, av01, 0);
    cv1 = vmlaq_lane_f32(cv1, bv, av01, 1);
    av23 = vget_high_f32(av);
    cv2 = vmlaq_lane_f32(cv2, bv, av23, 0);
    cv3 = vmlaq_lane_f32(cv3, bv, av23, 1);

    a += MR;
    b += NR;
  }
  float32x4x4_t cv = {cv0, cv1, cv2, cv3};
  int i, j;
  for (i = 0; i < mc; ++i) {
    for (j = 0; j < nc; ++j) {
      if (beta == 0.0) {
        C(i, j) = 0.0;
      } else if (beta != 1.0) {
        C(i, j) *= beta;
      }
      if (j == 0) {
        C(i, j) += alpha * vgetq_lane_f32(cv.val[i], 0);
      } else if (j == 1) {
        C(i, j) += alpha * vgetq_lane_f32(cv.val[i], 1);
      } else if (j == 2) {
        C(i, j) += alpha * vgetq_lane_f32(cv.val[i], 2);
      } else if (j == 3) {
        C(i, j) += alpha * vgetq_lane_f32(cv.val[i], 3);
      }
      if (C(i, j) < 0) {
        C(i, j) = 0;
      }
    }
  }
}

Z
zhaojiaying01 已提交
317 318 319
#elif defined(ARMV7)
void AddDot4x4(int k, float alpha, const float *a, int lda, const float *b,
               int ldb, float beta, float *C, int ldc, int mc, int nc) {
Z
zhaojiaying01 已提交
320
  int kc1 = k / 4, kc2 = k % 4;
Z
zhaojiaying01 已提交
321 322 323 324 325 326 327 328 329 330 331
  int bytes_ldc = 4 * ldc;
  int flag_alpha = (alpha == 1.0) ? 1 : 2;
  int flag_beta;
  if (beta == 0.0) {
    flag_beta = 0;
  } else if (beta == 1.0) {
    flag_beta = 1;
  } else {
    flag_beta = 2;
  }
  asm volatile(
Z
zhaojiaying01 已提交
332 333
      "pld        [%[a]]              \n\t"
      "pld        [%[b]]              \n\t"
Z
zhaojiaying01 已提交
334 335 336 337 338 339 340 341
      "vmov.f32   q10,    #0.0        \n\t"
      "vmov.f32   q11,    #0.0        \n\t"
      "vmov.f32   q12,    #0.0        \n\t"
      "vmov.f32   q13,    #0.0        \n\t"

      "subs       %[kc1], %[kc1], #1  \n\t"
      "blt        end_kc1_%=          \n\t"
      "loop_kc1_%=:                   \n\t"
Z
zhaojiaying01 已提交
342 343 344 345 346 347 348 349 350 351 352 353
      "pld        [%[a], #64]         \n\t"
      "pld        [%[b], #64]         \n\t"
      "vld1.32    {q0, q1}, [%[a]]!   \n\t"
      "vld1.32    {q2, q3}, [%[b]]!   \n\t"
      "vmla.f32   q10, q2, d0[0]      \n\t"
      "vmla.f32   q11, q2, d0[1]      \n\t"
      "vmla.f32   q12, q2, d1[0]      \n\t"
      "vmla.f32   q13, q2, d1[1]      \n\t"
      "vmla.f32   q10, q3, d2[0]      \n\t"
      "vmla.f32   q11, q3, d2[1]      \n\t"
      "vmla.f32   q12, q3, d3[0]      \n\t"
      "vmla.f32   q13, q3, d3[1]      \n\t"
Z
zhaojiaying01 已提交
354 355 356 357 358 359 360 361 362 363
      "vld1.32    {q0, q1}, [%[a]]!   \n\t"
      "vld1.32    {q2, q3}, [%[b]]!   \n\t"
      "vmla.f32   q10, q2, d0[0]      \n\t"
      "vmla.f32   q11, q2, d0[1]      \n\t"
      "vmla.f32   q12, q2, d1[0]      \n\t"
      "vmla.f32   q13, q2, d1[1]      \n\t"
      "vmla.f32   q10, q3, d2[0]      \n\t"
      "vmla.f32   q11, q3, d2[1]      \n\t"
      "vmla.f32   q12, q3, d3[0]      \n\t"
      "vmla.f32   q13, q3, d3[1]      \n\t"
Z
zhaojiaying01 已提交
364 365 366 367 368 369
      "subs       %[kc1], %[kc1], #1  \n\t"
      "bge        loop_kc1_%=         \n\t"
      "end_kc1_%=:                    \n\t"

      "subs       %[kc2], %[kc2], #1  \n\t"
      "blt        end_kc2_%=          \n\t"
Z
zhaojiaying01 已提交
370
      "loop_kc2_%=:                   \n\t"
Z
zhaojiaying01 已提交
371 372 373 374 375 376
      "vld1.32    {q0}, [%[a]]!       \n\t"
      "vld1.32    {q1}, [%[b]]!       \n\t"
      "vmla.f32   q10, q1, d0[0]      \n\t"
      "vmla.f32   q11, q1, d0[1]      \n\t"
      "vmla.f32   q12, q1, d1[0]      \n\t"
      "vmla.f32   q13, q1, d1[1]      \n\t"
Z
zhaojiaying01 已提交
377 378
      "subs       %[kc2], %[kc2], #1  \n\t"
      "bge        loop_kc2_%=         \n\t"
Z
zhaojiaying01 已提交
379 380 381 382 383 384 385
      "end_kc2_%=:                    \n\t"

      "cmp        %[mc],      #4      \n\t"
      "bne        temp_%=             \n\t"
      "cmp        %[nc],      #4      \n\t"
      "bne        temp_%=             \n\t"

Z
zhaojiaying01 已提交
386 387 388
      "vmov.f32   d8[0],    %[alpha]  \n\t"
      "vmov.f32   d8[1],    %[beta]   \n\t"

Z
zhaojiaying01 已提交
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
      "cmp        %[flag_alpha],  #1  \n\t"
      "bne        alpha_%=            \n\t"

      "alpha_%=:                      \n\t"
      "vmul.f32   q10, q10, d8[0]     \n\t"
      "vmul.f32   q11, q11, d8[0]     \n\t"
      "vmul.f32   q12, q12, d8[0]     \n\t"
      "vmul.f32   q13, q13, d8[0]     \n\t"

      "beta_%=:                       \n\t"
      "cmp        %[flag_beta],   #0  \n\t"
      "beq        memory_%=           \n\t"

      "mov        r4,     %[C]        \n\t"
      "mov        r6,     %[bytes_ldc]\n\t"
      "vld1.32    {q0}, [r4], r6      \n\t"
      "vld1.32    {q1}, [r4], r6      \n\t"
      "vld1.32    {q2}, [r4], r6      \n\t"
      "vld1.32    {q3}, [r4]          \n\t"
      "cmp        %[flag_beta],   #1  \n\t"
      "beq        beta_eq1_%=         \n\t"
      "bne        beta_ne1_%=         \n\t"

      "beta_eq1_%=:                   \n\t"
      "vadd.f32   q10, q10, q0        \n\t"
      "vadd.f32   q11, q11, q1        \n\t"
      "vadd.f32   q12, q12, q2        \n\t"
      "vadd.f32   q13, q13, q3        \n\t"
      "b          memory_%=           \n\t"

      "beta_ne1_%=:                   \n\t"
      "vmla.f32   q10, q0, d8[1]      \n\t"
      "vmla.f32   q11, q1, d8[1]      \n\t"
      "vmla.f32   q12, q2, d8[1]      \n\t"
      "vmla.f32   q13, q3, d8[1]      \n\t"

      "memory_%=:                     \n\t"
      "mov        r5,     %[C]        \n\t"
      "mov        r6,     %[bytes_ldc]\n\t"
      "vst1.32    {q10}, [r5], r6     \n\t"
      "vst1.32    {q11}, [r5], r6     \n\t"
      "vst1.32    {q12}, [r5], r6     \n\t"
      "vst1.32    {q13}, [r5]         \n\t"
      "b          end_%=              \n\t"

      "temp_%=:                       \n\t"
      "vst1.32    {q10, q11}, [%[ab]]!\n\t"
      "vst1.32    {q12, q13}, [%[ab]] \n\t"
      "end_%=:                        \n\t"
      :
      : [a] "r"(a), [b] "r"(b), [C] "r"(C), [ab] "r"(ab), [kc1] "r"(kc1),
        [kc2] "r"(kc2), [mc] "r"(mc), [nc] "r"(nc), [alpha] "r"(alpha),
        [beta] "r"(beta), [bytes_ldc] "r"(bytes_ldc),
        [flag_alpha] "r"(flag_alpha), [flag_beta] "r"(flag_beta)
Z
zhaojiaying01 已提交
443
      : "memory", "q0", "q1", "q2", "q3", "q4", "q10", "q11", "q12", "q13");
Z
zhaojiaying01 已提交
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468

  if (mc != MR || nc != NR) {
    int i, j;
    for (i = 0; i < mc; ++i) {
      for (j = 0; j < nc; ++j) {
        if (beta == 0.0) {
          if (alpha != 1.0) {
            C(i, j) = alpha * ab[i * MR + j];
          } else {
            C(i, j) = ab[i * MR + j];
          }
        } else {
          if (beta != 1.0) {
            C(i, j) *= beta;
          }
          if (alpha != 1.0) {
            C(i, j) += alpha * ab[i * MR + j];
          } else {
            C(i, j) += ab[i * MR + j];
          }
        }
      }
    }
  }
}
L
liuruilong 已提交
469 470

void AddDot4x4_relu(int k, float alpha, const float *a, int lda, const float *b,
L
liuruilong 已提交
471 472
                    int ldb, float beta, float *C, int ldc, int mc, int nc,
                    bool relu = false) {
L
liuruilong 已提交
473
  int kc1 = k / 4, kc2 = k % 4;
L
liuruilong 已提交
474 475 476 477 478 479 480 481 482 483 484
  int bytes_ldc = 4 * ldc;
  int flag_alpha = (alpha == 1.0) ? 1 : 2;
  int flag_beta;
  if (beta == 0.0) {
    flag_beta = 0;
  } else if (beta == 1.0) {
    flag_beta = 1;
  } else {
    flag_beta = 2;
  }
  asm volatile(
L
liuruilong 已提交
485 486
      "pld        [%[a]]              \n\t"
      "pld        [%[b]]              \n\t"
L
liuruilong 已提交
487 488 489 490
      "vmov.f32   q10,    #0.0        \n\t"
      "vmov.f32   q11,    #0.0        \n\t"
      "vmov.f32   q12,    #0.0        \n\t"
      "vmov.f32   q13,    #0.0        \n\t"
L
liuruilong 已提交
491

L
liuruilong 已提交
492 493 494
      "subs       %[kc1], %[kc1], #1  \n\t"
      "blt        end_kc1_%=          \n\t"
      "loop_kc1_%=:                   \n\t"
L
liuruilong 已提交
495 496 497 498 499 500 501 502 503 504 505 506
      "pld        [%[a], #64]         \n\t"
      "pld        [%[b], #64]         \n\t"
      "vld1.32    {q0, q1}, [%[a]]!   \n\t"
      "vld1.32    {q2, q3}, [%[b]]!   \n\t"
      "vmla.f32   q10, q2, d0[0]      \n\t"
      "vmla.f32   q11, q2, d0[1]      \n\t"
      "vmla.f32   q12, q2, d1[0]      \n\t"
      "vmla.f32   q13, q2, d1[1]      \n\t"
      "vmla.f32   q10, q3, d2[0]      \n\t"
      "vmla.f32   q11, q3, d2[1]      \n\t"
      "vmla.f32   q12, q3, d3[0]      \n\t"
      "vmla.f32   q13, q3, d3[1]      \n\t"
Z
zhaojiaying01 已提交
507 508 509 510 511 512 513 514 515 516
      "vld1.32    {q0, q1}, [%[a]]!   \n\t"
      "vld1.32    {q2, q3}, [%[b]]!   \n\t"
      "vmla.f32   q10, q2, d0[0]      \n\t"
      "vmla.f32   q11, q2, d0[1]      \n\t"
      "vmla.f32   q12, q2, d1[0]      \n\t"
      "vmla.f32   q13, q2, d1[1]      \n\t"
      "vmla.f32   q10, q3, d2[0]      \n\t"
      "vmla.f32   q11, q3, d2[1]      \n\t"
      "vmla.f32   q12, q3, d3[0]      \n\t"
      "vmla.f32   q13, q3, d3[1]      \n\t"
L
liuruilong 已提交
517 518 519 520 521 522
      "subs       %[kc1], %[kc1], #1  \n\t"
      "bge        loop_kc1_%=         \n\t"
      "end_kc1_%=:                    \n\t"

      "subs       %[kc2], %[kc2], #1  \n\t"
      "blt        end_kc2_%=          \n\t"
Z
zhaojiaying01 已提交
523
      "loop_kc2_%=:                   \n\t"
L
liuruilong 已提交
524 525 526 527 528 529
      "vld1.32    {q0}, [%[a]]!       \n\t"
      "vld1.32    {q1}, [%[b]]!       \n\t"
      "vmla.f32   q10, q1, d0[0]      \n\t"
      "vmla.f32   q11, q1, d0[1]      \n\t"
      "vmla.f32   q12, q1, d1[0]      \n\t"
      "vmla.f32   q13, q1, d1[1]      \n\t"
Z
zhaojiaying01 已提交
530 531
      "subs       %[kc2], %[kc2], #1  \n\t"
      "bge        loop_kc2_%=         \n\t"
L
liuruilong 已提交
532 533 534 535 536 537 538
      "end_kc2_%=:                    \n\t"

      "cmp        %[mc],      #4      \n\t"
      "bne        temp_%=             \n\t"
      "cmp        %[nc],      #4      \n\t"
      "bne        temp_%=             \n\t"

Z
zhaojiaying01 已提交
539 540 541
      "vmov.f32   d8[0],    %[alpha]  \n\t"
      "vmov.f32   d8[1],    %[beta]   \n\t"

L
liuruilong 已提交
542 543 544 545 546 547 548 549 550 551
      "cmp        %[flag_alpha],  #1  \n\t"
      "bne        alpha_%=            \n\t"

      "alpha_%=:                      \n\t"
      "vmul.f32   q10, q10, d8[0]     \n\t"
      "vmul.f32   q11, q11, d8[0]     \n\t"
      "vmul.f32   q12, q12, d8[0]     \n\t"
      "vmul.f32   q13, q13, d8[0]     \n\t"

      "beta_%=:                       \n\t"
L
liuruilong 已提交
552
      "cmp        %[flag_beta],   #0  \n\t"
L
liuruilong 已提交
553 554 555 556 557 558 559 560
      "beq        memory_%=           \n\t"

      "mov        r4,     %[C]        \n\t"
      "mov        r6,     %[bytes_ldc]\n\t"
      "vld1.32    {q0}, [r4], r6      \n\t"
      "vld1.32    {q1}, [r4], r6      \n\t"
      "vld1.32    {q2}, [r4], r6      \n\t"
      "vld1.32    {q3}, [r4]          \n\t"
L
liuruilong 已提交
561
      "cmp        %[flag_beta],   #1  \n\t"
L
liuruilong 已提交
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
      "beq        beta_eq1_%=         \n\t"
      "bne        beta_ne1_%=         \n\t"

      "beta_eq1_%=:                   \n\t"
      "vadd.f32   q10, q10, q0        \n\t"
      "vadd.f32   q11, q11, q1        \n\t"
      "vadd.f32   q12, q12, q2        \n\t"
      "vadd.f32   q13, q13, q3        \n\t"
      "b          memory_%=           \n\t"

      "beta_ne1_%=:                   \n\t"
      "vmla.f32   q10, q0, d8[1]      \n\t"
      "vmla.f32   q11, q1, d8[1]      \n\t"
      "vmla.f32   q12, q2, d8[1]      \n\t"
      "vmla.f32   q13, q3, d8[1]      \n\t"

      "memory_%=:                     \n\t"
Z
zhaojiaying01 已提交
579 580 581 582
      "vmax.f32 q10, q10, q14         \n\t"
      "vmax.f32 q11, q11, q14         \n\t"
      "vmax.f32 q12, q12, q14         \n\t"
      "vmax.f32 q13, q13, q14         \n\t"
L
liuruilong 已提交
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
      "mov        r5,     %[C]        \n\t"
      "mov        r6,     %[bytes_ldc]\n\t"
      "vst1.32    {q10}, [r5], r6     \n\t"
      "vst1.32    {q11}, [r5], r6     \n\t"
      "vst1.32    {q12}, [r5], r6     \n\t"
      "vst1.32    {q13}, [r5]         \n\t"
      "b          end_%=              \n\t"

      "temp_%=:                       \n\t"
      "vst1.32    {q10, q11}, [%[ab]]!\n\t"
      "vst1.32    {q12, q13}, [%[ab]] \n\t"
      "end_%=:                        \n\t"
      :
      : [a] "r"(a), [b] "r"(b), [C] "r"(C), [ab] "r"(ab), [kc1] "r"(kc1),
        [kc2] "r"(kc2), [mc] "r"(mc), [nc] "r"(nc), [alpha] "r"(alpha),
        [beta] "r"(beta), [bytes_ldc] "r"(bytes_ldc),
        [flag_alpha] "r"(flag_alpha), [flag_beta] "r"(flag_beta)
Z
zhaojiaying01 已提交
600 601
      : "memory", "q0", "q1", "q2", "q3", "q4", "q10", "q11", "q12", "q13",
        "q14");
L
liuruilong 已提交
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632

  if (mc != MR || nc != NR) {
    int i, j;
    for (i = 0; i < mc; ++i) {
      for (j = 0; j < nc; ++j) {
        if (beta == 0.0) {
          if (alpha != 1.0) {
            C(i, j) = alpha * ab[i * MR + j];
          } else {
            C(i, j) = ab[i * MR + j];
          }
        } else {
          if (beta != 1.0) {
            C(i, j) *= beta;
          }
          if (alpha != 1.0) {
            C(i, j) += alpha * ab[i * MR + j];
          } else {
            C(i, j) += ab[i * MR + j];
          }
        }
        if (relu) {
          if (C(i, j) < 0) {
            C(i, j) = 0;
          }
        }
      }
    }
  }
}

Z
zhaojiaying01 已提交
633
#else
634 635
void AddDot4x4(int k, float alpha, const float *a, int lda, const float *b,
               int ldb, float beta, float *C, int ldc, int mc, int nc) {
Z
zhaojiaying01 已提交
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
  float c[16] = {0};
  float reg_a0, reg_a1, reg_a2, reg_a3, reg_b0, reg_b1, reg_b2, reg_b3;

  for (int p = 0; p < k; p += 1) {
    reg_b0 = *b++;
    reg_b1 = *b++;
    reg_b2 = *b++;
    reg_b3 = *b++;

    reg_a0 = *a++;
    reg_a1 = *a++;
    reg_a2 = *a++;
    reg_a3 = *a++;

    // first row
    c[0] += reg_a0 * reg_b0;
    c[1] += reg_a0 * reg_b1;
    c[2] += reg_a0 * reg_b2;
    c[3] += reg_a0 * reg_b3;

    // second row
    c[4] += reg_a1 * reg_b0;
    c[5] += reg_a1 * reg_b1;
    c[6] += reg_a1 * reg_b2;
    c[7] += reg_a1 * reg_b3;

    // third row
    c[8] += reg_a2 * reg_b0;
    c[9] += reg_a2 * reg_b1;
    c[10] += reg_a2 * reg_b2;
    c[11] += reg_a2 * reg_b3;

    // fourth row
    c[12] += reg_a3 * reg_b0;
    c[13] += reg_a3 * reg_b1;
    c[14] += reg_a3 * reg_b2;
    c[15] += reg_a3 * reg_b3;
  }
  int i, j;
  for (i = 0; i < mc; ++i) {
    for (j = 0; j < nc; ++j) {
677 678 679 680 681 682 683 684 685 686
      if (beta == 0.0) {
        C(i, j) = 0.0;
      } else if (beta != 1.0) {
        C(i, j) *= beta;
      }
      if (alpha != 1.0) {
        C(i, j) += alpha * c[i * MR + j];
      } else {
        C(i, j) += c[i * MR + j];
      }
Z
zhaojiaying01 已提交
687 688 689
    }
  }
}
L
liuruilong 已提交
690 691

void AddDot4x4_relu(int k, float alpha, const float *a, int lda, const float *b,
L
liuruilong 已提交
692 693
                    int ldb, float beta, float *C, int ldc, int mc, int nc,
                    bool relu) {
L
liuruilong 已提交
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
  float c[16] = {0};
  float reg_a0, reg_a1, reg_a2, reg_a3, reg_b0, reg_b1, reg_b2, reg_b3;

  for (int p = 0; p < k; p += 1) {
    reg_b0 = *b++;
    reg_b1 = *b++;
    reg_b2 = *b++;
    reg_b3 = *b++;

    reg_a0 = *a++;
    reg_a1 = *a++;
    reg_a2 = *a++;
    reg_a3 = *a++;

    // first row
    c[0] += reg_a0 * reg_b0;
    c[1] += reg_a0 * reg_b1;
    c[2] += reg_a0 * reg_b2;
    c[3] += reg_a0 * reg_b3;

    // second row
    c[4] += reg_a1 * reg_b0;
    c[5] += reg_a1 * reg_b1;
    c[6] += reg_a1 * reg_b2;
    c[7] += reg_a1 * reg_b3;

    // third row
    c[8] += reg_a2 * reg_b0;
    c[9] += reg_a2 * reg_b1;
    c[10] += reg_a2 * reg_b2;
    c[11] += reg_a2 * reg_b3;

    // fourth row
    c[12] += reg_a3 * reg_b0;
    c[13] += reg_a3 * reg_b1;
    c[14] += reg_a3 * reg_b2;
    c[15] += reg_a3 * reg_b3;
  }
  int i, j;
  for (i = 0; i < mc; ++i) {
    for (j = 0; j < nc; ++j) {
      if (beta == 0.0) {
        C(i, j) = 0.0;
      } else if (beta != 1.0) {
        C(i, j) *= beta;
      }
      if (alpha != 1.0) {
        C(i, j) += alpha * c[i * MR + j];
      } else {
        C(i, j) += c[i * MR + j];
      }
      if (relu) {
        if (C(i, j) < 0) {
          C(i, j) = 0;
        }
      }
    }
  }
}

Z
zhaojiaying01 已提交
754
#endif
Z
zhaojiaying01 已提交
755 756 757 758 759

// 32位 float 矩阵乘法
void sgemm(int m, int n, int k, float alpha, const float *A, int lda,
           const float *B, int ldb, float beta, float *C, int ldc) {
  int i, j, p, mc, nc, kc;
760
  float beta_;
761 762

#ifdef ARMV7
763 764 765 766
  if (m == 1) {
    VectorKernel(1, n, k, alpha, A, lda, B, ldb, beta, C, ldc);
    return;
  }
767 768
#endif

Z
zhaojiaying01 已提交
769
  for (j = 0; j < n; j += NC) {
W
wangliu 已提交
770
    nc = s_min(n - j, NC);
Z
zhaojiaying01 已提交
771
    for (p = 0; p < k; p += KC) {
W
wangliu 已提交
772
      kc = s_min(k - p, KC);
Z
zhaojiaying01 已提交
773
      for (i = 0; i < m; i += MC) {
W
wangliu 已提交
774
        mc = s_min(m - i, MC);
775 776 777 778 779 780 781
        if (p != 0) {
          beta_ = 1.0;
        } else {
          beta_ = beta;
        }
        InnerKernel(mc, nc, kc, alpha, &A(i, p), lda, &B(p, j), ldb, beta_,
                    &C(i, j), ldc, i == 0);
Z
zhaojiaying01 已提交
782 783 784 785 786
      }
    }
  }
}

L
liuruilong 已提交
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
void sgemm_relu(int m, int n, int k, float alpha, const float *A, int lda,
                const float *B, int ldb, float beta, float *C, int ldc) {
  int i, j, p, mc, nc, kc;
  float beta_;
  for (j = 0; j < n; j += NC) {
    nc = s_min(n - j, NC);
    for (p = 0; p < k; p += KC) {
      kc = s_min(k - p, KC);
      for (i = 0; i < m; i += MC) {
        mc = s_min(m - i, MC);
        if (p != 0) {
          beta_ = 1.0;
        } else {
          beta_ = beta;
        }

        if (p + KC >= k) {
L
liuruilong 已提交
804 805
          InnerKernel_relu(mc, nc, kc, alpha, &A(i, p), lda, &B(p, j), ldb,
                           beta_, &C(i, j), ldc, i == 0, true);
L
liuruilong 已提交
806 807
        } else {
          InnerKernel(mc, nc, kc, alpha, &A(i, p), lda, &B(p, j), ldb, beta_,
L
liuruilong 已提交
808
                      &C(i, j), ldc, i == 0);
L
liuruilong 已提交
809 810 811 812 813 814
        }
      }
    }
  }
}

815
#ifdef ARMV7
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 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 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
void VectorKernel(int m, int n, int k, float alpha, const float *A, int lda,
                  const float *B, int ldb, float beta, float *C, int ldc) {
  float *bufferC = static_cast<float *>(memory::Alloc(sizeof(float) * n));

  const float *a0, *b0, *b1, *b2, *b3;
  float *c0, *C0;

  int volatile kc1 = k / 4;
  int volatile kc2 = k % 4;
  int volatile nc1 = n / 16;
  int _nc1 = n % 16;
  int volatile nc2 = _nc1 / 4;
  int volatile nc3 = _nc1 % 4;
  for (int i = 0; i < kc1; i++) {
    a0 = A + i * 4;
    b0 = B + i * 4 * ldb;
    b1 = b0 + ldb;
    b2 = b1 + ldb;
    b3 = b2 + ldb;
    c0 = bufferC;
    asm volatile(
        "pld        [%[a0], #16]          \n\t"
        "vld1.32    {q0}, [%[a0]]         \n\t"

        "subs       %[nc1], %[nc1], #1    \n\t"
        "blt        end_nc1_%=            \n\t"
        "loop_nc1_%=:                     \n\t"

        "cmp        %[i],       #0        \n\t"
        "beq        i_eq0_%=              \n\t"
        "bne        i_ne0_%=              \n\t"

        "i_eq0_%=:                        \n\t"
        "vmov.f32   q10,    #0.0          \n\t"
        "vmov.f32   q11,    #0.0          \n\t"
        "vmov.f32   q12,    #0.0          \n\t"
        "vmov.f32   q13,    #0.0          \n\t"
        "b          gemm_nc1_%=           \n\t"

        "i_ne0_%=:                        \n\t"
        "pld        [%[c0], #64]          \n\t"
        "vld1.32    {q10, q11}, [%[c0]]!  \n\t"
        "vld1.32    {q12, q13}, [%[c0]]   \n\t"
        "sub        %[c0], %[c0], #32     \n\t"

        "gemm_nc1_%=:                     \n\t"
        "pld        [%[b0], #64]          \n\t"
        "vld1.32    {q2, q3}, [%[b0]]!    \n\t"
        "vld1.32    {q4, q5}, [%[b0]]!    \n\t"
        "vmla.f32   q10, q2, d0[0]        \n\t"
        "vmla.f32   q11, q3, d0[0]        \n\t"
        "vmla.f32   q12, q4, d0[0]        \n\t"
        "vmla.f32   q13, q5, d0[0]        \n\t"

        "pld        [%[b1], #64]          \n\t"
        "vld1.32    {q2, q3}, [%[b1]]!    \n\t"
        "vld1.32    {q4, q5}, [%[b1]]!    \n\t"
        "vmla.f32   q10, q2, d0[1]        \n\t"
        "vmla.f32   q11, q3, d0[1]        \n\t"
        "vmla.f32   q12, q4, d0[1]        \n\t"
        "vmla.f32   q13, q5, d0[1]        \n\t"

        "pld        [%[b2], #64]          \n\t"
        "vld1.32    {q2, q3}, [%[b2]]!    \n\t"
        "vld1.32    {q4, q5}, [%[b2]]!    \n\t"
        "vmla.f32   q10, q2, d1[0]        \n\t"
        "vmla.f32   q11, q3, d1[0]        \n\t"
        "vmla.f32   q12, q4, d1[0]        \n\t"
        "vmla.f32   q13, q5, d1[0]        \n\t"

        "pld        [%[b3], #64]          \n\t"
        "vld1.32    {q2, q3}, [%[b3]]!    \n\t"
        "vld1.32    {q4, q5}, [%[b3]]!    \n\t"
        "vmla.f32   q10, q2, d1[1]        \n\t"
        "vmla.f32   q11, q3, d1[1]        \n\t"
        "vmla.f32   q12, q4, d1[1]        \n\t"
        "vmla.f32   q13, q5, d1[1]        \n\t"

        "vst1.32    {q10, q11}, [%[c0]]!  \n\t"
        "vst1.32    {q12, q13}, [%[c0]]!  \n\t"

        "subs       %[nc1], %[nc1], #1    \n\t"
        "bge        loop_nc1_%=           \n\t"
        "end_nc1_%=:                      \n\t"

        "subs       %[nc2], %[nc2], #1    \n\t"
        "blt        end_nc2_%=            \n\t"
        "loop_nc2_%=:                     \n\t"

        "cmp        %[i],       #0        \n\t"
        "beq        ii_eq0_%=             \n\t"
        "bne        ii_ne0_%=             \n\t"

        "ii_eq0_%=:                       \n\t"
        "vmov.f32   q10,    #0.0          \n\t"
        "b          gemm_nc2_%=           \n\t"

        "ii_ne0_%=:                       \n\t"
        "pld        [%[c0], #16]          \n\t"
        "vld1.32    {q10}, [%[c0]]        \n\t"

        "gemm_nc2_%=:                     \n\t"
        "pld        [%[b0], #16]          \n\t"
        "vld1.32    {q2}, [%[b0]]!        \n\t"
        "vmla.f32   q10, q2, d0[0]        \n\t"

        "pld        [%[b1], #16]          \n\t"
        "vld1.32    {q3}, [%[b1]]!        \n\t"
        "vmla.f32   q10, q3, d0[1]        \n\t"

        "pld        [%[b2], #16]          \n\t"
        "vld1.32    {q4}, [%[b2]]!        \n\t"
        "vmla.f32   q10, q4, d1[0]        \n\t"

        "pld        [%[b3], #16]          \n\t"
        "vld1.32    {q5}, [%[b3]]!        \n\t"
        "vmla.f32   q10, q5, d1[1]        \n\t"

        "vst1.32    {q10}, [%[c0]]!       \n\t"

        "subs       %[nc2], %[nc2], #1    \n\t"
        "bge        loop_nc2_%=           \n\t"
        "end_nc2_%=:                      \n\t"

        : [b0] "+r"(b0), [b1] "+r"(b1), [b2] "+r"(b2), [b3] "+r"(b3),
          [c0] "+r"(c0)
        : [a0] "r"(a0), [i] "r"(i), [nc1] "r"(nc1), [nc2] "r"(nc2)
        : "memory", "q0", "q2", "q3", "q4", "q5", "q10", "q11", "q12", "q13");

    for (int j = 0; j < nc3; j++) {
      if (i == 0) {
        *c0 = (*a0) * (*b0++);
      } else {
        *c0 += (*a0) * (*b0++);
      }
      *c0 += (*(a0 + 1)) * (*b1++);
      *c0 += (*(a0 + 2)) * (*b2++);
      *c0 += (*(a0 + 3)) * (*b3++);
      c0++;
    }
  }

  for (int i = 0; i < kc2; ++i) {
    a0 = A + 4 * kc1 + i;
    b0 = B + (4 * kc1 + i) * ldb;
    c0 = bufferC;
    asm volatile(
        "pld        [%[a0], #16]          \n\t"
        "vld1.32    {d0}, [%[a0]]         \n\t"

        "subs       %[nc1], %[nc1], #1    \n\t"
        "blt        end_nc1_%=            \n\t"
        "loop_nc1_%=:                     \n\t"

        "pld        [%[c0], #64]          \n\t"
        "vld1.32    {q10, q11}, [%[c0]]!  \n\t"
        "vld1.32    {q12, q13}, [%[c0]]   \n\t"
        "sub        %[c0], %[c0], #32     \n\t"

        "gemm_nc1_%=:                     \n\t"
        "pld        [%[b0], #64]          \n\t"
        "vld1.32    {q2, q3}, [%[b0]]!    \n\t"
        "vld1.32    {q4, q5}, [%[b0]]!    \n\t"
        "vmla.f32   q10, q2, d0[0]        \n\t"
        "vmla.f32   q11, q3, d0[0]        \n\t"
        "vmla.f32   q12, q4, d0[0]        \n\t"
        "vmla.f32   q13, q5, d0[0]        \n\t"

        "vst1.32    {q10, q11}, [%[c0]]!  \n\t"
        "vst1.32    {q12, q13}, [%[c0]]!  \n\t"

        "subs       %[nc1], %[nc1], #1    \n\t"
        "bge        loop_nc1_%=           \n\t"
        "end_nc1_%=:                      \n\t"

        "subs       %[nc2], %[nc2], #1    \n\t"
        "blt        end_nc2_%=            \n\t"
        "loop_nc2_%=:                     \n\t"

        "pld        [%[c0], #16]          \n\t"
        "vld1.32    {q10}, [%[c0]]        \n\t"

        "gemm_nc2_%=:                     \n\t"
        "vld1.32    {q2}, [%[b0]]!        \n\t"
        "vmla.f32   q10, q2, d0[0]        \n\t"

        "vst1.32    {q10}, [%[c0]]!       \n\t"

        "subs       %[nc2], %[nc2], #1    \n\t"
        "bge        loop_nc2_%=           \n\t"
        "end_nc2_%=:                      \n\t"

        : [b0] "+r"(b0), [b1] "+r"(b1), [b2] "+r"(b2), [b3] "+r"(b3),
          [c0] "+r"(c0)
        : [a0] "r"(a0), [nc1] "r"(nc1), [nc2] "r"(nc2)
        : "memory", "q0", "q2", "q3", "q4", "q5", "q10", "q11", "q12", "q13");

    for (int j = 0; j < nc3; j++) {
      *c0 += (*a0) * (*b0++);
      c0++;
    }
  }

  c0 = bufferC;
  C0 = C;
  for (int i = 0; i < n; i++) {
    if (beta == 1.0) {
      *C0++ += *c0++;
    } else {
      *C0++ = *c0++;
    }
  }
}
1029
#endif
1030

Z
zhaojiaying01 已提交
1031 1032 1033
}  // namespace math
}  // namespace operators
}  // namespace paddle_mobile