// File: /workspace/github_c_makefile_star2_O2/zpole[P]IICV/vl/gmm.c
// Function: vl_get_gmm_data_posteriors_f
double
VL_XCAT(vl_get_gmm_data_posteriors_, SFX)
(TYPE * posteriors,
 vl_size numClusters,
 vl_size numData,
 TYPE const * priors,
 TYPE const * means,
 vl_size dimension,
 TYPE const * covariances,
 TYPE const * data)
{
  vl_index i_d, i_cl;
  vl_size dim;
  double LL = 0;

  TYPE halfDimLog2Pi = (dimension / 2.0) * log(2.0*VL_PI);
  TYPE * logCovariances ;
  TYPE * logWeights ;
  TYPE * invCovariances ;

#if (FLT == VL_TYPE_FLOAT)
  VlFloatVector3ComparisonFunction distFn = vl_get_vector_3_comparison_function_f(VlDistanceMahalanobis) ;
#else
  VlDoubleVector3ComparisonFunction distFn = vl_get_vector_3_comparison_function_d(VlDistanceMahalanobis) ;
#endif

  logCovariances = vl_malloc(sizeof(TYPE) * numClusters) ;
  invCovariances = vl_malloc(sizeof(TYPE) * numClusters * dimension) ;
  logWeights = vl_malloc(sizeof(TYPE) * numClusters) ;

#if defined(_OPENMP)
#pragma omp parallel for private(i_cl,dim) num_threads(vl_get_max_threads())
#endif
  for (i_cl = 0 ; i_cl < (signed)numClusters ; ++ i_cl) {
    TYPE logSigma = 0 ;
    if (priors[i_cl] < VL_GMM_MIN_PRIOR) {
      logWeights[i_cl] = - (TYPE) VL_INFINITY_D ;
    } else {
      logWeights[i_cl] = log(priors[i_cl]);
    }
    for(dim = 0 ; dim < dimension ; ++ dim) {
      logSigma += log(covariances[i_cl*dimension + dim]);
      invCovariances [i_cl*dimension + dim] = (TYPE) 1.0 / covariances[i_cl*dimension + dim];
    }
    logCovariances[i_cl] = logSigma;
  } /* end of parallel region */

#if defined(_OPENMP)
#pragma omp parallel for private(i_cl,i_d) reduction(+:LL) \
num_threads(vl_get_max_threads())
#endif
  for (i_d = 0 ; i_d < (signed)numData ; ++ i_d) {
    TYPE clusterPosteriorsSum = 0;
    TYPE maxPosterior = (TYPE)(-VL_INFINITY_D) ;

    for (i_cl = 0 ; i_cl < (signed)numClusters ; ++ i_cl) {
      TYPE p =
      logWeights[i_cl]
      - halfDimLog2Pi
      - 0.5 * logCovariances[i_cl]
      - 0.5 * distFn (dimension,
                      data + i_d * dimension,
                      means + i_cl * dimension,
                      invCovariances + i_cl * dimension) ;
      posteriors[i_cl + i_d * numClusters] = p ;
      if (p > maxPosterior) { maxPosterior = p ; }
    }

    for (i_cl = 0 ; i_cl < (signed)numClusters ; ++i_cl) {
      TYPE p = posteriors[i_cl + i_d * numClusters] ;
      p =  exp(p - maxPosterior) ;
      posteriors[i_cl + i_d * numClusters] = p ;
      clusterPosteriorsSum += p ;
    }

    LL +=  log(clusterPosteriorsSum) + (double) maxPosterior ;

    for (i_cl = 0 ; i_cl < (signed)numClusters ; ++i_cl) {
      posteriors[i_cl + i_d * numClusters] /= clusterPosteriorsSum ;
    }
  } /* end of parallel region */

  vl_free(logCovariances);
  vl_free(logWeights);
  vl_free(invCovariances);

  return LL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %r9, %rbx
movq %r8, 0x58(%rsp)
movq %rcx, 0x8(%rsp)
movq %rdx, 0x10(%rsp)
movq %rsi, %r12
movq %rdi, %r15
movq %r9, %xmm0
punpckldq 0x1c86b(%rip), %xmm0 # xmm0 = xmm0[0],mem[0],xmm0[1],mem[1]
subpd 0x1c873(%rip), %xmm0    # 0x2d170
movq 0xb0(%rsp), %rbp
movapd %xmm0, %xmm1
unpckhpd %xmm0, %xmm1            # xmm1 = xmm1[1],xmm0[1]
addsd %xmm0, %xmm1
mulsd 0x1c8a7(%rip), %xmm1    # 0x2d1c0
mulsd 0x1cb7f(%rip), %xmm1    # 0x2d4a0
movapd %xmm1, 0x40(%rsp)
pushq $0x5
popq %rdi
callq 0x9a30
movq %rax, 0x70(%rsp)
leaq (,%r12,4), %r14
movq %r14, %rdi
callq 0x95e0
movq %rax, 0x20(%rsp)
movq %r14, %rdi
imulq %rbx, %rdi
callq 0x95e0
movq %rax, %r13
movq %r14, 0x50(%rsp)
movq %r14, %rdi
callq 0x95e0
movq %rax, 0x18(%rsp)
movslq %r12d, %r14
xorl %r12d, %r12d
testq %r14, %r14
cmovleq %r12, %r14
movq %rbx, 0x28(%rsp)
leaq (,%rbx,4), %rax
movq %rax, 0x38(%rsp)
movq %r13, 0x30(%rsp)
cmpq %r14, %r12
je 0x10a2f
movq 0x8(%rsp), %rax
cvtss2sd (%rax,%r12,4), %xmm0
movsd 0x1c9e6(%rip), %xmm1    # 0x2d390
ucomisd %xmm0, %xmm1
movss 0x1cb1a(%rip), %xmm1    # 0x2d4d0
ja 0x109c1
callq 0x9450
cvtsd2ss %xmm0, %xmm1
movq 0x18(%rsp), %rax
movss %xmm1, (%rax,%r12,4)
xorps %xmm1, %xmm1
xorl %ebx, %ebx
cmpq %rbx, 0x28(%rsp)
je 0x10a11
cvtss2sd (%rbp,%rbx,4), %xmm0
movss %xmm1, 0x4(%rsp)
callq 0x9450
cvtss2sd 0x4(%rsp), %xmm1
addsd %xmm0, %xmm1
cvtsd2ss %xmm1, %xmm1
movss 0x1c925(%rip), %xmm0    # 0x2d324
divss (%rbp,%rbx,4), %xmm0
movss %xmm0, (%r13,%rbx,4)
incq %rbx
jmp 0x109d1
movq 0x20(%rsp), %rax
movss %xmm1, (%rax,%r12,4)
incq %r12
movq 0x38(%rsp), %rax
addq %rax, %r13
addq %rax, %rbp
jmp 0x1098e
cvtsd2ss 0x40(%rsp), %xmm0
movss %xmm0, 0x40(%rsp)
movslq 0x10(%rsp), %rax
xorl %ecx, %ecx
testq %rax, %rax
cmovleq %rcx, %rax
movq %rax, 0x60(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
cmpq 0x60(%rsp), %rcx
je 0x10bb6
movq %rcx, 0x68(%rsp)
movq %rcx, %rax
imulq 0x28(%rsp), %rax
movq 0xb8(%rsp), %rcx
leaq (%rcx,%rax,4), %r13
movq 0x58(%rsp), %r12
movq 0x30(%rsp), %rbp
movss 0x1ca42(%rip), %xmm0    # 0x2d4d0
movss %xmm0, 0x4(%rsp)
xorl %ebx, %ebx
cmpq %rbx, %r14
je 0x10b20
movq 0x18(%rsp), %rax
movss (%rax,%rbx,4), %xmm0
subss 0x40(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
movq 0x20(%rsp), %rax
cvtss2sd (%rax,%rbx,4), %xmm2
movsd 0x1c72b(%rip), %xmm1    # 0x2d1f0
mulsd %xmm1, %xmm2
addsd %xmm0, %xmm2
movsd %xmm2, 0x8(%rsp)
movq 0x28(%rsp), %rdi
movq %r13, %rsi
movq %r12, %rdx
movq %rbp, %rcx
callq *0x70(%rsp)
cvtss2sd %xmm0, %xmm0
mulsd 0x1c6ff(%rip), %xmm0    # 0x2d1f0
addsd 0x8(%rsp), %xmm0
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r15,%rbx,4)
maxss 0x4(%rsp), %xmm0
incq %rbx
movq 0x38(%rsp), %rax
addq %rax, %rbp
addq %rax, %r12
movss %xmm0, 0x4(%rsp)
jmp 0x10a96
xorpd %xmm1, %xmm1
xorl %ebx, %ebx
cmpq %rbx, %r14
movss %xmm1, 0x8(%rsp)
je 0x10b5f
movss (%r15,%rbx,4), %xmm0
subss 0x4(%rsp), %xmm0
cvtss2sd %xmm0, %xmm0
callq 0x9ae0
movss 0x8(%rsp), %xmm1
cvtsd2ss %xmm0, %xmm0
movss %xmm0, (%r15,%rbx,4)
addss %xmm0, %xmm1
incq %rbx
jmp 0x10b26
cvtss2sd %xmm1, %xmm0
callq 0x9450
movss 0x8(%rsp), %xmm2
cvtss2sd 0x4(%rsp), %xmm1
addsd %xmm0, %xmm1
xorl %eax, %eax
movq 0x68(%rsp), %rcx
cmpq %rax, %r14
je 0x10b99
movss (%r15,%rax,4), %xmm0
divss %xmm2, %xmm0
movss %xmm0, (%r15,%rax,4)
incq %rax
jmp 0x10b7f
movsd 0x10(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
incq %rcx
addq 0x50(%rsp), %r15
jmp 0x10a57
movq 0x20(%rsp), %rdi
callq 0x95b0
movq 0x18(%rsp), %rdi
callq 0x95b0
movq 0x30(%rsp), %rdi
callq 0x95b0
movsd 0x10(%rsp), %xmm0
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq