Bug 1625473: Use type traits for AbsReturnType. r=froydnj
authorAndré Bargull <andre.bargull@gmail.com>
Sat, 28 Mar 2020 16:07:06 +0000
changeset 520977 89d69283ef77a4edfc4dddcf053e860b70064d71
parent 520976 f1f6abb3f29249c6d667d84558b9e38e5c0089bd
child 520978 a177dd13e9847169170f76cfad9acec79d8c06e7
push id111392
push userdvarga@mozilla.com
push dateSat, 28 Mar 2020 16:33:32 +0000
treeherderautoland@89d69283ef77 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1625473
milestone76.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1625473: Use type traits for AbsReturnType. r=froydnj Also removes the comment which allows adding more `AbsReturnType` specialisations, because that shouldn't be necessary anymore. Differential Revision: https://phabricator.services.mozilla.com/D68569
mfbt/MathAlgorithms.h
--- a/mfbt/MathAlgorithms.h
+++ b/mfbt/MathAlgorithms.h
@@ -81,78 +81,28 @@ inline std::enable_if_t<detail::AllowDep
   MOZ_ASSERT(aValue >= 0 ||
                  -(aValue + 1) != T((1ULL << (CHAR_BIT * sizeof(T) - 1)) - 1),
              "You can't negate the smallest possible negative integer!");
   return aValue >= 0 ? aValue : -aValue;
 }
 
 namespace detail {
 
-// For now mozilla::Abs only takes intN_T, the signed natural types, and
-// float/double/long double.  Feel free to add overloads for other standard,
-// signed types if you need them.
-
-template <typename T>
-struct AbsReturnTypeFixed;
-
-template <>
-struct AbsReturnTypeFixed<int8_t> {
-  typedef uint8_t Type;
-};
-template <>
-struct AbsReturnTypeFixed<int16_t> {
-  typedef uint16_t Type;
-};
-template <>
-struct AbsReturnTypeFixed<int32_t> {
-  typedef uint32_t Type;
-};
-template <>
-struct AbsReturnTypeFixed<int64_t> {
-  typedef uint64_t Type;
-};
-
 template <typename T, typename = void>
-struct AbsReturnType : AbsReturnTypeFixed<T> {};
+struct AbsReturnType;
 
 template <typename T>
-struct AbsReturnType<T, std::enable_if_t<std::is_same_v<T, char> && char(-1) < char(0)>> {
-  using Type = unsigned char;
-};
-template <>
-struct AbsReturnType<signed char> {
-  typedef unsigned char Type;
-};
-template <>
-struct AbsReturnType<short> {
-  typedef unsigned short Type;
-};
-template <>
-struct AbsReturnType<int> {
-  typedef unsigned int Type;
+struct AbsReturnType<
+    T, std::enable_if_t<std::is_integral_v<T> && std::is_signed_v<T>>> {
+  using Type = std::make_unsigned_t<T>;
 };
-template <>
-struct AbsReturnType<long> {
-  typedef unsigned long Type;
-};
-template <>
-struct AbsReturnType<long long> {
-  typedef unsigned long long Type;
-};
-template <>
-struct AbsReturnType<float> {
-  typedef float Type;
-};
-template <>
-struct AbsReturnType<double> {
-  typedef double Type;
-};
-template <>
-struct AbsReturnType<long double> {
-  typedef long double Type;
+
+template <typename T>
+struct AbsReturnType<T, std::enable_if_t<std::is_floating_point_v<T>>> {
+  using Type = T;
 };
 
 }  // namespace detail
 
 template <typename T>
 inline constexpr typename detail::AbsReturnType<T>::Type Abs(const T aValue) {
   using ReturnType = typename detail::AbsReturnType<T>::Type;
   return aValue >= 0 ? ReturnType(aValue) : ~ReturnType(aValue) + 1;