// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::Blur(int, double)
void R2Grid::
Blur(RNDimension dim, RNLength grid_sigma) 
{
  // Build filter
  RNScalar sigma = grid_sigma;
  int filter_radius = (int) (3 * sigma + 0.5);
  RNScalar *filter = new RNScalar [ filter_radius + 1 ];
  assert(filter);

  // Make buffer for temporary copy of row
  int res = Resolution(dim);
  RNScalar *buffer = new RNScalar [ res ];
  assert(buffer);

  // Fill filter with Gaussian 
  const RNScalar sqrt_two_pi = sqrt(RN_TWO_PI);
  double a = sqrt_two_pi * sigma;
  double fac = 1.0 / (a * a * a);
  double denom = 2.0 * sigma * sigma;
  for (int i = 0; i <= filter_radius; i++) {
    filter[i] = fac * exp(-i * i / denom);
  }

  // Convolve grid with filter 
  for (int j = 0; j < Resolution(1-dim); j++) { 
    for (int i = 0; i < Resolution(dim); i++) 
      buffer[i] = (dim == RN_X) ? GridValue(i, j) : GridValue(j, i); 
    for (int i = 0; i < Resolution(dim); i++) { 
      if (buffer[i] == R2_GRID_UNKNOWN_VALUE) continue;
      RNScalar sum = 0;
      RNScalar weight = 0;
      RNScalar value = buffer[i];
      if (value != R2_GRID_UNKNOWN_VALUE) {
        sum += filter[0] * value;
        weight += filter[0];
      }
      int nsamples = i;
      if (nsamples > filter_radius) nsamples = filter_radius;
      for (int m = 1; m <= nsamples; m++) {
        RNScalar value = buffer[i - m];;
        if (value != R2_GRID_UNKNOWN_VALUE) {
          sum += filter[m] * value;
          weight += filter[m];
        }
      }
      nsamples = XResolution() - 1 - i;
      if (nsamples > filter_radius) nsamples = filter_radius;
      for (int m = 1; m <= nsamples; m++) {
        RNScalar value = buffer[i + m];
        if (value != R2_GRID_UNKNOWN_VALUE) {
          sum += filter[m] * value;
          weight += filter[m];
        }
      }
      if (weight > 0) {
        RNScalar value = sum / weight;
        if (dim == RN_X) SetGridValue(i, j, value);
        else SetGridValue(j, i, value);
      }
    }
  }

  // Deallocate memory
  delete [] filter;
  delete [] buffer;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %esi, %ebx
movsd 0x46b42(%rip), %xmm1    # 0xa21d8
movsd %xmm0, (%rsp)
mulsd %xmm0, %xmm1
addsd 0x46989(%rip), %xmm1    # 0xa2030
cvttsd2si %xmm1, %r13d
movq %rdi, %r12
leal 0x1(%r13), %ebp
movslq %ebp, %rdi
shlq $0x3, %rdi
cmpl $-0x1, %r13d
movq $-0x1, %r14
cmovlq %r14, %rdi
callq 0xe030
movq %rax, %r15
movslq %ebx, %rax
movq %r12, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movslq 0xc0(%r12,%rax,4), %rax
leaq (,%rax,8), %rdi
testq %rax, %rax
cmovsq %r14, %rdi
callq 0xe030
movq %rax, %r12
leaq 0x4e8e4(%rip), %rax     # 0xa9fe8
movsd (%rax), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x5b718
sqrtsd %xmm0, %xmm0
jmp 0x5b71d
callq 0xe340
testl %r13d, %r13d
js 0x5b78b
movsd (%rsp), %xmm2
mulsd %xmm2, %xmm0
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movsd 0x468f9(%rip), %xmm0    # 0xa2038
divsd %xmm1, %xmm0
movsd %xmm0, 0x18(%rsp)
movapd %xmm2, %xmm0
addsd %xmm2, %xmm0
mulsd %xmm2, %xmm0
movsd %xmm0, (%rsp)
movl %ebp, %r14d
xorl %ebp, %ebp
movl %ebp, %eax
imull %ebp, %eax
negl %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd (%rsp), %xmm0
callq 0xe7f0
mulsd 0x18(%rsp), %xmm0
movsd %xmm0, (%r15,%rbp,8)
incq %rbp
cmpq %rbp, %r14
jne 0x5b75f
movl $0x1, %eax
subl %ebx, %eax
cltq
movq 0x8(%rsp), %rcx
movslq 0xc0(%rcx,%rax,4), %rax
movq %rax, (%rsp)
testq %rax, %rax
jle 0x5b95b
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
movl 0xc0(%rax,%rcx,4), %ecx
leaq -0x8(%r12), %rax
movq %rax, 0x10(%rsp)
movq %r12, %rax
addq $0x8, %rax
movq %rax, 0x20(%rsp)
xorl %edi, %edi
movsd 0x477d9(%rip), %xmm0    # 0xa2fb8
xorpd %xmm1, %xmm1
testl %ecx, %ecx
jle 0x5b94e
movq 0x8(%rsp), %rdx
movq 0xb8(%rdx), %rax
movl 0xc8(%rdx), %edx
movl %edx, %esi
imull %edi, %esi
movl %edi, %r8d
xorl %r9d, %r9d
leal (%rsi,%r9), %r10d
testl %ebx, %ebx
cmovnel %r8d, %r10d
movslq %r10d, %r10
movsd (%rax,%r10,8), %xmm2
movsd %xmm2, (%r12,%r9,8)
incq %r9
addl %edx, %r8d
cmpq %r9, %rcx
jne 0x5b808
testl %ecx, %ecx
jle 0x5b94e
movq 0x8(%rsp), %rdx
movl 0xc0(%rdx), %r8d
movq 0xb8(%rdx), %rax
movslq 0xc8(%rdx), %r9
movq %rdi, %rdx
imulq %r9, %rdx
leaq (%rax,%rdi,8), %rsi
movq %rsi, 0x18(%rsp)
leaq (%rax,%rdx,8), %r11
movq 0x20(%rsp), %rbp
movq 0x10(%rsp), %r10
xorl %eax, %eax
cmpl %eax, %r13d
movl %eax, %esi
cmovll %r13d, %esi
movsd (%r12,%rax,8), %xmm2
ucomisd %xmm0, %xmm2
jne 0x5b889
jnp 0x5b93a
movsd (%r15), %xmm3
mulsd %xmm3, %xmm2
addsd %xmm1, %xmm2
addsd %xmm1, %xmm3
testl %esi, %esi
jle 0x5b8d3
incl %esi
movl $0x1, %r14d
movq %r10, %rdx
movsd (%rdx), %xmm4
ucomisd %xmm0, %xmm4
jne 0x5b8b5
jnp 0x5b8c7
movsd (%r15,%r14,8), %xmm5
mulsd %xmm5, %xmm4
addsd %xmm4, %xmm2
addsd %xmm5, %xmm3
incq %r14
addq $-0x8, %rdx
cmpq %r14, %rsi
jne 0x5b8a9
movl %eax, %edx
notl %edx
addl %r8d, %edx
cmpl %r13d, %edx
cmovgel %r13d, %edx
testl %edx, %edx
jle 0x5b912
movl %edx, %edx
xorl %esi, %esi
movsd (%rbp,%rsi,8), %xmm4
ucomisd %xmm0, %xmm4
jne 0x5b8f7
jnp 0x5b90a
movsd 0x8(%r15,%rsi,8), %xmm5
mulsd %xmm5, %xmm4
addsd %xmm4, %xmm2
addsd %xmm5, %xmm3
incq %rsi
cmpq %rdx, %rsi
jb 0x5b8e9
ucomisd %xmm1, %xmm3
jbe 0x5b93a
movq %rax, %rdx
imulq %r9, %rdx
testl %ebx, %ebx
divsd %xmm3, %xmm2
movq 0x18(%rsp), %rsi
leaq (%rsi,%rdx,8), %rdx
leaq (%r11,%rax,8), %rsi
cmovneq %rdx, %rsi
movsd %xmm2, (%rsi)
incq %rax
addq $0x8, %r10
addq $0x8, %rbp
cmpq %rcx, %rax
jne 0x5b86e
incq %rdi
cmpq (%rsp), %rdi
jne 0x5b7e3
movq %r15, %rdi
callq 0xe5b0
movq %r12, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe5b0
nop