// File: /workspace/github_cpp_makefile_star2_O2/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 $0x38, %rsp
movl %esi, %ebx
movq %rdi, %r13
movsd 0x3f707(%rip), %xmm1    # 0x931d8
movsd %xmm0, (%rsp)
mulsd %xmm0, %xmm1
addsd 0x3f52e(%rip), %xmm1    # 0x93010
cvttsd2si %xmm1, %ebp
movslq %ebp, %r12
leaq 0x8(,%r12,8), %rdi
cmpl $-0x1, %r12d
pushq $-0x1
popq %r14
cmovlq %r14, %rdi
callq 0xe030
movq %rax, %r15
movslq %ebx, %rax
movq %r13, 0x8(%rsp)
movq %rax, 0x20(%rsp)
movslq 0xc0(%r13,%rax,4), %rax
leaq (,%rax,8), %rdi
testq %rax, %rax
cmovsq %r14, %rdi
callq 0xe030
movq %rax, %r14
leaq 0x47430(%rip), %rax     # 0x9af68
movsd (%rax), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x53b4c
sqrtsd %xmm0, %xmm0
jmp 0x53b51
callq 0xe350
movsd (%rsp), %xmm2
mulsd %xmm2, %xmm0
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movsd 0x3f49a(%rip), %xmm0    # 0x93008
divsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
movapd %xmm2, %xmm0
addsd %xmm2, %xmm0
mulsd %xmm2, %xmm0
movsd %xmm0, (%rsp)
xorl %r13d, %r13d
cmpq %r12, %r13
jg 0x53bb9
movl %r13d, %eax
imull %r13d, %eax
negl %eax
cvtsi2sd %eax, %xmm0
divsd (%rsp), %xmm0
callq 0xe820
mulsd 0x10(%rsp), %xmm0
movsd %xmm0, (%r15,%r13,8)
incq %r13
jmp 0x53b8c
pushq $0x1
popq %rax
subl %ebx, %eax
cltq
movq 0x8(%rsp), %rcx
movl 0xc0(%rcx,%rax,4), %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
movq %rax, 0x28(%rsp)
movq %r15, %r13
addq $0x8, %r13
movq %r14, %rax
addq $-0x8, %rax
movq %rax, 0x18(%rsp)
movsd 0x40375(%rip), %xmm0    # 0x93f68
xorpd %xmm1, %xmm1
xorl %r12d, %r12d
cmpq 0x28(%rsp), %r12
je 0x53d86
movq 0x8(%rsp), %rdx
movq 0x20(%rsp), %rax
movl 0xc0(%rdx,%rax,4), %r9d
movq 0xb8(%rdx), %rax
movslq 0xc8(%rdx), %r10
movq %r12, %rdx
imulq %r10, %rdx
testl %r9d, %r9d
cmovlel %ecx, %r9d
movl %r10d, %esi
imull %r12d, %esi
movl %r12d, %edi
xorl %r8d, %r8d
cmpq %r8, %r9
je 0x53c66
leal (%rsi,%r8), %r11d
testl %ebx, %ebx
cmovnel %edi, %r11d
movslq %r11d, %r11
movsd (%rax,%r11,8), %xmm2
movsd %xmm2, (%r14,%r8,8)
incq %r8
addl %r10d, %edi
jmp 0x53c40
movq 0x8(%rsp), %rsi
movl 0xc0(%rsi), %r11d
movq %r12, 0x30(%rsp)
leaq (%rax,%r12,8), %rsi
movq %rsi, 0x10(%rsp)
leaq (%rax,%rdx,8), %rax
movq %rax, (%rsp)
movq %r14, %r12
movq 0x18(%rsp), %rax
xorl %r8d, %r8d
cmpl %r8d, %ebp
movl %r8d, %edi
cmovll %ebp, %edi
testl %edi, %edi
cmovlel %ecx, %edi
cmpq %r9, %r8
je 0x53d79
movsd (%r14,%r8,8), %xmm2
ucomisd %xmm0, %xmm2
jne 0x53cbc
jnp 0x53d69
negq %rdi
movsd (%r15), %xmm3
mulsd %xmm3, %xmm2
addsd %xmm1, %xmm3
addsd %xmm1, %xmm2
movq %r13, %rdx
xorl %esi, %esi
cmpq %rsi, %rdi
je 0x53d00
movsd (%rax,%rsi,8), %xmm4
ucomisd %xmm0, %xmm4
jne 0x53ce7
jnp 0x53cf7
movsd (%rdx), %xmm5
mulsd %xmm5, %xmm4
addsd %xmm4, %xmm2
addsd %xmm5, %xmm3
decq %rsi
addq $0x8, %rdx
jmp 0x53cd5
movl %r8d, %edx
notl %edx
addl %r11d, %edx
cmpl %ebp, %edx
cmovgel %ebp, %edx
movslq %edx, %rdx
pushq $0x1
popq %rsi
cmpq %rdx, %rsi
jg 0x53d3d
movsd (%r12,%rsi,8), %xmm4
ucomisd %xmm0, %xmm4
jne 0x53d26
jnp 0x53d38
movsd (%r15,%rsi,8), %xmm5
mulsd %xmm5, %xmm4
addsd %xmm4, %xmm2
addsd %xmm5, %xmm3
incq %rsi
jmp 0x53d13
ucomisd %xmm1, %xmm3
jbe 0x53d69
movq %r8, %rdx
imulq %r10, %rdx
testl %ebx, %ebx
divsd %xmm3, %xmm2
movq 0x10(%rsp), %rsi
leaq (%rsi,%rdx,8), %rdx
movq (%rsp), %rsi
leaq (%rsi,%r8,8), %rsi
cmovneq %rdx, %rsi
movsd %xmm2, (%rsi)
incq %r8
addq $0x8, %rax
addq $0x8, %r12
jmp 0x53c93
movq 0x30(%rsp), %r12
incq %r12
jmp 0x53bfa
movq %r15, %rdi
callq 0xe5d0
movq %r14, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe5d0