// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::PercentileFilter(double, double)
void R2Grid::
PercentileFilter(RNLength grid_radius, RNScalar percentile)
{
  // Make copy of grid
  R2Grid copy(*this);

  // Get convenient variables
  RNScalar grid_radius_squared = grid_radius * grid_radius;
  int r = (int) grid_radius;
  assert(r >= 0);
  int max_samples = (2*r+1) * (2*r+1);
  RNScalar *samples = new RNScalar [ max_samples ];
  assert(samples);

  // Set every sample to be Kth percentile of surrounding region in input grid
  for (int cy = 0; cy < YResolution(); cy++) {
    for (int cx = 0; cx < XResolution(); cx++) {
      // Check if current value is unknown - if so, don't update
      if (GridValue(cx, cy) != R2_GRID_UNKNOWN_VALUE) {
        // Build list of grid values in neighborhood
        int nsamples = 0;
        int ymin = cy - r;
        int ymax = cy + r;
        if (ymin < 0) ymin = 0;
        if (ymax >= YResolution()) ymax = YResolution() - 1;
        for (int y = ymin; y <= ymax; y++) {
          int xmin = cx - r;
          int xmax = cx + r;
          if (xmin < 0) xmin = 0;
          if (xmax >= XResolution()) xmax = XResolution() - 1;
          int dy = y - cy;
          for (int x = xmin; x <= xmax; x++) {
            int dx = x - cx;
            int d_squared = dx*dx + dy*dy;
            if (d_squared > grid_radius_squared) continue;
            RNScalar sample = copy.GridValue(x, y);
            if (sample == R2_GRID_UNKNOWN_VALUE) continue;
            samples[nsamples++] = sample;
          }
        }

        // Check number of grid values in neighborhood
        if (nsamples == 0) {
          SetGridValue(cx, cy, R2_GRID_UNKNOWN_VALUE);
        }
        else {
          // Sort samples found in neighborhood
          qsort(samples, nsamples, sizeof(RNScalar), RNCompareScalars);

          // Set grid value to percentile of neighborhood
          int index = (int) (percentile * nsamples);
          if (index < 0) index = 0;
          else if (index >= nsamples) index = nsamples-1;
          SetGridValue(cx, cy, samples[index]);
        }
      }
    }
  }

  // Delete temporary memory
  delete [] samples;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x148, %rsp            # imm = 0x148
movsd %xmm1, 0x40(%rsp)
movsd %xmm0, 0x10(%rsp)
movq %rdi, %r15
leaq 0x78(%rsp), %rdi
movq %r15, %rsi
callq 0x59934
cvttsd2si 0x10(%rsp), %eax
movq %rax, 0x18(%rsp)
leal 0x1(,%rax,2), %edi
imull %edi, %edi
shlq $0x3, %rdi
callq 0xe030
movq %rax, %r14
movl 0xc4(%r15), %eax
testl %eax, %eax
movsd 0x10(%rsp), %xmm1
jle 0x5c594
mulsd %xmm1, %xmm1
movq 0x18(%rsp), %rcx
movl %ecx, %edx
negl %edx
movslq %ecx, %rcx
movq %rcx, 0x30(%rsp)
movl 0xc0(%r15), %r10d
movsd 0x46ca5(%rip), %xmm2    # 0xa2fb8
movq $0x0, (%rsp)
movl %edx, 0x20(%rsp)
movl %edx, %edi
movsd %xmm1, 0x10(%rsp)
movq %r15, 0x48(%rsp)
testl %edi, %edi
movl $0x0, %ebp
cmovgl %edi, %ebp
testl %r10d, %r10d
jle 0x5c57b
movl %edi, 0x24(%rsp)
movq 0x30(%rsp), %rax
movq (%rsp), %rcx
addq %rcx, %rax
movq %rax, 0x70(%rsp)
subl 0x18(%rsp), %ecx
testl %ecx, %ecx
movl $0x0, %eax
cmovlel %eax, %ecx
movl %ecx, 0x2c(%rsp)
movq 0xb8(%r15), %r9
movl 0xc8(%r15), %r11d
leaq (,%rbp,8), %rax
movq %rax, 0x50(%rsp)
xorl %r12d, %r12d
xorl %edi, %edi
movl 0x20(%rsp), %r8d
movq %rbp, 0x58(%rsp)
testl %r8d, %r8d
movl $0x0, %ebx
cmovgl %r8d, %ebx
movslq %r11d, %rsi
imulq (%rsp), %rsi
addq %rdi, %rsi
movsd (%r9,%rsi,8), %xmm0
ucomisd %xmm2, %xmm0
jne 0x5c3b9
jnp 0x5c55b
movslq 0xc4(%r15), %rcx
leal -0x1(%rcx), %eax
movq 0x70(%rsp), %rdx
cmpq %rcx, %rdx
cmovll %edx, %eax
cmpl %eax, 0x2c(%rsp)
jg 0x5c54d
movq %r11, 0x60(%rsp)
movq %r9, 0x68(%rsp)
movl %r8d, 0xc(%rsp)
movq 0x30(%rsp), %rcx
leaq (%rdi,%rcx), %r8
movq %rdi, 0x38(%rsp)
movl %edi, %r9d
subl 0x18(%rsp), %r9d
xorl %r13d, %r13d
testl %r9d, %r9d
cmovlel %r13d, %r9d
movl %r10d, 0x28(%rsp)
movslq %r10d, %rcx
leal -0x1(%rcx), %edx
cmpq %rcx, %r8
cmovgel %edx, %r8d
movslq 0x140(%rsp), %r10
movslq %r8d, %r11
cltq
movq 0x50(%rsp), %rdx
imulq %r10, %rdx
addq 0x130(%rsp), %rdx
shlq $0x3, %r10
movq %rbp, %rcx
cmpl %r8d, %r9d
jg 0x5c48c
movl %ecx, %ebp
subl (%rsp), %ebp
imull %ebp, %ebp
movq %rbx, %rdi
leal (%r12,%rdi), %r15d
imull %r15d, %r15d
addl %ebp, %r15d
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
ucomisd %xmm1, %xmm0
ja 0x5c480
movsd (%rdx,%rdi,8), %xmm0
ucomisd %xmm2, %xmm0
jne 0x5c474
jnp 0x5c480
movslq %r13d, %r15
incl %r13d
movsd %xmm0, (%r14,%r15,8)
leaq 0x1(%rdi), %r15
cmpq %r11, %rdi
movq %r15, %rdi
jl 0x5c44e
leaq 0x1(%rcx), %rdi
addq %r10, %rdx
cmpq %rax, %rcx
movq %rdi, %rcx
jl 0x5c43e
testl %r13d, %r13d
movq 0x48(%rsp), %r15
movl 0x28(%rsp), %r10d
movq 0x58(%rsp), %rbp
movq 0x38(%rsp), %rdi
movl 0xc(%rsp), %r8d
movq 0x68(%rsp), %r9
movq 0x60(%rsp), %r11
je 0x5c54d
movslq %r13d, %rsi
movl $0x8, %edx
movq %r14, %rdi
leaq 0x15148(%rip), %rcx     # 0x71621
callq 0xe880
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
mulsd 0x40(%rsp), %xmm0
cvttsd2si %xmm0, %eax
leal -0x1(%r13), %ecx
cmpl %eax, %r13d
cmovgl %eax, %ecx
testl %eax, %eax
movl $0x0, %eax
cmovsl %eax, %ecx
movslq %ecx, %rax
movsd (%r14,%rax,8), %xmm0
movq 0xb8(%r15), %r9
movslq 0xc8(%r15), %r11
movq %r11, %rax
imulq (%rsp), %rax
leaq (%r9,%rax,8), %rax
movq 0x38(%rsp), %rdi
movsd %xmm0, (%rax,%rdi,8)
movl 0xc0(%r15), %r10d
movsd 0x10(%rsp), %xmm1
movsd 0x46a72(%rip), %xmm2    # 0xa2fb8
movl 0xc(%rsp), %r8d
jmp 0x5c55b
movabsq $-0x3f8bf00000000000, %rax # imm = 0xC074100000000000
movq %rax, (%r9,%rsi,8)
incq %rdi
movslq %r10d, %rax
incl %r8d
decq %r12
cmpq %rax, %rdi
jl 0x5c390
movl 0xc4(%r15), %eax
movl 0x24(%rsp), %edi
movq (%rsp), %rsi
incq %rsi
movslq %eax, %rcx
incl %edi
movq %rsi, (%rsp)
cmpq %rcx, %rsi
jl 0x5c32c
movq %r14, %rdi
callq 0xe5b0
leaq 0x78(%rsp), %rdi
callq 0x59b0c
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5c5ba
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
callq 0x59b0c
movq %rbx, %rdi
callq 0xe7e0
nop