// File: /workspace/github_c_makefile_star2_O1/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
testq %rcx, %rcx
jle 0x260e8
movslq -0x10(%rsp), %r8
leaq (,%rsi,8), %r9
leaq 0x8(,%rsi,8), %rax
movq %rax, -0x8(%rsp)
leaq 0x8(%rdi), %r11
xorl %edx, %edx
movapd 0xd14d(%rip), %xmm0     # 0x33100
movsd 0xd9ed(%rip), %xmm1     # 0x339a8
movapd 0xd10d(%rip), %xmm2     # 0x330d0
movq %rdi, %r14
movq %rdi, %r15
xorpd %xmm3, %xmm3
movq $-0x1, %r12
xorl %eax, %eax
xorpd %xmm5, %xmm5
movapd %xmm3, %xmm6
leaq (%rdx,%rax), %r10
movsd (%r15,%rax,8), %xmm7
movapd %xmm7, %xmm3
andpd %xmm0, %xmm3
ucomisd %xmm6, %xmm3
movapd %xmm6, %xmm4
cmpltsd %xmm3, %xmm4
andpd %xmm4, %xmm7
andnpd %xmm5, %xmm4
orpd %xmm7, %xmm4
maxsd %xmm6, %xmm3
cmovaq %r10, %r12
leaq (%rdx,%rax), %r10
incq %r10
incq %rax
movapd %xmm4, %xmm5
cmpq %rcx, %r10
jl 0x25fda
movl $0x1, %eax
ucomisd %xmm3, %xmm1
ja 0x2619a
cmpq %r8, %rdx
jge 0x26069
leaq (%r14,%r12,8), %r12
xorl %r13d, %r13d
movq %rdx, %rbp
movsd (%r12,%r13), %xmm5
movsd (%r15,%r13), %xmm6
movsd %xmm6, (%r12,%r13)
divsd %xmm4, %xmm5
movsd %xmm5, (%r15,%r13)
incq %rbp
addq %r9, %r13
cmpq %r8, %rbp
jl 0x26042
leaq 0x1(%rdx), %r12
cmpq %rcx, %r12
jge 0x260c4
movq %rdx, %r13
imulq %rsi, %r13
movq %r11, %rbp
cmpq %r8, %rdx
jge 0x260b8
leaq (%rdi,%r12,8), %r10
movsd (%r10,%r13,8), %xmm4
xorpd %xmm2, %xmm4
xorl %r10d, %r10d
movq %rdx, %rbx
movsd (%r15,%r10), %xmm5
mulsd %xmm4, %xmm5
addsd (%rbp,%r10), %xmm5
movsd %xmm5, (%rbp,%r10)
incq %rbx
addq %r9, %r10
cmpq %r8, %rbx
jl 0x26095
incq %r12
addq $0x8, %rbp
cmpq %rcx, %r12
jl 0x2607c
ucomisd %xmm3, %xmm1
ja 0x2619a
incq %rdx
movq -0x8(%rsp), %rax
addq %rax, %r15
addq %r9, %r14
addq %rax, %r11
cmpq %rcx, %rdx
jne 0x25fc9
leaq -0x1(%rsi), %rbx
testq %rbx, %rbx
jle 0x26198
movslq -0x10(%rsp), %rax
movq %rax, %rcx
subq %rsi, %rcx
leaq (,%rsi,8), %rdx
leaq 0x8(,%rsi,8), %r9
imulq %rsi, %r9
leaq (%r9,%rdi), %r8
addq $-0x10, %r8
addq %rdi, %r9
addq $-0x8, %r9
movapd 0xcfa5(%rip), %xmm0     # 0x330d0
movq %rsi, %r10
movq %r10, %r11
movq %rbx, %r10
addq $-0x2, %r11
imulq %rsi, %rbx
movq %r8, %r14
cmpq %rsi, %rax
jle 0x26176
leaq (%rdi,%r11,8), %r15
movsd (%r15,%rbx,8), %xmm1
xorpd %xmm0, %xmm1
xorl %r15d, %r15d
movq %rcx, %r12
movsd (%r9,%r15), %xmm2
mulsd %xmm1, %xmm2
addsd (%r14,%r15), %xmm2
movsd %xmm2, (%r14,%r15)
addq %rdx, %r15
decq %r12
jne 0x26158
leaq -0x1(%r11), %r15
addq $-0x8, %r14
testq %r11, %r11
movq %r15, %r11
jg 0x2613f
leaq -0x1(%r10), %rbx
addq $-0x8, %r8
addq $-0x8, %r9
cmpq $0x2, %r10
jge 0x2612e
xorl %eax, %eax
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq