// File: /workspace/github_cpp_makefile_star2_O2/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 $0x78, %rsp
movsd %xmm0, 0x70(%rsp)
movq %rsi, %r15
movsd (%rdx), %xmm0
movsd (%rcx), %xmm1
movapd %xmm0, %xmm2
subsd %xmm1, %xmm2
movsd 0x6463f(%rip), %xmm4    # 0x93008
addsd %xmm4, %xmm2
cvttsd2si %xmm2, %eax
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %r8d
xorl %r9d, %r9d
testl %eax, %eax
cmovlel %r9d, %eax
movl %eax, 0x4(%rsp)
testl %r8d, %r8d
movq %rdx, 0x28(%rsp)
movupd 0x8(%rdx), %xmm2
movq %rcx, 0x30(%rsp)
movupd 0x8(%rcx), %xmm0
movapd %xmm2, %xmm1
subsd %xmm0, %xmm1
addsd %xmm4, %xmm1
cvttsd2si %xmm1, %eax
sets %dl
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 %xmm4, %xmm3
cvttsd2si %xmm3, %esi
cvttpd2dq %xmm2, %xmm2
testl %eax, %eax
cmovlel %r9d, %eax
testl %esi, %esi
cmovgl %esi, %r9d
pshufd $0x50, %xmm2, %xmm3     # xmm3 = xmm2[0,0,1,1]
movmskpd %xmm3, %esi
orb %sil, %dl
shrb %sil
orb %dl, %sil
testb $0x1, %sil
jne 0x2ec17
movl 0x238(%rdi), %ecx
cmpl %ecx, 0x4(%rsp)
jge 0x2ec17
movl 0x23c(%rdi), %esi
cmpl %esi, %eax
jge 0x2ec17
movl 0x240(%rdi), %edx
cmpl %edx, %r9d
jge 0x2ec17
leal -0x1(%rcx), %r12d
cmpl %r8d, %ecx
cmoval %r8d, %r12d
movd %xmm2, %ecx
leal -0x1(%rsi), %r8d
cmpl %ecx, %esi
cmoval %ecx, %r8d
movq %r8, 0x68(%rsp)
pshufd $0x55, %xmm2, %xmm2     # xmm2 = xmm2[1,1,1,1]
movd %xmm2, %ecx
leal -0x1(%rdx), %esi
cmpl %ecx, %edx
cmoval %ecx, %esi
movl %esi, 0x1c(%rsp)
cvtsi2sd %r9d, %xmm2
subsd %xmm1, %xmm2
cvtsi2sd %eax, %xmm1
movsd %xmm1, 0x48(%rsp)
addsd %xmm0, %xmm2
movsd %xmm2, 0x8(%rsp)
cvtsi2sdl 0x4(%rsp), %xmm0
movsd %xmm0, 0x58(%rsp)
movl %eax, %eax
movq %rax, 0x40(%rsp)
movq %rdi, 0x50(%rsp)
cmpl 0x1c(%rsp), %r9d
jg 0x2ec17
movsd 0x48(%rsp), %xmm0
movq 0x28(%rsp), %rax
subsd 0x8(%rax), %xmm0
movq 0x30(%rsp), %rax
addsd 0x8(%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x230(%rdi), %rax
movq %rax, 0x60(%rsp)
movl 0x248(%rdi), %r14d
movl %r9d, 0x20(%rsp)
imull %r9d, %r14d
movl 0x244(%rdi), %eax
movl %eax, 0x24(%rsp)
addl 0x4(%rsp), %r14d
movq 0x40(%rsp), %r13
cmpq 0x68(%rsp), %r13
ja 0x2ebf1
movsd 0x58(%rsp), %xmm0
movq 0x28(%rsp), %rax
subsd (%rax), %xmm0
movq 0x30(%rsp), %rax
addsd (%rax), %xmm0
movl 0x24(%rsp), %eax
imull %r13d, %eax
addl %r14d, %eax
cltq
movq 0x60(%rsp), %rcx
leaq (%rcx,%rax,8), %rbp
movl 0x4(%rsp), %ebx
cmpl %r12d, %ebx
jg 0x2ebd5
movq %r15, %rdi
movsd %xmm0, 0x38(%rsp)
movsd 0x38(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movsd 0x8(%rsp), %xmm2
callq 0x2be20
mulsd 0x70(%rsp), %xmm0
addsd (%rbp), %xmm0
movsd %xmm0, (%rbp)
movsd 0x38(%rsp), %xmm0
addq $0x8, %rbp
incl %ebx
addsd 0x64435(%rip), %xmm0    # 0x93008
jmp 0x2eb8a
incq %r13
movsd 0x10(%rsp), %xmm0
addsd 0x64422(%rip), %xmm0    # 0x93008
movsd %xmm0, 0x10(%rsp)
jmp 0x2eb4d
movl 0x20(%rsp), %r9d
incl %r9d
movsd 0x8(%rsp), %xmm0
addsd 0x64401(%rip), %xmm0    # 0x93008
movsd %xmm0, 0x8(%rsp)
movq 0x50(%rsp), %rdi
jmp 0x2eaf2
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq