Title: [284850] trunk/Source/WTF
Revision
284850
Author
[email protected]
Date
2021-10-25 17:16:10 -0700 (Mon, 25 Oct 2021)

Log Message

[WTF] Make Int128 operator* constexpr
https://bugs.webkit.org/show_bug.cgi?id=232270

Reviewed by Mark Lam.

We remove MSVC X64 (so, Windows only) switching in Int128 operator* since
it breaks constexpr-ness of operator*, which makes writing code difficult.
We can bring this kind of optimization back when C++20 std::is_constant_evaluated()
becomes available.

* wtf/Int128.h:
(WTF::operator*):

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (284849 => 284850)


--- trunk/Source/WTF/ChangeLog	2021-10-25 23:41:45 UTC (rev 284849)
+++ trunk/Source/WTF/ChangeLog	2021-10-26 00:16:10 UTC (rev 284850)
@@ -1,3 +1,18 @@
+2021-10-25  Yusuke Suzuki  <[email protected]>
+
+        [WTF] Make Int128 operator* constexpr
+        https://bugs.webkit.org/show_bug.cgi?id=232270
+
+        Reviewed by Mark Lam.
+
+        We remove MSVC X64 (so, Windows only) switching in Int128 operator* since
+        it breaks constexpr-ness of operator*, which makes writing code difficult.
+        We can bring this kind of optimization back when C++20 std::is_constant_evaluated()
+        becomes available.
+
+        * wtf/Int128.h:
+        (WTF::operator*):
+
 2021-10-25  Alex Christensen  <[email protected]>
 
         Use xpc_connection_copy_invalidation_reason where available for debugging daemon connection failures

Modified: trunk/Source/WTF/wtf/Int128.h (284849 => 284850)


--- trunk/Source/WTF/wtf/Int128.h	2021-10-25 23:41:45 UTC (rev 284849)
+++ trunk/Source/WTF/wtf/Int128.h	2021-10-26 00:16:10 UTC (rev 284850)
@@ -48,13 +48,9 @@
 // builtin type.  We need to make sure not to define operator wchar_t()
 // alongside operator unsigned short() in these instances.
 #define ABSL_INTERNAL_WCHAR_T __wchar_t
-#if CPU(X86_64)
-#include <intrin.h>
-#pragma intrinsic(_umul128)
-#endif  // defined(_M_X64)
-#else   // defined(_MSC_VER)
+#else
 #define ABSL_INTERNAL_WCHAR_T wchar_t
-#endif  // defined(_MSC_VER)
+#endif
 
 namespace WTF {
 
@@ -515,7 +511,7 @@
 constexpr UInt128Impl operator>>(UInt128Impl lhs, int amount);
 constexpr UInt128Impl operator+(UInt128Impl lhs, UInt128Impl rhs);
 constexpr UInt128Impl operator-(UInt128Impl lhs, UInt128Impl rhs);
-UInt128Impl operator*(UInt128Impl lhs, UInt128Impl rhs);
+constexpr UInt128Impl operator*(UInt128Impl lhs, UInt128Impl rhs);
 WTF_EXPORT_PRIVATE UInt128Impl operator/(UInt128Impl lhs, UInt128Impl rhs);
 WTF_EXPORT_PRIVATE UInt128Impl operator%(UInt128Impl lhs, UInt128Impl rhs);
 
@@ -812,14 +808,7 @@
       lhs, rhs);
 }
 
-inline UInt128Impl operator*(UInt128Impl lhs, UInt128Impl rhs) {
-#if COMPILER(MSVC) && CPU(X86_64)
-  uint64_t carry;
-  uint64_t low = _umul128(UInt128Low64(lhs), UInt128Low64(rhs), &carry);
-  return MakeUInt128(UInt128Low64(lhs) * UInt128High64(rhs) +
-                         UInt128High64(lhs) * UInt128Low64(rhs) + carry,
-                     low);
-#else
+constexpr UInt128Impl operator*(UInt128Impl lhs, UInt128Impl rhs) {
   uint64_t a32 = UInt128Low64(lhs) >> 32;
   uint64_t a00 = UInt128Low64(lhs) & 0xffffffff;
   uint64_t b32 = UInt128Low64(rhs) >> 32;
@@ -828,10 +817,9 @@
       MakeUInt128(UInt128High64(lhs) * UInt128Low64(rhs) +
                       UInt128Low64(lhs) * UInt128High64(rhs) + a32 * b32,
                   a00 * b00);
-  result += UInt128Impl(a32 * b00) << 32;
-  result += UInt128Impl(a00 * b32) << 32;
-  return result;
-#endif
+  UInt128Impl v1 = UInt128Impl(a32 * b00) << 32;
+  UInt128Impl v2 = UInt128Impl(a00 * b32) << 32;
+  return result + v1 + v2;
 }
 
 // Increment/decrement operators.
@@ -894,7 +882,7 @@
 constexpr Int128Impl operator-(Int128Impl v);
 constexpr Int128Impl operator+(Int128Impl lhs, Int128Impl rhs);
 constexpr Int128Impl operator-(Int128Impl lhs, Int128Impl rhs);
-Int128Impl operator*(Int128Impl lhs, Int128Impl rhs);
+constexpr Int128Impl operator*(Int128Impl lhs, Int128Impl rhs);
 WTF_EXPORT_PRIVATE Int128Impl operator/(Int128Impl lhs, Int128Impl rhs);
 WTF_EXPORT_PRIVATE Int128Impl operator%(Int128Impl lhs, Int128Impl rhs);
 constexpr Int128Impl operator|(Int128Impl lhs, Int128Impl rhs);
@@ -1192,7 +1180,7 @@
       lhs, rhs);
 }
 
-inline Int128Impl operator*(Int128Impl lhs, Int128Impl rhs) {
+constexpr Int128Impl operator*(Int128Impl lhs, Int128Impl rhs) {
   return MakeInt128(
       int128_internal::BitCastToSigned(UInt128High64(UInt128Impl(lhs) * rhs)),
       UInt128Low64(UInt128Impl(lhs) * rhs));
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to