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

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

  // Convolve grid with filter
  for (int k = 1; k < ZResolution()-1; k++) {
    for (int j = 1; j < YResolution()-1; j++) { 
      for (int i = 1; i < XResolution()-1; i++) { 
        RNScalar sum = 0;
        for (int dk = -1; dk <= 1; dk++) {
          for (int dj = -1; dj <= 1; dj++) {
            for (int di = -1; di <= 1; di++) {
              RNScalar value = copy.GridValue(i + di, j + dj, k + dk);
              sum += filter[dk+1][dj+1][di+1] * value;
            }
          }
        }
        SetGridValue(i, j, k, sum);
      }
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2a8, %rsp            # imm = 0x2A8
movq %rsi, 0x40(%rsp)
movq %rdi, %rbx
leaq 0x58(%rsp), %rdi
movq %rbx, %rsi
callq 0x2bbaa
movl 0x23c(%rbx), %ecx
movq %rbx, (%rsp)
movl 0x240(%rbx), %eax
xorl %edx, %edx
testl %eax, %eax
cmovlel %edx, %eax
testl %ecx, %ecx
movl $0x0, %esi
cmovgl %ecx, %esi
cmpl %esi, %edx
je 0x2dcd2
movq (%rsp), %r10
movq 0x230(%r10), %rdi
movl 0x248(%r10), %r8d
movl 0x238(%r10), %r9d
decl %r9d
movl 0x244(%r10), %r10d
imull %edx, %r10d
movq %rax, %r11
subq $0x1, %r11
jb 0x2dcce
movslq %r10d, %r10
andq $0x0, (%rdi,%r10,8)
leal (%r9,%r10), %ebx
movslq %ebx, %rbx
andq $0x0, (%rdi,%rbx,8)
addl %r8d, %r10d
jmp 0x2dcaf
incl %edx
jmp 0x2dc81
movq (%rsp), %rax
movslq 0x238(%rax), %r9
movl 0x240(%rax), %edx
decl %ecx
xorl %r8d, %r8d
testl %edx, %edx
movl $0x0, %eax
cmovgl %edx, %eax
testl %r9d, %r9d
movl $0x0, %edi
movq %r9, 0x18(%rsp)
cmovgl %r9d, %edi
cmpl %edi, %r8d
je 0x2dd4f
movq (%rsp), %r11
movq 0x230(%r11), %r9
movl 0x248(%r11), %r10d
movl 0x244(%r11), %r11d
imull %ecx, %r11d
movq %rax, %rbx
movl %r8d, %r14d
subq $0x1, %rbx
jb 0x2dd4a
movslq %r14d, %r14
andq $0x0, (%r9,%r14,8)
leal (%r11,%r14), %ebp
movslq %ebp, %r15
andq $0x0, (%r9,%r15,8)
addl %r10d, %r14d
jmp 0x2dd2b
incl %r8d
jmp 0x2dd03
movl %esi, %esi
xorl %eax, %eax
cmpl %edi, %eax
je 0x2dda4
movq (%rsp), %r11
movq 0x230(%r11), %r8
movl 0x244(%r11), %r9d
movl 0x240(%r11), %edx
leal -0x1(%rdx), %r10d
imull 0x248(%r11), %r10d
movq %rsi, %r11
movl %eax, %ebx
subq $0x1, %r11
jb 0x2dda0
movslq %ebx, %rbx
andq $0x0, (%r8,%rbx,8)
leal (%r10,%rbx), %ebp
movslq %ebp, %r14
andq $0x0, (%r8,%r14,8)
addl %r9d, %ebx
jmp 0x2dd81
incl %eax
jmp 0x2dd53
decl %edx
decq 0x18(%rsp)
movslq %ecx, %rax
movq %rax, 0x30(%rsp)
movslq %edx, %rax
movq %rax, 0x20(%rsp)
pushq $0x1
popq %rdx
movq $0x0, 0x8(%rsp)
pushq $-0x1
popq %rax
cmpq 0x20(%rsp), %rdx
jge 0x2defc
xorl %edi, %edi
pushq $0x1
popq %r8
movq %rdx, 0x28(%rsp)
cmpq 0x30(%rsp), %r8
jge 0x2dee8
movq 0x288(%rsp), %r15
movl 0x2a0(%rsp), %r12d
movl 0x29c(%rsp), %r13d
movq (%rsp), %rcx
movq 0x230(%rcx), %rsi
movq %rsi, 0x50(%rsp)
movl 0x248(%rcx), %esi
imull %edx, %esi
movq %rsi, 0x48(%rsp)
movl 0x244(%rcx), %esi
movq %r8, 0x38(%rsp)
imull %r8d, %esi
movq 0x8(%rsp), %rcx
imull %r12d, %ecx
movl %r13d, %r11d
movl %edi, 0x14(%rsp)
imull %edi, %r11d
addl %ecx, %r11d
pushq $0x1
popq %rdi
cmpq 0x18(%rsp), %rdi
jge 0x2ded0
xorpd %xmm0, %xmm0
movq 0x40(%rsp), %r14
movq %r11, %r9
movq %rax, %r8
cmpq $0x2, %r8
je 0x2deaf
movq %r14, %rbx
movq %r9, %rcx
movq %rax, %r10
cmpq $0x2, %r10
je 0x2dea3
incq %r10
xorl %ebp, %ebp
cmpq $0x3, %rbp
je 0x2de9a
leal (%rcx,%rbp), %edx
movslq %edx, %rdx
movsd (%rbx,%rbp,8), %xmm1
mulsd (%r15,%rdx,8), %xmm1
addsd %xmm1, %xmm0
incq %rbp
jmp 0x2de7a
addq %r13, %rcx
addq $0x18, %rbx
jmp 0x2de6f
incq %r8
addq %r12, %r9
addq $0x48, %r14
jmp 0x2de60
movq 0x48(%rsp), %rcx
addl %edi, %ecx
addl %esi, %ecx
movslq %ecx, %rcx
movq 0x50(%rsp), %rdx
movsd %xmm0, (%rdx,%rcx,8)
incq %rdi
incq %r11
jmp 0x2de4a
movq 0x38(%rsp), %r8
incq %r8
movl 0x14(%rsp), %edi
incl %edi
movq 0x28(%rsp), %rdx
jmp 0x2dde0
incq %rdx
movq 0x8(%rsp), %rcx
incl %ecx
movq %rcx, 0x8(%rsp)
jmp 0x2ddca
leaq 0x58(%rsp), %rdi
callq 0x2bc02
addq $0x2a8, %rsp            # imm = 0x2A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq