// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3MeshProperty.cpp
// Function: gaps::R3MeshProperty::Entropy() const
RNScalar R3MeshProperty::
Entropy(void) const
{
  // Get useful variables
  const int nbins = 50;
  RNScalar minimum = Percentile(1);
  RNScalar maximum = Percentile(99);
  RNScalar width = maximum - minimum;
  RNScalar scale = nbins / width;

  // Check nvalues
  if (nvalues == 0) return 0;

  // Make a histogram of values
  int count = 0;
  RNScalar bins[nbins];
  for (int i = 0; i < nbins; i++) bins[i] = 0;
  for (int i = 0; i < nvalues; i++) {
    if (values[i] == RN_UNKNOWN) continue;
    RNScalar bin = scale * (values[i] - minimum);
    int bin1 = (int) bin;
    if (bin1 < 0) bin1 = 0;
    if (bin1 >= nbins) bin1 = nbins - 1;
    int bin2 = bin1 + 1;
    if (bin2 >= nbins) bin2 = nbins - 1;
    RNScalar t = bin - bin1;
    if (t < 0) t = 0;
    if (t > 1) t = 1;
    bins[bin1] += 1 - t;
    bins[bin2] += t;
    count++;
  }

  // Normalize histogram 
  if (count > 0) {
    for (int i = 0; i < nbins; i++) {
      bins[i] /= count;
    }
  }

  // Compute entropy
  RNScalar entropy = 0;
  for (int i = 0; i < nbins; i++) {
    entropy -= bins[i] * (log(bins[i]) / log(2.0));
  }

  // Return entropy
  return entropy;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x1a8, %rsp            # imm = 0x1A8
movq %rdi, %r14
movsd 0x96525(%rip), %xmm0    # 0xdf020
callq 0x48c8a
movsd %xmm0, (%rsp)
movsd 0x9657b(%rip), %xmm0    # 0xdf088
movq %r14, %rdi
callq 0x48c8a
movl 0x408(%r14), %r15d
testq %r15, %r15
je 0x48c76
movsd %xmm0, 0x8(%rsp)
leaq 0x10(%rsp), %rbx
xorl %r12d, %r12d
movl $0x190, %edx            # imm = 0x190
movq %rbx, %rdi
xorl %esi, %esi
callq 0x21470
testl %r15d, %r15d
jle 0x48c2e
movsd (%rsp), %xmm5
movsd 0x8(%rsp), %xmm1
subsd %xmm5, %xmm1
movsd 0x988ee(%rip), %xmm0    # 0xe1450
divsd %xmm1, %xmm0
movq 0x410(%r14), %rax
movsd 0x9651b(%rip), %xmm1    # 0xdf090
movl $0x30, %edx
xorl %esi, %esi
xorl %ecx, %ecx
movsd 0x9649a(%rip), %xmm4    # 0xdf020
movsd (%rax,%rsi,8), %xmm2
ucomisd %xmm1, %xmm2
jne 0x48b93
jnp 0x48bfb
subsd %xmm5, %xmm2
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %edi
testl %edi, %edi
cmovlel %r12d, %edi
cmpl $0x31, %edi
movl $0x31, %r8d
cmovll %edi, %r8d
cmpl $0x30, %edi
cmovgel %edx, %edi
shll $0x3, %edi
xorps %xmm3, %xmm3
cvtsi2sd %r8d, %xmm3
subsd %xmm3, %xmm2
xorpd %xmm3, %xmm3
maxsd %xmm2, %xmm3
movapd %xmm4, %xmm2
minsd %xmm3, %xmm2
movapd %xmm4, %xmm3
subsd %xmm2, %xmm3
addsd 0x10(%rsp,%r8,8), %xmm3
movsd %xmm3, 0x10(%rsp,%r8,8)
addsd 0x8(%rdi,%rbx), %xmm2
movsd %xmm2, 0x8(%rdi,%rbx)
incl %ecx
incq %rsi
cmpq %rsi, %r15
jne 0x48b86
testl %ecx, %ecx
jle 0x48c2e
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
xorl %eax, %eax
movapd 0x10(%rsp,%rax,8), %xmm1
divpd %xmm0, %xmm1
movapd %xmm1, 0x10(%rsp,%rax,8)
addq $0x2, %rax
cmpq $0x32, %rax
jne 0x48c14
xorpd %xmm0, %xmm0
xorl %ebx, %ebx
movsd %xmm0, (%rsp)
movsd 0x10(%rsp,%rbx,8), %xmm0
movsd %xmm0, 0x8(%rsp)
callq 0x216c0
divsd 0x98806(%rip), %xmm0    # 0xe1458
mulsd 0x8(%rsp), %xmm0
movsd (%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
movsd (%rsp), %xmm0
incq %rbx
cmpq $0x32, %rbx
jne 0x48c34
jmp 0x48c7a
xorpd %xmm0, %xmm0
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r14
popq %r15
retq
nop