// File: /workspace/github_cpp_makefile_star2_O1/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:
movsd (%rdi), %xmm4
movapd %xmm4, %xmm3
maxsd %xmm0, %xmm3
leal -0x1(%rcx), %eax
movslq %ecx, %r9
movsd -0x8(%rdi,%r9,8), %xmm5
movapd %xmm5, %xmm2
minsd %xmm3, %xmm2
movsd (%rsi), %xmm0
movapd %xmm0, %xmm6
maxsd %xmm1, %xmm6
movslq %r8d, %r9
movsd -0x8(%rsi,%r9,8), %xmm3
movapd %xmm3, %xmm1
minsd %xmm6, %xmm1
ucomisd %xmm5, %xmm2
jne 0x95bd
jp 0x95bd
movl %eax, %r9d
jmp 0x9614
xorl %r9d, %r9d
ucomisd %xmm4, %xmm2
jne 0x95cc
jp 0x95cc
xorl %eax, %eax
jmp 0x9614
cmpl $0x3, %ecx
jl 0x9614
xorl %ecx, %ecx
leal (%rax,%rcx), %r9d
movl %r9d, %r10d
shrl $0x1f, %r10d
addl %r9d, %r10d
sarl %r10d
movslq %r10d, %r9
movsd (%rdi,%r9,8), %xmm4
ucomisd %xmm2, %xmm4
cmovbel %r9d, %ecx
cmovael %r9d, %eax
ucomisd %xmm4, %xmm2
cmovnel %ecx, %r9d
cmovpl %ecx, %r9d
movl %eax, %r10d
subl %r9d, %r10d
movl %r9d, %ecx
cmpl $0x1, %r10d
jg 0x95d3
pushq %rbp
movq %rsp, %rbp
pushq %r14
pushq %rbx
leal -0x1(%r8), %ecx
ucomisd %xmm3, %xmm1
jne 0x962c
jp 0x962c
movl %ecx, %r10d
jmp 0x9681
xorl %r10d, %r10d
ucomisd %xmm0, %xmm1
jne 0x963b
jp 0x963b
xorl %ecx, %ecx
jmp 0x9681
cmpl $0x3, %r8d
jl 0x9681
xorl %r11d, %r11d
leal (%r11,%rcx), %r10d
movl %r10d, %ebx
shrl $0x1f, %ebx
addl %r10d, %ebx
sarl %ebx
movslq %ebx, %r10
movsd (%rsi,%r10,8), %xmm0
ucomisd %xmm1, %xmm0
cmovbel %r10d, %r11d
cmovael %r10d, %ecx
ucomisd %xmm0, %xmm1
cmovnel %r11d, %r10d
cmovpl %r11d, %r10d
movl %ecx, %ebx
subl %r10d, %ebx
movl %r10d, %r11d
cmpl $0x1, %ebx
jg 0x9644
movl %r9d, %r11d
imull %r8d, %r11d
leal (%r10,%r11), %ebx
addl %ecx, %r11d
cmpl %eax, %r9d
jne 0x96a6
movslq %ebx, %rax
movsd (%rdx,%rax,8), %xmm0
movslq %r11d, %rax
movsd (%rdx,%rax,8), %xmm3
jmp 0x970a
imull %eax, %r8d
leal (%rcx,%r8), %r14d
addl %r10d, %r8d
movslq %ebx, %rbx
movsd (%rdx,%rbx,8), %xmm3
movslq %r8d, %r8
movsd (%rdx,%r8,8), %xmm0
subsd %xmm3, %xmm0
movslq %r9d, %r8
movsd (%rdi,%r8,8), %xmm4
subsd %xmm4, %xmm2
mulsd %xmm2, %xmm0
cltq
movsd (%rdi,%rax,8), %xmm5
subsd %xmm4, %xmm5
divsd %xmm5, %xmm0
addsd %xmm3, %xmm0
movslq %r11d, %rax
movsd (%rdx,%rax,8), %xmm4
movslq %r14d, %rax
movsd (%rdx,%rax,8), %xmm3
subsd %xmm4, %xmm3
mulsd %xmm2, %xmm3
divsd %xmm5, %xmm3
addsd %xmm4, %xmm3
cmpl %ecx, %r10d
popq %rbx
popq %r14
popq %rbp
je 0x973b
subsd %xmm0, %xmm3
movslq %r10d, %rax
movsd (%rsi,%rax,8), %xmm2
subsd %xmm2, %xmm1
mulsd %xmm3, %xmm1
movslq %ecx, %rax
movsd (%rsi,%rax,8), %xmm3
subsd %xmm2, %xmm3
divsd %xmm3, %xmm1
addsd %xmm1, %xmm0
retq