// File: /workspace/github_cpp_makefile_star2_O2/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 $0x78, %rsp
movq %rsi, %r15
movq (%rdx), %rbx
movq 0x8(%rdx), %r12
movq %r12, (%rsp)
cmpq $0x1, %rbx
setne %al
cmpq $0x1, %r12
setne %cl
testb %cl, %al
jne 0x27b73
cmpq %rdx, %r15
je 0x27b57
movq 0x10(%rdx), %rsi
movq %r15, %rdi
movq %rbx, %rdx
movq %r12, %rcx
callq 0x16f7e
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x17b90
movq %rdi, %r14
movq %r15, 0x18(%rsp)
movq 0x10(%rdx), %rax
movq %rax, 0x48(%rsp)
movq %r12, %r15
imulq %rbx, %r15
movq %r15, %rax
shrq $0x3e, %rax
leaq (,%r15,4), %rcx
xorl %edi, %edi
negq %rax
sbbq %rdi, %rdi
orq %rcx, %rdi
callq 0x9030
movq %rax, 0x10(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0xa05a
cmpq %rax, 0x8(%r14)
movq %rbx, 0x8(%rsp)
jae 0x27bca
movq (%r14), %r15
jmp 0x27be0
movq %rax, %rcx
movq %r15, %rax
xorl %edx, %edx
divq %rcx
movq %rax, %r15
cmpq $0x1, %rdx
sbbq $-0x1, %r15
movq %r12, %rax
xorl %edx, %edx
divq %r15
movq %rax, %r12
xorl %ebx, %ebx
cmpq $0x1, %rdx
sbbq $-0x1, %r12
leaq 0x30(%rsp), %rdi
leaq 0x50(%rsp), %rdx
movq %r15, %rsi
callq 0xa09c
leaq 0x70(%rsp), %rbp
leaq 0x28(%rsp), %r14
xorl %r13d, %r13d
subq $0x1, %r15
jb 0x27ca0
leaq 0x10(%rsp), %rax
movq %rax, 0x50(%rsp)
leaq 0x48(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x8(%rsp), %rax
movq %rax, 0x60(%rsp)
movq (%rsp), %rax
movq %rax, 0x68(%rsp)
movq %r13, %rcx
imulq %r12, %rcx
movq %rcx, 0x28(%rsp)
incq %r13
movq %r13, %rcx
imulq %r12, %rcx
movq %rcx, 0x20(%rsp)
cmpq %rax, %rcx
movq %rsp, %rcx
leaq 0x20(%rsp), %rax
cmovbq %rax, %rcx
movq %rbp, %rdi
leaq 0x50(%rsp), %rsi
movq %r14, %rdx
callq 0x27d0a
movq 0x30(%rsp), %rdi
addq %rbx, %rdi
movq %rbp, %rsi
callq 0xa174
movq %rbp, %rdi
callq 0xa18e
addq $0x8, %rbx
jmp 0x27c14
movq 0x30(%rsp), %r14
movq 0x38(%rsp), %rbx
movq 0x8(%rsp), %r15
cmpq %rbx, %r14
je 0x27cc2
movq %r14, %rdi
callq 0x95b0
addq $0x8, %r14
jmp 0x27caf
movq 0x10(%rsp), %rsi
movq (%rsp), %rdx
movq 0x18(%rsp), %rdi
movq %r15, %rcx
callq 0x17012
leaq 0x30(%rsp), %rdi
callq 0xa19c
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x27cf5
jmp 0x27cf5
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0xa19c
movq %rbx, %rdi
callq 0x95e0