// File: /workspace/github_c_makefile_star2_O3/zpole[P]IICV/vl/mathop.c
// Function: vl_gaussian_elimination
VL_EXPORT vl_bool
vl_gaussian_elimination (double * A, vl_size numRows, vl_size numColumns)
{
  vl_index i, j, ii, jj ;
  assert(A) ;
  assert(numRows <= numColumns) ;

#define Aat(i,j) A[(i) + (j)*numRows]

  /* Gauss elimination */
  for(j = 0 ; j < (signed)numRows ; ++j) {
    double maxa = 0 ;
    double maxabsa = 0 ;
    vl_index maxi = -1 ;
    double tmp ;

#if 0
    {
      vl_index iii, jjj ;
      for (iii = 0 ; iii < 2 ; ++iii) {
        for (jjj = 0 ; jjj < 3 ; ++jjj) {
          VL_PRINTF("%5.2g ", Aat(iii,jjj)) ;

        }
        VL_PRINTF("\n") ;
      }
      VL_PRINTF("\n") ;
    }
#endif

    /* look for the maximally stable pivot */
    for (i = j ; i < (signed)numRows ; ++i) {
      double a = Aat(i,j) ;
      double absa = vl_abs_d (a) ;
      if (absa > maxabsa) {
        maxa = a ;
        maxabsa = absa ;
        maxi = i ;
      }
    }
    i = maxi ;

    /* if singular give up */
    if (maxabsa < 1e-10) return VL_ERR_OVERFLOW ;

    /* swap j-th row with i-th row and normalize j-th row */
    for(jj = j ; jj < (signed)numColumns ; ++jj) {
      tmp = Aat(i,jj) ; Aat(i,jj) = Aat(j,jj) ; Aat(j,jj) = tmp ;
      Aat(j,jj) /= maxa ;
    }

#if 0
    {
      vl_index iii, jjj ;
      VL_PRINTF("after swap %d %d\n", j, i);
      for (iii = 0 ; iii < 2 ; ++iii) {
        for (jjj = 0 ; jjj < 3 ; ++jjj) {
          VL_PRINTF("%5.2g ", Aat(iii,jjj)) ;

        }
        VL_PRINTF("\n") ;
      }
      VL_PRINTF("\n") ;
    }
#endif

    /* elimination */
    for (ii = j+1 ; ii < (signed)numRows ; ++ii) {
      double x = Aat(ii,j) ;
      for (jj = j ; jj < (signed)numColumns ; ++jj) {
        Aat(ii,jj) -= x * Aat(j,jj) ;
      }
    }

#if 0
    {
      VL_PRINTF("after elimination\n");

      vl_index iii, jjj ;
      for (iii = 0 ; iii < 2 ; ++iii) {
        for (jjj = 0 ; jjj < 3 ; ++jjj) {
          VL_PRINTF("%5.2g ", Aat(iii,jjj)) ;

        }
        VL_PRINTF("\n") ;
      }
      VL_PRINTF("\n") ;
    }
#endif

  }

  /* backward substitution */
  for (i = numRows - 1 ; i > 0 ; --i) {
    /* substitute in all rows above */
    for (ii = i - 1 ; ii >= 0 ; --ii) {
      double x = Aat(ii,i) ;
      /* j = numRows */
      for (j = numRows ; j < (signed)numColumns ; ++j) {
        Aat(ii,j) -= x * Aat(i,j) ;
      }
    }
  }

#if 0
  {
    VL_PRINTF("after substitution\n");

    vl_index iii, jjj ;
    for (iii = 0 ; iii < 2 ; ++iii) {
      for (jjj = 0 ; jjj < 3 ; ++jjj) {
        VL_PRINTF("%5.2g ", Aat(iii,jjj)) ;

      }
      VL_PRINTF("\n") ;
    }
    VL_PRINTF("\n") ;
  }
#endif


  return VL_ERR_OK ;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
movq %rdx, -0x10(%rsp)
movslq %esi, %rcx
leaq 0x8(,%rsi,8), %rax
movq %rax, -0x8(%rsp)
testq %rcx, %rcx
jle 0x25f79
movslq -0x10(%rsp), %r8
leaq (,%rsi,8), %r9
leaq 0x8(%rdi), %r10
xorl %r12d, %r12d
movapd 0xc2af(%rip), %xmm0     # 0x32100
movsd 0xcb4f(%rip), %xmm1     # 0x329a8
movapd 0xc26f(%rip), %xmm2     # 0x320d0
movq %rdi, %rdx
movq %rdi, %rbx
movq %r12, %r15
movq %r12, %r11
imulq %rsi, %r11
xorpd %xmm4, %xmm4
movq $-0x1, %r12
xorl %r13d, %r13d
xorpd %xmm5, %xmm5
movapd %xmm4, %xmm6
leaq (%r15,%r13), %rax
movsd (%rbx,%r13,8), %xmm7
movapd %xmm7, %xmm4
andpd %xmm0, %xmm4
ucomisd %xmm6, %xmm4
movapd %xmm6, %xmm3
cmpltsd %xmm4, %xmm3
andpd %xmm3, %xmm7
andnpd %xmm5, %xmm3
orpd %xmm7, %xmm3
maxsd %xmm6, %xmm4
cmovaq %rax, %r12
leaq (%r15,%r13), %rax
incq %rax
incq %r13
movapd %xmm3, %xmm5
cmpq %rcx, %rax
jl 0x25e83
ucomisd %xmm4, %xmm1
ja 0x2602a
cmpq %r8, %r15
jge 0x25f0d
leaq (%rdx,%r12,8), %r12
xorl %r13d, %r13d
movq %r15, %rbp
movsd (%r12,%r13), %xmm4
movsd (%rbx,%r13), %xmm5
movsd %xmm5, (%r12,%r13)
divsd %xmm3, %xmm4
movsd %xmm4, (%rbx,%r13)
incq %rbp
addq %r9, %r13
cmpq %r8, %rbp
jl 0x25ee6
leaq 0x1(%r15), %r12
movq %r10, %r13
movq %r12, %rbp
cmpq %rcx, %r12
jge 0x25f62
cmpq %r8, %r15
jge 0x25f56
leaq (%rdi,%rbp,8), %rax
movsd (%rax,%r11,8), %xmm3
xorpd %xmm2, %xmm3
xorl %eax, %eax
movq %r15, %r14
movsd (%rbx,%rax), %xmm4
mulsd %xmm3, %xmm4
addsd (%r13,%rax), %xmm4
movsd %xmm4, (%r13,%rax)
incq %r14
addq %r9, %rax
cmpq %r8, %r14
jl 0x25f34
incq %rbp
addq $0x8, %r13
cmpq %rcx, %rbp
jl 0x25f1c
movq -0x8(%rsp), %rax
addq %rax, %rbx
addq %r9, %rdx
addq %rax, %r10
cmpq %rcx, %r12
jne 0x25e67
leaq -0x1(%rsi), %rbx
testq %rbx, %rbx
jle 0x26026
movslq -0x10(%rsp), %rcx
movq %rcx, %rdx
subq %rsi, %rdx
leaq (,%rsi,8), %r8
movq -0x8(%rsp), %rax
imulq %rsi, %rax
leaq (%rax,%rdi), %r9
addq $-0x10, %r9
addq %rdi, %rax
addq $-0x8, %rax
movapd 0xc117(%rip), %xmm0     # 0x320d0
movq %rsi, %r10
movq %r10, %r11
movq %rbx, %r10
addq $-0x2, %r11
imulq %rsi, %rbx
movq %r9, %r14
cmpq %rsi, %rcx
jle 0x26004
leaq (%rdi,%r11,8), %r15
movsd (%r15,%rbx,8), %xmm1
xorpd %xmm0, %xmm1
xorl %r15d, %r15d
movq %rdx, %r12
movsd (%rax,%r15), %xmm2
mulsd %xmm1, %xmm2
addsd (%r14,%r15), %xmm2
movsd %xmm2, (%r14,%r15)
addq %r8, %r15
decq %r12
jne 0x25fe6
leaq -0x1(%r11), %r15
addq $-0x8, %r14
testq %r11, %r11
movq %r15, %r11
jg 0x25fcd
leaq -0x1(%r10), %rbx
addq $-0x8, %r9
addq $-0x8, %rax
cmpq $0x2, %r10
jge 0x25fbc
xorl %eax, %eax
jmp 0x2602f
movl $0x1, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq