// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movapd %xmm0, %xmm4
movq %rsi, %r15
movsd (%rdx), %xmm0
movsd (%rcx), %xmm1
movapd %xmm0, %xmm2
subsd %xmm1, %xmm2
movsd 0x6fbca(%rip), %xmm5    # 0xa2038
addsd %xmm5, %xmm2
cvttsd2si %xmm2, %esi
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
xorl %r9d, %r9d
testl %esi, %esi
cmovlel %r9d, %esi
movl %esi, 0x8(%rsp)
testl %eax, %eax
movq %rdx, 0x20(%rsp)
movupd 0x8(%rdx), %xmm2
movq %rcx, 0x28(%rsp)
movupd 0x8(%rcx), %xmm0
movapd %xmm2, %xmm1
subsd %xmm0, %xmm1
addsd %xmm5, %xmm1
cvttsd2si %xmm1, %r10d
sets %cl
movapd %xmm2, %xmm1
unpckhpd %xmm2, %xmm1            # xmm1 = xmm1[1],xmm2[1]
addpd %xmm0, %xmm2
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
movapd %xmm1, %xmm3
subsd %xmm0, %xmm3
addsd %xmm5, %xmm3
cvttsd2si %xmm3, %edx
cvttpd2dq %xmm2, %xmm2
testl %r10d, %r10d
cmovlel %r9d, %r10d
testl %edx, %edx
cmovgl %edx, %r9d
pshufd $0x50, %xmm2, %xmm3     # xmm3 = xmm2[0,0,1,1]
movmskpd %xmm3, %edx
orb %dl, %cl
shrb %dl
orb %cl, %dl
testb $0x1, %dl
jne 0x32708
movl 0x238(%rdi), %ecx
cmpl %ecx, 0x8(%rsp)
jge 0x32708
movl 0x23c(%rdi), %edx
cmpl %edx, %r10d
jge 0x32708
movl 0x240(%rdi), %esi
cmpl %esi, %r9d
jge 0x32708
pshufd $0x55, %xmm2, %xmm3     # xmm3 = xmm2[1,1,1,1]
movd %xmm3, %r8d
leal -0x1(%rsi), %r11d
cmpl %r8d, %esi
cmoval %r8d, %r11d
movl %r11d, 0x14(%rsp)
cmpl %r11d, %r9d
ja 0x32708
movd %xmm2, %esi
leal -0x1(%rdx), %r8d
cmpl %esi, %edx
cmoval %esi, %r8d
leal -0x1(%rcx), %edx
cmpl %eax, %ecx
cmoval %eax, %edx
xorps %xmm2, %xmm2
cvtsi2sd %r9d, %xmm2
subsd %xmm1, %xmm2
addsd %xmm0, %xmm2
xorps %xmm0, %xmm0
cvtsi2sd %r10d, %xmm0
movsd %xmm0, 0x50(%rsp)
movl 0x8(%rsp), %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movsd %xmm0, 0x70(%rsp)
movq 0x230(%rdi), %rax
movq %rax, 0x68(%rsp)
movl 0x248(%rdi), %eax
movl 0x244(%rdi), %esi
movl %r10d, %edi
movq %rdi, 0x48(%rsp)
movq %r8, 0x58(%rsp)
leal 0x1(%r8), %edi
movq %rdi, 0x78(%rsp)
movl %edx, 0x1c(%rsp)
movl %edx, %r12d
subl %ecx, %r12d
incl %r12d
movl %eax, 0xc(%rsp)
imull %r9d, %eax
movl %esi, 0x18(%rsp)
imull %r10d, %esi
addl %ecx, %esi
addl %eax, %esi
movl %esi, 0x4(%rsp)
movsd %xmm4, 0x80(%rsp)
movl %r10d, 0x10(%rsp)
movq %r9, 0x60(%rsp)
cmpl 0x58(%rsp), %r10d
ja 0x326de
movsd 0x50(%rsp), %xmm1
movq 0x20(%rsp), %rax
subsd 0x8(%rax), %xmm1
movq 0x28(%rsp), %rax
addsd 0x8(%rax), %xmm1
movl 0x4(%rsp), %ebp
movq 0x48(%rsp), %r13
movsd %xmm2, 0x30(%rsp)
movl 0x1c(%rsp), %eax
cmpl %eax, 0x8(%rsp)
ja 0x326c8
movslq %ebp, %rax
movsd 0x70(%rsp), %xmm0
movq 0x20(%rsp), %rcx
subsd (%rcx), %xmm0
movq 0x68(%rsp), %rcx
leaq (%rcx,%rax,8), %rbx
movq 0x28(%rsp), %rax
addsd (%rax), %xmm0
xorl %r14d, %r14d
movsd %xmm1, 0x38(%rsp)
movsd %xmm0, 0x40(%rsp)
movq %r15, %rdi
movsd 0x40(%rsp), %xmm0
movsd 0x38(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
callq 0x2f0d6
movsd 0x38(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
movsd 0x6f99b(%rip), %xmm5    # 0xa2038
movsd 0x80(%rsp), %xmm4
mulsd %xmm4, %xmm0
addsd (%rbx,%r14,8), %xmm0
movsd %xmm0, (%rbx,%r14,8)
movsd 0x40(%rsp), %xmm0
addsd %xmm5, %xmm0
incq %r14
cmpl %r14d, %r12d
jne 0x32669
incq %r13
addsd %xmm5, %xmm1
addl 0x18(%rsp), %ebp
cmpq 0x78(%rsp), %r13
jne 0x3262e
movq 0x60(%rsp), %rcx
leal 0x1(%rcx), %eax
addsd %xmm5, %xmm2
movl 0x4(%rsp), %edx
addl 0xc(%rsp), %edx
movl %edx, 0x4(%rsp)
cmpl 0x14(%rsp), %ecx
movl %eax, %r9d
movl 0x10(%rsp), %r10d
jne 0x325f5
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq