// File: /workspace/github_cpp_makefile_star2_O1/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
movapd %xmm0, %xmm6
movsd 0x10(%rdx), %xmm0
movsd 0x10(%rcx), %xmm2
movapd %xmm0, %xmm1
subsd %xmm2, %xmm1
addsd 0x6fa0c(%rip), %xmm1    # 0xa2038
cvttsd2si %xmm1, %eax
movq %rdx, 0x18(%rsp)
movupd (%rdx), %xmm5
movq %rcx, 0x20(%rsp)
movupd (%rcx), %xmm3
movapd %xmm5, %xmm1
addpd %xmm3, %xmm1
cvttpd2dq %xmm1, %xmm1
movapd %xmm0, %xmm4
addsd %xmm2, %xmm4
cvttsd2si %xmm4, %edx
xorl %r10d, %r10d
testl %eax, %eax
cmovgl %eax, %r10d
pshufd $0x50, %xmm1, %xmm4     # xmm4 = xmm1[0,0,1,1]
movmskpd %xmm4, %eax
testl %eax, %eax
setne %al
testl %edx, %edx
sets %cl
orb %al, %cl
jne 0x328be
movq %rsi, %r15
subpd %xmm3, %xmm5
addpd 0x708d3(%rip), %xmm5    # 0xa2f60
cvttpd2dq %xmm5, %xmm3
pxor %xmm4, %xmm4
movapd %xmm3, %xmm7
pcmpgtd %xmm4, %xmm7
pand %xmm3, %xmm7
movl 0x238(%rdi), %esi
movd %xmm7, %eax
cmpl %esi, %eax
jge 0x328be
movl 0x23c(%rdi), %r8d
pshufd $0x55, %xmm7, %xmm3     # xmm3 = xmm7[1,1,1,1]
movd %xmm3, %ecx
cmpl %r8d, %ecx
jge 0x328be
movl 0x240(%rdi), %r9d
cmpl %r9d, %r10d
jge 0x328be
leal -0x1(%r9), %r11d
cmpl %edx, %r9d
cmovgl %edx, %r11d
movl %r11d, 0xc(%rsp)
cmpl %r11d, %r10d
jg 0x328be
movd %r8d, %xmm3
movd %esi, %xmm4
punpckldq %xmm3, %xmm4    # xmm4 = xmm4[0],xmm3[0],xmm4[1],xmm3[1]
pcmpeqd %xmm5, %xmm5
paddd %xmm4, %xmm5
movdqa %xmm4, %xmm3
pcmpgtd %xmm1, %xmm3
pand %xmm3, %xmm1
pandn %xmm5, %xmm3
xorps %xmm4, %xmm4
cvtsi2sd %r10d, %xmm4
por %xmm1, %xmm3
subsd %xmm0, %xmm4
cvtsi2sd %ecx, %xmm0
movsd %xmm0, 0x38(%rsp)
addsd %xmm4, %xmm2
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x50(%rsp)
movq 0x230(%rdi), %rdx
movq %rdx, 0x48(%rsp)
movl 0x248(%rdi), %edx
movl 0x244(%rdi), %esi
pcmpgtd %xmm3, %xmm7
pshufd $0x50, %xmm7, %xmm0     # xmm0 = xmm7[0,0,1,1]
movmskpd %xmm0, %edi
movl %edi, 0x10(%rsp)
shrb %dil
movb %dil, 0x3(%rsp)
movl %ecx, %edi
movq %rdi, 0x30(%rsp)
pshufd $0x55, %xmm3, %xmm0     # xmm0 = xmm3[1,1,1,1]
movd %xmm0, %edi
incl %edi
movq %rdi, 0x58(%rsp)
movd %xmm3, %r14d
subl %eax, %r14d
incl %r14d
movl %esi, 0x14(%rsp)
imull %esi, %ecx
movl %edx, 0x8(%rsp)
imull %r10d, %edx
addl %eax, %edx
addl %ecx, %edx
movl %edx, 0x4(%rsp)
movsd 0x6f881(%rip), %xmm0    # 0xa2038
movsd %xmm6, 0x60(%rsp)
movq %r10, 0x40(%rsp)
cmpb $0x0, 0x3(%rsp)
jne 0x32899
movsd 0x38(%rsp), %xmm1
movq 0x18(%rsp), %rax
subsd 0x8(%rax), %xmm1
movq 0x20(%rsp), %rax
addsd 0x8(%rax), %xmm1
movl 0x4(%rsp), %r13d
movq 0x30(%rsp), %rbp
movsd %xmm2, 0x68(%rsp)
testb $0x1, 0x10(%rsp)
jne 0x32882
movslq %r13d, %rax
movsd 0x50(%rsp), %xmm3
movq 0x18(%rsp), %rcx
subsd (%rcx), %xmm3
movq 0x48(%rsp), %rcx
leaq (%rcx,%rax,8), %r12
movq 0x20(%rsp), %rax
addsd (%rax), %xmm3
xorl %ebx, %ebx
movsd %xmm1, 0x28(%rsp)
movsd %xmm3, 0x70(%rsp)
movq %r15, %rdi
movapd %xmm3, %xmm0
movsd 0x28(%rsp), %xmm1
callq 0x2f3bc
movsd 0x70(%rsp), %xmm3
movsd 0x28(%rsp), %xmm1
movsd 0x68(%rsp), %xmm2
movsd 0x60(%rsp), %xmm6
mulsd %xmm6, %xmm0
addsd (%r12,%rbx,8), %xmm0
movsd %xmm0, (%r12,%rbx,8)
movsd 0x6f7c2(%rip), %xmm0    # 0xa2038
addsd %xmm0, %xmm3
incq %rbx
cmpl %ebx, %r14d
jne 0x3282e
incq %rbp
addsd %xmm0, %xmm1
addl 0x14(%rsp), %r13d
cmpq 0x58(%rsp), %rbp
jne 0x327f7
movq 0x40(%rsp), %rcx
leal 0x1(%rcx), %eax
addsd %xmm0, %xmm2
movl 0x4(%rsp), %edx
addl 0x8(%rsp), %edx
movl %edx, 0x4(%rsp)
cmpl 0xc(%rsp), %ecx
movl %eax, %r10d
jne 0x327bd
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop