// File: /workspace/github_cpp_makefile_star2_O1/btbujiangjun[P]abcdl/src/algebra/MatrixHelper.cpp
// Function: abcdl::algebra::MatrixHelper<int>::transpose(abcdl::algebra::Matrix<int>&, abcdl::algebra::Matrix<int> const&)
void MatrixHelper<T>::transpose(Matrix<T>& mat, const Matrix<T>& mat_a){
	size_t rows = mat_a.rows();
	size_t cols = mat_a.cols();
	if(rows == 1 || cols == 1){
        if(&mat != &mat_a){
    		mat.set_data(mat_a.data(), rows, cols);
        }
		mat.reshape(cols, rows);
        return ;
	}

    T* src_data       = mat_a.data();
    T* data           = new T[mat_a.get_size()];
    size_t num_thread = _po.get_num_thread(mat_a.get_size(), _po.get_block_size(mat_a.get_size()));
    size_t block_size = cols / num_thread;
    if(cols % 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, &src_data, rows, cols](size_t start_idx, size_t end_idx){
				for(size_t ti = start_idx; ti < end_idx; ti++){
		    		for(size_t tj = 0; tj != rows; tj++){
                        data[ti * rows + tj] = src_data[tj * cols + ti];
	    			}
		    	}
            }, i * block_size , std::min(cols, (i + 1) * block_size)
        );
    }

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

    mat.set_shallow_data(data, cols, rows);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1d8, %rsp            # imm = 0x1D8
movq %rsi, %r13
movq (%rdx), %rbp
movq 0x8(%rdx), %r15
movq %r15, (%rsp)
cmpq $0x1, %rbp
setne %al
cmpq $0x1, %r15
setne %cl
testb %cl, %al
jne 0x30381
cmpq %rdx, %r13
je 0x30357
movq 0x8(%r13), %rax
imulq (%r13), %rax
movq 0x10(%rdx), %r14
movq %r15, %r12
imulq %rbp, %r12
cmpq %r12, %rax
je 0x3033c
movq 0x10(%r13), %rdi
testq %rdi, %rdi
je 0x30319
callq 0x9420
movq %r12, %rax
shrq $0x3e, %rax
leaq (,%r12,4), %rcx
xorl %edi, %edi
negq %rax
sbbq %rdi, %rdi
orq %rcx, %rdi
callq 0x9030
movq %rax, 0x10(%r13)
movq %rbp, (%r13)
movq %r15, 0x8(%r13)
movq 0x10(%r13), %rdi
shlq $0x2, %r12
movq %r14, %rsi
movq %r12, %rdx
callq 0x92b0
movq (%rsp), %rbx
movq %rbx, %rax
imulq %rbp, %rax
movq 0x8(%r13), %rcx
imulq (%r13), %rcx
cmpq %rcx, %rax
jne 0x304ff
movq %rbx, (%r13)
movq %rbp, 0x8(%r13)
jmp 0x304ed
movq %rdi, %r14
movq %r13, 0x10(%rsp)
movq 0x10(%rdx), %rax
movq %rax, 0x48(%rsp)
movq %r15, %r13
imulq %rbp, %r13
movq %r13, %rax
shrq $0x3e, %rax
leaq (,%r13,4), %rcx
xorl %edi, %edi
negq %rax
sbbq %rdi, %rdi
orq %rcx, %rdi
callq 0x9030
movq %rax, 0x8(%rsp)
movq (%r14), %r12
movq 0x8(%r14), %rsi
movq %r13, %rax
xorl %edx, %edx
divq %r12
cmpq $0x1, %rdx
sbbq $-0x1, %rax
cmpq %r13, %rsi
movq %r13, %rcx
cmovbq %rsi, %rcx
cmpq %rsi, %rax
cmovaeq %rax, %rcx
cmpq %rcx, %rsi
jb 0x303fd
movq %r13, %rax
xorl %edx, %edx
divq %rcx
movq %rax, %r12
cmpq $0x1, %rdx
sbbq $-0x1, %r12
movq %r15, %rax
xorl %edx, %edx
divq %r12
movq %rax, %r15
cmpq $0x1, %rdx
sbbq $-0x1, %r15
leaq 0x30(%rsp), %rdi
leaq 0x50(%rsp), %rdx
movq %r12, %rsi
callq 0xa482
xorl %r13d, %r13d
leaq 0x20(%rsp), %r14
leaq 0x8(%rsp), %rax
movq %rax, 0x50(%rsp)
leaq 0x48(%rsp), %rax
movq %rax, 0x58(%rsp)
movq %rbp, 0x60(%rsp)
movq (%rsp), %rax
movq %rax, 0x68(%rsp)
movq %r13, %rcx
imulq %r15, %rcx
movq %rcx, 0x20(%rsp)
leaq 0x1(%r13), %rbx
movq %rbx, %rcx
imulq %r15, %rcx
movq %rcx, 0x18(%rsp)
cmpq %rax, %rcx
movq %rsp, %rcx
leaq 0x18(%rsp), %rax
cmovbq %rax, %rcx
leaq 0x28(%rsp), %rdi
leaq 0x50(%rsp), %rsi
movq %r14, %rdx
callq 0x30572
movq 0x30(%rsp), %rax
cmpq $0x0, (%rax,%r13,8)
jne 0x30540
movq 0x28(%rsp), %rcx
movq %rcx, (%rax,%r13,8)
movq %rbx, %r13
cmpq %rbx, %r12
jne 0x3042a
movq 0x30(%rsp), %r14
movq 0x38(%rsp), %rbx
cmpq %rbx, %r14
movq 0x10(%rsp), %r12
je 0x304cf
movq %r14, %rdi
callq 0x95a0
addq $0x8, %r14
cmpq %rbx, %r14
jne 0x304be
movq 0x8(%rsp), %rsi
movq (%rsp), %rdx
movq %r12, %rdi
movq %rbp, %rcx
callq 0x18a60
leaq 0x30(%rsp), %rdi
callq 0xa594
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xbf47(%rip), %rsi      # 0x3c44d
leaq 0x50(%rsp), %r14
movq %r14, %rdi
movl $0x88, %edx
movl $0x3, %ecx
callq 0x3894e
leaq 0xc191(%rip), %rsi      # 0x3c6b5
movl $0x28, %edx
movq %r14, %rdi
callq 0x93b0
leaq 0x50(%rsp), %rdi
callq 0x38b76
jmp 0x30374
callq 0x90c0
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x38b76
jmp 0x3056a
jmp 0x3055d
movq %rax, %rbx
jmp 0x3056a
jmp 0x3055d
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0xa594
movq %rbx, %rdi
callq 0x95d0