// File: 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
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movl %ecx, -0x24(%rbp)
movl %r8d, -0x28(%rbp)
movsd %xmm0, -0x30(%rbp)
movsd %xmm1, -0x38(%rbp)
movsd -0x30(%rbp), %xmm1
movq -0x10(%rbp), %rax
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xc661
movq -0x10(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x30(%rbp)
movsd -0x30(%rbp), %xmm0
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
ucomisd (%rax,%rcx,8), %xmm0
jbe 0xc691
movq -0x10(%rbp), %rax
movl -0x24(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x30(%rbp)
movsd -0x38(%rbp), %xmm1
movq -0x18(%rbp), %rax
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0xc6b1
movq -0x18(%rbp), %rax
movsd (%rax), %xmm0
movsd %xmm0, -0x38(%rbp)
movsd -0x38(%rbp), %xmm0
movq -0x18(%rbp), %rax
movl -0x28(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
ucomisd (%rax,%rcx,8), %xmm0
jbe 0xc6e1
movq -0x18(%rbp), %rax
movl -0x28(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x38(%rbp)
movl -0x24(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x3c(%rbp)
movl $0x0, -0x40(%rbp)
movl -0x3c(%rbp), %eax
addl -0x40(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, -0x44(%rbp)
movsd -0x30(%rbp), %xmm0
movq -0x10(%rbp), %rax
movslq -0x3c(%rbp), %rcx
ucomisd (%rax,%rcx,8), %xmm0
jne 0xc723
jp 0xc723
movl -0x3c(%rbp), %eax
movl %eax, -0x40(%rbp)
jmp 0xc7aa
movsd -0x30(%rbp), %xmm0
movq -0x10(%rbp), %rax
movslq -0x40(%rbp), %rcx
ucomisd (%rax,%rcx,8), %xmm0
jne 0xc741
jp 0xc741
movl -0x40(%rbp), %eax
movl %eax, -0x3c(%rbp)
jmp 0xc7a8
jmp 0xc743
movl -0x3c(%rbp), %eax
subl -0x40(%rbp), %eax
cmpl $0x1, %eax
jle 0xc7a6
movl -0x3c(%rbp), %eax
addl -0x40(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, -0x44(%rbp)
movq -0x10(%rbp), %rax
movslq -0x44(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd -0x30(%rbp), %xmm0
jne 0xc780
jp 0xc780
movl -0x44(%rbp), %eax
movl %eax, -0x3c(%rbp)
movl %eax, -0x40(%rbp)
jmp 0xc7a4
movq -0x10(%rbp), %rax
movslq -0x44(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd -0x30(%rbp), %xmm0
jbe 0xc79c
movl -0x44(%rbp), %eax
movl %eax, -0x3c(%rbp)
jmp 0xc7a2
movl -0x44(%rbp), %eax
movl %eax, -0x40(%rbp)
jmp 0xc7a4
jmp 0xc743
jmp 0xc7a8
jmp 0xc7aa
movl -0x28(%rbp), %eax
subl $0x1, %eax
movl %eax, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movl -0x48(%rbp), %eax
addl -0x4c(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, -0x50(%rbp)
movsd -0x38(%rbp), %xmm0
movq -0x18(%rbp), %rax
movslq -0x48(%rbp), %rcx
ucomisd (%rax,%rcx,8), %xmm0
jne 0xc7ec
jp 0xc7ec
movl -0x48(%rbp), %eax
movl %eax, -0x4c(%rbp)
jmp 0xc873
movsd -0x38(%rbp), %xmm0
movq -0x18(%rbp), %rax
movslq -0x4c(%rbp), %rcx
ucomisd (%rax,%rcx,8), %xmm0
jne 0xc80a
jp 0xc80a
movl -0x4c(%rbp), %eax
movl %eax, -0x48(%rbp)
jmp 0xc871
jmp 0xc80c
movl -0x48(%rbp), %eax
subl -0x4c(%rbp), %eax
cmpl $0x1, %eax
jle 0xc86f
movl -0x48(%rbp), %eax
addl -0x4c(%rbp), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, -0x50(%rbp)
movq -0x18(%rbp), %rax
movslq -0x50(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd -0x38(%rbp), %xmm0
jne 0xc849
jp 0xc849
movl -0x50(%rbp), %eax
movl %eax, -0x48(%rbp)
movl %eax, -0x4c(%rbp)
jmp 0xc86d
movq -0x18(%rbp), %rax
movslq -0x50(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
ucomisd -0x38(%rbp), %xmm0
jbe 0xc865
movl -0x50(%rbp), %eax
movl %eax, -0x48(%rbp)
jmp 0xc86b
movl -0x50(%rbp), %eax
movl %eax, -0x4c(%rbp)
jmp 0xc86d
jmp 0xc80c
jmp 0xc871
jmp 0xc873
movl -0x40(%rbp), %eax
imull -0x28(%rbp), %eax
addl -0x4c(%rbp), %eax
movl %eax, -0x54(%rbp)
movl -0x40(%rbp), %eax
imull -0x28(%rbp), %eax
addl -0x48(%rbp), %eax
movl %eax, -0x58(%rbp)
movl -0x3c(%rbp), %eax
imull -0x28(%rbp), %eax
addl -0x4c(%rbp), %eax
movl %eax, -0x5c(%rbp)
movl -0x3c(%rbp), %eax
imull -0x28(%rbp), %eax
addl -0x48(%rbp), %eax
movl %eax, -0x60(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x3c(%rbp), %eax
jne 0xc8d8
movq -0x20(%rbp), %rax
movslq -0x54(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x68(%rbp)
movq -0x20(%rbp), %rax
movslq -0x58(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, -0x70(%rbp)
jmp 0xc9a0
movq -0x20(%rbp), %rax
movslq -0x54(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x20(%rbp), %rax
movslq -0x5c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
movq -0x20(%rbp), %rax
movslq -0x54(%rbp), %rcx
subsd (%rax,%rcx,8), %xmm1
movsd -0x30(%rbp), %xmm2
movq -0x10(%rbp), %rax
movslq -0x40(%rbp), %rcx
subsd (%rax,%rcx,8), %xmm2
mulsd %xmm2, %xmm1
movq -0x10(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movq -0x10(%rbp), %rax
movslq -0x40(%rbp), %rcx
subsd (%rax,%rcx,8), %xmm2
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, -0x68(%rbp)
movq -0x20(%rbp), %rax
movslq -0x58(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm0
movq -0x20(%rbp), %rax
movslq -0x60(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm1
movq -0x20(%rbp), %rax
movslq -0x58(%rbp), %rcx
subsd (%rax,%rcx,8), %xmm1
movsd -0x30(%rbp), %xmm2
movq -0x10(%rbp), %rax
movslq -0x40(%rbp), %rcx
subsd (%rax,%rcx,8), %xmm2
mulsd %xmm2, %xmm1
movq -0x10(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movq -0x10(%rbp), %rax
movslq -0x40(%rbp), %rcx
subsd (%rax,%rcx,8), %xmm2
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, -0x70(%rbp)
movl -0x4c(%rbp), %eax
cmpl -0x48(%rbp), %eax
jne 0xc9b4
movsd -0x68(%rbp), %xmm0
movsd %xmm0, -0x8(%rbp)
jmp 0xca00
movsd -0x68(%rbp), %xmm0
movsd -0x70(%rbp), %xmm1
subsd -0x68(%rbp), %xmm1
movsd -0x38(%rbp), %xmm2
movq -0x18(%rbp), %rax
movslq -0x4c(%rbp), %rcx
subsd (%rax,%rcx,8), %xmm2
mulsd %xmm2, %xmm1
movq -0x18(%rbp), %rax
movslq -0x48(%rbp), %rcx
movsd (%rax,%rcx,8), %xmm2
movq -0x18(%rbp), %rax
movslq -0x4c(%rbp), %rcx
subsd (%rax,%rcx,8), %xmm2
divsd %xmm2, %xmm1
addsd %xmm1, %xmm0
movsd %xmm0, -0x8(%rbp)
movsd -0x8(%rbp), %xmm0
popq %rbp
retq
nopw (%rax,%rax)