// File: /workspace/github_cpp_makefile_star2_O1/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
movq %rdi, %r15
movsd 0x45af5(%rip), %xmm1    # 0xa21d8
movsd %xmm0, 0x8(%rsp)
mulsd %xmm0, %xmm1
addsd 0x4593b(%rip), %xmm1    # 0xa2030
cvttsd2si %xmm1, %r12d
movslq %r12d, %rax
leaq 0x8(,%rax,8), %rdi
cmpl $-0x1, %eax
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 0x4d875(%rip), %rax     # 0xa9fc8
movsd (%rax), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x5c767
sqrtsd %xmm0, %xmm0
jmp 0x5c76c
callq 0xe340
testl %r12d, %r12d
js 0x5c7e2
movsd 0x8(%rsp), %xmm2
mulsd %xmm2, %xmm0
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movsd 0x458a9(%rip), %xmm0    # 0xa2038
divsd %xmm1, %xmm0
movsd %xmm0, 0x18(%rsp)
movapd %xmm2, %xmm0
addsd %xmm2, %xmm0
mulsd %xmm2, %xmm0
movsd %xmm0, 0x8(%rsp)
leal 0x1(%r12), %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 0x5c7b3
movq 0x10(%rsp), %rax
movl 0xc4(%rax), %eax
testl %eax, %eax
jle 0x5c97c
movq 0x10(%rsp), %rsi
movl 0xc0(%rsi), %ecx
movq 0xb8(%rsi), %rdx
movslq 0xc8(%rsi), %rsi
shlq $0x3, %rsi
movq %rsi, 0x18(%rsp)
leaq -0x8(%r15), %rsi
movq %rsi, 0x8(%rsp)
leaq 0x8(%r15), %rsi
movq %rsi, 0x20(%rsp)
xorl %r9d, %r9d
movsd 0x46784(%rip), %xmm0    # 0xa2fb8
xorpd %xmm1, %xmm1
testl %ecx, %ecx
jle 0x5c851
xorl %esi, %esi
movsd (%rdx,%rsi,8), %xmm2
movsd %xmm2, (%r15,%rsi,8)
incq %rsi
cmpq %rsi, %rcx
jne 0x5c83e
testl %ecx, %ecx
jle 0x5c96b
movq 0x10(%rsp), %rsi
movslq 0xc8(%rsi), %r10
imulq %r9, %r10
shlq $0x3, %r10
addq 0xb8(%rsi), %r10
movq 0x20(%rsp), %r11
movq 0x8(%rsp), %rsi
xorl %edi, %edi
cmpl %edi, %r12d
movl %edi, %ebp
cmovll %r12d, %ebp
movsd (%r15,%rdi,8), %xmm2
ucomisd %xmm0, %xmm2
jne 0x5c89b
jnp 0x5c957
ucomisd %xmm0, %xmm2
jne 0x5c8a3
jnp 0x5c8b6
movsd (%r14), %xmm3
mulsd %xmm3, %xmm2
addsd %xmm1, %xmm2
addsd %xmm1, %xmm3
jmp 0x5c8be
xorpd %xmm2, %xmm2
xorpd %xmm3, %xmm3
cmpl %r12d, %edi
movl %r12d, %r8d
cmovll %edi, %r8d
testl %r8d, %r8d
jle 0x5c904
incl %ebp
movl $0x1, %r8d
movq %rsi, %r13
movsd (%r13), %xmm4
ucomisd %xmm0, %xmm4
jne 0x5c8e6
jnp 0x5c8f8
movsd (%r14,%r8,8), %xmm5
mulsd %xmm5, %xmm4
addsd %xmm4, %xmm2
addsd %xmm5, %xmm3
incq %r8
addq $-0x8, %r13
cmpq %r8, %rbp
jne 0x5c8d8
movl %edi, %r8d
notl %r8d
addl %ecx, %r8d
cmpl %r12d, %r8d
cmovgel %r12d, %r8d
testl %r8d, %r8d
jle 0x5c947
movl %r8d, %r8d
xorl %ebx, %ebx
movsd (%r11,%rbx,8), %xmm4
ucomisd %xmm0, %xmm4
jne 0x5c92c
jnp 0x5c93f
movsd 0x8(%r14,%rbx,8), %xmm5
mulsd %xmm5, %xmm4
addsd %xmm4, %xmm2
addsd %xmm5, %xmm3
incq %rbx
cmpq %r8, %rbx
jb 0x5c91e
ucomisd %xmm1, %xmm3
jbe 0x5c957
divsd %xmm3, %xmm2
movsd %xmm2, (%r10,%rdi,8)
incq %rdi
addq $0x8, %rsi
addq $0x8, %r11
cmpq %rcx, %rdi
jne 0x5c880
incq %r9
addq 0x18(%rsp), %rdx
cmpq %rax, %r9
jne 0x5c838
movq 0x10(%rsp), %rcx
movslq 0xc0(%rcx), %rcx
movq %rcx, 0x18(%rsp)
testq %rcx, %rcx
jle 0x5cb1e
movq 0x10(%rsp), %rcx
movq 0xb8(%rcx), %rdx
movslq 0xc8(%rcx), %rsi
shlq $0x3, %rsi
leaq -0x8(%r15), %rcx
movq %rcx, 0x8(%rsp)
movq %r15, %rcx
addq $0x8, %rcx
movq %rcx, 0x20(%rsp)
xorl %r9d, %r9d
movsd 0x465eb(%rip), %xmm0    # 0xa2fb8
xorpd %xmm1, %xmm1
testl %eax, %eax
jle 0x5c9ef
movq %rdx, %rcx
xorl %edi, %edi
movsd (%rcx), %xmm2
movsd %xmm2, (%r15,%rdi,8)
incq %rdi
addq %rsi, %rcx
cmpq %rdi, %rax
jne 0x5c9da
testl %eax, %eax
jle 0x5cb0c
leaq (,%r9,8), %r10
movq 0x10(%rsp), %rcx
addq 0xb8(%rcx), %r10
movslq 0xc8(%rcx), %r11
movq 0x20(%rsp), %r13
movq 0x8(%rsp), %rbx
xorl %r8d, %r8d
cmpl %r8d, %r12d
movl %r8d, %edi
cmovll %r12d, %edi
movsd (%r15,%r8,8), %xmm2
ucomisd %xmm0, %xmm2
jne 0x5ca3b
jnp 0x5caf8
ucomisd %xmm0, %xmm2
jne 0x5ca43
jnp 0x5ca56
movsd (%r14), %xmm3
mulsd %xmm3, %xmm2
addsd %xmm1, %xmm2
addsd %xmm1, %xmm3
jmp 0x5ca5e
xorpd %xmm2, %xmm2
xorpd %xmm3, %xmm3
cmpl %r12d, %r8d
movl %r12d, %ecx
cmovll %r8d, %ecx
testl %ecx, %ecx
jle 0x5caa1
incl %edi
movl $0x1, %ecx
movq %rbx, %rbp
movsd (%rbp), %xmm4
ucomisd %xmm0, %xmm4
jne 0x5ca83
jnp 0x5ca95
movsd (%r14,%rcx,8), %xmm5
mulsd %xmm5, %xmm4
addsd %xmm4, %xmm2
addsd %xmm5, %xmm3
incq %rcx
addq $-0x8, %rbp
cmpq %rcx, %rdi
jne 0x5ca76
movl %r8d, %ecx
notl %ecx
addl %eax, %ecx
cmpl %r12d, %ecx
cmovgel %r12d, %ecx
testl %ecx, %ecx
jle 0x5cae1
movl %ecx, %ecx
xorl %edi, %edi
movsd (%r13,%rdi,8), %xmm4
ucomisd %xmm0, %xmm4
jne 0x5cac6
jnp 0x5cad9
movsd 0x8(%r14,%rdi,8), %xmm5
mulsd %xmm5, %xmm4
addsd %xmm4, %xmm2
addsd %xmm5, %xmm3
incq %rdi
cmpq %rcx, %rdi
jb 0x5cab7
ucomisd %xmm1, %xmm3
jbe 0x5caf8
divsd %xmm3, %xmm2
movq %r8, %rcx
imulq %r11, %rcx
movsd %xmm2, (%r10,%rcx,8)
incq %r8
addq $0x8, %rbx
addq $0x8, %r13
cmpq %rax, %r8
jne 0x5ca1f
incq %r9
addq $0x8, %rdx
cmpq 0x18(%rsp), %r9
jne 0x5c9d1
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