// File: /workspace/github_cpp_makefile_star2_O3/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 $0x2b8, %rsp            # imm = 0x2B8
movq %rsi, 0x58(%rsp)
movq %rdi, %r15
leaq 0x68(%rsp), %rdi
movq %r15, %rsi
callq 0x2ee42
movl 0x23c(%r15), %r12d
testl %r12d, %r12d
jle 0x31533
movl 0x240(%r15), %eax
movq 0x230(%r15), %rcx
movl 0x248(%r15), %edx
movl 0x244(%r15), %esi
movl 0x238(%r15), %edi
movq %rdi, %r13
decl %edi
xorl %r8d, %r8d
xorl %r9d, %r9d
xorl %r10d, %r10d
movq %rax, %r11
movl %r9d, %ebx
testl %eax, %eax
jle 0x31523
movslq %ebx, %rbx
movq %r8, (%rcx,%rbx,8)
leal (%rdi,%rbx), %ebp
movslq %ebp, %r14
movq %r8, (%rcx,%r14,8)
addl %edx, %ebx
decq %r11
jne 0x3150b
incl %r10d
addl %esi, %r9d
cmpl %r12d, %r10d
jne 0x31501
movq %r13, %r14
jmp 0x3153a
movl 0x238(%r15), %r14d
testl %r14d, %r14d
movq %r15, 0x28(%rsp)
jle 0x315f7
movl 0x240(%r15), %eax
movq 0x230(%r15), %rcx
movl 0x248(%r15), %edx
leal -0x1(%r12), %esi
imull 0x244(%r15), %esi
xorl %edi, %edi
xorl %r8d, %r8d
movq %rax, %r9
movl %r8d, %r10d
testl %eax, %eax
jle 0x31593
movslq %r10d, %r10
movq %rdi, (%rcx,%r10,8)
leal (%rsi,%r10), %r11d
movslq %r11d, %r11
movq %rdi, (%rcx,%r11,8)
addl %edx, %r10d
decq %r9
jne 0x31579
incl %r8d
cmpl %r14d, %r8d
jne 0x3156f
testl %r14d, %r14d
jle 0x315f7
movq 0x230(%r15), %rdx
movl 0x244(%r15), %esi
movl 0x240(%r15), %ecx
leal -0x1(%rcx), %eax
movl 0x248(%r15), %edi
imull %eax, %edi
xorl %r8d, %r8d
xorl %r9d, %r9d
movq %r12, %r10
movl %r9d, %r11d
testl %r12d, %r12d
jle 0x315ed
movslq %r11d, %r11
movq %r8, (%rdx,%r11,8)
leal (%rdi,%r11), %ebx
movslq %ebx, %rbx
movq %r8, (%rdx,%rbx,8)
addl %esi, %r11d
decq %r10
jne 0x315d3
incl %r9d
cmpl %r14d, %r9d
jne 0x315c8
jmp 0x31601
movl 0x240(%r15), %ecx
leal -0x1(%rcx), %eax
cmpl $0x3, %ecx
jl 0x3178c
leal -0x1(%r12), %ecx
movq %rcx, 0x40(%rsp)
leal -0x1(%r14), %ecx
movq %rcx, 0x50(%rsp)
movl %eax, %eax
movq %rax, 0x30(%rsp)
movl $0x1, %eax
movq %rax, 0x8(%rsp)
movq $0x0, (%rsp)
movq %r12, 0x38(%rsp)
movq %r14, 0x48(%rsp)
cmpl $0x3, %r12d
jl 0x31765
movq 0x298(%rsp), %r10
movl 0x2b0(%rsp), %r11d
movl 0x2ac(%rsp), %r15d
movq 0x28(%rsp), %rax
movq 0x230(%rax), %rcx
movq %rcx, 0x60(%rsp)
movl 0x248(%rax), %r13d
imull 0x8(%rsp), %r13d
movl 0x244(%rax), %eax
movl %eax, 0x1c(%rsp)
movl %r11d, %eax
imull (%rsp), %eax
movq %rax, 0x20(%rsp)
movl $0x1, %eax
movq %rax, 0x10(%rsp)
cmpl $0x3, %r14d
jl 0x31743
movl 0x1c(%rsp), %ebp
imull 0x10(%rsp), %ebp
movl $0x1, %r9d
movq 0x20(%rsp), %rax
xorpd %xmm0, %xmm0
movq $-0x1, %rcx
movq 0x58(%rsp), %rdx
movq %rax, %r8
movq %rdx, %rbx
movq %r8, %rdi
movq $-0x1, %r14
incq %r14
xorl %esi, %esi
leal (%rdi,%rsi), %r12d
movslq %r12d, %r12
movsd (%rbx,%rsi,8), %xmm1
mulsd (%r10,%r12,8), %xmm1
addsd %xmm1, %xmm0
incq %rsi
cmpq $0x3, %rsi
jne 0x316e3
addq %r15, %rdi
addq $0x18, %rbx
cmpq $0x2, %r14
jne 0x316de
incq %rcx
addq %r11, %r8
addq $0x48, %rdx
cmpq $0x2, %rcx
jne 0x316d1
leal (%r9,%r13), %ecx
addl %ebp, %ecx
movslq %ecx, %rcx
movq 0x60(%rsp), %rdx
movsd %xmm0, (%rdx,%rcx,8)
incq %r9
incq %rax
cmpq 0x50(%rsp), %r9
jne 0x316be
movq 0x10(%rsp), %rcx
incq %rcx
addq %r15, 0x20(%rsp)
movq %rcx, 0x10(%rsp)
cmpq 0x40(%rsp), %rcx
movq 0x48(%rsp), %r14
jne 0x316a0
movq 0x8(%rsp), %rcx
incq %rcx
movq (%rsp), %rax
incl %eax
movq %rax, (%rsp)
movq %rcx, 0x8(%rsp)
cmpq 0x30(%rsp), %rcx
movq 0x38(%rsp), %r12
jne 0x31640
leaq 0x68(%rsp), %rdi
callq 0x2ee9e
addq $0x2b8, %rsp            # imm = 0x2B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq