// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::Blur(double)
void R2Grid::
Blur(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 = XResolution();
  if (res < YResolution()) res = YResolution();
  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 in X direction
  for (int j = 0; j < YResolution(); j++) { 
    for (int i = 0; i < XResolution(); i++) 
      buffer[i] = GridValue(i, j); 
    for (int i = 0; i < XResolution(); 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) SetGridValue(i, j, sum / weight);
    }
  }

  // Convolve grid with filter in Y direction
  for (int j = 0; j < XResolution(); j++) { 
    for (int i = 0; i < YResolution(); i++) 
      buffer[i] = GridValue(j, i); 
    for (int i = 0; i < YResolution(); 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 = YResolution() - 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) SetGridValue(j, i, sum / weight);
    }
  }

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %r12
movsd 0x3f41b(%rip), %xmm1    # 0x931d8
movsd %xmm0, 0x8(%rsp)
mulsd %xmm0, %xmm1
addsd 0x3f241(%rip), %xmm1    # 0x93010
cvttsd2si %xmm1, %ebp
movslq %ebp, %r15
leaq 0x8(,%r15,8), %rdi
cmpl $-0x1, %r15d
pushq $-0x1
popq %rbx
cmovlq %rbx, %rdi
callq 0xe030
movq %rax, %r14
movl 0xc0(%r12), %eax
movq %r12, 0x18(%rsp)
movl 0xc4(%r12), %ecx
cmpl %ecx, %eax
cmovgl %eax, %ecx
movslq %ecx, %rax
leaq (,%rax,8), %rdi
testl %eax, %eax
cmovsq %rbx, %rdi
callq 0xe030
movq %rax, %rbx
leaq 0x4713d(%rip), %rax     # 0x9af68
movsd (%rax), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x53e3f
sqrtsd %xmm0, %xmm0
jmp 0x53e44
callq 0xe350
movsd 0x8(%rsp), %xmm2
mulsd %xmm2, %xmm0
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movsd 0x3f1a6(%rip), %xmm0    # 0x93008
divsd %xmm1, %xmm0
movsd %xmm0, 0x28(%rsp)
movapd %xmm2, %xmm0
addsd %xmm2, %xmm0
mulsd %xmm2, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %r12d, %r12d
cmpq %r15, %r12
jg 0x53eaf
movl %r12d, %eax
imull %r12d, %eax
negl %eax
cvtsi2sd %eax, %xmm0
divsd 0x8(%rsp), %xmm0
callq 0xe820
mulsd 0x28(%rsp), %xmm0
movsd %xmm0, (%r14,%r12,8)
incq %r12
jmp 0x53e81
movq 0x18(%rsp), %rax
movl 0xc0(%rax), %ecx
movl 0xc4(%rax), %eax
xorl %edx, %edx
testl %ecx, %ecx
movl $0x0, %edi
cmovgl %ecx, %edi
testl %eax, %eax
movl $0x0, %esi
movl %eax, 0x28(%rsp)
cmovgl %eax, %esi
movl %esi, 0x10(%rsp)
leaq 0x8(%r14), %r11
leaq -0x8(%rbx), %rax
movq %rax, 0x20(%rsp)
movsd 0x40075(%rip), %xmm0    # 0x93f68
xorpd %xmm1, %xmm1
pushq $0x1
popq %r10
xorl %r8d, %r8d
cmpl 0x10(%rsp), %r8d
je 0x54024
movq 0x18(%rsp), %rsi
movq 0xb8(%rsi), %rax
movl 0xc8(%rsi), %esi
imull %r8d, %esi
movslq %esi, %rsi
leaq (%rax,%rsi,8), %r12
xorl %eax, %eax
cmpq %rax, %rdi
je 0x53f3d
movsd (%r12,%rax,8), %xmm2
movsd %xmm2, (%rbx,%rax,8)
incq %rax
jmp 0x53f28
movl %r8d, 0x8(%rsp)
movq %rbx, %r15
movq 0x20(%rsp), %r13
xorl %esi, %esi
cmpl %esi, %ebp
movl %esi, %r9d
cmovll %ebp, %r9d
testl %r9d, %r9d
cmovlel %edx, %r9d
cmpq %rdi, %rsi
je 0x54017
movsd (%rbx,%rsi,8), %xmm2
ucomisd %xmm0, %xmm2
jne 0x53f76
jnp 0x54007
negq %r9
movsd (%r14), %xmm3
mulsd %xmm3, %xmm2
addsd %xmm1, %xmm3
addsd %xmm1, %xmm2
movq %r11, %r8
xorl %eax, %eax
cmpq %rax, %r9
je 0x53fbd
movsd (%r13,%rax,8), %xmm4
ucomisd %xmm0, %xmm4
jne 0x53fa3
jnp 0x53fb4
movsd (%r8), %xmm5
mulsd %xmm5, %xmm4
addsd %xmm4, %xmm2
addsd %xmm5, %xmm3
decq %rax
addq $0x8, %r8
jmp 0x53f8f
movl %esi, %eax
notl %eax
addl %ecx, %eax
cmpl %ebp, %eax
cmovgel %ebp, %eax
cltq
movq %r10, %r8
cmpq %rax, %r8
jg 0x53ff7
movsd (%r15,%r8,8), %xmm4
ucomisd %xmm0, %xmm4
jne 0x53fe0
jnp 0x53ff2
movsd (%r14,%r8,8), %xmm5
mulsd %xmm5, %xmm4
addsd %xmm4, %xmm2
addsd %xmm5, %xmm3
incq %r8
jmp 0x53fcd
ucomisd %xmm1, %xmm3
jbe 0x54007
divsd %xmm3, %xmm2
movsd %xmm2, (%r12,%rsi,8)
incq %rsi
addq $0x8, %r13
addq $0x8, %r15
jmp 0x53f4c
movl 0x8(%rsp), %r8d
incl %r8d
jmp 0x53efe
movq 0x18(%rsp), %rax
movl 0xc0(%rax), %eax
xorl %edx, %edx
testl %eax, %eax
cmovlel %edx, %eax
movq %rax, 0x20(%rsp)
movl 0x10(%rsp), %esi
movq %r14, %r11
addq $0x8, %r11
movq %rbx, %rax
addq $-0x8, %rax
movq %rax, 0x30(%rsp)
xorpd %xmm1, %xmm1
pushq $0x1
popq %r9
xorl %edi, %edi
xorl %r10d, %r10d
cmpq 0x20(%rsp), %r10
je 0x541a5
movq 0x18(%rsp), %rcx
movq 0xb8(%rcx), %rax
movslq 0xc8(%rcx), %r12
movq %rdi, 0x8(%rsp)
leaq (%rax,%rdi), %rcx
leaq (,%r12,8), %rdi
xorl %r8d, %r8d
cmpq %r8, %rsi
je 0x540a8
movsd (%rcx), %xmm2
movsd %xmm2, (%rbx,%r8,8)
incq %r8
addq %rdi, %rcx
jmp 0x54091
movq %r10, 0x10(%rsp)
leaq (%rax,%r10,8), %r13
movq %rbx, %r15
movq 0x30(%rsp), %r8
xorl %r10d, %r10d
cmpl %r10d, %ebp
movl %r10d, %ecx
cmovll %ebp, %ecx
testl %ecx, %ecx
cmovlel %edx, %ecx
cmpq %rsi, %r10
je 0x5418f
movsd (%rbx,%r10,8), %xmm2
ucomisd %xmm0, %xmm2
jne 0x540e5
jnp 0x5417f
negq %rcx
movsd (%r14), %xmm3
mulsd %xmm3, %xmm2
addsd %xmm1, %xmm3
addsd %xmm1, %xmm2
movq %r11, %rdi
xorl %eax, %eax
cmpq %rax, %rcx
je 0x5412a
movsd (%r8,%rax,8), %xmm4
ucomisd %xmm0, %xmm4
jne 0x54111
jnp 0x54121
movsd (%rdi), %xmm5
mulsd %xmm5, %xmm4
addsd %xmm4, %xmm2
addsd %xmm5, %xmm3
decq %rax
addq $0x8, %rdi
jmp 0x540fe
movl %r10d, %eax
notl %eax
addl 0x28(%rsp), %eax
cmpl %ebp, %eax
cmovgel %ebp, %eax
cltq
movq %r9, %rcx
cmpq %rax, %rcx
jg 0x54167
movsd (%r15,%rcx,8), %xmm4
ucomisd %xmm0, %xmm4
jne 0x54150
jnp 0x54162
movsd (%r14,%rcx,8), %xmm5
mulsd %xmm5, %xmm4
addsd %xmm4, %xmm2
addsd %xmm5, %xmm3
incq %rcx
jmp 0x5413d
ucomisd %xmm1, %xmm3
jbe 0x5417f
divsd %xmm3, %xmm2
movq %r10, %rax
imulq %r12, %rax
movsd %xmm2, (%r13,%rax,8)
incq %r10
addq $0x8, %r8
addq $0x8, %r15
jmp 0x540bc
movq 0x10(%rsp), %r10
incq %r10
movq 0x8(%rsp), %rdi
addq $0x8, %rdi
jmp 0x5405f
movq %r14, %rdi
callq 0xe5d0
movq %rbx, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe5d0
nop