From ecccfe033777d6ae7d29bcf0cfc30412f7d3be24 Mon Sep 17 00:00:00 2001 From: Fernando Sahmkow Date: Fri, 15 Feb 2019 20:04:11 -0400 Subject: Use u128 on Clock Cycles calculation. --- src/common/uint128.cpp | 24 +++++++++++++++++++++++- src/common/uint128.h | 23 +++-------------------- 2 files changed, 26 insertions(+), 21 deletions(-) (limited to 'src/common') diff --git a/src/common/uint128.cpp b/src/common/uint128.cpp index aea7f03e2..8548ba808 100644 --- a/src/common/uint128.cpp +++ b/src/common/uint128.cpp @@ -1,7 +1,25 @@ +#ifdef _MSC_VER +#include + +#pragma intrinsic(_umul128) +#endif +#include "common/uint128.h" namespace Common { +u128 Multiply64Into128(u64 a, u64 b) { +#ifdef _MSC_VER + u128 result; + result[0] = _umul128(a, b, &result[1]); +#else + unsigned __int128 tmp = a; + tmp *= b; + u128 result; + std::memcpy(&result, &tmp, sizeof(u128)); +#endif + return result; +} -std::pair udiv128(u128 dividend, u64 divisor) { +std::pair Divide128On64(u128 dividend, u64 divisor) { u64 remainder = dividend[0] % divisor; u64 accum = dividend[0] / divisor; if (dividend[1] == 0) @@ -12,6 +30,10 @@ std::pair udiv128(u128 dividend, u64 divisor) { u64 second_segment = (first_segment % divisor) << 32; accum += (second_segment / divisor); remainder += second_segment % divisor; + if (remainder >= divisor) { + accum++; + remainder -= divisor; + } return {accum, remainder}; } diff --git a/src/common/uint128.h b/src/common/uint128.h index fda313bcc..45e384c33 100644 --- a/src/common/uint128.h +++ b/src/common/uint128.h @@ -1,30 +1,13 @@ #include #include -#include #include +#include #include "common/common_types.h" namespace Common { -#ifdef _MSC_VER -#include - -#pragma intrinsic(_umul128) -#endif - -inline u128 umul128(u64 a, u64 b) { -#ifdef _MSC_VER -u128 result; -result[0] = _umul128(a, b, &result[1]); -#else -unsigned __int128 tmp = a; -tmp *= b; -u128 result; -std::memcpy(&result, &tmp, sizeof(u128)); -#endif -return result; -} +u128 Multiply64Into128(u64 a, u64 b); -std::pair udiv128(u128 dividend, u64 divisor); +std::pair Divide128On64(u128 dividend, u64 divisor); } // namespace Common -- cgit v1.2.3