• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
euclidian_addition(T rhs,T lhs)15 T euclidian_addition(T rhs, T lhs)
16 {
17     const T modulus = maxValue - minValue + 1;
18     T ret = rhs + lhs;
19     if (ret > maxValue)
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>
euclidian_subtraction(T lhs,T rhs)29 T euclidian_subtraction(T lhs, T rhs)
30 {
31     const T modulus = maxValue - minValue + 1;
32     T ret = lhs - rhs;
33     if (ret < minValue)
34         ret += modulus;
35     if (ret > maxValue)     // this can happen if T is unsigned
36         ret += modulus;
37     return ret;
38 }
39