// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::Add(gaps::R3Grid const&, gaps::R3Point const&, gaps::R3Point const&, double)
void R3Grid::
Add(const R3Grid& filter, const R3Point& grid_position, const R3Point& filter_origin, RNScalar amplitude)
{
  // Determine extent of filter in grid coordinates
  int x1 = (int) (grid_position.X() - filter_origin.X() + 1);
  int y1 = (int) (grid_position.Y() - filter_origin.Y() + 1);
  int z1 = (int) (grid_position.Z() - filter_origin.Z() + 1);
  int x2 = (int) (grid_position.X() + filter_origin.X());
  int y2 = (int) (grid_position.Y() + filter_origin.Y());
  int z2 = (int) (grid_position.Z() + filter_origin.Z());
  if (x1 < 0) x1 = 0;
  if (y1 < 0) y1 = 0;
  if (z1 < 0) z1 = 0;
  if (x2 < 0) return;
  if (y2 < 0) return;
  if (z2 < 0) return;
  if (x1 >= XResolution()) return;
  if (y1 >= YResolution()) return;
  if (z1 >= ZResolution()) return;
  if (x2 >= XResolution()) x2 = XResolution()-1;
  if (y2 >= YResolution()) y2 = YResolution()-1;
  if (z2 >= ZResolution()) z2 = ZResolution()-1;

  // Add amplitude*filter to grid (aligning grid_position and filter_origin)
  RNScalar sz = z1 - grid_position.Z() + filter_origin.Z();
  assert((sz >= 0) && (sz < filter.ZResolution()));
  for (int gz = z1; gz <= z2; gz++, sz += 1) {
    RNScalar sy = y1 - grid_position.Y() + filter_origin.Y();
    assert((sy >= 0) && (sy < filter.YResolution()));
    for (int gy = y1; gy <= y2; gy++, sy += 1) {
      RNScalar sx = x1 - grid_position.X() + filter_origin.X();
      assert((sx >= 0) && (sx < filter.XResolution()));
      RNScalar *grid_valuesp = &grid_values[gz * grid_sheet_size + gy * grid_row_size + x1];
      for (int gx = x1; gx <= x2; gx++, sx += 1) {
        (*grid_valuesp++) += amplitude * filter.GridValue(sx, sy, sz);
      }
    }
  }
}

// Assembly:
subq $0x108, %rsp            # imm = 0x108
movq %rdi, 0x100(%rsp)
movq %rsi, 0xf8(%rsp)
movq %rdx, 0xf0(%rsp)
movq %rcx, 0xe8(%rsp)
movsd %xmm0, 0xe0(%rsp)
movq 0x100(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x37820
movsd %xmm0, 0x60(%rsp)
movq 0xe8(%rsp), %rdi
callq 0x37820
movaps %xmm0, %xmm1
movsd 0x60(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd 0xd66e6(%rip), %xmm1    # 0x133008
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0xdc(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x37870
movsd %xmm0, 0x68(%rsp)
movq 0xe8(%rsp), %rdi
callq 0x37870
movaps %xmm0, %xmm1
movsd 0x68(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd 0xd66a2(%rip), %xmm1    # 0x133008
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0xd8(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x378c0
movsd %xmm0, 0x70(%rsp)
movq 0xe8(%rsp), %rdi
callq 0x378c0
movaps %xmm0, %xmm1
movsd 0x70(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd 0xd665e(%rip), %xmm1    # 0x133008
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0xd4(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x37820
movsd %xmm0, 0x78(%rsp)
movq 0xe8(%rsp), %rdi
callq 0x37820
movaps %xmm0, %xmm1
movsd 0x78(%rsp), %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0xd0(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x37870
movsd %xmm0, 0x80(%rsp)
movq 0xe8(%rsp), %rdi
callq 0x37870
movaps %xmm0, %xmm1
movsd 0x80(%rsp), %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0xcc(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x378c0
movsd %xmm0, 0x88(%rsp)
movq 0xe8(%rsp), %rdi
callq 0x378c0
movaps %xmm0, %xmm1
movsd 0x88(%rsp), %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0xc8(%rsp)
cmpl $0x0, 0xdc(%rsp)
jge 0x5ca82
movl $0x0, 0xdc(%rsp)
cmpl $0x0, 0xd8(%rsp)
jge 0x5ca97
movl $0x0, 0xd8(%rsp)
cmpl $0x0, 0xd4(%rsp)
jge 0x5caac
movl $0x0, 0xd4(%rsp)
cmpl $0x0, 0xd0(%rsp)
jge 0x5cabb
jmp 0x5ce78
cmpl $0x0, 0xcc(%rsp)
jge 0x5caca
jmp 0x5ce78
cmpl $0x0, 0xc8(%rsp)
jge 0x5cad9
jmp 0x5ce78
movq 0x58(%rsp), %rdi
movl 0xdc(%rsp), %eax
movl %eax, 0x54(%rsp)
callq 0x138c0
movl %eax, %ecx
movl 0x54(%rsp), %eax
cmpl %ecx, %eax
jl 0x5cafd
jmp 0x5ce78
movq 0x58(%rsp), %rdi
movl 0xd8(%rsp), %eax
movl %eax, 0x50(%rsp)
callq 0x138e0
movl %eax, %ecx
movl 0x50(%rsp), %eax
cmpl %ecx, %eax
jl 0x5cb21
jmp 0x5ce78
movq 0x58(%rsp), %rdi
movl 0xd4(%rsp), %eax
movl %eax, 0x4c(%rsp)
callq 0x13900
movl %eax, %ecx
movl 0x4c(%rsp), %eax
cmpl %ecx, %eax
jl 0x5cb45
jmp 0x5ce78
movq 0x58(%rsp), %rdi
movl 0xd0(%rsp), %eax
movl %eax, 0x48(%rsp)
callq 0x138c0
movl %eax, %ecx
movl 0x48(%rsp), %eax
cmpl %ecx, %eax
jl 0x5cb78
movq 0x58(%rsp), %rdi
callq 0x138c0
subl $0x1, %eax
movl %eax, 0xd0(%rsp)
movq 0x58(%rsp), %rdi
movl 0xcc(%rsp), %eax
movl %eax, 0x44(%rsp)
callq 0x138e0
movl %eax, %ecx
movl 0x44(%rsp), %eax
cmpl %ecx, %eax
jl 0x5cbab
movq 0x58(%rsp), %rdi
callq 0x138e0
subl $0x1, %eax
movl %eax, 0xcc(%rsp)
movq 0x58(%rsp), %rdi
movl 0xc8(%rsp), %eax
movl %eax, 0x40(%rsp)
callq 0x13900
movl %eax, %ecx
movl 0x40(%rsp), %eax
cmpl %ecx, %eax
jl 0x5cbde
movq 0x58(%rsp), %rdi
callq 0x13900
subl $0x1, %eax
movl %eax, 0xc8(%rsp)
cvtsi2sdl 0xd4(%rsp), %xmm0
movsd %xmm0, 0x30(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x378c0
movaps %xmm0, %xmm1
movsd 0x30(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x38(%rsp)
movq 0xe8(%rsp), %rdi
callq 0x378c0
movaps %xmm0, %xmm1
movsd 0x38(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xc0(%rsp)
movl 0xd4(%rsp), %eax
movl %eax, 0xbc(%rsp)
movl 0xbc(%rsp), %eax
cmpl 0xc8(%rsp), %eax
jg 0x5ce78
cvtsi2sdl 0xd8(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x37870
movaps %xmm0, %xmm1
movsd 0x20(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x28(%rsp)
movq 0xe8(%rsp), %rdi
callq 0x37870
movaps %xmm0, %xmm1
movsd 0x28(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xb0(%rsp)
movl 0xd8(%rsp), %eax
movl %eax, 0xac(%rsp)
movl 0xac(%rsp), %eax
cmpl 0xcc(%rsp), %eax
jg 0x5ce46
cvtsi2sdl 0xdc(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x37820
movaps %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0xe8(%rsp), %rdi
callq 0x37820
movq 0x58(%rsp), %rsi
movaps %xmm0, %xmm1
movsd 0x18(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xa0(%rsp)
movq 0x230(%rsi), %rax
movl 0xbc(%rsp), %ecx
imull 0x248(%rsi), %ecx
movl 0xac(%rsp), %edx
imull 0x244(%rsi), %edx
addl %edx, %ecx
addl 0xdc(%rsp), %ecx
movslq %ecx, %rcx
shlq $0x3, %rcx
addq %rcx, %rax
movq %rax, 0x98(%rsp)
movl 0xdc(%rsp), %eax
movl %eax, 0x94(%rsp)
movl 0x94(%rsp), %eax
cmpl 0xd0(%rsp), %eax
jg 0x5ce14
movsd 0xe0(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0xf8(%rsp), %rdi
movsd 0xa0(%rsp), %xmm0
movsd 0xb0(%rsp), %xmm1
movsd 0xc0(%rsp), %xmm2
callq 0x56e50
movaps %xmm0, %xmm2
movsd 0x8(%rsp), %xmm0
movq 0x98(%rsp), %rax
movq %rax, %rcx
addq $0x8, %rcx
movq %rcx, 0x98(%rsp)
movsd (%rax), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, (%rax)
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
movsd 0xd620b(%rip), %xmm0    # 0x133008
addsd 0xa0(%rsp), %xmm0
movsd %xmm0, 0xa0(%rsp)
jmp 0x5cd69
jmp 0x5ce16
movl 0xac(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xac(%rsp)
movsd 0xd61d9(%rip), %xmm0    # 0x133008
addsd 0xb0(%rsp), %xmm0
movsd %xmm0, 0xb0(%rsp)
jmp 0x5ccb2
jmp 0x5ce48
movl 0xbc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xbc(%rsp)
movsd 0xd61a7(%rip), %xmm0    # 0x133008
addsd 0xc0(%rsp), %xmm0
movsd %xmm0, 0xc0(%rsp)
jmp 0x5cc3e
addq $0x108, %rsp            # imm = 0x108
retq