inline clamp_by_boundaries() internal function