// File: /workspace/github_cpp_makefile_star2_O2/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 %rbx
subq $0x1a0, %rsp            # imm = 0x1A0
movq %rdi, %rbx
movsd 0x85c23(%rip), %xmm0    # 0xc9020
callq 0x43578
movsd %xmm0, (%rsp)
movsd 0x85c71(%rip), %xmm0    # 0xc9080
movq %rbx, %rdi
callq 0x43578
movl 0x408(%rbx), %eax
testl %eax, %eax
je 0x4356b
movsd (%rsp), %xmm5
subsd %xmm5, %xmm0
movsd 0x87fda(%rip), %xmm1    # 0xcb410
divsd %xmm0, %xmm1
xorl %ecx, %ecx
movsd 0x85bdc(%rip), %xmm4    # 0xc9020
cmpq $0x32, %rcx
je 0x43455
andq $0x0, 0x10(%rsp,%rcx,8)
incq %rcx
jmp 0x43444
movq 0x410(%rbx), %rcx
xorl %edx, %edx
testl %eax, %eax
cmovlel %edx, %eax
movsd 0x85c1d(%rip), %xmm0    # 0xc9088
pushq $0x31
popq %rdi
pushq $0x30
popq %r8
leaq 0x10(%rsp), %r9
xorl %r10d, %r10d
xorl %esi, %esi
cmpq %r10, %rax
je 0x434fa
movsd (%rcx,%r10,8), %xmm2
ucomisd %xmm0, %xmm2
jne 0x4348f
jnp 0x434f5
subsd %xmm5, %xmm2
mulsd %xmm1, %xmm2
cvttsd2si %xmm2, %r11d
testl %r11d, %r11d
cmovlel %edx, %r11d
cmpl $0x31, %r11d
movl %edi, %ebx
cmovll %r11d, %ebx
cmpl $0x30, %r11d
cmovgel %r8d, %r11d
shll $0x3, %r11d
cvtsi2sd %ebx, %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,%rbx,8), %xmm3
movsd %xmm3, 0x10(%rsp,%rbx,8)
addsd 0x8(%r11,%r9), %xmm2
movsd %xmm2, 0x8(%r11,%r9)
incl %esi
incq %r10
jmp 0x4347c
testl %esi, %esi
jle 0x4351f
cvtsi2sd %esi, %xmm0
xorl %eax, %eax
cmpq $0x32, %rax
je 0x4351f
movsd 0x10(%rsp,%rax,8), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp,%rax,8)
incq %rax
jmp 0x43504
xorpd %xmm0, %xmm0
xorl %ebx, %ebx
cmpq $0x32, %rbx
je 0x4356f
movsd 0x10(%rsp,%rbx,8), %xmm1
movsd %xmm1, 0x8(%rsp)
movsd %xmm0, (%rsp)
movapd %xmm1, %xmm0
callq 0x216c0
divsd 0x87ecb(%rip), %xmm0    # 0xcb418
mulsd 0x8(%rsp), %xmm0
movsd (%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
movsd (%rsp), %xmm0
incq %rbx
jmp 0x43525
xorpd %xmm0, %xmm0
addq $0x1a0, %rsp            # imm = 0x1A0
popq %rbx
retq