| 1 |
//===----------------------------------------------------------------------===// |
| 2 |
// |
| 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 |
| 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 |
constexpr T euclidian_addition(T rhs, T lhs) { |
| 16 |
const T modulus = maxValue - minValue + 1; |
| 17 |
T ret = rhs + lhs; |
| 18 |
if (ret > maxValue) |
| 19 |
ret -= modulus; |
| 20 |
return ret; |
| 21 |
} |
| 22 |
|
| 23 |
// Assumption: minValue < maxValue |
| 24 |
// Assumption: minValue <= rhs <= maxValue |
| 25 |
// Assumption: minValue <= lhs <= maxValue |
| 26 |
// Assumption: minValue >= 0 |
| 27 |
template <typename T, T minValue, T maxValue> |
| 28 |
constexpr T euclidian_subtraction(T lhs, T rhs) { |
| 29 |
const T modulus = maxValue - minValue + 1; |
| 30 |
T ret = lhs - rhs; |
| 31 |
if (ret < minValue) |
| 32 |
ret += modulus; |
| 33 |
if (ret > maxValue) // this can happen if T is unsigned |
| 34 |
ret += modulus; |
| 35 |
return ret; |
| 36 |
} |