brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.2 KiB · 6e841a0 Raw
37 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9 10//  Assumption: minValue < maxValue11//  Assumption: minValue <= rhs <= maxValue12//  Assumption: minValue <= lhs <= maxValue13//  Assumption: minValue >= 014template <typename T, T minValue, T maxValue>15constexpr 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 < maxValue24//  Assumption: minValue <= rhs <= maxValue25//  Assumption: minValue <= lhs <= maxValue26//  Assumption: minValue >= 027template <typename T, T minValue, T maxValue>28constexpr 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 unsigned34    ret += modulus;35  return ret;36}37