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

  // Determine Gaussian filter extent
  int f = (int) (3 * sigma + 0.5);
  int xmin = (int) (x - f + 0.5);
  int xmax = (int) (x + f + 0.5);
  int ymin = (int) (y - f + 0.5);
  int ymax = (int) (y + f + 0.5);
  if (xmin < 0) xmin = 0;
  if (xmax > XResolution()-1) xmax = XResolution()-1;
  if (ymin < 0) ymin = 0;
  if (ymax > YResolution()-1) ymax = YResolution()-1;

  // Filter sample with Gaussian
  RNScalar sum = 0.0;
  RNScalar weight = 0.0;
  double denom = 2.0 * sigma * sigma;
  for (int j = ymin; j <= ymax; j++) {
    for (int i = xmin; i <= xmax; i++) {
      RNScalar value = GridValue(i, j);
      if (value != R2_GRID_UNKNOWN_VALUE) { 
        RNScalar dx = x - i;
        RNScalar dy = y - j;
        RNScalar d = dx*dx + dy*dy;
        RNScalar w = exp(-d / denom);
        sum += w * value;
        weight += w;
      }
    }
  }

  // Normalize value based on total weight of Gaussian samples
  if (weight > 0) return sum / weight;
  else return R2_GRID_UNKNOWN_VALUE;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
xorpd %xmm3, %xmm3
movsd %xmm0, (%rsp)
ucomisd %xmm0, %xmm3
xorpd %xmm0, %xmm0
jbe 0x60500
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
ucomisd %xmm1, %xmm0
ja 0x604f1
movq %rdi, %rbx
movsd %xmm1, 0x8(%rsp)
movl 0xc0(%rdi), %eax
leal -0x1(%rax), %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
movsd (%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x606f1
movq %rcx, %rdi
movl 0xc4(%rbx), %ecx
leal -0x1(%rcx), %ebp
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movsd 0x8(%rsp), %xmm4
ucomisd %xmm0, %xmm4
xorpd %xmm0, %xmm0
ja 0x604f1
movsd 0x41c7f(%rip), %xmm0    # 0xa21d8
mulsd %xmm2, %xmm0
movsd 0x41acb(%rip), %xmm1    # 0xa2030
addsd %xmm1, %xmm0
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm0
movsd (%rsp), %xmm3
subsd %xmm0, %xmm3
addsd %xmm1, %xmm3
cvttsd2si %xmm3, %r14d
movapd %xmm4, %xmm3
subsd %xmm0, %xmm3
addsd %xmm1, %xmm3
cvttsd2si %xmm3, %edx
movapd %xmm0, %xmm3
addsd %xmm4, %xmm3
addsd %xmm1, %xmm3
cvttsd2si %xmm3, %esi
xorl %r12d, %r12d
testl %r14d, %r14d
cmovlel %r12d, %r14d
testl %edx, %edx
cmovgl %edx, %r12d
cmpl %esi, %ecx
cmovgl %esi, %ebp
xorpd %xmm4, %xmm4
xorpd %xmm3, %xmm3
cmpl %ebp, %r12d
jg 0x606c7
movapd %xmm2, %xmm1
addsd %xmm2, %xmm1
mulsd %xmm2, %xmm1
movsd %xmm1, 0x18(%rsp)
addsd (%rsp), %xmm0
addsd 0x41a48(%rip), %xmm0    # 0xa2030
cvttsd2si %xmm0, %ecx
cmpl %ecx, %eax
cmovgl %ecx, %edi
movq %rdi, 0x10(%rsp)
leal 0x1(%rdi), %r13d
cmpl 0x10(%rsp), %r14d
jg 0x606b6
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movsd 0x8(%rsp), %xmm1
subsd %xmm0, %xmm1
mulsd %xmm1, %xmm1
movsd %xmm1, 0x20(%rsp)
movl %r14d, %r15d
movq 0xb8(%rbx), %rax
movl 0xc8(%rbx), %ecx
imull %r12d, %ecx
addl %r15d, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd 0x42970(%rip), %xmm0    # 0xa2fb8
jne 0x6064c
jnp 0x606aa
xorps %xmm1, %xmm1
cvtsi2sd %r15d, %xmm1
movsd %xmm0, 0x28(%rsp)
movsd (%rsp), %xmm0
subsd %xmm1, %xmm0
mulsd %xmm0, %xmm0
addsd 0x20(%rsp), %xmm0
xorpd 0x419ab(%rip), %xmm0    # 0xa2020
divsd 0x18(%rsp), %xmm0
movapd %xmm3, 0x40(%rsp)
movapd %xmm4, 0x30(%rsp)
callq 0xe7f0
movapd 0x30(%rsp), %xmm4
movapd 0x40(%rsp), %xmm3
movsd 0x28(%rsp), %xmm1
mulsd %xmm0, %xmm1
addsd %xmm1, %xmm3
addsd %xmm0, %xmm4
incl %r15d
cmpl %r15d, %r13d
jne 0x60624
leal 0x1(%r12), %eax
cmpl %ebp, %r12d
movl %eax, %r12d
jne 0x605fa
divsd %xmm4, %xmm3
xorpd %xmm1, %xmm1
cmpltsd %xmm4, %xmm1
andpd %xmm1, %xmm3
movsd 0x428d8(%rip), %xmm0    # 0xa2fb8
andnpd %xmm0, %xmm1
movapd %xmm1, %xmm0
orpd %xmm3, %xmm0
jmp 0x604f1
xorpd %xmm0, %xmm0
jmp 0x604f1