/* * Copyright 2016 The WebRTC Project Authors. All rights reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. An additional intellectual property rights grant can be found * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree. */ // This file defines six constexpr functions: // // rtc::SafeEq // == // rtc::SafeNe // != // rtc::SafeLt // < // rtc::SafeLe // <= // rtc::SafeGt // > // rtc::SafeGe // >= // // They each accept two arguments of arbitrary types, and in almost all cases, // they simply call the appropriate comparison operator. However, if both // arguments are integers, they don't compare them using C++'s quirky rules, // but instead adhere to the true mathematical definitions. It is as if the // arguments were first converted to infinite-range signed integers, and then // compared, although of course nothing expensive like that actually takes // place. In practice, for signed/signed and unsigned/unsigned comparisons and // some mixed-signed comparisons with a compile-time constant, the overhead is // zero; in the remaining cases, it is just a few machine instructions (no // branches). #ifndef RTC_BASE_NUMERICS_SAFE_COMPARE_H_ #define RTC_BASE_NUMERICS_SAFE_COMPARE_H_ #include #include #include #include #include "rtc_base/type_traits.h" namespace rtc { namespace safe_cmp_impl { template struct LargerIntImpl : std::false_type {}; template <> struct LargerIntImpl : std::true_type { using type = int16_t; }; template <> struct LargerIntImpl : std::true_type { using type = int32_t; }; template <> struct LargerIntImpl : std::true_type { using type = int64_t; }; // LargerInt::value is true iff there's a signed type that's larger // than T1 (and no larger than the larger of T2 and int*, for performance // reasons); and if there is such a type, LargerInt::type is an alias // for it. template struct LargerInt : LargerIntImpl {}; template constexpr typename std::make_unsigned::type MakeUnsigned(T a) { return static_cast::type>(a); } // Overload for when both T1 and T2 have the same signedness. template ::value == std::is_signed::value>::type* = nullptr> constexpr bool Cmp(T1 a, T2 b) { return Op::Op(a, b); } // Overload for signed - unsigned comparison that can be promoted to a bigger // signed type. template ::value && std::is_unsigned::value && LargerInt::value>::type* = nullptr> constexpr bool Cmp(T1 a, T2 b) { return Op::Op(a, static_cast::type>(b)); } // Overload for unsigned - signed comparison that can be promoted to a bigger // signed type. template ::value && std::is_signed::value && LargerInt::value>::type* = nullptr> constexpr bool Cmp(T1 a, T2 b) { return Op::Op(static_cast::type>(a), b); } // Overload for signed - unsigned comparison that can't be promoted to a bigger // signed type. template ::value && std::is_unsigned::value && !LargerInt::value>::type* = nullptr> constexpr bool Cmp(T1 a, T2 b) { return a < 0 ? Op::Op(-1, 0) : Op::Op(safe_cmp_impl::MakeUnsigned(a), b); } // Overload for unsigned - signed comparison that can't be promoted to a bigger // signed type. template ::value && std::is_signed::value && !LargerInt::value>::type* = nullptr> constexpr bool Cmp(T1 a, T2 b) { return b < 0 ? Op::Op(0, -1) : Op::Op(a, safe_cmp_impl::MakeUnsigned(b)); } #define RTC_SAFECMP_MAKE_OP(name, op) \ struct name { \ template \ static constexpr bool Op(T1 a, T2 b) { \ return a op b; \ } \ }; RTC_SAFECMP_MAKE_OP(EqOp, ==) RTC_SAFECMP_MAKE_OP(NeOp, !=) RTC_SAFECMP_MAKE_OP(LtOp, <) RTC_SAFECMP_MAKE_OP(LeOp, <=) RTC_SAFECMP_MAKE_OP(GtOp, >) RTC_SAFECMP_MAKE_OP(GeOp, >=) #undef RTC_SAFECMP_MAKE_OP } // namespace safe_cmp_impl #define RTC_SAFECMP_MAKE_FUN(name) \ template \ constexpr \ typename std::enable_if::value && IsIntlike::value, \ bool>::type Safe##name(T1 a, T2 b) { \ /* Unary plus here turns enums into real integral types. */ \ return safe_cmp_impl::Cmp(+a, +b); \ } \ template \ constexpr \ typename std::enable_if::value || !IsIntlike::value, \ bool>::type Safe##name(const T1& a, \ const T2& b) { \ return safe_cmp_impl::name##Op::Op(a, b); \ } RTC_SAFECMP_MAKE_FUN(Eq) RTC_SAFECMP_MAKE_FUN(Ne) RTC_SAFECMP_MAKE_FUN(Lt) RTC_SAFECMP_MAKE_FUN(Le) RTC_SAFECMP_MAKE_FUN(Gt) RTC_SAFECMP_MAKE_FUN(Ge) #undef RTC_SAFECMP_MAKE_FUN } // namespace rtc #endif // RTC_BASE_NUMERICS_SAFE_COMPARE_H_