Marshall Clow | 5b08c17 | 2018-10-16 17:27:54 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Marshall Clow | 5b08c17 | 2018-10-16 17:27:54 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | |
| 10 | // Assumption: minValue < maxValue |
| 11 | // Assumption: minValue <= rhs <= maxValue |
| 12 | // Assumption: minValue <= lhs <= maxValue |
| 13 | // Assumption: minValue >= 0 |
| 14 | template <typename T, T minValue, T maxValue> |
| 15 | T euclidian_addition(T rhs, T lhs) |
| 16 | { |
| 17 | const T modulus = maxValue - minValue + 1; |
| 18 | T ret = rhs + lhs; |
Stephan T. Lavavej | dec8905 | 2018-11-14 03:06:06 +0000 | [diff] [blame] | 19 | if (ret > maxValue) |
Marshall Clow | 5b08c17 | 2018-10-16 17:27:54 +0000 | [diff] [blame] | 20 | ret -= modulus; |
| 21 | return ret; |
| 22 | } |
| 23 | |
| 24 | // Assumption: minValue < maxValue |
| 25 | // Assumption: minValue <= rhs <= maxValue |
| 26 | // Assumption: minValue <= lhs <= maxValue |
| 27 | // Assumption: minValue >= 0 |
| 28 | template <typename T, T minValue, T maxValue> |
| 29 | T euclidian_subtraction(T lhs, T rhs) |
| 30 | { |
| 31 | const T modulus = maxValue - minValue + 1; |
| 32 | T ret = lhs - rhs; |
Stephan T. Lavavej | dec8905 | 2018-11-14 03:06:06 +0000 | [diff] [blame] | 33 | if (ret < minValue) |
Marshall Clow | 5b08c17 | 2018-10-16 17:27:54 +0000 | [diff] [blame] | 34 | ret += modulus; |
| 35 | if (ret > maxValue) // this can happen if T is unsigned |
| 36 | ret += modulus; |
| 37 | return ret; |
| 38 | } |