// File: 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:
subq $0x4d8, %rsp            # imm = 0x4D8
movq %rdi, 0x4c8(%rsp)
movq %rsi, 0x4c0(%rsp)
movq %rdx, 0x4b8(%rsp)
movq %rcx, 0x4b0(%rsp)
movq %r8, 0x4a8(%rsp)
movl %r9d, 0x4a4(%rsp)
movq 0x4c8(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0x4b8(%rsp), %rdi
callq 0x31a10
movq %rax, 0x498(%rsp)
movq 0x4b8(%rsp), %rdi
callq 0x31a20
movq %rax, 0x490(%rsp)
movq 0x4b0(%rsp), %rdi
callq 0x31a10
movq %rax, 0x488(%rsp)
movq 0x4b0(%rsp), %rdi
callq 0x31a20
movq %rax, 0x480(%rsp)
movq 0x4b8(%rsp), %rdi
callq 0x31a30
movq %rax, 0x458(%rsp)
cmpl $0x0, 0x4a4(%rsp)
jne 0x695eb
movq 0x488(%rsp), %rax
imulq 0x480(%rsp), %rax
cmpq $0x1, %rax
je 0x695eb
movq 0x4b8(%rsp), %rdi
callq 0x31a10
movq 0x488(%rsp), %rcx
subq $0x1, %rcx
shlq %rcx
addq %rcx, %rax
movq %rax, 0x478(%rsp)
movq 0x4b8(%rsp), %rdi
callq 0x31a20
movq 0x480(%rsp), %rcx
subq $0x1, %rcx
shlq %rcx
addq %rcx, %rax
movq %rax, 0x470(%rsp)
movq 0x478(%rsp), %rax
imulq 0x470(%rsp), %rax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x9030
movq %rax, 0x468(%rsp)
movq 0x468(%rsp), %rdi
movq 0x478(%rsp), %rdx
shlq $0x2, %rdx
imulq 0x470(%rsp), %rdx
xorl %esi, %esi
callq 0x9210
movq $0x0, 0x450(%rsp)
movq 0x450(%rsp), %rax
cmpq 0x498(%rsp), %rax
je 0x695e6
movq 0x468(%rsp), %rdi
movq 0x450(%rsp), %rax
addq 0x488(%rsp), %rax
subq $0x1, %rax
imulq 0x470(%rsp), %rax
addq 0x480(%rsp), %rax
subq $0x1, %rax
shlq $0x2, %rax
addq %rax, %rdi
movq 0x458(%rsp), %rsi
movq 0x450(%rsp), %rax
imulq 0x490(%rsp), %rax
shlq $0x2, %rax
addq %rax, %rsi
movq 0x490(%rsp), %rdx
shlq $0x2, %rdx
callq 0x9370
movq 0x450(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x450(%rsp)
jmp 0x6954e
jmp 0x6973d
cmpl $0x1, 0x4a4(%rsp)
jne 0x696c5
movq 0x498(%rsp), %rax
movq %rax, 0x478(%rsp)
movq 0x490(%rsp), %rax
movq %rax, 0x470(%rsp)
movq 0x458(%rsp), %rax
movq %rax, 0x468(%rsp)
movq 0x478(%rsp), %rax
cmpq 0x488(%rsp), %rax
jb 0x6964d
movq 0x470(%rsp), %rax
cmpq 0x480(%rsp), %rax
jae 0x696c3
leaq 0x28fd4(%rip), %rsi     # 0x92628
leaq 0x2c8(%rsp), %rdi
movq %rdi, 0x48(%rsp)
movl $0x149, %edx            # imm = 0x149
movl $0x3, %ecx
callq 0x89860
movq 0x48(%rsp), %rdi
leaq 0x2900a(%rip), %rsi     # 0x92686
callq 0xc340
jmp 0x69683
leaq 0x2c8(%rsp), %rdi
callq 0x89c20
movb $0x0, 0x4d7(%rsp)
jmp 0x69c01
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c0(%rsp)
movl %eax, 0x2bc(%rsp)
leaq 0x2c8(%rsp), %rdi
callq 0x89c20
jmp 0x69c12
jmp 0x6973b
leaq 0x28f5c(%rip), %rsi     # 0x92628
leaq 0x130(%rsp), %rdi
movq %rdi, 0x40(%rsp)
movl $0x14e, %edx            # imm = 0x14E
movl $0x3, %ecx
callq 0x89860
movq 0x40(%rsp), %rdi
leaq 0x28fbb(%rip), %rsi     # 0x926af
callq 0x69c20
jmp 0x696fb
leaq 0x130(%rsp), %rdi
callq 0x89c20
movb $0x0, 0x4d7(%rsp)
jmp 0x69c01
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c0(%rsp)
movl %eax, 0x2bc(%rsp)
leaq 0x130(%rsp), %rdi
callq 0x89c20
jmp 0x69c12
jmp 0x6973d
movq 0x478(%rsp), %rax
subq 0x488(%rsp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq 0x4a8(%rsp)
cmpq $0x0, %rdx
jne 0x69786
movq 0x478(%rsp), %rax
subq 0x488(%rsp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq 0x4a8(%rsp)
addq $0x1, %rax
movq %rax, 0x38(%rsp)
jmp 0x697ab
movq 0x478(%rsp), %rax
subq 0x488(%rsp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq 0x4a8(%rsp)
addq $0x2, %rax
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rax
movq %rax, 0x128(%rsp)
movq 0x470(%rsp), %rax
subq 0x480(%rsp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq 0x4a8(%rsp)
cmpq $0x0, %rdx
jne 0x69801
movq 0x470(%rsp), %rax
subq 0x480(%rsp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq 0x4a8(%rsp)
addq $0x1, %rax
movq %rax, 0x30(%rsp)
jmp 0x69826
movq 0x470(%rsp), %rax
subq 0x480(%rsp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq 0x4a8(%rsp)
addq $0x2, %rax
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rax
movq %rax, 0x120(%rsp)
movq 0x128(%rsp), %rax
imulq 0x120(%rsp), %rax
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x9030
movq %rax, 0x460(%rsp)
movq 0x4b0(%rsp), %rdi
callq 0x31a30
movq 0x50(%rsp), %rdi
movq %rax, 0x118(%rsp)
movq 0x128(%rsp), %rax
imulq 0x120(%rsp), %rax
imulq 0x488(%rsp), %rax
imulq 0x480(%rsp), %rax
movq %rax, 0x110(%rsp)
movq 0x110(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x110(%rsp), %rsi
callq 0xa8e0
movq 0x50(%rsp), %rdi
movq 0x28(%rsp), %rsi
movq %rax, %rdx
callq 0xa970
movq %rax, 0x108(%rsp)
movq 0x128(%rsp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq 0x108(%rsp)
movq %rax, 0x100(%rsp)
movq 0x128(%rsp), %rax
xorl %ecx, %ecx
movl %ecx, %edx
divq 0x108(%rsp)
cmpq $0x0, %rdx
je 0x69930
movq 0x100(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x100(%rsp)
movq 0x108(%rsp), %rax
movq %rax, 0x18(%rsp)
leaq 0xe7(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0xa9f0
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
leaq 0xe8(%rsp), %rdi
callq 0xaa00
jmp 0x69968
leaq 0xe7(%rsp), %rdi
callq 0xaa80
movq $0x0, 0xd8(%rsp)
movq 0xd8(%rsp), %rax
cmpq 0x108(%rsp), %rax
je 0x69b33
leaq 0x468(%rsp), %rax
movq %rax, 0x88(%rsp)
leaq 0x460(%rsp), %rax
movq %rax, 0x90(%rsp)
leaq 0x118(%rsp), %rax
movq %rax, 0x98(%rsp)
leaq 0x478(%rsp), %rax
movq %rax, 0xa0(%rsp)
leaq 0x470(%rsp), %rax
movq %rax, 0xa8(%rsp)
leaq 0x488(%rsp), %rax
movq %rax, 0xb0(%rsp)
leaq 0x480(%rsp), %rax
movq %rax, 0xb8(%rsp)
leaq 0x120(%rsp), %rax
movq %rax, 0xc0(%rsp)
leaq 0x4a8(%rsp), %rax
movq %rax, 0xc8(%rsp)
movq 0xd8(%rsp), %rax
movq 0x100(%rsp), %rcx
imulq %rcx, %rax
movq %rax, 0x80(%rsp)
movq 0xd8(%rsp), %rax
incq %rax
movq 0x100(%rsp), %rcx
imulq %rcx, %rax
movq %rax, 0x78(%rsp)
leaq 0x128(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0xaa90
movq %rax, 0x10(%rsp)
jmp 0x69a78
movq 0x10(%rsp), %rcx
leaq 0xd0(%rsp), %rdi
leaq 0x88(%rsp), %rsi
leaq 0x80(%rsp), %rdx
callq 0x69c50
jmp 0x69a9c
movq 0xd8(%rsp), %rsi
leaq 0xe8(%rsp), %rdi
callq 0xaba0
movq %rax, %rdi
leaq 0xd0(%rsp), %rsi
callq 0xabc0
leaq 0xd0(%rsp), %rdi
callq 0xac00
movq 0xd8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xd8(%rsp)
jmp 0x69981
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c0(%rsp)
movl %eax, 0x2bc(%rsp)
leaq 0xe7(%rsp), %rdi
callq 0xaa80
jmp 0x69c12
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c0(%rsp)
movl %eax, 0x2bc(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0xacf0
jmp 0x69c12
leaq 0xe8(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x70(%rsp), %rdi
callq 0xac20
movq %rax, 0x68(%rsp)
movq 0x70(%rsp), %rdi
callq 0xac50
movq %rax, 0x60(%rsp)
leaq 0x68(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0xac80
testb $0x1, %al
jne 0x69b73
jmp 0x69b9c
leaq 0x68(%rsp), %rdi
callq 0xacc0
movq %rax, 0x58(%rsp)
movq 0x58(%rsp), %rdi
callq 0x96c0
jmp 0x69b8e
jmp 0x69b90
leaq 0x68(%rsp), %rdi
callq 0xacd0
jmp 0x69b5e
movq 0x4c0(%rsp), %rdi
movq 0x460(%rsp), %rsi
movq 0x128(%rsp), %rdx
movq 0x120(%rsp), %rcx
callq 0x31fe0
jmp 0x69bc3
cmpl $0x0, 0x4a4(%rsp)
jne 0x69bec
movq 0x468(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x69bea
movq 0x8(%rsp), %rdi
callq 0x9540
jmp 0x69bec
movb $0x1, 0x4d7(%rsp)
leaq 0xe8(%rsp), %rdi
callq 0xacf0
movb 0x4d7(%rsp), %al
andb $0x1, %al
addq $0x4d8, %rsp            # imm = 0x4D8
retq
movq 0x2c0(%rsp), %rdi
callq 0x9700
nop