Repository: ggerganov/llama.cpp
File: ggml/src/ggml-cpu/llamafile/sgemm.cpp (C++)

Code before:
    float *C;
    const int64_t k;
    const int64_t lda;
    const int64_t ldb;
    const int64_t ldc;
    const int ith;
    const int nth;
};

class tinyBLAS_PPC {
  public:
    tinyBLAS_PPC(int64_t k,
                const float *A, int64_t lda,
                const float *B, int64_t ldb,
                float *C, int64_t ldc,
                int ith, int nth)
        : A(A), B(B), C(C), k(k), lda(lda), ldb(ldb), ldc(ldc), ith(ith), nth(nth) {
    }

    void matmul(int64_t m, int64_t n) {
       mnpack(0, m, 0, n);
    }

  private:

    void (tinyBLAS_PPC::*kernel)(int64_t, int64_t);

    inline void vector_permute_store_4(vector float *src, float *vecOffset) {
       vector float t1, t2, t3, t4, t5, t6, t7, t8;
           t1 = vec_mergeh(src[0], src[1]);
           t2 = vec_mergeh(src[2], src[3]);
           t3 = vec_mergel(src[0], src[1]);
           t4 = vec_mergel(src[2], src[3]);

           t5 = vec_xxpermdi(t1, t2, 0);
           t6 = vec_xxpermdi(t1, t2, 3);
           t7 = vec_xxpermdi(t3, t4, 0);
           t8 = vec_xxpermdi(t3, t4, 3);

           vec_xst(t5, 0, vecOffset);
           vec_xst(t6, 0, vecOffset + 4);
           vec_xst(t7, 0, vecOffset + 8);
           vec_xst(t8, 0, vecOffset + 12);
       }

    inline void vector_permute_store_8(vector float *src, float *vecOffset) {
       vector float t1, t2, t3, t4, t5, t6, t7, t8;
          t1 = vec_mergeh(src[0], src[1]);
          t2 = vec_mergeh(src[2], src[3]);
          t3 = vec_mergeh(src[4], src[5]);
          t4 = vec_mergeh(src[6], src[7]);

Review comment (suggestion):
```suggestion
        if (m % mc == 0 && n % nc == 0 && k % kc == 0) {
            matmul_tiled(m, n, mc, nc, kc);
        } else {
            mnpack(0, m, 0, n);
        }
```

Code after:
    float *C;
    const int64_t k;
    const int64_t lda;
    const int64_t ldb;
    const int64_t ldc;
    const int ith;
    const int nth;
};

class tinyBLAS_PPC {
  public:
    tinyBLAS_PPC(int64_t k,
                const float * A, int64_t lda,
                const float * B, int64_t ldb,
                float * C, int64_t ldc,
                int ith, int nth)
        : A(A), B(B), C(C), k(k), lda(lda), ldb(ldb), ldc(ldc), ith(ith), nth(nth) {
    }

    void matmul(int64_t m, int64_t n) {
        int64_t mc = 256; int64_t nc = 256; int64_t kc = 256;
        if (m % mc == 0 && n % nc == 0 && k % kc == 0) {
            matmul_tiled(m, n, mc, nc, kc);
        } else {
            mnpack(0, m, 0, n);
        }
    }

  private:

    inline void save_acc(acc_t * ACC, int64_t ii, int64_t jj) {
        vec_t vec_C[4];
        __builtin_mma_disassemble_acc(vec_C, ACC);
        for (int I = 0; I < 4; I++) {
            for (int J = 0; J < 4; J++) {
                *((float *)(C+ii+((jj+J)*ldc)+I)) = *((float *)&vec_C[I]+J);
            }
        }
    }

    inline void add_save_acc(acc_t * ACC, int64_t ii, int64_t jj) {
        vec_t vec_C[4];
        __builtin_mma_disassemble_acc(vec_C, ACC);
        for (int I = 0; I < 4; I++) {
            for (int J = 0; J < 4; J++) {
                float * c_ptr = (float *)(C+ii+((jj+J)*ldc)+I);
                *c_ptr += *((float *)&vec_C[I]+J);
            }
        }
    }