// File: 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:
subq $0x328, %rsp            # imm = 0x328
movq %rdi, 0x320(%rsp)
movq %rsi, 0x318(%rsp)
movq 0x320(%rsp), %rsi
movq %rsi, 0x68(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x568c0
movl $0x0, 0xc4(%rsp)
movq 0x68(%rsp), %rdi
movl 0xc4(%rsp), %eax
movl %eax, 0x60(%rsp)
callq 0x138e0
movl %eax, 0x64(%rsp)
jmp 0x5ae07
movl 0x60(%rsp), %eax
movl 0x64(%rsp), %ecx
cmpl %ecx, %eax
jge 0x5aef5
movl $0x0, 0xb0(%rsp)
movq 0x68(%rsp), %rdi
movl 0xb0(%rsp), %eax
movl %eax, 0x58(%rsp)
callq 0x13900
movl %eax, 0x5c(%rsp)
jmp 0x5ae3d
movl 0x58(%rsp), %eax
movl 0x5c(%rsp), %ecx
cmpl %ecx, %eax
jge 0x5aedd
movq 0x68(%rsp), %rdi
movl 0xc4(%rsp), %edx
movl 0xb0(%rsp), %ecx
xorl %esi, %esi
xorps %xmm0, %xmm0
callq 0x6bc60
jmp 0x5ae6c
movq 0x68(%rsp), %rdi
callq 0x138c0
movl %eax, 0x54(%rsp)
jmp 0x5ae7c
movq 0x68(%rsp), %rdi
movl 0x54(%rsp), %esi
decl %esi
movl 0xc4(%rsp), %edx
movl 0xb0(%rsp), %ecx
xorps %xmm0, %xmm0
callq 0x6bc60
jmp 0x5ae9f
jmp 0x5aea1
movl 0xb0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb0(%rsp)
jmp 0x5ae22
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb8(%rsp)
movl %eax, 0xb4(%rsp)
leaq 0xc8(%rsp), %rdi
callq 0x56960
jmp 0x5b34f
jmp 0x5aedf
movl 0xc4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc4(%rsp)
jmp 0x5adec
movl $0x0, 0xac(%rsp)
movq 0x68(%rsp), %rdi
movl 0xac(%rsp), %eax
movl %eax, 0x4c(%rsp)
callq 0x138c0
movl %eax, 0x50(%rsp)
jmp 0x5af1b
movl 0x4c(%rsp), %eax
movl 0x50(%rsp), %ecx
cmpl %ecx, %eax
jge 0x5afe7
movl $0x0, 0xa8(%rsp)
movq 0x68(%rsp), %rdi
movl 0xa8(%rsp), %eax
movl %eax, 0x44(%rsp)
callq 0x13900
movl %eax, 0x48(%rsp)
jmp 0x5af51
movl 0x44(%rsp), %eax
movl 0x48(%rsp), %ecx
cmpl %ecx, %eax
jge 0x5afcf
movq 0x68(%rsp), %rdi
movl 0xac(%rsp), %esi
movl 0xa8(%rsp), %ecx
xorl %edx, %edx
xorps %xmm0, %xmm0
callq 0x6bc60
jmp 0x5af7c
movq 0x68(%rsp), %rdi
movl 0xac(%rsp), %eax
movl %eax, 0x3c(%rsp)
callq 0x138e0
movl %eax, 0x40(%rsp)
jmp 0x5af97
movl 0x3c(%rsp), %esi
movq 0x68(%rsp), %rdi
movl 0x40(%rsp), %edx
decl %edx
movl 0xa8(%rsp), %ecx
xorps %xmm0, %xmm0
callq 0x6bc60
jmp 0x5afb7
jmp 0x5afb9
movl 0xa8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa8(%rsp)
jmp 0x5af36
jmp 0x5afd1
movl 0xac(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xac(%rsp)
jmp 0x5af00
movl $0x0, 0xa4(%rsp)
movq 0x68(%rsp), %rdi
movl 0xa4(%rsp), %eax
movl %eax, 0x34(%rsp)
callq 0x138c0
movl %eax, 0x38(%rsp)
jmp 0x5b00d
movl 0x34(%rsp), %eax
movl 0x38(%rsp), %ecx
cmpl %ecx, %eax
jge 0x5b0e1
movl $0x0, 0xa0(%rsp)
movq 0x68(%rsp), %rdi
movl 0xa0(%rsp), %eax
movl %eax, 0x2c(%rsp)
callq 0x138e0
movl %eax, 0x30(%rsp)
jmp 0x5b043
movl 0x2c(%rsp), %eax
movl 0x30(%rsp), %ecx
cmpl %ecx, %eax
jge 0x5b0c9
movq 0x68(%rsp), %rdi
movl 0xa4(%rsp), %esi
movl 0xa0(%rsp), %edx
xorl %ecx, %ecx
xorps %xmm0, %xmm0
callq 0x6bc60
jmp 0x5b06e
movq 0x68(%rsp), %rdi
movl 0xa4(%rsp), %eax
movl %eax, 0x20(%rsp)
movl 0xa0(%rsp), %eax
movl %eax, 0x24(%rsp)
callq 0x13900
movl %eax, 0x28(%rsp)
jmp 0x5b094
movl 0x24(%rsp), %edx
movl 0x20(%rsp), %esi
movq 0x68(%rsp), %rdi
movl 0x28(%rsp), %ecx
decl %ecx
xorps %xmm0, %xmm0
callq 0x6bc60
jmp 0x5b0b1
jmp 0x5b0b3
movl 0xa0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa0(%rsp)
jmp 0x5b028
jmp 0x5b0cb
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0x5aff2
movl $0x1, 0x9c(%rsp)
movq 0x68(%rsp), %rdi
movl 0x9c(%rsp), %eax
movl %eax, 0x18(%rsp)
callq 0x13900
movl %eax, 0x1c(%rsp)
jmp 0x5b107
movl 0x18(%rsp), %eax
movl 0x1c(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x5b33a
movl $0x1, 0x98(%rsp)
movq 0x68(%rsp), %rdi
movl 0x98(%rsp), %eax
movl %eax, 0x10(%rsp)
callq 0x138e0
movl %eax, 0x14(%rsp)
jmp 0x5b140
movl 0x10(%rsp), %eax
movl 0x14(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x5b322
movl $0x1, 0x94(%rsp)
movq 0x68(%rsp), %rdi
movl 0x94(%rsp), %eax
movl %eax, 0x8(%rsp)
callq 0x138c0
movl %eax, 0xc(%rsp)
jmp 0x5b179
movl 0x8(%rsp), %eax
movl 0xc(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0x5b30a
xorps %xmm0, %xmm0
movsd %xmm0, 0x88(%rsp)
movl $0xffffffff, 0x84(%rsp) # imm = 0xFFFFFFFF
cmpl $0x1, 0x84(%rsp)
jg 0x5b2c8
movl $0xffffffff, 0x80(%rsp) # imm = 0xFFFFFFFF
cmpl $0x1, 0x80(%rsp)
jg 0x5b2b0
movl $0xffffffff, 0x7c(%rsp) # imm = 0xFFFFFFFF
cmpl $0x1, 0x7c(%rsp)
jg 0x5b298
movl 0x94(%rsp), %esi
movl 0x7c(%rsp), %eax
addl %eax, %esi
movl 0x98(%rsp), %edx
movl 0x80(%rsp), %eax
addl %eax, %edx
movl 0x9c(%rsp), %ecx
movl 0x84(%rsp), %eax
addl %eax, %ecx
leaq 0xc8(%rsp), %rdi
callq 0x6bad0
movsd %xmm0, (%rsp)
jmp 0x5b21e
movsd (%rsp), %xmm0
movsd %xmm0, 0x70(%rsp)
movq 0x318(%rsp), %rax
movl 0x84(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
imulq $0x48, %rcx, %rcx
addq %rcx, %rax
movl 0x80(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x7c(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x70(%rsp), %xmm2
movsd 0x88(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x88(%rsp)
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x5b1d2
jmp 0x5b29a
movl 0x80(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x80(%rsp)
jmp 0x5b1bc
jmp 0x5b2b2
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x84(%rsp)
jmp 0x5b1a3
movq 0x68(%rsp), %rdi
movl 0x94(%rsp), %esi
movl 0x98(%rsp), %edx
movl 0x9c(%rsp), %ecx
movsd 0x88(%rsp), %xmm0
callq 0x6bc60
jmp 0x5b2f2
jmp 0x5b2f4
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x5b15e
jmp 0x5b30c
movl 0x98(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x98(%rsp)
jmp 0x5b125
jmp 0x5b324
movl 0x9c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x9c(%rsp)
jmp 0x5b0ec
leaq 0xc8(%rsp), %rdi
callq 0x56960
addq $0x328, %rsp            # imm = 0x328
retq
movq 0xb8(%rsp), %rdi
callq 0xf890
nopl (%rax)