Index: src/base/logging.h |
diff --git a/src/base/logging.h b/src/base/logging.h |
index eae8a7fdb6aa68a5343a38936a3059128c451e2e..ac1cf5627e71b859cb52b91904ea446e19f3c7d7 100644 |
--- a/src/base/logging.h |
+++ b/src/base/logging.h |
@@ -115,21 +115,75 @@ DEFINE_MAKE_CHECK_OP_STRING(char const*) |
DEFINE_MAKE_CHECK_OP_STRING(void const*) |
#undef DEFINE_MAKE_CHECK_OP_STRING |
+// is_signed_vs_unsigned::value is true if both types are integral, Lhs is |
+// signed, and Rhs is unsigned. False in all other cases. |
+template <typename Lhs, typename Rhs> |
+struct is_signed_vs_unsigned { |
+ enum : bool { |
+ value = std::is_integral<Lhs>::value && std::is_integral<Rhs>::value && |
+ std::is_signed<Lhs>::value && std::is_unsigned<Rhs>::value |
+ }; |
+}; |
+// Same thing, other way around: Lhs is unsigned, Rhs signed. |
+template <typename Lhs, typename Rhs> |
+struct is_unsigned_vs_signed : public is_signed_vs_unsigned<Rhs, Lhs> {}; |
+ |
+// Specialize the compare functions for signed vs. unsigned comparisons. |
+// std::enable_if ensures that this template is only instantiable if both Lhs |
+// and Rhs are integral types, and their signedness does not match. |
+#define MAKE_UNSIGNED(Type, value) \ |
+ static_cast<typename std::make_unsigned<Type>::type>(value) |
+#define DEFINE_SIGNED_MISMATCH_COMP(CHECK, NAME, IMPL) \ |
+ template <typename Lhs, typename Rhs> \ |
+ V8_INLINE typename std::enable_if<CHECK<Lhs, Rhs>::value, bool>::type \ |
+ Cmp##NAME##Impl(Lhs const& lhs, Rhs const& rhs) { \ |
+ return IMPL; \ |
+ } |
+DEFINE_SIGNED_MISMATCH_COMP(is_signed_vs_unsigned, EQ, |
+ lhs >= 0 && MAKE_UNSIGNED(Lhs, lhs) == rhs) |
+DEFINE_SIGNED_MISMATCH_COMP(is_signed_vs_unsigned, LT, |
+ lhs < 0 || MAKE_UNSIGNED(Lhs, lhs) < rhs) |
+DEFINE_SIGNED_MISMATCH_COMP(is_signed_vs_unsigned, LE, |
+ lhs <= 0 || MAKE_UNSIGNED(Lhs, lhs) <= rhs) |
+DEFINE_SIGNED_MISMATCH_COMP(is_signed_vs_unsigned, NE, !CmpEQImpl(lhs, rhs)) |
+DEFINE_SIGNED_MISMATCH_COMP(is_signed_vs_unsigned, GT, !CmpLEImpl(lhs, rhs)) |
+DEFINE_SIGNED_MISMATCH_COMP(is_signed_vs_unsigned, GE, !CmpLTImpl(lhs, rhs)) |
+DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, EQ, CmpEQImpl(rhs, lhs)) |
+DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, NE, CmpNEImpl(rhs, lhs)) |
+DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, LT, CmpGTImpl(rhs, lhs)) |
+DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, LE, CmpGEImpl(rhs, lhs)) |
+DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, GT, CmpLTImpl(rhs, lhs)) |
+DEFINE_SIGNED_MISMATCH_COMP(is_unsigned_vs_signed, GE, CmpLEImpl(rhs, lhs)) |
Igor Sheludko
2016/11/24 15:37:12
#undef DEFINE_SIGNED_MISMATCH_COMP
Clemens Hammacher
2016/11/24 17:06:30
Oh, sure. And #undef MAKE_UNSIGNED. Done.
|
+ |
// Helper functions for CHECK_OP macro. |
// The (float, float) and (double, double) instantiations are explicitly |
-// externialized to ensure proper 32/64-bit comparisons on x86. |
+// externalized to ensure proper 32/64-bit comparisons on x86. |
+// The Cmp##NAME##Impl function is only instantiable if one of the two types is |
+// not integral or their signedness matches (i.e. whenever no specialization is |
+// required, see above). Otherwise it is disabled by the enable_if construct, |
+// and the compiler will pick a specialization from above. |
#define DEFINE_CHECK_OP_IMPL(NAME, op) \ |
template <typename Lhs, typename Rhs> \ |
+ V8_INLINE \ |
+ typename std::enable_if<!is_signed_vs_unsigned<Lhs, Rhs>::value && \ |
+ !is_unsigned_vs_signed<Lhs, Rhs>::value, \ |
+ bool>::type \ |
+ Cmp##NAME##Impl(typename PassType<Lhs>::type lhs, \ |
+ typename PassType<Rhs>::type rhs) { \ |
+ return lhs op rhs; \ |
+ } \ |
+ template <typename Lhs, typename Rhs> \ |
V8_INLINE std::string* Check##NAME##Impl(typename PassType<Lhs>::type lhs, \ |
typename PassType<Rhs>::type rhs, \ |
char const* msg) { \ |
- return V8_LIKELY(lhs op rhs) ? nullptr \ |
- : MakeCheckOpString<Lhs, Rhs>(lhs, rhs, msg); \ |
+ bool cmp = Cmp##NAME##Impl<Lhs, Rhs>(lhs, rhs); \ |
+ return V8_LIKELY(cmp) ? nullptr \ |
+ : MakeCheckOpString<Lhs, Rhs>(lhs, rhs, msg); \ |
} \ |
extern template V8_BASE_EXPORT std::string* Check##NAME##Impl<float, float>( \ |
- const float lhs, const float rhs, char const* msg); \ |
+ float lhs, float rhs, char const* msg); \ |
extern template V8_BASE_EXPORT std::string* \ |
- Check##NAME##Impl<double, double>(const double lhs, const double rhs, \ |
+ Check##NAME##Impl<double, double>(double lhs, double rhs, \ |
char const* msg); |
DEFINE_CHECK_OP_IMPL(EQ, ==) |
DEFINE_CHECK_OP_IMPL(NE, !=) |