// File: /workspace/github_cpp_makefile_star2_O2/btbujiangjun[P]abcdl/src/algebra/MatrixHelper.cpp
// Function: abcdl::algebra::MatrixHelper<int>::expand(abcdl::algebra::Matrix<int>&, abcdl::algebra::Matrix<int> const&, unsigned long, unsigned long)
void MatrixHelper<T>::expand(Matrix<T>& result,
                             const Matrix<T>& mat,
                             const size_t row_dim,
                             const size_t col_dim){

    CHECK(row_dim * col_dim > 1);
   
    size_t col_a = mat.cols();
    size_t row   = mat.rows() * row_dim;
    size_t col   = col_a * col_dim;
    size_t size  = row * col;

    size_t num_thread = _po.get_num_thread(size, _po.get_block_size(size));
    size_t block_size = row / num_thread;
    if(row % num_thread != 0){
        block_size += 1;
    }

    std::vector<std::thread> threads(num_thread);
    T* data = mat.data();
    T* new_data = new T[size];
    memset(new_data, 0, sizeof(T) * size);

    for(size_t i = 0; i != num_thread; i++){
        threads[i] = std::thread(
            [&data, &new_data,&col, &col_a, &row_dim, &col_dim](size_t start_idx, size_t end_idx){
				for(size_t ti = start_idx; ti < end_idx; ti++){
					for(size_t tj = 0; tj != col; tj++){
                        new_data[ti * col + tj] = data[ti / row_dim * col_a + tj / col_dim];
					}
				}
            }, i * block_size , std::min(row, (i + 1) * block_size)
        );
    }

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

    result.set_shallow_data(new_data, row, col);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1f8, %rsp            # imm = 0x1F8
movq %r8, %r15
movq %rcx, %r12
movq %rdx, %r13
movq %rsi, 0x18(%rsp)
movq %rdi, %r14
movq %rcx, 0x60(%rsp)
movq %r8, 0x58(%rsp)
movq %r8, %rax
imulq %rcx, %rax
cmpq $0x1, %rax
jbe 0x27514
movq 0x8(%r13), %rax
movq %rax, 0x50(%rsp)
imulq (%r13), %r12
movq %r12, (%rsp)
imulq %rax, %r15
movq %r15, 0x10(%rsp)
imulq %r12, %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0xa05a
cmpq %rax, 0x8(%r14)
jae 0x27392
movq (%r14), %r14
jmp 0x273a8
movq %rax, %rcx
movq %r15, %rax
xorl %edx, %edx
divq %rcx
movq %rax, %r14
cmpq $0x1, %rdx
sbbq $-0x1, %r14
movq %r12, %rax
xorl %edx, %edx
divq %r14
movq %rax, %r12
cmpq $0x1, %rdx
sbbq $-0x1, %r12
leaq 0x38(%rsp), %rdi
leaq 0x70(%rsp), %rdx
movq %r14, %rsi
callq 0xa09c
movq 0x10(%r13), %rax
movq %rax, 0x30(%rsp)
movq %r15, %rax
shrq $0x3e, %rax
shlq $0x2, %r15
xorl %edi, %edi
negq %rax
sbbq %rdi, %rdi
orq %r15, %rdi
callq 0x9030
leaq 0x8(%rsp), %rcx
movq %rax, (%rcx)
xorl %ebx, %ebx
movq %rax, %rdi
xorl %esi, %esi
movq %r15, %rdx
callq 0x91e0
leaq 0x68(%rsp), %r15
leaq 0x28(%rsp), %rbp
xorl %r13d, %r13d
subq $0x1, %r14
jb 0x274c3
leaq 0x30(%rsp), %rax
movq %rax, 0x70(%rsp)
leaq 0x8(%rsp), %rax
movq %rax, 0x78(%rsp)
leaq 0x10(%rsp), %rax
movq %rax, 0x80(%rsp)
leaq 0x50(%rsp), %rax
movq %rax, 0x88(%rsp)
leaq 0x60(%rsp), %rax
movq %rax, 0x90(%rsp)
leaq 0x58(%rsp), %rax
movq %rax, 0x98(%rsp)
movq %r13, %rax
imulq %r12, %rax
movq %rax, 0x28(%rsp)
incq %r13
movq %r13, %rax
imulq %r12, %rax
cmpq (%rsp), %rax
movq %rax, 0x20(%rsp)
movq %rsp, %rcx
leaq 0x20(%rsp), %rax
cmovbq %rax, %rcx
movq %r15, %rdi
leaq 0x70(%rsp), %rsi
movq %rbp, %rdx
callq 0x2757c
movq 0x38(%rsp), %rdi
addq %rbx, %rdi
movq %r15, %rsi
callq 0xa174
movq %r15, %rdi
callq 0xa18e
addq $0x8, %rbx
jmp 0x27415
movq 0x38(%rsp), %r14
movq 0x40(%rsp), %rbx
cmpq %rbx, %r14
je 0x274e0
movq %r14, %rdi
callq 0x95b0
addq $0x8, %r14
jmp 0x274cd
movq 0x8(%rsp), %rsi
movq (%rsp), %rdx
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rdi
callq 0x17012
leaq 0x38(%rsp), %rdi
callq 0xa19c
addq $0x1f8, %rsp            # imm = 0x1F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc6d5(%rip), %rsi      # 0x33bf0
leaq 0x70(%rsp), %rbp
pushq $0x3
popq %rcx
movq %rbp, %rdi
movl $0x101, %edx            # imm = 0x101
callq 0x2ee18
leaq 0xc6f3(%rip), %rsi      # 0x33c2a
movq %rbp, %rdi
callq 0x9370
leaq 0x70(%rsp), %rdi
callq 0x2f054
jmp 0x2735d
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x2f054
jmp 0x27573
movq %rax, %rbx
jmp 0x27573
jmp 0x27566
jmp 0x27566
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0xa19c
movq %rbx, %rdi
callq 0x95e0
nop