Number of mirrors at the edges of the interval, until it is set in the interval

I have the following simple function

mirror (index int, int lB, int uB)
while (index < lB || index >= uB)
yes < lB){index = lB + (lB-index);}
        if(index >= uB) {index = uB-1 - (index-uB);}
return index;

It is used for each index to give a mirror edge behavior and ensure that the result is a valid index.

Are there ways to improve this, with or without changing the result for external indices? [lB-duB+d)[lB-duB+d)[lB-duB+d)[lB-duB+d) ,where d> = uB-lB.

mirror (i, lB, uB) it has to be in [LBuB)[lBuB)[LBuB)[lBuB) Of course.

What are the concerns about performance?

  • this function is called very often
  • it is called from a kernel (do the relevant things? divergence of the thread?)

Are there possible improvements for the special case? lB == 0?