// File: /workspace/github_cpp_makefile_star2_O2/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 $0xe8, %rsp
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x51fb4
movl 0xc0(%r14), %eax
movq 0xb8(%r14), %rdi
movl 0xc4(%r14), %esi
leal -0x1(%rsi), %ecx
movq %r14, (%rsp)
movl 0xc8(%r14), %edx
imull %ecx, %edx
movslq %edx, %rdx
xorl %r8d, %r8d
testl %eax, %eax
movl $0x0, %r9d
cmovgl %eax, %r9d
leaq (%rdi,%rdx,8), %r10
movabsq $-0x3f8bf00000000000, %rdx # imm = 0xC074100000000000
cmpq %r8, %r9
je 0x5504c
movq %rdx, (%rdi,%r8,8)
movq %rdx, (%r10,%r8,8)
incq %r8
jmp 0x5503a
movq (%rsp), %r8
movq 0xb8(%r8), %rdi
movslq 0xc8(%r8), %r8
decl %eax
cltq
xorl %r9d, %r9d
testl %esi, %esi
cmovgl %esi, %r9d
shlq $0x3, %r8
subq $0x1, %r9
jb 0x55081
movq %rdx, (%rdi)
movq %rdx, (%rdi,%rax,8)
addq %r8, %rdi
jmp 0x5506f
movslq %ecx, %rcx
movq %rcx, 0x8(%rsp)
pushq $0x1
popq %r8
xorl %r11d, %r11d
movsd 0x3eed0(%rip), %xmm0    # 0x93f68
pushq $-0x1
popq %rcx
cmpq 0x8(%rsp), %r8
jge 0x5516d
movq 0xd0(%rsp), %r9
movl 0xe0(%rsp), %r10d
movslq %r10d, %rdx
movq %r8, %rdi
imulq %rdx, %rdi
movq (%rsp), %rsi
movslq 0xc8(%rsi), %r15
imulq %r8, %r15
leaq (%r9,%rdi,8), %r12
shlq $0x3, %r15
addq 0xb8(%rsi), %r15
movq %r11, 0x10(%rsp)
imull %r11d, %edx
pushq $0x1
popq %r13
cmpq %rax, %r13
jge 0x5515d
movsd (%r12,%r13,8), %xmm1
ucomisd %xmm0, %xmm1
jne 0x550fe
jnp 0x55155
xorpd %xmm1, %xmm1
movq %rbx, %rbp
movq %rdx, %r11
movq %rcx, %rdi
cmpq $0x2, %rdi
je 0x5514f
xorl %r14d, %r14d
cmpq $0x3, %r14
je 0x5513f
leal (%r11,%r14), %esi
movslq %esi, %rsi
movsd (%r9,%rsi,8), %xmm2
ucomisd %xmm0, %xmm2
jne 0x5512f
jnp 0x5514b
mulsd (%rbp,%r14,8), %xmm2
addsd %xmm2, %xmm1
incq %r14
jmp 0x55114
incq %rdi
addq %r10, %r11
addq $0x18, %rbp
jmp 0x5510b
movapd %xmm0, %xmm1
movsd %xmm1, (%r15,%r13,8)
incq %r13
incq %rdx
jmp 0x550eb
incq %r8
movq 0x10(%rsp), %r11
incl %r11d
jmp 0x5509b
leaq 0x18(%rsp), %rdi
callq 0x5216c
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop