// File: /workspace/github_c_makefile_star2_O2/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
movslq %esi, %r8
xorl %r9d, %r9d
testq %r8, %r8
movl $0x0, %eax
cmovgq %r8, %rax
movq %rax, -0x8(%rsp)
movslq %edx, %rcx
leaq (,%rsi,8), %rdx
leaq 0x8(,%rsi,8), %rax
movq %rax, -0x18(%rsp)
leaq 0x8(%rdi), %rax
movq %rax, -0x10(%rsp)
movsd 0xbeee(%rip), %xmm1     # 0x2d968
movapd 0xb61e(%rip), %xmm0     # 0x2d0a0
movapd 0xb656(%rip), %xmm2     # 0x2d0e0
movq %rdi, %r15
movq %rdi, %r12
movq %r8, %r13
cmpq %rcx, %r9
movq %rcx, %rax
cmovgq %r9, %rax
cmpq -0x8(%rsp), %r9
je 0x21bad
movq %r9, %r14
imulq %rsi, %r14
xorpd %xmm3, %xmm3
xorl %ebx, %ebx
xorpd %xmm4, %xmm4
movabsq $-0x1, %r11
cmpq %rbx, %r13
je 0x21b08
leaq (%r9,%rbx), %r10
movsd (%r12,%rbx,8), %xmm5
movapd %xmm5, %xmm6
andpd %xmm2, %xmm6
ucomisd %xmm4, %xmm6
movapd %xmm4, %xmm7
cmpltsd %xmm6, %xmm7
andpd %xmm7, %xmm5
andnpd %xmm3, %xmm7
orpd %xmm5, %xmm7
cmovaq %r10, %r11
maxsd %xmm4, %xmm6
incq %rbx
movapd %xmm7, %xmm3
movapd %xmm6, %xmm4
jmp 0x21ac3
ucomisd %xmm4, %xmm1
ja 0x21c34
leaq (%r15,%r11,8), %r11
xorl %r10d, %r10d
movq %rax, %rbx
cmpq %rbx, %r9
je 0x21b45
movsd (%r11,%r10), %xmm4
movsd (%r12,%r10), %xmm5
movsd %xmm5, (%r11,%r10)
divsd %xmm3, %xmm4
movsd %xmm4, (%r12,%r10)
decq %rbx
addq %rdx, %r10
jmp 0x21b1c
movq -0x10(%rsp), %rbx
movq %r9, %r11
incq %r11
cmpq %r8, %r11
jge 0x21b92
leaq (%rdi,%r11,8), %r10
movsd (%r10,%r14,8), %xmm3
xorpd %xmm0, %xmm3
xorl %r10d, %r10d
movq %rax, %rbp
cmpq %rbp, %r9
je 0x21b8c
movsd (%r12,%r10), %xmm4
mulsd %xmm3, %xmm4
addsd (%rbx,%r10), %xmm4
movsd %xmm4, (%rbx,%r10)
decq %rbp
addq %rdx, %r10
jmp 0x21b69
addq $0x8, %rbx
jmp 0x21b4d
incq %r9
decq %r13
movq -0x18(%rsp), %rax
addq %rax, %r12
addq %rdx, %r15
addq %rax, -0x10(%rsp)
jmp 0x21a93
movq -0x18(%rsp), %rax
imulq %rsi, %rax
leaq (%rax,%rdi), %r12
addq $-0x10, %r12
leaq (%rax,%rdi), %r8
addq $-0x8, %r8
xorl %eax, %eax
movq %rsi, %r10
leaq -0x1(%r10), %r9
testq %r9, %r9
jle 0x21c39
addq $-0x2, %r10
movq %r9, %r11
imulq %rsi, %r11
movq %r12, %rbx
testq %r10, %r10
js 0x21c27
leaq (%rdi,%r10,8), %r14
movsd (%r14,%r11,8), %xmm1
xorpd %xmm0, %xmm1
xorl %r14d, %r14d
movq %rsi, %r15
cmpq %rcx, %r15
jge 0x21c1e
movsd (%r8,%r14), %xmm2
mulsd %xmm1, %xmm2
addsd (%rbx,%r14), %xmm2
movsd %xmm2, (%rbx,%r14)
incq %r15
addq %rdx, %r14
jmp 0x21bfb
decq %r10
addq $-0x8, %rbx
jmp 0x21be2
addq $-0x8, %r12
addq $-0x8, %r8
movq %r9, %r10
jmp 0x21bcb
movl $0x1, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq