// File: /workspace/github_cpp_makefile_star2_O3/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 $0x28, %rsp
movsd 0x46848(%rip), %xmm1    # 0xa21d8
movsd %xmm0, 0x8(%rsp)
mulsd %xmm0, %xmm1
addsd 0x4668e(%rip), %xmm1    # 0xa2030
cvttsd2si %xmm1, %r12d
movq %rdi, %r15
leal 0x1(%r12), %ebp
movslq %ebp, %rdi
shlq $0x3, %rdi
cmpl $-0x1, %r12d
movq $-0x1, %rbx
cmovlq %rbx, %rdi
callq 0xe030
movq %rax, %r14
movl 0xc0(%r15), %eax
movq %r15, 0x10(%rsp)
movl 0xc4(%r15), %ecx
cmpl %ecx, %eax
cmovgl %eax, %ecx
movslq %ecx, %rax
leaq (,%rax,8), %rdi
testl %eax, %eax
cmovsq %rbx, %rdi
callq 0xe030
movq %rax, %r15
leaq 0x4e5e3(%rip), %rax     # 0xa9fe8
movsd (%rax), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x5ba19
sqrtsd %xmm0, %xmm0
jmp 0x5ba1e
callq 0xe340
testl %r12d, %r12d
js 0x5ba91
movsd 0x8(%rsp), %xmm2
mulsd %xmm2, %xmm0
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movsd 0x465f7(%rip), %xmm0    # 0xa2038
divsd %xmm1, %xmm0
movsd %xmm0, 0x18(%rsp)
movapd %xmm2, %xmm0
addsd %xmm2, %xmm0
mulsd %xmm2, %xmm0
movsd %xmm0, 0x8(%rsp)
movl %ebp, %ebx
xorl %r13d, %r13d
movl %r13d, %eax
imull %r13d, %eax
negl %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x8(%rsp), %xmm0
callq 0xe7f0
mulsd 0x18(%rsp), %xmm0
movsd %xmm0, (%r14,%r13,8)
incq %r13
cmpq %r13, %rbx
jne 0x5ba62
movq 0x10(%rsp), %rcx
movl 0xc0(%rcx), %eax
movl 0xc4(%rcx), %ecx
testl %ecx, %ecx
jle 0x5bc09
movq 0x10(%rsp), %rsi
movq 0xb8(%rsi), %rdx
movslq 0xc8(%rsi), %rsi
shlq $0x3, %rsi
leaq -0x8(%r15), %rdi
movq %rdi, 0x18(%rsp)
leaq 0x8(%r15), %rdi
movq %rdi, 0x8(%rsp)
xorl %r9d, %r9d
movsd 0x474da(%rip), %xmm0    # 0xa2fb8
xorpd %xmm1, %xmm1
testl %eax, %eax
jle 0x5bbfa
xorl %edi, %edi
movsd (%rdx,%rdi,8), %xmm2
movsd %xmm2, (%r15,%rdi,8)
incq %rdi
cmpq %rdi, %rax
jne 0x5baec
testl %eax, %eax
jle 0x5bbfa
movq 0x10(%rsp), %rdi
movslq 0xc8(%rdi), %r10
imulq %r9, %r10
shlq $0x3, %r10
addq 0xb8(%rdi), %r10
movq 0x8(%rsp), %r11
movq 0x18(%rsp), %r13
xorl %edi, %edi
cmpl %edi, %r12d
movl %edi, %ebp
cmovll %r12d, %ebp
movsd (%r15,%rdi,8), %xmm2
ucomisd %xmm0, %xmm2
jne 0x5bb49
jnp 0x5bbe6
movsd (%r14), %xmm3
mulsd %xmm3, %xmm2
addsd %xmm1, %xmm2
addsd %xmm1, %xmm3
testl %ebp, %ebp
jle 0x5bb93
incl %ebp
movl $0x1, %r8d
movq %r13, %rbx
movsd (%rbx), %xmm4
ucomisd %xmm0, %xmm4
jne 0x5bb75
jnp 0x5bb87
movsd (%r14,%r8,8), %xmm5
mulsd %xmm5, %xmm4
addsd %xmm4, %xmm2
addsd %xmm5, %xmm3
incq %r8
addq $-0x8, %rbx
cmpq %r8, %rbp
jne 0x5bb69
movl %edi, %r8d
notl %r8d
addl %eax, %r8d
cmpl %r12d, %r8d
cmovgel %r12d, %r8d
testl %r8d, %r8d
jle 0x5bbd6
movl %r8d, %r8d
xorl %ebx, %ebx
movsd (%r11,%rbx,8), %xmm4
ucomisd %xmm0, %xmm4
jne 0x5bbbb
jnp 0x5bbce
movsd 0x8(%r14,%rbx,8), %xmm5
mulsd %xmm5, %xmm4
addsd %xmm4, %xmm2
addsd %xmm5, %xmm3
incq %rbx
cmpq %r8, %rbx
jb 0x5bbad
ucomisd %xmm1, %xmm3
jbe 0x5bbe6
divsd %xmm3, %xmm2
movsd %xmm2, (%r10,%rdi,8)
incq %rdi
addq $0x8, %r13
addq $0x8, %r11
cmpq %rax, %rdi
jne 0x5bb2e
incq %r9
addq %rsi, %rdx
cmpq %rcx, %r9
jne 0x5bae2
testl %eax, %eax
jle 0x5bd87
movq 0x10(%rsp), %rsi
movq 0xb8(%rsi), %rdx
movslq 0xc8(%rsi), %rsi
shlq $0x3, %rsi
leaq -0x8(%r15), %rdi
movq %rdi, 0x8(%rsp)
movq %r15, %rdi
addq $0x8, %rdi
movq %rdi, 0x20(%rsp)
xorl %r9d, %r9d
movsd 0x47370(%rip), %xmm0    # 0xa2fb8
xorpd %xmm1, %xmm1
testl %ecx, %ecx
jle 0x5bd77
movq %rdx, %rdi
xorl %r8d, %r8d
movsd (%rdi), %xmm2
movsd %xmm2, (%r15,%r8,8)
incq %r8
addq %rsi, %rdi
cmpq %r8, %rcx
jne 0x5bc5a
testl %ecx, %ecx
jle 0x5bd77
leaq (,%r9,8), %r10
movq 0x10(%rsp), %rdi
addq 0xb8(%rdi), %r10
movslq 0xc8(%rdi), %rdi
movq %rdi, 0x18(%rsp)
movq 0x20(%rsp), %r13
movq 0x8(%rsp), %r11
xorl %r8d, %r8d
cmpl %r8d, %r12d
movl %r8d, %edi
cmovll %r12d, %edi
movsd (%r15,%r8,8), %xmm2
ucomisd %xmm0, %xmm2
jne 0x5bcc0
jnp 0x5bd63
movsd (%r14), %xmm3
mulsd %xmm3, %xmm2
addsd %xmm1, %xmm2
addsd %xmm1, %xmm3
testl %edi, %edi
jle 0x5bd0a
incl %edi
movl $0x1, %ebx
movq %r11, %rbp
movsd (%rbp), %xmm4
ucomisd %xmm0, %xmm4
jne 0x5bcec
jnp 0x5bcfe
movsd (%r14,%rbx,8), %xmm5
mulsd %xmm5, %xmm4
addsd %xmm4, %xmm2
addsd %xmm5, %xmm3
incq %rbx
addq $-0x8, %rbp
cmpq %rbx, %rdi
jne 0x5bcdf
movl %r8d, %edi
notl %edi
addl %ecx, %edi
cmpl %r12d, %edi
cmovgel %r12d, %edi
testl %edi, %edi
jle 0x5bd4a
movl %edi, %edi
xorl %ebx, %ebx
movsd (%r13,%rbx,8), %xmm4
ucomisd %xmm0, %xmm4
jne 0x5bd2f
jnp 0x5bd42
movsd 0x8(%r14,%rbx,8), %xmm5
mulsd %xmm5, %xmm4
addsd %xmm4, %xmm2
addsd %xmm5, %xmm3
incq %rbx
cmpq %rdi, %rbx
jb 0x5bd20
ucomisd %xmm1, %xmm3
jbe 0x5bd63
divsd %xmm3, %xmm2
movq %r8, %rdi
imulq 0x18(%rsp), %rdi
movsd %xmm2, (%r10,%rdi,8)
incq %r8
addq $0x8, %r11
addq $0x8, %r13
cmpq %rcx, %r8
jne 0x5bca4
incq %r9
addq $0x8, %rdx
cmpq %rax, %r9
jne 0x5bc4c
movq %r14, %rdi
callq 0xe5b0
movq %r15, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe5b0
nop