// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::Convolve(double const (*) [3])
void R2Grid::
Convolve(const RNScalar filter[3][3]) 
{
  // Make temporary copy of grid
  R2Grid copy(*this);

  // Mark boundaries unknown
  for (int i = 0; i < XResolution(); i++) { 
    SetGridValue(i, 0, R2_GRID_UNKNOWN_VALUE);
    SetGridValue(i, YResolution()-1, R2_GRID_UNKNOWN_VALUE);
  }
  for (int j = 0; j < YResolution(); j++) { 
    SetGridValue(0, j, R2_GRID_UNKNOWN_VALUE);
    SetGridValue(XResolution()-1, j, R2_GRID_UNKNOWN_VALUE);
  }

  // Convolve grid with 3x3 filter
  for (int j = 1; j < YResolution()-1; j++) { 
    for (int i = 1; i < XResolution()-1; i++) { 
      RNScalar value = copy.GridValue(i, j);
      if (value != R2_GRID_UNKNOWN_VALUE) {
        RNScalar sum = 0;
        RNBoolean unknown = FALSE;
        for (int dj = -1; dj <= 1; dj++) {
          for (int di = -1; di <= 1; di++) {
            value = copy.GridValue(i + di, j + dj);
            if (value == R2_GRID_UNKNOWN_VALUE) { unknown = TRUE; break; }
            else sum += filter[dj+1][di+1] * value;
          }
          if (unknown) break; 
        }
        if (unknown) SetGridValue(i, j, R2_GRID_UNKNOWN_VALUE);
        else SetGridValue(i, j, sum);
      }
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x28(%rsp), %rdi
movq %rbx, %rsi
callq 0x59934
movslq 0xc0(%rbx), %r10
movabsq $-0x3f8bf00000000000, %rdx # imm = 0xC074100000000000
testq %r10, %r10
movq %rbx, 0x8(%rsp)
jle 0x5cdc2
movq 0xb8(%rbx), %rcx
movslq 0xc4(%rbx), %r11
movq %r11, %rsi
decq %rsi
movslq 0xc8(%rbx), %rdi
imulq %rsi, %rdi
leaq (%rcx,%rdi,8), %rsi
xorl %edi, %edi
movq %rdx, (%rcx,%rdi,8)
movq %rdx, (%rsi,%rdi,8)
incq %rdi
cmpq %rdi, %r10
jne 0x5cdb0
jmp 0x5cdc9
movl 0xc4(%rbx), %r11d
testl %r11d, %r11d
jle 0x5cf00
movq 0x8(%rsp), %rax
movq 0xb8(%rax), %rcx
movslq 0xc8(%rax), %rsi
movl %r11d, %edi
shlq $0x3, %rsi
movq %rdx, (%rcx)
movq %rdx, -0x8(%rcx,%r10,8)
addq %rsi, %rcx
decq %rdi
jne 0x5cdec
cmpl $0x3, %r11d
jl 0x5cf00
decl %r11d
leal -0x1(%r10), %edx
movq 0xe0(%rsp), %rsi
movl 0xf0(%rsp), %edi
movslq %edi, %rax
movq %rax, 0x10(%rsp)
movl $0x1, %r9d
xorl %r8d, %r8d
movsd 0x46183(%rip), %xmm0    # 0xa2fb8
movq %r10, 0x20(%rsp)
movq %r11, 0x18(%rsp)
cmpl $0x3, %r10d
jl 0x5cee7
movq %r9, %rcx
imulq 0x10(%rsp), %rcx
movq 0x8(%rsp), %rax
movslq 0xc8(%rax), %r11
imulq %r9, %r11
leaq (%rsi,%rcx,8), %r15
shlq $0x3, %r11
addq 0xb8(%rax), %r11
movl $0x1, %r12d
movq %r8, %r10
movsd (%r15,%r12,8), %xmm1
ucomisd %xmm0, %xmm1
jne 0x5ce88
jnp 0x5cedc
xorpd %xmm1, %xmm1
movq $-0x1, %rbp
movq %r14, %rbx
movq %r10, %r13
xorl %ecx, %ecx
leal (%rcx,%r13), %eax
cltq
movsd (%rsi,%rax,8), %xmm2
ucomisd %xmm0, %xmm2
jne 0x5ceae
jnp 0x5ced2
mulsd (%rbx,%rcx,8), %xmm2
addsd %xmm2, %xmm1
incq %rcx
cmpq $0x3, %rcx
jne 0x5ce9b
incq %rbp
addq %rdi, %r13
addq $0x18, %rbx
cmpq $0x2, %rbp
jne 0x5ce99
jmp 0x5ced6
movapd %xmm0, %xmm1
movsd %xmm1, (%r11,%r12,8)
incq %r12
incq %r10
cmpq %rdx, %r12
jne 0x5ce7a
incq %r9
addq %rdi, %r8
movq 0x18(%rsp), %r11
cmpq %r11, %r9
movq 0x20(%rsp), %r10
jne 0x5ce3f
leaq 0x28(%rsp), %rdi
callq 0x59b0c
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq