// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::GridValue(double, double, double) const
RNScalar R3Grid::
GridValue(RNScalar x, RNScalar y, RNScalar z) const
{
  // Check if within bounds
  if ((x < 0) || (x > grid_resolution[0]-1)) return 0.0;
  if ((y < 0) || (y > grid_resolution[1]-1)) return 0.0;
  if ((z < 0) || (z > grid_resolution[2]-1)) return 0.0;

  // Trilinear interpolation
  int ix1 = (int) x;
  int iy1 = (int) y;
  int iz1 = (int) z;
  int ix2 = ix1 + 1;
  int iy2 = iy1 + 1;
  int iz2 = iz1 + 1;
  if (ix2 >= grid_resolution[0]) ix2 = ix1;
  if (iy2 >= grid_resolution[1]) iy2 = iy1;
  if (iz2 >= grid_resolution[2]) iz2 = iz1;
  RNScalar dx = x - ix1;
  RNScalar dy = y - iy1;
  RNScalar dz = z - iz1;
  RNScalar value = 0.0;
  value += GridValue(ix1, iy1, iz1) * (1.0-dx) * (1.0-dy) * (1.0-dz);
  value += GridValue(ix1, iy1, iz2) * (1.0-dx) * (1.0-dy) * dz;
  value += GridValue(ix1, iy2, iz1) * (1.0-dx) * dy * (1.0-dz);
  value += GridValue(ix1, iy2, iz2) * (1.0-dx) * dy * dz;
  value += GridValue(ix2, iy1, iz1) * dx * (1.0-dy) * (1.0-dz);
  value += GridValue(ix2, iy1, iz2) * dx * (1.0-dy) * dz;
  value += GridValue(ix2, iy2, iz1) * dx * dy * (1.0-dz);
  value += GridValue(ix2, iy2, iz2) * dx * dy * dz;
  return value;
}

// Assembly:
pushq %rbp
pushq %r14
pushq %rbx
xorpd %xmm3, %xmm3
ucomisd %xmm0, %xmm3
jbe 0x2f3d3
xorpd %xmm0, %xmm0
popq %rbx
popq %r14
popq %rbp
retq
ucomisd %xmm1, %xmm3
ja 0x2f3ca
movl 0x238(%rdi), %esi
leal -0x1(%rsi), %eax
xorps %xmm3, %xmm3
cvtsi2sd %eax, %xmm3
ucomisd %xmm3, %xmm0
ja 0x2f3ca
xorpd %xmm3, %xmm3
ucomisd %xmm2, %xmm3
ja 0x2f3ca
movl 0x23c(%rdi), %r11d
leal -0x1(%r11), %eax
xorps %xmm3, %xmm3
cvtsi2sd %eax, %xmm3
ucomisd %xmm3, %xmm1
ja 0x2f3ca
movl 0x240(%rdi), %ebx
leal -0x1(%rbx), %eax
xorps %xmm3, %xmm3
cvtsi2sd %eax, %xmm3
ucomisd %xmm3, %xmm2
ja 0x2f3ca
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
cvttpd2dq %xmm1, %xmm0
cvttsd2si %xmm2, %edx
pshufd $0x55, %xmm0, %xmm3     # xmm3 = xmm0[1,1,1,1]
movd %xmm3, %r10d
leal 0x1(%r10), %r9d
movd %xmm0, %r8d
leal 0x1(%r8), %eax
leal 0x1(%rdx), %ecx
cmpl %esi, %r9d
cmovgel %r10d, %r9d
cmpl %r11d, %eax
cmovgel %r8d, %eax
cmpl %ebx, %ecx
cmovgel %edx, %ecx
cvtdq2pd %xmm0, %xmm0
subpd %xmm0, %xmm1
cvttpd2dq %xmm2, %xmm0
cvtdq2pd %xmm0, %xmm0
subsd %xmm0, %xmm2
movq 0x230(%rdi), %rsi
movl 0x248(%rdi), %ebx
imull %ebx, %edx
movl 0x244(%rdi), %edi
imull %edi, %r8d
leal (%rdx,%r10), %r11d
leal (%r11,%r8), %ebp
movslq %ebp, %r14
movapd 0x73abf(%rip), %xmm4    # 0xa2f60
subpd %xmm1, %xmm4
movapd %xmm4, %xmm5
unpckhpd %xmm4, %xmm5            # xmm5 = xmm5[1],xmm4[1]
movsd (%rsi,%r14,8), %xmm0
mulsd %xmm5, %xmm0
mulsd %xmm4, %xmm0
movsd 0x72b75(%rip), %xmm3    # 0xa2038
subsd %xmm2, %xmm3
mulsd %xmm3, %xmm0
xorpd %xmm6, %xmm6
addsd %xmm0, %xmm6
imull %ebx, %ecx
addl %ecx, %r10d
leal (%r10,%r8), %ebx
movslq %ebx, %rbx
movsd (%rsi,%rbx,8), %xmm0
mulsd %xmm5, %xmm0
mulsd %xmm4, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm6, %xmm0
imull %edi, %eax
addl %eax, %r11d
movslq %r11d, %rdi
movsd (%rsi,%rdi,8), %xmm6
mulsd %xmm5, %xmm6
mulsd %xmm1, %xmm6
mulsd %xmm3, %xmm6
addsd %xmm0, %xmm6
addl %eax, %r10d
movslq %r10d, %rdi
mulsd (%rsi,%rdi,8), %xmm5
mulsd %xmm1, %xmm5
mulsd %xmm2, %xmm5
addsd %xmm6, %xmm5
addl %r9d, %edx
leal (%rdx,%r8), %edi
movslq %edi, %rdi
movapd %xmm1, %xmm0
unpckhpd %xmm1, %xmm0            # xmm0 = xmm0[1],xmm1[1]
movsd (%rsi,%rdi,8), %xmm6
mulsd %xmm0, %xmm6
mulsd %xmm4, %xmm6
mulsd %xmm3, %xmm6
addsd %xmm5, %xmm6
addl %r9d, %ecx
addl %ecx, %r8d
movslq %r8d, %rdi
movsd (%rsi,%rdi,8), %xmm5
mulsd %xmm0, %xmm5
mulsd %xmm4, %xmm5
mulsd %xmm2, %xmm5
addsd %xmm6, %xmm5
addl %eax, %edx
movslq %edx, %rdx
movsd (%rsi,%rdx,8), %xmm4
mulsd %xmm0, %xmm4
mulsd %xmm1, %xmm4
mulsd %xmm3, %xmm4
addsd %xmm5, %xmm4
addl %eax, %ecx
movslq %ecx, %rax
mulsd (%rsi,%rax,8), %xmm0
mulsd %xmm1, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm4, %xmm0
jmp 0x2f3ce