safe_compare.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright 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. // This file defines six constexpr functions:
  11. //
  12. // rtc::SafeEq // ==
  13. // rtc::SafeNe // !=
  14. // rtc::SafeLt // <
  15. // rtc::SafeLe // <=
  16. // rtc::SafeGt // >
  17. // rtc::SafeGe // >=
  18. //
  19. // They each accept two arguments of arbitrary types, and in almost all cases,
  20. // they simply call the appropriate comparison operator. However, if both
  21. // arguments are integers, they don't compare them using C++'s quirky rules,
  22. // but instead adhere to the true mathematical definitions. It is as if the
  23. // arguments were first converted to infinite-range signed integers, and then
  24. // compared, although of course nothing expensive like that actually takes
  25. // place. In practice, for signed/signed and unsigned/unsigned comparisons and
  26. // some mixed-signed comparisons with a compile-time constant, the overhead is
  27. // zero; in the remaining cases, it is just a few machine instructions (no
  28. // branches).
  29. #ifndef RTC_BASE_NUMERICS_SAFE_COMPARE_H_
  30. #define RTC_BASE_NUMERICS_SAFE_COMPARE_H_
  31. #include <stddef.h>
  32. #include <stdint.h>
  33. #include <type_traits>
  34. #include <utility>
  35. #include "rtc_base/type_traits.h"
  36. namespace rtc {
  37. namespace safe_cmp_impl {
  38. template <size_t N>
  39. struct LargerIntImpl : std::false_type {};
  40. template <>
  41. struct LargerIntImpl<sizeof(int8_t)> : std::true_type {
  42. using type = int16_t;
  43. };
  44. template <>
  45. struct LargerIntImpl<sizeof(int16_t)> : std::true_type {
  46. using type = int32_t;
  47. };
  48. template <>
  49. struct LargerIntImpl<sizeof(int32_t)> : std::true_type {
  50. using type = int64_t;
  51. };
  52. // LargerInt<T1, T2>::value is true iff there's a signed type that's larger
  53. // than T1 (and no larger than the larger of T2 and int*, for performance
  54. // reasons); and if there is such a type, LargerInt<T1, T2>::type is an alias
  55. // for it.
  56. template <typename T1, typename T2>
  57. struct LargerInt
  58. : LargerIntImpl<sizeof(T1) < sizeof(T2) || sizeof(T1) < sizeof(int*)
  59. ? sizeof(T1)
  60. : 0> {};
  61. template <typename T>
  62. constexpr typename std::make_unsigned<T>::type MakeUnsigned(T a) {
  63. return static_cast<typename std::make_unsigned<T>::type>(a);
  64. }
  65. // Overload for when both T1 and T2 have the same signedness.
  66. template <typename Op,
  67. typename T1,
  68. typename T2,
  69. typename std::enable_if<std::is_signed<T1>::value ==
  70. std::is_signed<T2>::value>::type* = nullptr>
  71. constexpr bool Cmp(T1 a, T2 b) {
  72. return Op::Op(a, b);
  73. }
  74. // Overload for signed - unsigned comparison that can be promoted to a bigger
  75. // signed type.
  76. template <typename Op,
  77. typename T1,
  78. typename T2,
  79. typename std::enable_if<std::is_signed<T1>::value &&
  80. std::is_unsigned<T2>::value &&
  81. LargerInt<T2, T1>::value>::type* = nullptr>
  82. constexpr bool Cmp(T1 a, T2 b) {
  83. return Op::Op(a, static_cast<typename LargerInt<T2, T1>::type>(b));
  84. }
  85. // Overload for unsigned - signed comparison that can be promoted to a bigger
  86. // signed type.
  87. template <typename Op,
  88. typename T1,
  89. typename T2,
  90. typename std::enable_if<std::is_unsigned<T1>::value &&
  91. std::is_signed<T2>::value &&
  92. LargerInt<T1, T2>::value>::type* = nullptr>
  93. constexpr bool Cmp(T1 a, T2 b) {
  94. return Op::Op(static_cast<typename LargerInt<T1, T2>::type>(a), b);
  95. }
  96. // Overload for signed - unsigned comparison that can't be promoted to a bigger
  97. // signed type.
  98. template <typename Op,
  99. typename T1,
  100. typename T2,
  101. typename std::enable_if<std::is_signed<T1>::value &&
  102. std::is_unsigned<T2>::value &&
  103. !LargerInt<T2, T1>::value>::type* = nullptr>
  104. constexpr bool Cmp(T1 a, T2 b) {
  105. return a < 0 ? Op::Op(-1, 0) : Op::Op(safe_cmp_impl::MakeUnsigned(a), b);
  106. }
  107. // Overload for unsigned - signed comparison that can't be promoted to a bigger
  108. // signed type.
  109. template <typename Op,
  110. typename T1,
  111. typename T2,
  112. typename std::enable_if<std::is_unsigned<T1>::value &&
  113. std::is_signed<T2>::value &&
  114. !LargerInt<T1, T2>::value>::type* = nullptr>
  115. constexpr bool Cmp(T1 a, T2 b) {
  116. return b < 0 ? Op::Op(0, -1) : Op::Op(a, safe_cmp_impl::MakeUnsigned(b));
  117. }
  118. #define RTC_SAFECMP_MAKE_OP(name, op) \
  119. struct name { \
  120. template <typename T1, typename T2> \
  121. static constexpr bool Op(T1 a, T2 b) { \
  122. return a op b; \
  123. } \
  124. };
  125. RTC_SAFECMP_MAKE_OP(EqOp, ==)
  126. RTC_SAFECMP_MAKE_OP(NeOp, !=)
  127. RTC_SAFECMP_MAKE_OP(LtOp, <)
  128. RTC_SAFECMP_MAKE_OP(LeOp, <=)
  129. RTC_SAFECMP_MAKE_OP(GtOp, >)
  130. RTC_SAFECMP_MAKE_OP(GeOp, >=)
  131. #undef RTC_SAFECMP_MAKE_OP
  132. } // namespace safe_cmp_impl
  133. #define RTC_SAFECMP_MAKE_FUN(name) \
  134. template <typename T1, typename T2> \
  135. constexpr \
  136. typename std::enable_if<IsIntlike<T1>::value && IsIntlike<T2>::value, \
  137. bool>::type Safe##name(T1 a, T2 b) { \
  138. /* Unary plus here turns enums into real integral types. */ \
  139. return safe_cmp_impl::Cmp<safe_cmp_impl::name##Op>(+a, +b); \
  140. } \
  141. template <typename T1, typename T2> \
  142. constexpr \
  143. typename std::enable_if<!IsIntlike<T1>::value || !IsIntlike<T2>::value, \
  144. bool>::type Safe##name(const T1& a, \
  145. const T2& b) { \
  146. return safe_cmp_impl::name##Op::Op(a, b); \
  147. }
  148. RTC_SAFECMP_MAKE_FUN(Eq)
  149. RTC_SAFECMP_MAKE_FUN(Ne)
  150. RTC_SAFECMP_MAKE_FUN(Lt)
  151. RTC_SAFECMP_MAKE_FUN(Le)
  152. RTC_SAFECMP_MAKE_FUN(Gt)
  153. RTC_SAFECMP_MAKE_FUN(Ge)
  154. #undef RTC_SAFECMP_MAKE_FUN
  155. } // namespace rtc
  156. #endif // RTC_BASE_NUMERICS_SAFE_COMPARE_H_