// File: /workspace/github_cpp_makefile_star2_O2/DavidLBruce[P]sim3dof/build/../src/MathUtils.cpp
// Function: lut2d(double*, double*, double*, int, int, double, double)
double lut2d( double *xlist,
              double *ylist,
              double *lut,
              int    x_dim,
              int    y_dim,
              double x,
              double y )
{
  int xu;
  int xl;
  int xm;

  int yu;
  int yl;
  int ym;

  int    i1;
  int    i2;
  int    i3;
  int    i4;
  double fxy1;
  double fxy2;

  if (x < xlist[0])
    x = xlist[0];
  if (x > xlist[x_dim-1])
    x = xlist[x_dim-1];
  if (y < ylist[0])
    y = ylist[0];
  if (y > ylist[y_dim-1])
    y = ylist[y_dim-1];

  xu = x_dim - 1;
  xl = 0;
  xm = (xu + xl) / 2;

  if (x == xlist[xu])
    xl = xu;
  else if (x == xlist[xl])
    xu = xl;
  else {
    while (xu - xl > 1) {
      xm = (xu + xl) / 2;
      if (xlist[xm] == x)
        xl = xu = xm;
      else if (xlist[xm] > x)
        xu = xm;
      else xl = xm;
    }
  }

  yu = y_dim - 1;
  yl = 0;
  ym = (yu + yl) / 2;

  if (y == ylist[yu])
    yl = yu;
  else if (y == ylist[yl])
    yu = yl;
  else {
    while (yu - yl>1) {
      ym = (yu + yl) / 2;
      if (ylist[ym] == y)
        yl = yu = ym;
      else if (ylist[ym] > y)
        yu = ym;
      else
        yl = ym;
    }
  }

  i1 = xl * y_dim + yl;
  i2 = xl * y_dim + yu;
  i3 = xu * y_dim + yl;
  i4 = xu * y_dim + yu;

  if (xl == xu) {
    fxy1 = lut[i1];
    fxy2 = lut[i2];
  } else {
    fxy1 = lut[i1] + (lut[i3] - lut[i1]) * (x -xlist[xl]) / (xlist[xu] - xlist[xl]);
    fxy2 = lut[i2] + (lut[i4] - lut[i2]) * (x - xlist[xl])/(xlist[xu]-xlist[xl]);
  }

  if (yl==yu)
    return fxy1;
  else
    return fxy1 + (fxy2 - fxy1)*(y - ylist[yl])/(ylist[yu] - ylist[yl]);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r14
pushq %rbx
movq %rdx, %r9
movsd (%rdi), %xmm4
movapd %xmm4, %xmm3
maxsd %xmm0, %xmm3
movslq %ecx, %rax
decl %ecx
movsd -0x8(%rdi,%rax,8), %xmm5
movapd %xmm5, %xmm2
minsd %xmm3, %xmm2
movsd (%rsi), %xmm0
movapd %xmm0, %xmm6
maxsd %xmm1, %xmm6
movslq %r8d, %rax
movsd -0x8(%rsi,%rax,8), %xmm3
movapd %xmm3, %xmm1
minsd %xmm6, %xmm1
ucomisd %xmm5, %xmm2
movl $0x2, %r11d
jne 0x938b
jp 0x938b
movl %ecx, %r10d
movl %ecx, %ebx
jmp 0x939b
xorl %r10d, %r10d
ucomisd %xmm4, %xmm2
movl $0x0, %ebx
jne 0x93f2
jp 0x93f2
leal -0x1(%r8), %r14d
ucomisd %xmm3, %xmm1
jne 0x93af
jp 0x93af
movl %r14d, %eax
movl %r14d, %ecx
jmp 0x942d
xorl %eax, %eax
ucomisd %xmm0, %xmm1
movl $0x0, %ecx
jne 0x93be
jnp 0x942d
xorl %ecx, %ecx
movl %r14d, %eax
subl %ecx, %eax
cmpl $0x2, %eax
jl 0x942a
leal (%rcx,%r14), %eax
cltd
idivl %r11d
cltq
movsd (%rsi,%rax,8), %xmm0
ucomisd %xmm1, %xmm0
cmovbel %eax, %ecx
cmovael %eax, %r14d
ucomisd %xmm0, %xmm1
cmovnel %ecx, %eax
cmovpl %ecx, %eax
movl %eax, %ecx
jmp 0x93c0
xorl %r10d, %r10d
movl %ecx, %eax
subl %r10d, %eax
cmpl $0x2, %eax
jl 0x9387
leal (%rcx,%r10), %eax
cltd
idivl %r11d
cltq
movsd (%rdi,%rax,8), %xmm4
ucomisd %xmm2, %xmm4
cmovbel %eax, %r10d
cmovael %eax, %ecx
ucomisd %xmm4, %xmm2
cmovnel %r10d, %eax
cmovpl %r10d, %eax
movl %eax, %r10d
jmp 0x93f5
movl %r14d, %eax
movl %r10d, %edx
imull %r8d, %edx
leal (%rcx,%rdx), %r11d
addl %eax, %edx
cmpl %ebx, %r10d
jne 0x9453
movslq %r11d, %rdi
movsd (%r9,%rdi,8), %xmm0
movslq %edx, %rdx
movsd (%r9,%rdx,8), %xmm3
jmp 0x94bc
imull %ebx, %r8d
leal (%rax,%r8), %r14d
addl %ecx, %r8d
movslq %r11d, %r11
movsd (%r9,%r11,8), %xmm3
movslq %r8d, %r8
movsd (%r9,%r8,8), %xmm0
subsd %xmm3, %xmm0
movslq %r10d, %r8
movsd (%rdi,%r8,8), %xmm4
subsd %xmm4, %xmm2
mulsd %xmm2, %xmm0
movslq %ebx, %r8
movsd (%rdi,%r8,8), %xmm5
subsd %xmm4, %xmm5
divsd %xmm5, %xmm0
addsd %xmm3, %xmm0
movslq %edx, %rdx
movsd (%r9,%rdx,8), %xmm4
movslq %r14d, %rdx
movsd (%r9,%rdx,8), %xmm3
subsd %xmm4, %xmm3
mulsd %xmm2, %xmm3
divsd %xmm5, %xmm3
addsd %xmm4, %xmm3
cmpl %eax, %ecx
je 0x94e7
subsd %xmm0, %xmm3
movslq %ecx, %rcx
movsd (%rsi,%rcx,8), %xmm2
subsd %xmm2, %xmm1
mulsd %xmm3, %xmm1
cltq
movsd (%rsi,%rax,8), %xmm3
subsd %xmm2, %xmm3
divsd %xmm3, %xmm1
addsd %xmm1, %xmm0
popq %rbx
popq %r14
popq %rbp
retq