
      subroutine tableintrp (xx, n, x, jlo)
c
      implicit none
c
c ----------------------------------------------------------------------
c --- correlated table search routine. use jlo from previous call to
c --- get jlo for current value of x. if jlo from previous call is
c --- no good(jlo=0 or jlo=n) then do a binary search.
c --- this routine returns jlo so that
c   xx(jlo) .le. x .le. xx(jlo+1) if jlo = 1
c   xx(jlo) .lt. x .le. xx(jlo+1) if jlo =2,3...n-1
c   it is assumed that xx(j),j = 1,2..n is monotonically
c   increasing; this is NOT checked for.
c   this is a modified version of the Numerical Recipes subroutine HUNT
c ------------------------------------------------------------------ HSJ
c
      integer  n, jlo, jhi, jmid, inc
      real*8   x, xx(n)
c
      if      (x .lt. xx(1)) then
        jlo = 0                     ! indicates out of range below xx(1)
      else if (x .le. xx(2)) then
        jlo = 1                     ! xx(1) .le. x .le. xx(2)
      else if (x .le. xx(n)) then   ! xx(2) .lt. x .le. xx(n)
c
c       check if jlo from previous call is usable:
c
        if (jlo .eq. 0 .or. jlo .eq. n) then
          jlo = 2
          jhi = n
          go to 15           ! no correlation go directly to bisection
        else                 ! 1 .le. jlo .lt. n
c
c         bracket x, then use bisection:
c         start with  jlo  from previous call:
c
          inc = 1
          if (x .gt. xx(jlo)) then    ! search up
    4       jhi = jlo + inc
            if (jhi .ge. n) then
              jhi = n
            else if (x .gt. xx(jhi)) then
              inc = inc + inc
              jlo = jhi
              go to 4
            end if
          else
    5       jhi = jlo
            jlo = jlo-inc
            if (jlo .le. 1) then
              jlo = 1
            else if (x .le. xx(jlo)) then
              inc = inc + inc
              go to 5
            end if
          end if
        end if
c
c         bisection:
c
   10     if (jhi-jlo .eq. 1)  return
   15     jmid = (jhi + jlo) / 2
          if (x .gt. xx(jmid)) then
            jlo = jmid
          else           ! x .le. xx(jmid)
            jhi = jmid
          end if
          go to 10
c
      else               ! x .gt. xx(n)
        jlo = n
      end if
c
      return
c
      end
