mod_ops.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef RTC_BASE_NUMERICS_MOD_OPS_H_
  11. #define RTC_BASE_NUMERICS_MOD_OPS_H_
  12. #include <algorithm>
  13. #include <type_traits>
  14. #include "rtc_base/checks.h"
  15. namespace webrtc {
  16. template <unsigned long M> // NOLINT
  17. inline unsigned long Add(unsigned long a, unsigned long b) { // NOLINT
  18. RTC_DCHECK_LT(a, M);
  19. unsigned long t = M - b % M; // NOLINT
  20. unsigned long res = a - t; // NOLINT
  21. if (t > a)
  22. return res + M;
  23. return res;
  24. }
  25. template <unsigned long M> // NOLINT
  26. inline unsigned long Subtract(unsigned long a, unsigned long b) { // NOLINT
  27. RTC_DCHECK_LT(a, M);
  28. unsigned long sub = b % M; // NOLINT
  29. if (a < sub)
  30. return M - (sub - a);
  31. return a - sub;
  32. }
  33. // Calculates the forward difference between two wrapping numbers.
  34. //
  35. // Example:
  36. // uint8_t x = 253;
  37. // uint8_t y = 2;
  38. //
  39. // ForwardDiff(x, y) == 5
  40. //
  41. // 252 253 254 255 0 1 2 3
  42. // #################################################
  43. // | | x | | | | | y | |
  44. // #################################################
  45. // |----->----->----->----->----->
  46. //
  47. // ForwardDiff(y, x) == 251
  48. //
  49. // 252 253 254 255 0 1 2 3
  50. // #################################################
  51. // | | x | | | | | y | |
  52. // #################################################
  53. // -->-----> |----->---
  54. //
  55. // If M > 0 then wrapping occurs at M, if M == 0 then wrapping occurs at the
  56. // largest value representable by T.
  57. template <typename T, T M>
  58. inline typename std::enable_if<(M > 0), T>::type ForwardDiff(T a, T b) {
  59. static_assert(std::is_unsigned<T>::value,
  60. "Type must be an unsigned integer.");
  61. RTC_DCHECK_LT(a, M);
  62. RTC_DCHECK_LT(b, M);
  63. return a <= b ? b - a : M - (a - b);
  64. }
  65. template <typename T, T M>
  66. inline typename std::enable_if<(M == 0), T>::type ForwardDiff(T a, T b) {
  67. static_assert(std::is_unsigned<T>::value,
  68. "Type must be an unsigned integer.");
  69. return b - a;
  70. }
  71. template <typename T>
  72. inline T ForwardDiff(T a, T b) {
  73. return ForwardDiff<T, 0>(a, b);
  74. }
  75. // Calculates the reverse difference between two wrapping numbers.
  76. //
  77. // Example:
  78. // uint8_t x = 253;
  79. // uint8_t y = 2;
  80. //
  81. // ReverseDiff(y, x) == 5
  82. //
  83. // 252 253 254 255 0 1 2 3
  84. // #################################################
  85. // | | x | | | | | y | |
  86. // #################################################
  87. // <-----<-----<-----<-----<-----|
  88. //
  89. // ReverseDiff(x, y) == 251
  90. //
  91. // 252 253 254 255 0 1 2 3
  92. // #################################################
  93. // | | x | | | | | y | |
  94. // #################################################
  95. // ---<-----| |<-----<--
  96. //
  97. // If M > 0 then wrapping occurs at M, if M == 0 then wrapping occurs at the
  98. // largest value representable by T.
  99. template <typename T, T M>
  100. inline typename std::enable_if<(M > 0), T>::type ReverseDiff(T a, T b) {
  101. static_assert(std::is_unsigned<T>::value,
  102. "Type must be an unsigned integer.");
  103. RTC_DCHECK_LT(a, M);
  104. RTC_DCHECK_LT(b, M);
  105. return b <= a ? a - b : M - (b - a);
  106. }
  107. template <typename T, T M>
  108. inline typename std::enable_if<(M == 0), T>::type ReverseDiff(T a, T b) {
  109. static_assert(std::is_unsigned<T>::value,
  110. "Type must be an unsigned integer.");
  111. return a - b;
  112. }
  113. template <typename T>
  114. inline T ReverseDiff(T a, T b) {
  115. return ReverseDiff<T, 0>(a, b);
  116. }
  117. // Calculates the minimum distance between to wrapping numbers.
  118. //
  119. // The minimum distance is defined as min(ForwardDiff(a, b), ReverseDiff(a, b))
  120. template <typename T, T M = 0>
  121. inline T MinDiff(T a, T b) {
  122. static_assert(std::is_unsigned<T>::value,
  123. "Type must be an unsigned integer.");
  124. return std::min(ForwardDiff<T, M>(a, b), ReverseDiff<T, M>(a, b));
  125. }
  126. } // namespace webrtc
  127. #endif // RTC_BASE_NUMERICS_MOD_OPS_H_