From 6b875f0625acc6c6a4f8899b829176baaf7d8502 Mon Sep 17 00:00:00 2001 From: Thomas Miller Date: Wed, 14 Nov 2018 16:19:43 -0800 Subject: Implement mul_overflow for MSVC arm64 Change-Id: Ia7c79614e6ef21222fb9683b540ac51b45a77c49 Reviewed-by: Thiago Macieira --- src/corelib/global/qnumeric_p.h | 74 +++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 48 deletions(-) diff --git a/src/corelib/global/qnumeric_p.h b/src/corelib/global/qnumeric_p.h index e318c3759b..5326d9485b 100644 --- a/src/corelib/global/qnumeric_p.h +++ b/src/corelib/global/qnumeric_p.h @@ -58,14 +58,21 @@ #if defined(Q_CC_MSVC) # include -#endif - -#if defined(Q_CC_MSVC) -#include +# include +# if defined(Q_PROCESSOR_X86_64) || defined(Q_PROCESSOR_ARM_64) +# define Q_INTRINSIC_MUL_OVERFLOW64 +# define Q_UMULH(v1, v2) __umulh(v1, v2); +# define Q_SMULH(v1, v2) __mulh(v1, v2); +# pragma intrinsic(__umulh) +# pragma intrinsic(__mulh) +# endif #endif # if defined(Q_OS_INTEGRITY) && defined(Q_PROCESSOR_ARM_64) #include +# define Q_INTRINSIC_MUL_OVERFLOW64 +# define Q_UMULH(v1, v2) __MULUH64(v1, v2); +# define Q_SMULH(v1, v2) __MULSH64(v1, v2); #endif #if !defined(Q_CC_MSVC) && (defined(Q_OS_QNX) || defined(Q_CC_INTEL)) @@ -327,26 +334,26 @@ mul_overflow(T v1, T v2, T *r) return lr > std::numeric_limits::max() || lr < std::numeric_limits::min(); } -# if defined(Q_OS_INTEGRITY) && defined(Q_PROCESSOR_ARM_64) +# if defined(Q_INTRINSIC_MUL_OVERFLOW64) template <> inline bool mul_overflow(quint64 v1, quint64 v2, quint64 *r) { *r = v1 * v2; - return __MULUH64(v1, v2); + return Q_UMULH(v1, v2); } template <> inline bool mul_overflow(qint64 v1, qint64 v2, qint64 *r) { - qint64 high = __MULSH64(v1, v2); - if (high == 0) { - *r = v1 * v2; - return *r < 0; - } - if (high == -1) { - *r = v1 * v2; - return *r >= 0; - } - return true; + // This is slightly more complex than the unsigned case above: the sign bit + // of 'low' must be replicated as the entire 'high', so the only valid + // values for 'high' are 0 and -1. Use unsigned multiply since it's the same + // as signed for the low bits and use a signed right shift to verify that + // 'high' is nothing but sign bits that match the sign of 'low'. + + qint64 high = __mulh(v1, v2); + *r = qint64(quint64(v1) * quint64(v2)); + return (*r >> 63) != high; } +# if defined(Q_OS_INTEGRITY) && defined(Q_PROCESSOR_ARM_64) template <> inline bool mul_overflow(uint64_t v1, uint64_t v2, uint64_t *r) { return mul_overflow(v1,v2,reinterpret_cast(r)); @@ -356,8 +363,8 @@ template <> inline bool mul_overflow(int64_t v1, int64_t v2, int64_t *r) { return mul_overflow(v1,v2,reinterpret_cast(r)); } - -#endif +# endif // OS_INTEGRITY ARM64 +# endif // Q_INTRINSIC_MUL_OVERFLOW64 # if defined(Q_CC_MSVC) && defined(Q_PROCESSOR_X86) // We can use intrinsics for the unsigned operations with MSVC @@ -369,37 +376,8 @@ template <> inline bool add_overflow(unsigned v1, unsigned v2, unsigned *r) # if defined(Q_PROCESSOR_X86_64) template <> inline bool add_overflow(quint64 v1, quint64 v2, quint64 *r) { return _addcarry_u64(0, v1, v2, reinterpret_cast(r)); } - -# pragma intrinsic(_umul128) -template <> inline bool mul_overflow(quint64 v1, quint64 v2, quint64 *r) -{ - // use 128-bit multiplication with the _umul128 intrinsic - // https://msdn.microsoft.com/en-us/library/3dayytw9.aspx - quint64 high; - *r = _umul128(v1, v2, &high); - return high; -} - -# pragma intrinsic(_mul128) -template <> inline bool mul_overflow(qint64 v1, qint64 v2, qint64 *r) -{ - // Use 128-bit multiplication with the _mul128 intrinsic - // https://msdn.microsoft.com/en-us/library/82cxdw50.aspx - - // This is slightly more complex than the unsigned case above: the sign bit - // of 'low' must be replicated as the entire 'high', so the only valid - // values for 'high' are 0 and -1. - - qint64 high; - *r = _mul128(v1, v2, &high); - if (high == 0) - return *r < 0; - if (high == -1) - return *r >= 0; - return true; -} # endif // x86-64 -# endif // MSVC x86 +# endif // MSVC X86 #endif // !GCC } #endif // Q_CLANG_QDOC -- cgit v1.2.3