From 873305b9d85de44fc9e2150697822c8d3ccdef65 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Fri, 26 Feb 2010 16:30:43 +0100 Subject: Change the 32-bit rollover on Windows and Symbian to be more efficient. If it rolls over, then the newer value will be lower than the older one. So just add 2^32 to the value and we'll have proper calculations. --- src/corelib/tools/qelapsedtimer_symbian.cpp | 31 +++++++++++++---------------- src/corelib/tools/qelapsedtimer_win.cpp | 30 ++++++++++++---------------- 2 files changed, 27 insertions(+), 34 deletions(-) diff --git a/src/corelib/tools/qelapsedtimer_symbian.cpp b/src/corelib/tools/qelapsedtimer_symbian.cpp index 06e6f99..6360dbf 100644 --- a/src/corelib/tools/qelapsedtimer_symbian.cpp +++ b/src/corelib/tools/qelapsedtimer_symbian.cpp @@ -50,7 +50,15 @@ static quint64 getMillisecondFromTick() static TInt nanokernel_tick_period; if (!nanokernel_tick_period) HAL::Get(HAL::ENanoTickPeriod, nanokernel_tick_period); - return nanokernel_tick_period * User::NTickCount(); + + static quint32 highdword = 0; + static quint32 lastval = 0; + quint32 val = User::NTickCount(); + if (val < lastval) + ++highdword; + lastval = val; + + return nanokernel_tick_period * (val | (quint64(highdword) << 32)); } QElapsedTimer::ClockType QElapsedTimer::clockType() @@ -58,18 +66,6 @@ QElapsedTimer::ClockType QElapsedTimer::clockType() return TickCounter; } -static qint64 difference(qint64 a, qint64 b) -{ - qint64 retval = a - b; - // there can be 32-bit rollover - // assume there were rollovers if the difference is negative by more than - // 75% of the 32-bit range - - if (retval < Q_INT64_C(-0xc0000000)) - retval += Q_UINT64_C(0x100000000); - return retval; -} - bool QElapsedTimer::isMonotonic() { return true; @@ -85,12 +81,13 @@ qint64 QElapsedTimer::restart() { qint64 oldt1 = t1; t1 = getMillisecondFromTick(); - return difference(t1, oldt1); + t2 = 0; + return t1 - oldt1; } qint64 QElapsedTimer::elapsed() const { - return difference(getMillisecondFromTick(), t1); + return getMillisecondFromTick() - t1; } qint64 QElapsedTimer::msecsSinceReference() const @@ -100,7 +97,7 @@ qint64 QElapsedTimer::msecsSinceReference() const qint64 QElapsedTimer::msecsTo(const QElapsedTimer &other) const { - return difference(other.t1, t1); + return other.t1 - t1; } qint64 QElapsedTimer::secsTo(const QElapsedTimer &other) const @@ -110,7 +107,7 @@ qint64 QElapsedTimer::secsTo(const QElapsedTimer &other) const bool operator<(const QElapsedTimer &v1, const QElapsedTimer &v2) { - return difference(v1.t1, v2.t1) < 0; + return (v1.t1 - v2.t1) < 0; } QT_END_NAMESPACE diff --git a/src/corelib/tools/qelapsedtimer_win.cpp b/src/corelib/tools/qelapsedtimer_win.cpp index ce37fa2..135196a 100644 --- a/src/corelib/tools/qelapsedtimer_win.cpp +++ b/src/corelib/tools/qelapsedtimer_win.cpp @@ -73,7 +73,14 @@ static quint64 getTickCount() resolveLibs(); if (ptrGetTickCount64) return ptrGetTickCount64(); - return GetTickCount(); + + static quint32 highdword = 0; + static quint32 lastval = 0; + quint32 val = GetTickCount(); + if (val < lastval) + ++highdword; + lastval = val; + return val | (quint64(highdword) << 32); } QElapsedTimer::ClockType QElapsedTimer::clockType() @@ -81,18 +88,6 @@ QElapsedTimer::ClockType QElapsedTimer::clockType() return TickCounter; } -static qint64 difference(qint64 a, qint64 b) -{ - qint64 retval = a - b; - // if we're not using GetTickCount64, then there can be 32-bit rollover - // assume there were rollovers if the difference is negative by more than - // 75% of the 32-bit range - - if (!ptrGetTickCount64 && retval < Q_INT64_C(-0xc0000000)) - retval += Q_UINT64_C(0x100000000); - return retval; -} - bool QElapsedTimer::isMonotonic() { return true; @@ -108,12 +103,13 @@ qint64 QElapsedTimer::restart() { qint64 oldt1 = t1; t1 = getTickCount(); - return difference(t1, oldt1); + t2 = 0; + return t1 - oldt1; } qint64 QElapsedTimer::elapsed() const { - return difference(getTickCount(), t1); + return getTickCount() - t1; } qint64 QElapsedTimer::msecsSinceReference() const @@ -123,7 +119,7 @@ qint64 QElapsedTimer::msecsSinceReference() const qint64 QElapsedTimer::msecsTo(const QElapsedTimer &other) const { - return difference(other.t1, t1); + return other.t1 - t1; } qint64 QElapsedTimer::secsTo(const QElapsedTimer &other) const @@ -133,7 +129,7 @@ qint64 QElapsedTimer::secsTo(const QElapsedTimer &other) const bool operator<(const QElapsedTimer &v1, const QElapsedTimer &v2) { - return difference(v1.t1, v2.t1) < 0; + return (v1.t1 - v2.t1) < 0; } QT_END_NAMESPACE -- cgit v0.12