// File: /workspace/github_cpp_makefile_star2_O3/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, %rbp
movq (%rdx), %rbx
movq 0x8(%rdx), %r15
movq %r15, (%rsp)
cmpq $0x1, %rbx
setne %al
cmpq $0x1, %r15
setne %cl
testb %cl, %al
jne 0x3015e
cmpq %rdx, %rbp
je 0x302cf
movq 0x10(%rdx), %r12
movq 0x8(%rbp), %rax
imulq (%rbp), %rax
movq 0x10(%rbp), %rdi
movq %r15, %r13
imulq %rbx, %r13
cmpq %r13, %rax
jne 0x302d8
leaq (,%r13,4), %r14
jmp 0x30308
movq %rdi, %r14
movq %rbp, 0x10(%rsp)
movq 0x10(%rdx), %rax
movq %rax, 0x48(%rsp)
movq %r15, %r13
imulq %rbx, %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 0x301da
movq %r13, %rax
xorl %edx, %edx
divq %rcx
movq %rax, %r12
cmpq $0x1, %rdx
sbbq $-0x1, %r12
movq %rbx, %rbp
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 0xa43e
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 0x303b4
movq 0x30(%rsp), %rax
cmpq $0x0, (%rax,%r13,8)
jne 0x30381
movq 0x28(%rsp), %rcx
movq %rcx, (%rax,%r13,8)
movq %rbx, %r13
cmpq %rbx, %r12
jne 0x3020a
movq 0x30(%rsp), %r14
movq 0x38(%rsp), %rbx
cmpq %rbx, %r14
movq 0x10(%rsp), %r12
je 0x302af
movq %r14, %rdi
callq 0x95a0
addq $0x8, %r14
cmpq %rbx, %r14
jne 0x3029e
movq 0x8(%rsp), %rsi
movq (%rsp), %rdx
movq %r12, %rdi
movq %rbp, %rcx
callq 0x18808
leaq 0x30(%rsp), %rdi
callq 0xa536
jmp 0x30331
movq %r15, %r13
imulq %rbx, %r13
jmp 0x3031b
testq %rdi, %rdi
je 0x302e2
callq 0x9420
movq %r13, %rax
shrq $0x3e, %rax
leaq (,%r13,4), %r14
xorl %edi, %edi
negq %rax
sbbq %rdi, %rdi
orq %r14, %rdi
callq 0x9030
movq %rax, %rdi
movq %rax, 0x10(%rbp)
movq %rbx, (%rbp)
movq %r15, 0x8(%rbp)
movq %r12, %rsi
movq %r14, %rdx
callq 0x92b0
movq 0x8(%rbp), %rax
imulq (%rbp), %rax
cmpq %rax, %r13
jne 0x30343
movq %r15, (%rbp)
movq %rbx, 0x8(%rbp)
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xc103(%rip), %rsi      # 0x3c44d
leaq 0x50(%rsp), %r14
movq %r14, %rdi
movl $0x88, %edx
movl $0x3, %ecx
callq 0x387b6
leaq 0xc34d(%rip), %rsi      # 0x3c6b5
movl $0x28, %edx
movq %r14, %rdi
callq 0x93b0
leaq 0x50(%rsp), %rdi
callq 0x389de
jmp 0x30329
callq 0x90c0
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x389de
jmp 0x303ab
jmp 0x3039e
movq %rax, %rbx
jmp 0x303ab
jmp 0x3039e
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0xa536
movq %rbx, %rdi
callq 0x95d0
nop