// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::GridValue(double, double) const
RNScalar R2Grid::
GridValue(RNScalar x, RNScalar y) 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;

  // Bilinear interpolation
  int ix1 = (int) x;
  int iy1 = (int) y;
  int ix2 = ix1 + 1;
  int iy2 = iy1 + 1;
  if (ix2 >= grid_resolution[0]) ix2 = ix1;
  if (iy2 >= grid_resolution[1]) iy2 = iy1;
  RNScalar dx = x - ix1;
  RNScalar dy = y - iy1;
  RNScalar value11 = GridValue(ix1, iy1);
  RNScalar value12 = GridValue(ix1, iy2);
  RNScalar value21 = GridValue(ix2, iy1);
  RNScalar value22 = GridValue(ix2, iy2);
  RNScalar weight11 = (1.0-dx) * (1.0-dy);
  RNScalar weight12 = (1.0-dx) * dy;
  RNScalar weight21 = dx * (1.0-dy);
  RNScalar weight22 = dx * dy;
  RNScalar value = 0;
  RNScalar weight = 0;
  if (value11 != R2_GRID_UNKNOWN_VALUE) { value += weight11 * value11; weight += weight11; }
  if (value12 != R2_GRID_UNKNOWN_VALUE) { value += weight12 * value12; weight += weight12; }
  if (value21 != R2_GRID_UNKNOWN_VALUE) { value += weight21 * value21; weight += weight21; }
  if (value22 != R2_GRID_UNKNOWN_VALUE) { value += weight22 * value22; weight += weight22; }
  if (weight == 0) return R2_GRID_UNKNOWN_VALUE;
  return value / weight;
}

// Assembly:
xorpd %xmm2, %xmm2
ucomisd %xmm0, %xmm2
jbe 0x5a28b
movapd %xmm2, %xmm0
retq
ucomisd %xmm1, %xmm2
ja 0x5a286
movl 0xc0(%rdi), %eax
leal -0x1(%rax), %ecx
cvtsi2sd %ecx, %xmm3
ucomisd %xmm3, %xmm0
ja 0x5a286
movl 0xc4(%rdi), %r8d
leal -0x1(%r8), %ecx
xorps %xmm3, %xmm3
cvtsi2sd %ecx, %xmm3
ucomisd %xmm3, %xmm1
ja 0x5a286
unpcklpd %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
cvttpd2dq %xmm0, %xmm1
movd %xmm1, %r9d
leal 0x1(%r9), %esi
pshufd $0x55, %xmm1, %xmm2     # xmm2 = xmm1[1,1,1,1]
movd %xmm2, %edx
cmpl %eax, %esi
cmovgel %r9d, %esi
leal 0x1(%rdx), %ecx
cmpl %r8d, %ecx
cmovgel %edx, %ecx
cvtdq2pd %xmm1, %xmm1
subpd %xmm1, %xmm0
movq 0xb8(%rdi), %rax
movl 0xc8(%rdi), %edi
imull %edi, %edx
leal (%rdx,%r9), %r8d
movslq %r8d, %r8
movsd (%rax,%r8,8), %xmm6
imull %edi, %ecx
addl %ecx, %r9d
movslq %r9d, %rdi
addl %esi, %edx
addl %esi, %ecx
movapd 0x48c41(%rip), %xmm4    # 0xa2f60
subpd %xmm0, %xmm4
movapd %xmm0, %xmm5
unpckhpd %xmm0, %xmm5            # xmm5 = xmm5[1],xmm0[1]
movapd %xmm4, %xmm3
unpckhpd %xmm4, %xmm3            # xmm3 = xmm3[1],xmm4[1]
movsd 0x48c7d(%rip), %xmm1    # 0xa2fb8
ucomisd %xmm1, %xmm6
movapd %xmm4, %xmm7
mulsd %xmm3, %xmm7
mulsd %xmm7, %xmm6
pxor %xmm2, %xmm2
addsd %xmm2, %xmm6
jne 0x5a35b
jp 0x5a35b
xorpd %xmm6, %xmm6
movsd (%rax,%rdi,8), %xmm8
mulsd %xmm5, %xmm4
addsd %xmm2, %xmm7
jne 0x5a371
jp 0x5a371
xorpd %xmm7, %xmm7
movslq %edx, %rdx
ucomisd %xmm1, %xmm8
mulsd %xmm4, %xmm8
addsd %xmm6, %xmm8
jne 0x5a38c
jp 0x5a38c
movapd %xmm6, %xmm8
movsd (%rax,%rdx,8), %xmm6
mulsd %xmm0, %xmm3
addsd %xmm7, %xmm4
jne 0x5a3a1
jp 0x5a3a1
movapd %xmm7, %xmm4
movslq %ecx, %rcx
ucomisd %xmm1, %xmm6
mulsd %xmm3, %xmm6
addsd %xmm8, %xmm6
jne 0x5a3ba
jp 0x5a3ba
movapd %xmm8, %xmm6
movsd (%rax,%rcx,8), %xmm7
mulsd %xmm5, %xmm0
addsd %xmm4, %xmm3
jne 0x5a3cf
jp 0x5a3cf
movapd %xmm4, %xmm3
ucomisd %xmm1, %xmm7
mulsd %xmm0, %xmm7
addsd %xmm6, %xmm7
jne 0x5a3e3
jp 0x5a3e3
movapd %xmm6, %xmm7
addsd %xmm3, %xmm0
jne 0x5a3ef
jp 0x5a3ef
movapd %xmm3, %xmm0
divsd %xmm0, %xmm7
cmpeqsd %xmm0, %xmm2
movapd %xmm2, %xmm0
andnpd %xmm7, %xmm0
andpd %xmm1, %xmm2
orpd %xmm0, %xmm2
jmp 0x5a286
nop