// File: /workspace/github_cpp_makefile_star2_O2/btbujiangjun[P]abcdl/src/algebra/MatrixHelper.cpp
// Function: abcdl::algebra::MatrixHelper<int>::convn(abcdl::algebra::Matrix<int>&, abcdl::algebra::Matrix<int> const&, abcdl::algebra::Matrix<int> const&, unsigned long, abcdl::algebra::Convn_type)
bool MatrixHelper<T>::convn(Matrix<T>& result,
                            const Matrix<T>& mat,
                            const Matrix<T>& kernal,
                            const size_t stride,
                            const Convn_type type){
    size_t rows       = mat.rows();
    size_t cols       = mat.cols();
    size_t kernal_row = kernal.rows();
    size_t kernal_col = kernal.cols();

    size_t data_row;
    size_t data_col;

    T* data;
    T* new_data;
    T* src_data = mat.data();

    if(type == FULL && kernal_row * kernal_col != 1){
        data_row = mat.rows() + 2 * (kernal_row - 1);
        data_col = mat.cols() + 2 * (kernal_col - 1);

        data = new T[data_row * data_col];
        memset(data, 0, sizeof(T) * data_row * data_col);

        //padding 0, (kernal_row - 1)*(kernal_col - 1)
        for(size_t i = 0; i != rows; i++){
            memcpy(&data[(i + kernal_row - 1) * data_col + kernal_col - 1], &src_data[i * cols], sizeof(T)* cols);
        }
    }else if(type == VALID){
        data_row = rows;
        data_col = cols;
        data     = src_data;
        
        if(data_row < kernal_row || data_col < kernal_col){
            LOG(FATAL) << "Convn error: kernal size large than mat.";
            return false;
        }
    }else{
        //todo
        LOG(FATAL) << "todo:not support SAME type";
        return false;
    }


    size_t conv_row = (data_row - kernal_row) % stride == 0 ? (data_row - kernal_row) / stride + 1 : (data_row - kernal_row) / stride + 2;
    size_t conv_col = (data_col - kernal_col) % stride == 0 ? (data_col - kernal_col) / stride + 1 : (data_col - kernal_col) / stride + 2;

    new_data = new T[conv_row * conv_col];
    T* kernal_data = kernal.data();
    
    size_t size = conv_row * conv_col * kernal_row * kernal_col;
    size_t num_thread = _po.get_num_thread(size, _po.get_block_size(size));
    size_t block_size = conv_row / num_thread;
    if(conv_row % num_thread != 0){
        block_size += 1;
    }

    std::vector<std::thread> threads(num_thread);
    for(size_t i = 0; i != num_thread; i++){
        threads[i] = std::thread(
            [&data, &new_data, &kernal_data, &data_row, &data_col, &kernal_row, &kernal_col, &conv_col, &stride](size_t start_idx, size_t end_idx){
				for(size_t ti = start_idx; ti < end_idx; ti++){
                    for(size_t tj = 0; tj != conv_col; tj++){
                        T sum = 0;
                        for(size_t k_i = 0; k_i != kernal_row; k_i++){
                           size_t row = ti * stride + k_i;
                            for(size_t k_j = 0; k_j != kernal_col; k_j++){
                                size_t col = tj * stride + k_j;
                                //skip out of range, in other word, fill 0
                                if(row < data_row && col < data_col){
                                    T a = data[row * data_col + col];
                                    T b = kernal_data[k_i * kernal_col + k_j];
                                    if(a != 0 && b != 0){
                                        sum += a * b;
                                    }
                                }
                            }
                        }
                        new_data[ti * conv_col + tj] = sum;
                    }
				}
            }, i * block_size , std::min(conv_row, (i + 1) * block_size)
        );
    }

    for(auto& thread : threads){
        thread.join();
    }

    result.set_shallow_data(new_data, conv_row, conv_col);

    if(type == abcdl::algebra::FULL){
    	delete[] data;
    }

    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x258, %rsp            # imm = 0x258
movq %rcx, %rbp
movq %rdi, %r13
movq %r8, 0xc0(%rsp)
movq (%rdx), %rbx
movq 0x8(%rdx), %r14
movq (%rcx), %rcx
movq %rcx, 0xb8(%rsp)
movq 0x8(%rbp), %r12
movq %r12, 0xb0(%rsp)
movq 0x10(%rdx), %r15
testl %r9d, %r9d
je 0x276c3
cmpl $0x1, %r9d
jne 0x276d0
movq %rsi, 0x20(%rsp)
movl %r9d, 0x4(%rsp)
movq %rbx, 0x40(%rsp)
movq %r14, 0x38(%rsp)
movq %r15, 0x10(%rsp)
cmpq %rcx, %rbx
setb %al
cmpq %r12, %r14
movq %rcx, %r15
setb %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x27800
leaq 0xc556(%rip), %rsi      # 0x33bf0
leaq 0xd0(%rsp), %rbx
pushq $0x3
popq %rcx
movq %rbx, %rdi
movl $0x149, %edx            # imm = 0x149
callq 0x2ee18
leaq 0xc595(%rip), %rsi      # 0x33c4e
movq %rbx, %rdi
callq 0x9370
jmp 0x276fe
movq %r12, %rax
imulq %rcx, %rax
cmpq $0x1, %rax
jne 0x27712
leaq 0xc519(%rip), %rsi      # 0x33bf0
leaq 0xd0(%rsp), %rbx
pushq $0x3
popq %rcx
movq %rbx, %rdi
movl $0x14e, %edx            # imm = 0x14E
callq 0x2ee18
leaq 0xc581(%rip), %rsi      # 0x33c77
movq %rbx, %rdi
callq 0x9370
leaq 0xd0(%rsp), %rdi
callq 0x2f054
xorl %eax, %eax
jmp 0x27a41
movq %r8, 0x68(%rsp)
movq %rbp, 0x70(%rsp)
movq %r13, 0x78(%rsp)
movq %rsi, 0x20(%rsp)
movl %r9d, 0x4(%rsp)
leaq -0x2(%rbx,%rcx,2), %rax
movq %rax, 0x40(%rsp)
leaq (%r14,%r12,2), %rdx
movq %rdx, 0x50(%rsp)
leaq -0x2(%r14,%r12,2), %r13
movq %r13, 0x38(%rsp)
movq %r13, %rbp
movq %rax, 0x60(%rsp)
imulq %rax, %rbp
movq %rbp, %rax
shrq $0x3e, %rax
shlq $0x2, %rbp
xorl %edi, %edi
negq %rax
sbbq %rdi, %rdi
orq %rbp, %rdi
movq %rcx, 0x18(%rsp)
callq 0x9030
movq %rax, 0x48(%rsp)
movq %rax, 0x10(%rsp)
movq %rax, %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x91e0
shlq $0x2, %r14
movq 0x18(%rsp), %rax
decq %rax
movq %r13, 0x58(%rsp)
imulq %r13, %rax
leaq (,%r12,4), %rcx
leaq (%rcx,%rax,4), %rax
movq 0x48(%rsp), %rcx
leaq (%rcx,%rax), %rbp
addq $-0x4, %rbp
movq 0x50(%rsp), %rax
leaq -0x8(,%rax,4), %r13
subq $0x1, %rbx
jb 0x277e2
movq %rbp, %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x92c0
addq %r13, %rbp
addq %r14, %r15
jmp 0x277c6
movq 0x58(%rsp), %r14
movq 0x60(%rsp), %rbx
movq 0x78(%rsp), %r13
movq 0x70(%rsp), %rbp
movq 0x18(%rsp), %r15
movq 0x68(%rsp), %r8
subq %r15, %rbx
movq %rbx, %rax
xorl %edx, %edx
divq %r8
movq %rax, %rbx
xorl %edi, %edi
cmpq $0x1, %rdx
sbbq $0x0, %rbx
addq $0x2, %rbx
movq %rbx, 0x8(%rsp)
subq %r12, %r14
movq %r14, %rax
xorl %edx, %edx
divq %r8
movq %rax, %r14
cmpq $0x1, %rdx
sbbq $0x0, %r14
addq $0x2, %r14
movq %r14, 0x28(%rsp)
imulq %rbx, %r14
movq %r14, %rax
shrq $0x3e, %rax
leaq (,%r14,4), %rcx
negq %rax
sbbq %rdi, %rdi
orq %rcx, %rdi
callq 0x9030
movq %rax, 0x30(%rsp)
movq 0x10(%rbp), %rax
movq %rax, 0xa8(%rsp)
imulq %r15, %r12
imulq %r14, %r12
movq %r13, %rdi
movq %r12, %rsi
callq 0xa05a
cmpq %rax, 0x8(%r13)
jae 0x27891
movq (%r13), %r14
jmp 0x278a7
movq %rax, %rcx
movq %r12, %rax
xorl %edx, %edx
divq %rcx
movq %rax, %r14
cmpq $0x1, %rdx
sbbq $-0x1, %r14
movq %rbx, %rax
xorl %edx, %edx
divq %r14
movq %rax, %rbx
cmpq $0x1, %rdx
sbbq $-0x1, %rbx
leaq 0x90(%rsp), %rdi
leaq 0xd0(%rsp), %rdx
movq %r14, %rsi
callq 0xa09c
xorl %ebp, %ebp
leaq 0xc8(%rsp), %r15
leaq 0x88(%rsp), %r13
xorl %r12d, %r12d
subq $0x1, %r14
jb 0x279e0
leaq 0x10(%rsp), %rax
movq %rax, 0xd0(%rsp)
leaq 0x30(%rsp), %rax
movq %rax, 0xd8(%rsp)
leaq 0xa8(%rsp), %rax
movq %rax, 0xe0(%rsp)
leaq 0x40(%rsp), %rax
movq %rax, 0xe8(%rsp)
leaq 0x38(%rsp), %rax
movq %rax, 0xf0(%rsp)
leaq 0xb8(%rsp), %rax
movq %rax, 0xf8(%rsp)
leaq 0xb0(%rsp), %rax
movq %rax, 0x100(%rsp)
leaq 0x28(%rsp), %rax
movq %rax, 0x108(%rsp)
leaq 0xc0(%rsp), %rax
movq %rax, 0x110(%rsp)
movq %r12, %rax
imulq %rbx, %rax
movq %rax, 0x88(%rsp)
incq %r12
movq %r12, %rax
imulq %rbx, %rax
cmpq 0x8(%rsp), %rax
movq %rax, 0x80(%rsp)
leaq 0x8(%rsp), %rcx
leaq 0x80(%rsp), %rax
cmovbq %rax, %rcx
movq %r15, %rdi
leaq 0xd0(%rsp), %rsi
movq %r13, %rdx
callq 0x27a88
movq 0x90(%rsp), %rdi
addq %rbp, %rdi
movq %r15, %rsi
callq 0xa174
movq %r15, %rdi
callq 0xa18e
addq $0x8, %rbp
jmp 0x278e7
movq 0x90(%rsp), %rbx
movq 0x98(%rsp), %r14
cmpq %r14, %rbx
je 0x27a03
movq %rbx, %rdi
callq 0x95b0
addq $0x8, %rbx
jmp 0x279f0
movq 0x30(%rsp), %rsi
movq 0x8(%rsp), %rdx
movq 0x28(%rsp), %rcx
movq 0x20(%rsp), %rdi
callq 0x17012
cmpl $0x0, 0x4(%rsp)
jne 0x27a32
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x27a32
callq 0x9420
leaq 0x90(%rsp), %rdi
callq 0xa19c
movb $0x1, %al
addq $0x258, %rsp            # imm = 0x258
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x27a5c
jmp 0x27a70
movq %rax, %rbx
jmp 0x27a80
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
callq 0x2f054
jmp 0x27a80
jmp 0x27a70
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0xa19c
movq %rbx, %rdi
callq 0x95e0