// File: tomfunkhouser[P]gaps/pkgs/RNBasics/RNSvd.cpp
// Function: gaps::householder_zero_col(double*, double*, int, int, int, int, double*)
void householder_zero_col(RNScalar *a,RNScalar *u,int i,int j,int m,int n,RNScalar *hv)
	/* hv is a work vector, length m */
{
    RNScalar	scale,	/* a scale factor for X1 */
		sigma,		/* the norm^2 of X1 */
		snorm,		/* +/- the norm of X1 */
		f, h, s, dot;
    int k,l;

    /* we will scale X1 by its l1-norm.  squawk! */
    for (scale=0, k=i; k<m; k++) scale += ((RNScalar) fabs(REF2D(a,m,n,k,j)));

    /* if X1 is 0, no point doing anything else */
    if (!scale) return;

    /* divide out the l1-norm, and calculate sigma = |X|^2 */
    for (k=i; k<m; k++) hv[k] = REF2D(a,m,n,k,j) / scale;
    for (sigma=0, k=i; k<m; k++) sigma += hv[k]*hv[k];

    /* The householder vector is X1 +/- (|X1|,0,0,...).  We will
    ** contruct this vector in hv and use it to perform the householder
    ** transformation on the matrix.  The plus or minus on the norm
    ** is to reduce roundoff error.  */
    f = hv[i];
    snorm = ((RNScalar) ((f > 0) ? -sqrt(sigma) : sqrt(sigma)));
    h = f*snorm - sigma;	/* eqn 11.2.4 in Press et.al. */
    hv[i] = f - snorm;

    /* set the first column of a to be the (|X1|,0,...) -- this is
    ** what we would get by performing the transformation, but this
    ** way's faster */
    REF2D(a,m,n,i,j) = scale * snorm;
    for (k=i+1; k<m; k++) REF2D(a,m,n,k,j) = 0;

    /* Now perform the householder transformation on the rest of
    ** the columns.  If the householder vector is X, and -half its norm^2
    ** is h, the householder transform is P_ij : delta_ij + x_i x_j / h.
    ** We don't actually create the P matrix, we just do the calcuation.
    */
    for (k=j+1; k<n; k++) {
	for (dot=0, l=i; l<m; l++) dot += REF2D(a,m,n,l,k)*hv[l];
	s = dot/h;
	for (l=i; l<m; l++) REF2D(a,m,n,l,k) += s*hv[l];
    }

    /* Similarly, perform the householder transformation on (all)
    ** the rows of u.  Note that it's the rows of u rather than
    ** the columns, because we want U to invert what we've done to a.
    */
    for (k=0; k<m; k++) {
	for (dot=0, l=i; l<m; l++) dot += REF2D(u,m,m,k,l)*hv[l];
	s = dot/h;
	for (l=i; l<m; l++) REF2D(u,m,m,k,l) += s*hv[l];
    }
}

// Assembly:
subq $0x68, %rsp
movq 0x70(%rsp), %rax
movq %rdi, 0x60(%rsp)
movq %rsi, 0x58(%rsp)
movl %edx, 0x54(%rsp)
movl %ecx, 0x50(%rsp)
movl %r8d, 0x4c(%rsp)
movl %r9d, 0x48(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x40(%rsp)
movl 0x54(%rsp), %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
cmpl 0x4c(%rsp), %eax
jge 0xd21fe
movq 0x60(%rsp), %rax
movl 0x48(%rsp), %ecx
imull 0xc(%rsp), %ecx
addl 0x50(%rsp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movaps 0x6108f(%rip), %xmm1    # 0x133270
pand %xmm1, %xmm0
addsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0xd21b6
movsd 0x40(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xd2214
jp 0xd2214
jmp 0xd25d2
movl 0x54(%rsp), %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
cmpl 0x4c(%rsp), %eax
jge 0xd2262
movq 0x60(%rsp), %rax
movl 0x48(%rsp), %ecx
imull 0xc(%rsp), %ecx
addl 0x50(%rsp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
divsd 0x40(%rsp), %xmm0
movq 0x70(%rsp), %rax
movslq 0xc(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0xd221c
xorps %xmm0, %xmm0
movsd %xmm0, 0x38(%rsp)
movl 0x54(%rsp), %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
cmpl 0x4c(%rsp), %eax
jge 0xd22bc
movq 0x70(%rsp), %rax
movslq 0xc(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x70(%rsp), %rax
movslq 0xc(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm2
movsd 0x38(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x38(%rsp)
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0xd2273
movq 0x70(%rsp), %rax
movslq 0x54(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x28(%rsp)
movsd 0x28(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xd2309
movsd 0x38(%rsp), %xmm0
callq 0xf390
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, (%rsp)
jmp 0xd2319
movsd 0x38(%rsp), %xmm0
callq 0xf390
movsd %xmm0, (%rsp)
movsd (%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
movsd 0x28(%rsp), %xmm0
movsd 0x30(%rsp), %xmm2
movsd 0x38(%rsp), %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x20(%rsp)
movsd 0x28(%rsp), %xmm0
subsd 0x30(%rsp), %xmm0
movq 0x70(%rsp), %rax
movslq 0x54(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movsd 0x40(%rsp), %xmm0
mulsd 0x30(%rsp), %xmm0
movq 0x60(%rsp), %rax
movl 0x48(%rsp), %ecx
imull 0x54(%rsp), %ecx
addl 0x50(%rsp), %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
cmpl 0x4c(%rsp), %eax
jge 0xd23db
movq 0x60(%rsp), %rax
movl 0x48(%rsp), %ecx
imull 0xc(%rsp), %ecx
addl 0x50(%rsp), %ecx
movslq %ecx, %rcx
xorps %xmm0, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0xd23a7
movl 0x50(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
cmpl 0x48(%rsp), %eax
jge 0xd24d8
xorps %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movl 0x54(%rsp), %eax
movl %eax, 0x8(%rsp)
movl 0x8(%rsp), %eax
cmpl 0x4c(%rsp), %eax
jge 0xd2459
movq 0x60(%rsp), %rax
movl 0x48(%rsp), %ecx
imull 0x8(%rsp), %ecx
addl 0xc(%rsp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x70(%rsp), %rax
movslq 0x8(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm2
movsd 0x10(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
movl 0x8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8(%rsp)
jmp 0xd2405
movsd 0x10(%rsp), %xmm0
divsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movl 0x54(%rsp), %eax
movl %eax, 0x8(%rsp)
movl 0x8(%rsp), %eax
cmpl 0x4c(%rsp), %eax
jge 0xd24c6
movsd 0x18(%rsp), %xmm0
movq 0x70(%rsp), %rax
movslq 0x8(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm2
movq 0x60(%rsp), %rax
movl 0x48(%rsp), %ecx
imull 0x8(%rsp), %ecx
addl 0xc(%rsp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl 0x8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8(%rsp)
jmp 0xd2473
jmp 0xd24c8
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0xd23e6
movl $0x0, 0xc(%rsp)
movl 0xc(%rsp), %eax
cmpl 0x4c(%rsp), %eax
jge 0xd25d2
xorps %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movl 0x54(%rsp), %eax
movl %eax, 0x8(%rsp)
movl 0x8(%rsp), %eax
cmpl 0x4c(%rsp), %eax
jge 0xd2553
movq 0x58(%rsp), %rax
movl 0x4c(%rsp), %ecx
imull 0xc(%rsp), %ecx
addl 0x8(%rsp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movq 0x70(%rsp), %rax
movslq 0x8(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm2
movsd 0x10(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
movl 0x8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8(%rsp)
jmp 0xd24ff
movsd 0x10(%rsp), %xmm0
divsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movl 0x54(%rsp), %eax
movl %eax, 0x8(%rsp)
movl 0x8(%rsp), %eax
cmpl 0x4c(%rsp), %eax
jge 0xd25c0
movsd 0x18(%rsp), %xmm0
movq 0x70(%rsp), %rax
movslq 0x8(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm2
movq 0x58(%rsp), %rax
movl 0x4c(%rsp), %ecx
imull 0xc(%rsp), %ecx
addl 0x8(%rsp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax,%rcx,8)
movl 0x8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8(%rsp)
jmp 0xd256d
jmp 0xd25c2
movl 0xc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc(%rsp)
jmp 0xd24e0
addq $0x68, %rsp
retq
nopw (%rax,%rax)