// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/RNBasics/RNSvd.cpp
// Function: gaps::RNSvdDecompose(int, int, double const*, double*, double*, double*)
void RNSvdDecompose(int m, int n, 
    const RNScalar *a,
    RNScalar *u, RNScalar *w, RNScalar *vt)
{
    int i, j, k, min = MIN(m,n);
    RNScalar *g, *p, wi;

    g = ALLOC2D(m,n);
    bidiagonalize(a,m,n,u,g,vt);


    bidiagonal_svd(g,m,n,u,vt);
    for (i=0; i<min; i++) w[i] = REF2D(g,m,n,i,i);

    /* insertion sort singular values.  Note that since
    ** the svd algorithm produces values sorted or nearly sorted,
    ** an insertion sort is efficient.
    */
    for (i=1; i<min; i++) {
	if (w[i] > w[i-1]) {
	    /* save w[i], and col i of u and row i of vt.  use "g" as buffer */
	    wi = w[i];
	    p=g;
	    for (k=0; k<m; k++) *p++ = REF2D(u,m,min,k,i);
	    for (k=0; k<n; k++) *p++ = REF2D(vt,min,n,i,k);
	    /* slide columns over */
	    for (j=i; j>0 && wi > w[j-1]; j--) {
		w[j] = w[j-1];
		for (k=0; k<m; k++) REF2D(u,m,min,k,j)=REF2D(u,m,min,k,j-1);
		for (k=0; k<n; k++) REF2D(vt,min,n,j,k)=REF2D(vt,min,n,j-1,k);
	    }
	    /* put original i stuff where we ended up */
	    w[j] = wi;
	    p=g;
	    for (k=0; k<m; k++) REF2D(u,m,min,k,j) = *p++;
	    for (k=0; k<n; k++) REF2D(vt,min,n,j,k) = *p++;
	}
    }
	    
    FREE(g);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, 0x18(%rsp)
movq %r8, %r14
movq %rcx, %r13
movq %rcx, 0x10(%rsp)
movl %esi, %r15d
movl %edi, %r12d
cmpl %esi, %edi
movl %esi, %ebp
cmovll %edi, %ebp
movq %rdx, (%rsp)
movl %esi, %edi
imull %r12d, %edi
shlq $0x3, %rdi
callq 0xe5e0
movq %rax, %rbx
movq (%rsp), %rdi
movl %r12d, %esi
movl %r15d, %edx
movq %r13, %rcx
movq %rax, %r8
movq 0x18(%rsp), %r13
movq %r13, %r9
callq 0x70ac2
movq %rbx, %rdi
movl %r12d, (%rsp)
movl %r12d, %esi
movq %r15, 0x8(%rsp)
movl %r15d, %edx
movq 0x10(%rsp), %rcx
movq %r13, %r8
callq 0x70d4c
movq %rbx, %rdi
testl %ebp, %ebp
jle 0x712f9
movq 0x8(%rsp), %rax
incl %eax
movl %ebp, %esi
xorl %ecx, %ecx
xorl %edx, %edx
movslq %ecx, %rcx
movsd (%rdi,%rcx,8), %xmm0
movsd %xmm0, (%r14,%rdx,8)
incq %rdx
addl %eax, %ecx
cmpq %rdx, %rsi
jne 0x7114b
cmpl $0x1, %ebp
je 0x712f9
movq 0x8(%rsp), %rax
movslq %eax, %r13
movl %eax, %edx
movl (%rsp), %ecx
movq 0x10(%rsp), %rax
leaq 0x8(%rax), %r9
leaq (,%rsi,8), %r8
leaq (,%rdx,8), %rbp
movq 0x18(%rsp), %rax
leaq (%rax,%rdx,8), %r10
leaq (%rax,%r13,8), %r11
movq %r11, (%rsp)
shlq $0x3, %r13
movq %r13, %r15
negq %r15
movl $0x1, %r12d
movq %rax, 0x20(%rsp)
movq %rdi, 0x28(%rsp)
movq %rsi, 0x40(%rsp)
movq %r13, 0x38(%rsp)
movq %rbp, 0x30(%rsp)
movsd (%r14,%r12,8), %xmm0
ucomisd -0x8(%r14,%r12,8), %xmm0
movq %rcx, %r11
movq %r9, %rbx
jbe 0x712d3
movsd (%rbx), %xmm1
movsd %xmm1, (%rdi)
addq $0x8, %rdi
addq %r8, %rbx
decq %r11
jne 0x711e5
xorl %eax, %eax
movsd (%r10,%rax,8), %xmm1
movsd %xmm1, (%rdi,%rax,8)
incq %rax
cmpq %rax, %rdx
jne 0x711fb
movq 0x20(%rsp), %rbx
movq (%rsp), %r11
movq %r9, %rax
movq %r12, %rbp
movsd -0x8(%r14,%rbp,8), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x71275
leaq -0x1(%rbp), %r13
movsd %xmm1, (%r14,%rbp,8)
movq %rcx, %rsi
movq %rax, %rdi
movsd -0x8(%rdi), %xmm1
movsd %xmm1, (%rdi)
addq %r8, %rdi
decq %rsi
jne 0x7123a
xorl %esi, %esi
movsd (%rbx,%rsi,8), %xmm1
movsd %xmm1, (%r11,%rsi,8)
incq %rsi
cmpq %rsi, %rdx
jne 0x7124d
addq $-0x8, %rax
addq %r15, %r11
addq %r15, %rbx
cmpq $0x2, %rbp
movq %r13, %rbp
jge 0x7121d
xorl %ebp, %ebp
movslq %ebp, %rax
movsd %xmm0, (%r14,%rax,8)
movq 0x10(%rsp), %rsi
leaq (%rsi,%rax,8), %r11
movq %rcx, %rbx
movq 0x28(%rsp), %rdi
movsd (%rdi), %xmm0
addq $0x8, %rdi
movsd %xmm0, (%r11)
addq %r8, %r11
decq %rbx
jne 0x7128f
imull 0x8(%rsp), %ebp
movslq %ebp, %rax
movq 0x18(%rsp), %rsi
leaq (%rsi,%rax,8), %rax
xorl %esi, %esi
movq 0x38(%rsp), %r13
movq 0x30(%rsp), %rbp
movsd (%rdi,%rsi,8), %xmm0
movsd %xmm0, (%rax,%rsi,8)
incq %rsi
cmpq %rsi, %rdx
jne 0x712c1
incq %r12
addq $0x8, %r9
addq %rbp, %r10
addq %r13, (%rsp)
addq %r13, 0x20(%rsp)
movq 0x40(%rsp), %rsi
cmpq %rsi, %r12
movq 0x28(%rsp), %rdi
jne 0x711cc
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe540