From eae5b9e0490b024de61e79c1688ad4c254342d8e Mon Sep 17 00:00:00 2001 From: Nekotekina Date: Wed, 13 Mar 2019 00:06:12 +0300 Subject: [PATCH] Rewrite mulh64/umulh64 Use __int128_t/__uint128_t idioms on GCC/Clang. May result in better codegen by avoiding inline asm. --- Utilities/asm.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Utilities/asm.h b/Utilities/asm.h index 564fa6de99..fae1ef5316 100644 --- a/Utilities/asm.h +++ b/Utilities/asm.h @@ -157,18 +157,18 @@ namespace utils #endif } - inline u64 umulh64(u64 a, u64 b) + constexpr u64 umulh64(u64 a, u64 b) { - u64 result; - __asm__("mulq %[b]" : "=d"(result) : [a] "a"(a), [b] "rm"(b)); - return result; + const __uint128_t x = a; + const __uint128_t y = b; + return (x * y) >> 64; } - inline s64 mulh64(s64 a, s64 b) + constexpr s64 mulh64(s64 a, s64 b) { - s64 result; - __asm__("imulq %[b]" : "=d"(result) : [a] "a"(a), [b] "rm"(b)); - return result; + const __int128_t x = a; + const __int128_t y = b; + return (x * y) >> 64; } #elif defined(_MSC_VER)