// File: /workspace/github_cpp_makefile_star2_O2/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
movapd %xmm0, %xmm3
xorpd %xmm0, %xmm0
movsd %xmm3, 0x8(%rsp)
ucomisd %xmm3, %xmm0
jbe 0x5824d
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
ucomisd %xmm1, %xmm0
ja 0x5823e
movq %rdi, %rbx
movl 0xc0(%rdi), %eax
leal -0x1(%rax), %ebp
cvtsi2sd %ebp, %xmm3
movsd 0x8(%rsp), %xmm4
ucomisd %xmm3, %xmm4
ja 0x5823e
movl 0xc4(%rbx), %ecx
leal -0x1(%rcx), %r14d
cvtsi2sd %r14d, %xmm3
ucomisd %xmm3, %xmm1
ja 0x5823e
movsd 0x3af4c(%rip), %xmm0    # 0x931d8
mulsd %xmm2, %xmm0
movsd 0x3ad78(%rip), %xmm4    # 0x93010
addsd %xmm4, %xmm0
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm0
movsd 0x8(%rsp), %xmm5
movapd %xmm5, %xmm3
subsd %xmm0, %xmm3
addsd %xmm4, %xmm3
cvttsd2si %xmm3, %r8d
movapd %xmm0, %xmm3
addsd %xmm5, %xmm3
addsd %xmm4, %xmm3
cvttsd2si %xmm3, %edx
movapd %xmm1, %xmm3
subsd %xmm0, %xmm3
addsd %xmm4, %xmm3
cvttsd2si %xmm3, %esi
addsd %xmm1, %xmm0
addsd %xmm4, %xmm0
cvttsd2si %xmm0, %edi
xorl %r12d, %r12d
testl %r8d, %r8d
cmovlel %r12d, %r8d
movq %r8, 0x10(%rsp)
cmpl %edx, %eax
cmovgl %edx, %ebp
testl %esi, %esi
cmovgl %esi, %r12d
cmpl %edi, %ecx
cmovgl %edi, %r14d
movapd %xmm2, %xmm0
addsd %xmm2, %xmm0
mulsd %xmm2, %xmm0
movsd %xmm0, 0x18(%rsp)
xorpd %xmm3, %xmm3
xorpd %xmm2, %xmm2
movsd %xmm1, 0x20(%rsp)
cmpl %r14d, %r12d
jg 0x583f0
cvtsi2sd %r12d, %xmm0
movapd %xmm1, %xmm4
subsd %xmm0, %xmm4
mulsd %xmm4, %xmm4
movsd %xmm4, 0x28(%rsp)
movslq %r12d, %r13
movq 0x10(%rsp), %r15
cmpl %ebp, %r15d
jg 0x583e8
movq 0xb8(%rbx), %rax
movslq 0xc8(%rbx), %rcx
imulq %r13, %rcx
addq %r15, %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd 0x3bbee(%rip), %xmm0    # 0x93f68
jne 0x5837e
jnp 0x583e0
movapd %xmm2, 0x40(%rsp)
cvtsi2sd %r15d, %xmm2
movsd %xmm0, 0x30(%rsp)
movsd 0x8(%rsp), %xmm0
subsd %xmm2, %xmm0
mulsd %xmm0, %xmm0
addsd 0x28(%rsp), %xmm0
xorpd 0x3ac85(%rip), %xmm0    # 0x93030
divsd 0x18(%rsp), %xmm0
movsd %xmm3, 0x38(%rsp)
callq 0xe820
movsd 0x38(%rsp), %xmm3
movapd 0x40(%rsp), %xmm2
movsd 0x20(%rsp), %xmm1
movsd 0x30(%rsp), %xmm4
mulsd %xmm0, %xmm4
addsd %xmm4, %xmm2
addsd %xmm0, %xmm3
incq %r15
jmp 0x5834f
incl %r12d
jmp 0x58327
divsd %xmm3, %xmm2
xorpd %xmm0, %xmm0
cmpltsd %xmm3, %xmm0
andpd %xmm0, %xmm2
movsd 0x3bb5f(%rip), %xmm1    # 0x93f68
andnpd %xmm1, %xmm0
orpd %xmm2, %xmm0
jmp 0x5823e