      integer function ibsearch(a0,k,tabbm) 
c bernd berg oct 3 2008.
c binary search to position a0 on the discretization grid.
      include '../../libs/fortran/implicit.sta'
      include '../../libs/fortran/constants.par'
      include '../../libs/fortran_par/bmha.par'
      parameter(n2max=13, i2p12=2**12,i2p11=2**11,i2p10=2**10,i2p9=2**9,
     &      i2p8=2**8,i2p7=2**2,i2p6=2**6,i2p5=2**5,i2p4=2**4,i2p3=2**3)
      dimension tabbm(nbm2,nbm1),ip2(1+n2log2-n2max:n2log2)
      data ip2/i2p12,i2p11,i2p10,i2p9,i2p8,i2p7,i2p6,i2p5,
     &         i2p4,i2p3,4,2,1/
      save ip2
      ii=ip2(1)
      do i=2,n2log2
        ii=ii+ip2(i)*sign(one,a0-tabbm(ii,k))
      end do
      if(tabbm(ii,k).ge.a0) then
        ibsearch=ii
      else
        ibsearch=ii+1
      end if
      return
      end
