1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 11 // Assumption: minValue < maxValue 12 // Assumption: minValue <= rhs <= maxValue 13 // Assumption: minValue <= lhs <= maxValue 14 // Assumption: minValue >= 0 15 template <typename T, T minValue, T maxValue> euclidian_addition(T rhs,T lhs)16T euclidian_addition(T rhs, T lhs) 17 { 18 const T modulus = maxValue - minValue + 1; 19 T ret = rhs + lhs; 20 if (ret > maxValue) 21 ret -= modulus; 22 return ret; 23 } 24 25 // Assumption: minValue < maxValue 26 // Assumption: minValue <= rhs <= maxValue 27 // Assumption: minValue <= lhs <= maxValue 28 // Assumption: minValue >= 0 29 template <typename T, T minValue, T maxValue> euclidian_subtraction(T lhs,T rhs)30T euclidian_subtraction(T lhs, T rhs) 31 { 32 const T modulus = maxValue - minValue + 1; 33 T ret = lhs - rhs; 34 if (ret < minValue) 35 ret += modulus; 36 if (ret > maxValue) // this can happen if T is unsigned 37 ret += modulus; 38 return ret; 39 } 40