// File: /workspace/github_c_makefile_star2_O0/zpole[P]IICV/vl/fisher.c
// Function: vl_fisher_encode_f
static vl_size
VL_XCAT(_vl_fisher_encode_, SFX)
(TYPE * enc,
 TYPE const * means, vl_size dimension, vl_size numClusters,
 TYPE const * covariances,
 TYPE const * priors,
 TYPE const * data, vl_size numData,
 int flags)
{
  vl_size dim;
  vl_index i_cl, i_d;
  vl_size numTerms = 0 ;
  TYPE * posteriors ;
  TYPE * sqrtInvSigma;

  assert(numClusters >= 1) ;
  assert(dimension >= 1) ;

  posteriors = vl_malloc(sizeof(TYPE) * numClusters * numData);
  sqrtInvSigma = vl_malloc(sizeof(TYPE) * dimension * numClusters);

  memset(enc, 0, sizeof(TYPE) * 2 * dimension * numClusters) ;

  for (i_cl = 0 ; i_cl < (signed)numClusters ; ++i_cl) {
    for(dim = 0; dim < dimension; dim++) {
      sqrtInvSigma[i_cl*dimension + dim] = sqrt(1.0 / covariances[i_cl*dimension + dim]);
    }
  }

  VL_XCAT(vl_get_gmm_data_posteriors_, SFX)(posteriors, numClusters, numData,
                                            priors,
                                            means, dimension,
                                            covariances,
                                            data) ;

  /* sparsify posterior assignments with the FAST option */
  if (flags & VL_FISHER_FLAG_FAST) {
    for(i_d = 0; i_d < (signed)numData; i_d++) {
      /* find largest posterior assignment for datum i_d */
      vl_index best = 0 ;
      TYPE bestValue = posteriors[i_d * numClusters] ;
      for (i_cl = 1 ; i_cl < (signed)numClusters; ++ i_cl) {
        TYPE p = posteriors[i_cl + i_d * numClusters] ;
        if (p > bestValue) {
          bestValue = p ;
          best = i_cl ;
        }
      }
      /* make all posterior assignments zero but the best one */
      for (i_cl = 0 ; i_cl < (signed)numClusters; ++ i_cl) {
        posteriors[i_cl + i_d * numClusters] =
        (TYPE)(i_cl == best) ;
      }
    }
  }

#if defined(_OPENMP)
#pragma omp parallel for default(shared) private(i_cl, i_d, dim) num_threads(vl_get_max_threads()) reduction(+:numTerms)
#endif
  for(i_cl = 0; i_cl < (signed)numClusters; ++ i_cl) {
    TYPE uprefix;
    TYPE vprefix;

    TYPE * uk = enc + i_cl*dimension ;
    TYPE * vk = enc + i_cl*dimension + numClusters * dimension ;

    /*
    If the GMM component is degenerate and has a null prior, then it
    must have null posterior as well. Hence it is safe to skip it.  In
    practice, we skip over it even if the prior is very small; if by
    any chance a feature is assigned to such a mode, then its weight
    would be very high due to the division by priors[i_cl] below.
    */
    if (priors[i_cl] < 1e-6) { continue ; }

    for(i_d = 0; i_d < (signed)numData; i_d++) {
      TYPE p = posteriors[i_cl + i_d * numClusters] ;
      if (p < 1e-6) continue ;
      numTerms += 1;
      for(dim = 0; dim < dimension; dim++) {
        TYPE diff = data[i_d*dimension + dim] - means[i_cl*dimension + dim] ;
        diff *= sqrtInvSigma[i_cl*dimension + dim] ;
        *(uk + dim) += p * diff ;
        *(vk + dim) += p * (diff * diff - 1);
      }
    }

    if (numData > 0) {
      uprefix = 1/(numData*sqrt(priors[i_cl]));
      vprefix = 1/(numData*sqrt(2*priors[i_cl]));
      for(dim = 0; dim < dimension; dim++) {
        *(uk + dim) = *(uk + dim) * uprefix;
        *(vk + dim) = *(vk + dim) * vprefix;
      }
    }
  }

  vl_free(posteriors);
  vl_free(sqrtInvSigma) ;

  if (flags & VL_FISHER_FLAG_SQUARE_ROOT) {
    for(dim = 0; dim < 2 * dimension * numClusters ; dim++) {
      TYPE z = enc [dim] ;
      if (z >= 0) {
        enc[dim] = VL_XCAT(vl_sqrt_, SFX)(z) ;
      } else {
        enc[dim] = - VL_XCAT(vl_sqrt_, SFX)(- z) ;
      }
    }
  }

  if (flags & VL_FISHER_FLAG_NORMALIZED) {
    TYPE n = 0 ;
    for(dim = 0 ; dim < 2 * dimension * numClusters ; dim++) {
      TYPE z = enc [dim] ;
      n += z * z ;
    }
    n = VL_XCAT(vl_sqrt_, SFX)(n) ;
    n = VL_MAX(n, 1e-12) ;
    for(dim = 0 ; dim < 2 * dimension * numClusters ; dim++) {
      enc[dim] /= n ;
    }
  }

  return numTerms ;
}

// Assembly:
subq $0xe8, %rsp
movl 0x100(%rsp), %eax
movq 0xf8(%rsp), %rax
movq 0xf0(%rsp), %rax
movq %rdi, 0xe0(%rsp)
movq %rsi, 0xd8(%rsp)
movq %rdx, 0xd0(%rsp)
movq %rcx, 0xc8(%rsp)
movq %r8, 0xc0(%rsp)
movq %r9, 0xb8(%rsp)
movq $0x0, 0x98(%rsp)
movq 0xc8(%rsp), %rdi
shlq $0x2, %rdi
imulq 0xf8(%rsp), %rdi
callq 0x9660
movq %rax, 0x90(%rsp)
movq 0xd0(%rsp), %rdi
shlq $0x2, %rdi
imulq 0xc8(%rsp), %rdi
callq 0x9660
movq %rax, 0x88(%rsp)
movq 0xe0(%rsp), %rdi
movq 0xd0(%rsp), %rdx
shlq $0x3, %rdx
imulq 0xc8(%rsp), %rdx
xorl %esi, %esi
callq 0x9480
movq $0x0, 0xa8(%rsp)
movq 0xa8(%rsp), %rax
movq 0xc8(%rsp), %rcx
movslq %ecx, %rcx
cmpq %rcx, %rax
jge 0x15c31
movq $0x0, 0xb0(%rsp)
movq 0xb0(%rsp), %rax
cmpq 0xd0(%rsp), %rax
jae 0x15c16
movq 0xc0(%rsp), %rax
movq 0xa8(%rsp), %rcx
imulq 0xd0(%rsp), %rcx
addq 0xb0(%rsp), %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0x4350e(%rip), %xmm0    # 0x590d8
divsd %xmm1, %xmm0
callq 0x9b20
cvtsd2ss %xmm0, %xmm0
movq 0x88(%rsp), %rax
movq 0xa8(%rsp), %rcx
imulq 0xd0(%rsp), %rcx
addq 0xb0(%rsp), %rcx
movss %xmm0, (%rax,%rcx,4)
movq 0xb0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xb0(%rsp)
jmp 0x15b86
jmp 0x15c18
movq 0xa8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xa8(%rsp)
jmp 0x15b5e
movq 0x90(%rsp), %rdi
movq 0xc8(%rsp), %rsi
movq 0xf8(%rsp), %rdx
movq 0xb8(%rsp), %rcx
movq 0xd8(%rsp), %r8
movq 0xd0(%rsp), %r9
movq 0xc0(%rsp), %r10
movq 0xf0(%rsp), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x9930
movl 0x100(%rsp), %eax
andl $0x4, %eax
cmpl $0x0, %eax
je 0x15e1d
movq $0x0, 0xa0(%rsp)
movq 0xa0(%rsp), %rax
movq 0xf8(%rsp), %rcx
movslq %ecx, %rcx
cmpq %rcx, %rax
jge 0x15e1b
movq $0x0, 0x80(%rsp)
movq 0x90(%rsp), %rax
movq 0xa0(%rsp), %rcx
imulq 0xc8(%rsp), %rcx
movss (%rax,%rcx,4), %xmm0
movss %xmm0, 0x7c(%rsp)
movq $0x1, 0xa8(%rsp)
movq 0xa8(%rsp), %rax
movq 0xc8(%rsp), %rcx
movslq %ecx, %rcx
cmpq %rcx, %rax
jge 0x15d81
movq 0x90(%rsp), %rax
movq 0xa8(%rsp), %rcx
movq 0xa0(%rsp), %rdx
imulq 0xc8(%rsp), %rdx
addq %rdx, %rcx
movss (%rax,%rcx,4), %xmm0
movss %xmm0, 0x78(%rsp)
movss 0x78(%rsp), %xmm0
ucomiss 0x7c(%rsp), %xmm0
jbe 0x15d66
movss 0x78(%rsp), %xmm0
movss %xmm0, 0x7c(%rsp)
movq 0xa8(%rsp), %rax
movq %rax, 0x80(%rsp)
jmp 0x15d68
movq 0xa8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xa8(%rsp)
jmp 0x15cf6
movq $0x0, 0xa8(%rsp)
movq 0xa8(%rsp), %rax
movq 0xc8(%rsp), %rcx
movslq %ecx, %rcx
cmpq %rcx, %rax
jge 0x15e00
movq 0xa8(%rsp), %rax
cmpq 0x80(%rsp), %rax
sete %al
andb $0x1, %al
movzbl %al, %eax
cvtsi2ss %eax, %xmm0
movq 0x90(%rsp), %rax
movq 0xa8(%rsp), %rcx
movq 0xa0(%rsp), %rdx
imulq 0xc8(%rsp), %rdx
addq %rdx, %rcx
movss %xmm0, (%rax,%rcx,4)
movq 0xa8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xa8(%rsp)
jmp 0x15d8d
jmp 0x15e02
movq 0xa0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xa0(%rsp)
jmp 0x15c9e
jmp 0x15e1d
movq $0x0, 0xa8(%rsp)
movq 0xa8(%rsp), %rax
movq 0xc8(%rsp), %rcx
movslq %ecx, %rcx
cmpq %rcx, %rax
jge 0x16240
movq 0xe0(%rsp), %rax
movq 0xa8(%rsp), %rcx
imulq 0xd0(%rsp), %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0x68(%rsp)
movq 0xe0(%rsp), %rax
movq 0xa8(%rsp), %rcx
imulq 0xd0(%rsp), %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq 0xc8(%rsp), %rcx
imulq 0xd0(%rsp), %rcx
shlq $0x2, %rcx
addq %rcx, %rax
movq %rax, 0x60(%rsp)
movq 0xb8(%rsp), %rax
movq 0xa8(%rsp), %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0x43380(%rip), %xmm0    # 0x59248
ucomisd %xmm1, %xmm0
jbe 0x15ed3
jmp 0x16227
movq $0x0, 0xa0(%rsp)
movq 0xa0(%rsp), %rax
movq 0xf8(%rsp), %rcx
movslq %ecx, %rcx
cmpq %rcx, %rax
jge 0x160a1
movq 0x90(%rsp), %rax
movq 0xa8(%rsp), %rcx
movq 0xa0(%rsp), %rdx
imulq 0xc8(%rsp), %rdx
addq %rdx, %rcx
movss (%rax,%rcx,4), %xmm0
movss %xmm0, 0x5c(%rsp)
movss 0x5c(%rsp), %xmm0
cvtss2sd %xmm0, %xmm1
movsd 0x4330c(%rip), %xmm0    # 0x59248
ucomisd %xmm1, %xmm0
jbe 0x15f47
jmp 0x16088
movq 0x98(%rsp), %rax
addq $0x1, %rax
movq %rax, 0x98(%rsp)
movq $0x0, 0xb0(%rsp)
movq 0xb0(%rsp), %rax
cmpq 0xd0(%rsp), %rax
jae 0x16086
movq 0xf0(%rsp), %rax
movq 0xa0(%rsp), %rcx
imulq 0xd0(%rsp), %rcx
addq 0xb0(%rsp), %rcx
movss (%rax,%rcx,4), %xmm0
movq 0xd8(%rsp), %rax
movq 0xa8(%rsp), %rcx
imulq 0xd0(%rsp), %rcx
addq 0xb0(%rsp), %rcx
subss (%rax,%rcx,4), %xmm0
movss %xmm0, 0x58(%rsp)
movq 0x88(%rsp), %rax
movq 0xa8(%rsp), %rcx
imulq 0xd0(%rsp), %rcx
addq 0xb0(%rsp), %rcx
movss (%rax,%rcx,4), %xmm0
mulss 0x58(%rsp), %xmm0
movss %xmm0, 0x58(%rsp)
movss 0x5c(%rsp), %xmm0
movss 0x58(%rsp), %xmm2
movq 0x68(%rsp), %rax
movq 0xb0(%rsp), %rcx
movss (%rax,%rcx,4), %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, (%rax,%rcx,4)
movss 0x5c(%rsp), %xmm0
movss 0x58(%rsp), %xmm2
movss 0x58(%rsp), %xmm1
mulss %xmm1, %xmm2
movss 0x4320a(%rip), %xmm1    # 0x59254
addss %xmm1, %xmm2
movq 0x60(%rsp), %rax
movq 0xb0(%rsp), %rcx
movss (%rax,%rcx,4), %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, (%rax,%rcx,4)
movq 0xb0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xb0(%rsp)
jmp 0x15f67
jmp 0x16088
movq 0xa0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xa0(%rsp)
jmp 0x15edf
cmpq $0x0, 0xf8(%rsp)
jbe 0x16225
movq 0xf8(%rsp), %xmm0
movaps 0x430a0(%rip), %xmm1    # 0x59160
movaps %xmm1, 0x20(%rsp)
punpckldq %xmm1, %xmm0    # xmm0 = xmm0[0],xmm1[0],xmm0[1],xmm1[1]
movapd 0x4309f(%rip), %xmm1    # 0x59170
movaps %xmm1, 0x30(%rsp)
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0xb8(%rsp), %rax
movq 0xa8(%rsp), %rcx
movss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x9b20
movsd 0x18(%rsp), %xmm3
movaps 0x20(%rsp), %xmm2
movaps 0x30(%rsp), %xmm1
mulsd %xmm0, %xmm3
movsd 0x42fb3(%rip), %xmm0    # 0x590d8
divsd %xmm3, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x74(%rsp)
movq 0xf8(%rsp), %xmm0
punpckldq %xmm2, %xmm0    # xmm0 = xmm0[0],xmm2[0],xmm0[1],xmm2[1]
subpd %xmm1, %xmm0
movaps %xmm0, %xmm1
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
addsd %xmm1, %xmm0
movsd %xmm0, 0x40(%rsp)
movq 0xb8(%rsp), %rax
movq 0xa8(%rsp), %rcx
movss 0x430e3(%rip), %xmm0    # 0x59250
mulss (%rax,%rcx,4), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x9b20
movsd 0x40(%rsp), %xmm1
mulsd %xmm0, %xmm1
movsd 0x42f4b(%rip), %xmm0    # 0x590d8
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x70(%rsp)
movq $0x0, 0xb0(%rsp)
movq 0xb0(%rsp), %rax
cmpq 0xd0(%rsp), %rax
jae 0x16223
movq 0x68(%rsp), %rax
movq 0xb0(%rsp), %rcx
movss (%rax,%rcx,4), %xmm0
mulss 0x74(%rsp), %xmm0
movq 0x68(%rsp), %rax
movq 0xb0(%rsp), %rcx
movss %xmm0, (%rax,%rcx,4)
movq 0x60(%rsp), %rax
movq 0xb0(%rsp), %rcx
movss (%rax,%rcx,4), %xmm0
mulss 0x70(%rsp), %xmm0
movq 0x60(%rsp), %rax
movq 0xb0(%rsp), %rcx
movss %xmm0, (%rax,%rcx,4)
movq 0xb0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xb0(%rsp)
jmp 0x161a7
jmp 0x16225
jmp 0x16227
movq 0xa8(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xa8(%rsp)
jmp 0x15e29
movq 0x90(%rsp), %rdi
callq 0x9620
movq 0x88(%rsp), %rdi
callq 0x9620
movl 0x100(%rsp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x16340
movq $0x0, 0xb0(%rsp)
movq 0xb0(%rsp), %rax
movq 0xd0(%rsp), %rcx
shlq %rcx
imulq 0xc8(%rsp), %rcx
cmpq %rcx, %rax
jae 0x1633e
movq 0xe0(%rsp), %rax
movq 0xb0(%rsp), %rcx
movss (%rax,%rcx,4), %xmm0
movss %xmm0, 0x54(%rsp)
movss 0x54(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomiss %xmm1, %xmm0
jb 0x162e9
movss 0x54(%rsp), %xmm0
callq 0x16ee0
movq 0xe0(%rsp), %rax
movq 0xb0(%rsp), %rcx
movss %xmm0, (%rax,%rcx,4)
jmp 0x16323
movss 0x54(%rsp), %xmm0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
callq 0x16ee0
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
movq 0xe0(%rsp), %rax
movq 0xb0(%rsp), %rcx
movss %xmm0, (%rax,%rcx,4)
jmp 0x16325
movq 0xb0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xb0(%rsp)
jmp 0x16279
jmp 0x16340
movl 0x100(%rsp), %eax
andl $0x2, %eax
cmpl $0x0, %eax
je 0x1649e
xorps %xmm0, %xmm0
movss %xmm0, 0x50(%rsp)
movq $0x0, 0xb0(%rsp)
movq 0xb0(%rsp), %rax
movq 0xd0(%rsp), %rcx
shlq %rcx
imulq 0xc8(%rsp), %rcx
cmpq %rcx, %rax
jae 0x163da
movq 0xe0(%rsp), %rax
movq 0xb0(%rsp), %rcx
movss (%rax,%rcx,4), %xmm0
movss %xmm0, 0x4c(%rsp)
movss 0x4c(%rsp), %xmm0
movss 0x4c(%rsp), %xmm2
movss 0x50(%rsp), %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, 0x50(%rsp)
movq 0xb0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xb0(%rsp)
jmp 0x16368
movss 0x50(%rsp), %xmm0
callq 0x16ee0
movss %xmm0, 0x50(%rsp)
movss 0x50(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x42e43(%rip), %xmm1    # 0x59240
ucomisd %xmm1, %xmm0
jbe 0x16415
movss 0x50(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
jmp 0x16425
movsd 0x42e23(%rip), %xmm0    # 0x59240
movsd %xmm0, 0x10(%rsp)
jmp 0x16425
movsd 0x10(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x50(%rsp)
movq $0x0, 0xb0(%rsp)
movq 0xb0(%rsp), %rax
movq 0xd0(%rsp), %rcx
shlq %rcx
imulq 0xc8(%rsp), %rcx
cmpq %rcx, %rax
jae 0x1649c
movss 0x50(%rsp), %xmm1
movq 0xe0(%rsp), %rax
movq 0xb0(%rsp), %rcx
movss (%rax,%rcx,4), %xmm0
divss %xmm1, %xmm0
movss %xmm0, (%rax,%rcx,4)
movq 0xb0(%rsp), %rax
addq $0x1, %rax
movq %rax, 0xb0(%rsp)
jmp 0x16441
jmp 0x1649e
movq 0x98(%rsp), %rax
addq $0xe8, %rsp
retq
nop