// File: /workspace/github_cpp_makefile_star2_O1/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 $0x228, %rsp            # imm = 0x228
movq %rcx, %r12
movabsq $0x3fffffffffffffff, %r10 # imm = 0x3FFFFFFFFFFFFFFF
movq %r8, 0x60(%rsp)
movq (%rdx), %rbx
movq 0x8(%rdx), %r13
movq (%rcx), %rcx
movq %rcx, 0x38(%rsp)
movq 0x8(%r12), %rax
movq %rax, 0x30(%rsp)
movq 0x10(%rdx), %rbp
testl %r9d, %r9d
jne 0x2febe
movq %rax, %rdx
imulq %rcx, %rdx
cmpq $0x1, %rdx
je 0x2febe
movq %rdi, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
movl %r9d, 0xc(%rsp)
leaq (%rbx,%rcx,2), %rcx
addq $-0x2, %rcx
movq %rcx, 0x28(%rsp)
leaq -0x2(,%rax,2), %rax
addq %r13, %rax
movq %rax, 0x18(%rsp)
imulq %rcx, %rax
leaq (,%rax,4), %r14
cmpq %r10, %rax
movq $-0x1, %r15
movq %r14, %rdi
cmovaq %r15, %rdi
callq 0x9030
movq %rax, 0x10(%rsp)
movq %rax, %rdi
xorl %esi, %esi
movq %r14, %rdx
callq 0x91d0
testq %rbx, %rbx
je 0x2ff32
shlq $0x2, %r13
negq %rbx
movq 0x38(%rsp), %rax
addq %r15, %rax
imulq 0x18(%rsp), %rax
movq 0x30(%rsp), %rcx
shlq $0x2, %rax
addq 0x10(%rsp), %rax
leaq (%rax,%rcx,4), %rdi
addq $-0x4, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x92b0
leaq (%rbx,%r15), %rax
incq %rax
incq %r15
addq %r13, %rbp
cmpq $-0x1, %rax
jne 0x2fe7a
jmp 0x2ff32
cmpl $0x1, %r9d
jne 0x30185
movq %rdi, 0x48(%rsp)
movq %rsi, 0x40(%rsp)
movl %r9d, 0xc(%rsp)
movq %rbx, 0x28(%rsp)
movq %r13, 0x18(%rsp)
movq %rbp, 0x10(%rsp)
cmpq %rcx, %rbx
setb %cl
cmpq %rax, %r13
setb %al
orb %cl, %al
cmpb $0x1, %al
jne 0x2ff32
leaq 0xdd41(%rip), %rsi      # 0x3dc40
leaq 0xa0(%rsp), %rbx
movq %rbx, %rdi
movl $0x149, %edx            # imm = 0x149
movl $0x3, %ecx
callq 0x3894e
leaq 0xdd7e(%rip), %rsi      # 0x3dc9e
movl $0x28, %edx
movq %rbx, %rdi
callq 0x93b0
jmp 0x301ba
movq 0x28(%rsp), %rax
movq 0x38(%rsp), %rbx
subq %rbx, %rax
movq 0x60(%rsp), %rcx
xorl %edx, %edx
divq %rcx
movq %rax, %r13
cmpq $0x1, %rdx
sbbq $0x0, %r13
addq $0x2, %r13
movq %r13, 0x20(%rsp)
movq 0x18(%rsp), %rax
movq 0x30(%rsp), %rbp
subq %rbp, %rax
xorl %edx, %edx
divq %rcx
movq %rax, %r14
cmpq $0x1, %rdx
sbbq $0x0, %r14
addq $0x2, %r14
movq %r14, 0x50(%rsp)
imulq %r13, %r14
leaq (,%r14,4), %rax
movabsq $0x3fffffffffffffff, %rcx # imm = 0x3FFFFFFFFFFFFFFF
cmpq %rcx, %r14
movq $-0x1, %rdi
cmovbeq %rax, %rdi
callq 0x9030
movq %rax, 0x58(%rsp)
movq 0x10(%r12), %rax
movq %rax, 0x98(%rsp)
imulq %rbx, %rbp
imulq %r14, %rbp
movq 0x48(%rsp), %rax
movq (%rax), %r12
movq 0x8(%rax), %rsi
movq %rbp, %rax
xorl %edx, %edx
divq %r12
cmpq $0x1, %rdx
sbbq $-0x1, %rax
cmpq %rbp, %rsi
movq %rbp, %rcx
cmovbq %rsi, %rcx
cmpq %rsi, %rax
cmovaeq %rax, %rcx
cmpq %rcx, %rsi
jb 0x3000b
movq %rbp, %rax
xorl %edx, %edx
divq %rcx
movq %rax, %r12
cmpq $0x1, %rdx
sbbq $-0x1, %r12
movq %r13, %rax
xorl %edx, %edx
divq %r12
movq %rax, %r15
cmpq $0x1, %rdx
sbbq $-0x1, %r15
leaq 0x80(%rsp), %rdi
leaq 0xa0(%rsp), %rdx
movq %r12, %rsi
callq 0xa482
xorl %ebx, %ebx
leaq 0xa0(%rsp), %rbp
leaq 0x70(%rsp), %r14
leaq 0x10(%rsp), %rax
movq %rax, 0xa0(%rsp)
leaq 0x58(%rsp), %rax
movq %rax, 0xa8(%rsp)
leaq 0x98(%rsp), %rax
movq %rax, 0xb0(%rsp)
leaq 0x28(%rsp), %rax
movq %rax, 0xb8(%rsp)
leaq 0x18(%rsp), %rax
movq %rax, 0xc0(%rsp)
leaq 0x38(%rsp), %rax
movq %rax, 0xc8(%rsp)
leaq 0x30(%rsp), %rax
movq %rax, 0xd0(%rsp)
leaq 0x50(%rsp), %rax
movq %rax, 0xd8(%rsp)
leaq 0x60(%rsp), %rax
movq %rax, 0xe0(%rsp)
movq %rbx, %rax
imulq %r15, %rax
movq %rax, 0x70(%rsp)
leaq 0x1(%rbx), %r13
movq %r13, %rax
imulq %r15, %rax
movq %rax, 0x68(%rsp)
leaq 0x68(%rsp), %rcx
cmpq 0x20(%rsp), %rax
jb 0x300ea
leaq 0x20(%rsp), %rcx
leaq 0x78(%rsp), %rdi
movq %rbp, %rsi
movq %r14, %rdx
callq 0x30216
movq 0x80(%rsp), %rax
cmpq $0x0, (%rax,%rbx,8)
jne 0x301db
movq 0x78(%rsp), %rcx
movq %rcx, (%rax,%rbx,8)
movq %r13, %rbx
cmpq %r13, %r12
jne 0x30045
movq 0x80(%rsp), %r14
movq 0x88(%rsp), %rbx
cmpq %rbx, %r14
je 0x30145
movq %r14, %rdi
callq 0x95a0
addq $0x8, %r14
jmp 0x30132
movq 0x58(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq 0x50(%rsp), %rcx
movq 0x40(%rsp), %rdi
callq 0x18a60
cmpl $0x0, 0xc(%rsp)
jne 0x30174
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x30174
callq 0x9420
leaq 0x80(%rsp), %rdi
callq 0xa594
movb $0x1, %al
jmp 0x301c9
leaq 0xdab4(%rip), %rsi      # 0x3dc40
leaq 0xa0(%rsp), %rbx
movq %rbx, %rdi
movl $0x14e, %edx            # imm = 0x14E
movl $0x3, %ecx
callq 0x3894e
leaq 0xdb1a(%rip), %rsi      # 0x3dcc7
movl $0x1a, %edx
movq %rbx, %rdi
callq 0x93b0
leaq 0xa0(%rsp), %rdi
callq 0x38b76
xorl %eax, %eax
addq $0x228, %rsp            # imm = 0x228
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x90c0
jmp 0x301e2
movq %rax, %rbx
leaq 0xa0(%rsp), %rdi
callq 0x38b76
jmp 0x3020d
jmp 0x301fd
movq %rax, %rbx
jmp 0x3020d
jmp 0x301fd
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0xa594
movq %rbx, %rdi
callq 0x95d0
nop