We can use subtraction and std::min instead of modulo and explicit if statements. This commit does not change the behavior.