// File: /workspace/github_c_makefile_star2_O3/ugobas[P]tnm/kinetic_tnm.c
// Function: Kinetic_sqrt
int Kinetic_sqrt(struct Jacobian *J, int *N_kin,
		 int N, int CONTROL, float E_MIN)
{
  // Compute kinetic energy tensor T=T_sqrt T_sqrt^T (only internal dof)
  // Lower tridiagonal matrix T_sqrt[i][j] with j<=i
  int k, l;
  (*N_kin)=N;

  if(CONTROL==0){
    if(START)printf("Kinetic_sqrt with Cholevsky decomposition\n");
    if(choldc(J->T_sqrt, J->T, N)==0){
      //J->T_sqrt_inv=Cholevsky_inversion_d2f(J->T_sqrt, N);
      for(k=0; k<N; k++){
	double *Lk=J->T_sqrt[k]+k+1;
	for(l=k+1; l<N; l++){*Lk=0; Lk++;}
      }
      return(0);
    }
  }

  // New decomposition diagonalizing kinetic energy
  printf("Square root of kinetic energy through diagonalization\n");
  {
    float **eigen_vector=Allocate_mat2_f(N, N);
    float eigen_value[N], t, t_inv;
    int num=0, ik;
    if(J->T_sqrt_inv)Empty_matrix_f(J->T_sqrt_inv, N);
    J->T_sqrt_inv=Allocate_mat2_f(N, N);
    d_Diagonalize(N, J->T, eigen_value, eigen_vector, 1);
    for(k=0; k<N; k++){
      if(eigen_value[k]<E_MIN){t=0; num++; continue;}
      else{t=sqrt(eigen_value[k]); t_inv=1./t; ik=k-num;}
      for(l=0; l<N; l++){
	J->T_sqrt[l][ik]=t*eigen_vector[k][l];
	if(t)J->T_sqrt_inv[ik][l]=eigen_vector[k][l]*t_inv;
      }
    }
    if(num)printf("WARNING, kinetic energy has %d null modes\n", num);
    (*N_kin)-=num;
    Empty_matrix_f(eigen_vector, N);
  }

  return(0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movss %xmm0, -0x30(%rbp)
movl %edx, %r13d
movq %rsi, %rbx
movq %rdi, %r15
movl %edx, (%rsi)
testl %ecx, %ecx
jne 0x220f2
cmpl $0x0, 0x46de0(%rip)     # 0x68eac
je 0x220da
leaq 0x36936(%rip), %rdi     # 0x58a0b
callq 0x2090
movq 0x20(%r15), %rsi
movq 0x28(%r15), %rdi
movl %r13d, %edx
callq 0x4987c
testl %eax, %eax
je 0x22267
movq %rbx, -0x40(%rbp)
leaq 0x36938(%rip), %rdi     # 0x58a35
callq 0x2090
movl %r13d, %edi
movl %r13d, %esi
callq 0x4971a
movq %rax, -0x38(%rbp)
movq %rsp, -0x48(%rbp)
movl %r13d, %r12d
movl %r13d, %ebx
movq %rsp, %r13
leaq 0xf(,%r12,4), %rax
andq $-0x10, %rax
subq %rax, %r13
movq %r13, %rsp
movq 0x30(%r15), %rdi
testq %rdi, %rdi
je 0x22140
movl %ebx, %esi
callq 0x49678
movl %ebx, %edi
movl %ebx, %esi
callq 0x4971a
movq %rax, 0x30(%r15)
movq 0x20(%r15), %rsi
movl %ebx, %edi
movq %r13, %rdx
movq -0x38(%rbp), %rcx
movl $0x1, %r8d
callq 0x4a559
movl %ebx, -0x2c(%rbp)
testl %ebx, %ebx
jle 0x22234
xorl %ebx, %ebx
xorps %xmm5, %xmm5
movss 0x2ef9f(%rip), %xmm6    # 0x5111c
xorl %r14d, %r14d
movss -0x30(%rbp), %xmm4
movss (%r13,%rbx,4), %xmm0
ucomiss %xmm0, %xmm4
jbe 0x22196
incl %r14d
jmp 0x2220c
ucomiss %xmm5, %xmm0
jb 0x221a1
sqrtss %xmm0, %xmm0
jmp 0x221b6
callq 0x21d0
movss 0x2ef6e(%rip), %xmm6    # 0x5111c
xorps %xmm5, %xmm5
movss -0x30(%rbp), %xmm4
movaps %xmm6, %xmm1
divss %xmm0, %xmm1
movl %ebx, %edx
subl %r14d, %edx
movq -0x38(%rbp), %rax
movq (%rax,%rbx,8), %rax
movq 0x28(%r15), %rcx
movslq %edx, %rdx
xorl %esi, %esi
ucomiss %xmm5, %xmm0
movss (%rax,%rsi,4), %xmm2
movaps %xmm0, %xmm3
mulss %xmm2, %xmm3
cvtss2sd %xmm3, %xmm3
movq (%rcx,%rsi,8), %rdi
movsd %xmm3, (%rdi,%rdx,8)
jne 0x221f3
jnp 0x22204
mulss %xmm1, %xmm2
movq 0x30(%r15), %rdi
movq (%rdi,%rdx,8), %rdi
movss %xmm2, (%rdi,%rsi,4)
incq %rsi
cmpq %rsi, %r12
jne 0x221d3
incq %rbx
cmpq %r12, %rbx
jne 0x22185
testl %r14d, %r14d
movq -0x40(%rbp), %rbx
je 0x22240
leaq 0x36496(%rip), %rdi     # 0x586be
movl %r14d, %esi
xorl %eax, %eax
callq 0x20f0
jmp 0x22243
xorl %r14d, %r14d
movl -0x2c(%rbp), %esi
movq -0x40(%rbp), %rbx
jmp 0x22246
xorl %r14d, %r14d
movl -0x2c(%rbp), %esi
subl %r14d, (%rbx)
movq -0x38(%rbp), %rdi
callq 0x49678
movq -0x48(%rbp), %rsp
xorl %eax, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testl %r13d, %r13d
jle 0x22256
movq 0x28(%r15), %rbx
movl %r13d, %r14d
addl $-0x2, %r13d
movq %r14, %r15
negq %r15
movl $0x1, %r12d
cmpq %r14, %r12
jae 0x222a7
leaq (,%r12,8), %rdi
movl %r13d, %eax
leaq 0x8(,%rax,8), %rdx
addq -0x8(%rbx,%r12,8), %rdi
xorl %esi, %esi
callq 0x2110
decl %r13d
leaq (%r15,%r12), %rax
incq %rax
incq %r12
cmpq $0x1, %rax
jne 0x22283
jmp 0x22256