// File: /workspace/github_cpp_makefile_star2_O1/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 %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x1a8, %rsp            # imm = 0x1A8
movq %rdi, %r14
movsd 0x97e9e(%rip), %xmm0    # 0xe1020
callq 0x49310
movsd %xmm0, (%rsp)
movsd 0x97ef4(%rip), %xmm0    # 0xe1088
movq %r14, %rdi
callq 0x49310
movl 0x408(%r14), %r15d
testq %r15, %r15
je 0x492fd
movsd %xmm0, 0x8(%rsp)
leaq 0x10(%rsp), %rbx
xorl %ebp, %ebp
movl $0x190, %edx            # imm = 0x190
movq %rbx, %rdi
xorl %esi, %esi
callq 0x21470
testl %r15d, %r15d
movsd 0x97e4d(%rip), %xmm4    # 0xe1020
movsd (%rsp), %xmm5
jle 0x4928a
movsd 0x8(%rsp), %xmm1
subsd %xmm5, %xmm1
movsd 0x9a270(%rip), %xmm0    # 0xe3460
divsd %xmm1, %xmm0
movq 0x410(%r14), %rax
xorl %ecx, %ecx
movsd 0x97e8b(%rip), %xmm1    # 0xe1090
movl $0x30, %edx
xorl %esi, %esi
xorl %ebp, %ebp
movsd (%rax,%rsi,8), %xmm2
ucomisd %xmm1, %xmm2
jne 0x4921b
jnp 0x49282
subsd %xmm5, %xmm2
mulsd %xmm0, %xmm2
cvttsd2si %xmm2, %edi
testl %edi, %edi
cmovlel %ecx, %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 %ebp
incq %rsi
cmpq %rsi, %r15
jne 0x4920e
testl %ebp, %ebp
jle 0x492b5
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %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 0x4929b
xorpd %xmm0, %xmm0
xorl %ebx, %ebx
movsd %xmm0, (%rsp)
movsd 0x10(%rsp,%rbx,8), %xmm0
movsd %xmm0, 0x8(%rsp)
callq 0x216c0
divsd 0x9a18f(%rip), %xmm0    # 0xe3468
mulsd 0x8(%rsp), %xmm0
movsd (%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, (%rsp)
movsd (%rsp), %xmm0
incq %rbx
cmpq $0x32, %rbx
jne 0x492bb
jmp 0x49301
xorpd %xmm0, %xmm0
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
nop