template function for bsf/bsr