// File: /workspace/github_cpp_makefile_star2_O3/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 $0x248, %rsp            # imm = 0x248
movq %rcx, %rbp
movq %rdi, %r13
movq %r8, 0xb8(%rsp)
movq (%rdx), %rbx
movq 0x8(%rdx), %r14
movq (%rcx), %rdi
movq %rdi, 0xb0(%rsp)
movq 0x8(%rcx), %r15
movq %r15, 0xa8(%rsp)
movq 0x10(%rdx), %r12
testl %r9d, %r9d
je 0x2fc7d
cmpl $0x1, %r9d
jne 0x2fc8a
movq %rsi, 0x48(%rsp)
movl %r9d, 0x4(%rsp)
movq %rbx, 0x68(%rsp)
movq %r14, 0x60(%rsp)
movq %r12, 0x20(%rsp)
cmpq %rdi, %rbx
setb %al
cmpq %r15, %r14
setb %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x2fdd3
leaq 0xdfd3(%rip), %rsi      # 0x3dc20
leaq 0xc0(%rsp), %rbx
movq %rbx, %rdi
movl $0x149, %edx            # imm = 0x149
movl $0x3, %ecx
callq 0x387b6
leaq 0xe010(%rip), %rsi      # 0x3dc7e
movl $0x28, %edx
movq %rbx, %rdi
callq 0x93b0
jmp 0x2fcbf
movq %r15, %rax
imulq %rdi, %rax
cmpq $0x1, %rax
jne 0x2fcd3
leaq 0xdf8f(%rip), %rsi      # 0x3dc20
leaq 0xc0(%rsp), %rbx
movq %rbx, %rdi
movl $0x14e, %edx            # imm = 0x14E
movl $0x3, %ecx
callq 0x387b6
leaq 0xdff5(%rip), %rsi      # 0x3dca7
movl $0x1a, %edx
movq %rbx, %rdi
callq 0x93b0
leaq 0xc0(%rsp), %rdi
callq 0x389de
xorl %eax, %eax
jmp 0x30018
movq %r13, 0x40(%rsp)
movq %r8, 0x30(%rsp)
movq %rbp, 0x38(%rsp)
movq %rsi, 0x48(%rsp)
movl %r9d, 0x4(%rsp)
leaq -0x2(%rbx,%rdi,2), %rax
movq %rax, 0x68(%rsp)
leaq -0x2(%r14,%r15,2), %r13
movq %r13, 0x60(%rsp)
movq %r13, %rbp
movq %rax, 0x28(%rsp)
imulq %rax, %rbp
movq %rbp, %rax
shrq $0x3e, %rax
shlq $0x2, %rbp
movq %rdi, 0x10(%rsp)
xorl %edi, %edi
negq %rax
sbbq %rdi, %rdi
orq %rbp, %rdi
callq 0x9030
movq %rax, 0x20(%rsp)
movq %rax, 0x8(%rsp)
movq %rax, %rdi
xorl %esi, %esi
movq %rbp, %rdx
callq 0x91d0
testq %rbx, %rbx
je 0x2fdb7
shlq $0x2, %r14
movq 0x10(%rsp), %rax
decq %rax
imulq %r13, %rax
leaq (,%r15,4), %rcx
leaq (%rcx,%rax,4), %rax
movq 0x8(%rsp), %rcx
leaq (%rcx,%rax), %rbp
addq $-0x4, %rbp
movq %r13, 0x8(%rsp)
leaq (,%r13,4), %r13
movq %rbp, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x92b0
addq %r14, %r12
addq %r13, %rbp
decq %rbx
jne 0x2fd7e
movq 0x8(%rsp), %r14
movq 0x28(%rsp), %rbx
movq 0x40(%rsp), %r13
movq 0x38(%rsp), %rbp
movq 0x10(%rsp), %rdi
movq 0x30(%rsp), %r8
jmp 0x2fdd3
movq %r13, %r14
movq 0x28(%rsp), %rbx
movq 0x38(%rsp), %rbp
movq 0x10(%rsp), %rdi
movq 0x30(%rsp), %r8
movq 0x40(%rsp), %r13
subq %rdi, %rbx
movq %rbx, %rax
xorl %edx, %edx
divq %r8
movq %rax, %rbx
movq %rdi, %r12
xorl %edi, %edi
cmpq $0x1, %rdx
sbbq $0x0, %rbx
addq $0x2, %rbx
movq %rbx, 0x18(%rsp)
subq %r15, %r14
movq %r14, %rax
xorl %edx, %edx
divq %r8
movq %rax, %r14
cmpq $0x1, %rdx
sbbq $0x0, %r14
addq $0x2, %r14
movq %r14, 0x50(%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, 0x58(%rsp)
movq 0x10(%rbp), %rax
movq %rax, 0xa0(%rsp)
imulq %r12, %r15
imulq %r14, %r15
movq (%r13), %r14
movq 0x8(%r13), %rsi
movq %r15, %rax
xorl %edx, %edx
divq %r14
cmpq $0x1, %rdx
sbbq $-0x1, %rax
cmpq %r15, %rsi
movq %r15, %rcx
cmovbq %rsi, %rcx
cmpq %rsi, %rax
cmovaeq %rax, %rcx
cmpq %rcx, %rsi
jb 0x2fe91
movq %r15, %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 0x88(%rsp), %rdi
leaq 0xc0(%rsp), %rdx
movq %r14, %rsi
callq 0xa43e
xorl %ebp, %ebp
leaq 0xc0(%rsp), %r12
leaq 0x78(%rsp), %r13
leaq 0x20(%rsp), %rax
movq %rax, 0xc0(%rsp)
leaq 0x58(%rsp), %rax
movq %rax, 0xc8(%rsp)
leaq 0xa0(%rsp), %rax
movq %rax, 0xd0(%rsp)
leaq 0x68(%rsp), %rax
movq %rax, 0xd8(%rsp)
leaq 0x60(%rsp), %rax
movq %rax, 0xe0(%rsp)
leaq 0xb0(%rsp), %rax
movq %rax, 0xe8(%rsp)
leaq 0xa8(%rsp), %rax
movq %rax, 0xf0(%rsp)
leaq 0x50(%rsp), %rax
movq %rax, 0xf8(%rsp)
leaq 0xb8(%rsp), %rax
movq %rax, 0x100(%rsp)
movq %rbp, %rax
imulq %rbx, %rax
movq %rax, 0x78(%rsp)
leaq 0x1(%rbp), %r15
movq %r15, %rax
imulq %rbx, %rax
movq %rax, 0x70(%rsp)
leaq 0x70(%rsp), %rcx
cmpq 0x18(%rsp), %rax
jb 0x2ff79
leaq 0x18(%rsp), %rcx
leaq 0x80(%rsp), %rdi
movq %r12, %rsi
movq %r13, %rdx
callq 0x30064
movq 0x88(%rsp), %rax
cmpq $0x0, (%rax,%rbp,8)
jne 0x3002a
movq 0x80(%rsp), %rcx
movq %rcx, (%rax,%rbp,8)
movq %r15, %rbp
cmpq %r15, %r14
jne 0x2fecb
movq 0x88(%rsp), %rbx
movq 0x90(%rsp), %r14
cmpq %r14, %rbx
je 0x2ffda
movq %rbx, %rdi
callq 0x95a0
addq $0x8, %rbx
jmp 0x2ffc7
movq 0x58(%rsp), %rsi
movq 0x18(%rsp), %rdx
movq 0x50(%rsp), %rcx
movq 0x48(%rsp), %rdi
callq 0x18808
cmpl $0x0, 0x4(%rsp)
jne 0x30009
movq 0x20(%rsp), %rdi
testq %rdi, %rdi
je 0x30009
callq 0x9420
leaq 0x88(%rsp), %rdi
callq 0xa536
movb $0x1, %al
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x90c0
jmp 0x30038
jmp 0x3004c
movq %rax, %rbx
jmp 0x3005c
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0x389de
jmp 0x3005c
jmp 0x3004c
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0xa536
movq %rbx, %rdi
callq 0x95d0