summaryrefslogtreecommitdiff
path: root/hrtimer.cpp
diff options
context:
space:
mode:
authorweidai <weidai@57ff6487-cd31-0410-9ec3-f628ee90f5f0>2003-06-19 17:09:07 +0000
committerweidai <weidai@57ff6487-cd31-0410-9ec3-f628ee90f5f0>2003-06-19 17:09:07 +0000
commitab1ed4b45d7c6ea11785065027986d389e5b673a (patch)
tree7e300cb2827b1bf3cc05695e5e89b5ef58b1be93 /hrtimer.cpp
parentb0034a8989f961db2fcfb7673e487031e3115cb5 (diff)
downloadcryptopp-ab1ed4b45d7c6ea11785065027986d389e5b673a.tar.gz
sync with private branch
git-svn-id: svn://svn.code.sf.net/p/cryptopp/code/trunk/c5@81 57ff6487-cd31-0410-9ec3-f628ee90f5f0
Diffstat (limited to 'hrtimer.cpp')
-rw-r--r--hrtimer.cpp51
1 files changed, 36 insertions, 15 deletions
diff --git a/hrtimer.cpp b/hrtimer.cpp
index 5245841..9c8da7d 100644
--- a/hrtimer.cpp
+++ b/hrtimer.cpp
@@ -21,9 +21,10 @@ NAMESPACE_BEGIN(CryptoPP)
word64 Timer::GetCurrentTimerValue()
{
#if defined(CRYPTOPP_WIN32_AVAILABLE)
- FILETIME now;
- GetSystemTimeAsFileTime(&now);
- return now.dwLowDateTime + ((word64)now.dwHighDateTime << 32);
+ LARGE_INTEGER now;
+ if (!QueryPerformanceCounter(&now))
+ throw Exception(Exception::OTHER_ERROR, "Timer: QueryPerformanceCounter failed with error " + IntToString(GetLastError()));
+ return now.QuadPart;
#elif defined(CRYPTOPP_UNIX_AVAILABLE)
timeval now;
gettimeofday(&now, NULL);
@@ -35,20 +36,33 @@ word64 Timer::GetCurrentTimerValue()
#endif
}
-unsigned long Timer::ConvertTo(word64 t, Unit unit)
+word64 Timer::TicksPerSecond()
{
- switch (unit)
+#if defined(CRYPTOPP_WIN32_AVAILABLE)
+ static LARGE_INTEGER freq;
+ if (freq.QuadPart == 0)
{
- case SECONDS:
- return (unsigned long)(t / (TicksPerMillisecond() * 1000));
- case MILLISECONDS:
- return (unsigned long)(t / TicksPerMillisecond());
- case MICROSECONDS:
- assert(TicksPerMillisecond() % 1000 == 0);
- return (unsigned long)(t / (TicksPerMillisecond() / 1000));
+ if (!QueryPerformanceFrequency(&freq))
+ throw Exception(Exception::OTHER_ERROR, "Timer: QueryPerformanceFrequency failed with error " + IntToString(GetLastError()));
}
- assert(false);
- return 0;
+ return freq.QuadPart;
+#elif defined(CRYPTOPP_UNIX_AVAILABLE) || defined(macintosh)
+ return 1000000;
+#endif
+}
+
+word64 Timer::ConvertTo(word64 t, Unit unit)
+{
+ static unsigned long unitsPerSecondTable[] = {1, 1000, 1000*1000, 1000*1000*1000};
+
+ assert(unit < sizeof(unitsPerSecondTable) / sizeof(unitsPerSecondTable[0]));
+ unsigned long unitsPerSecond = unitsPerSecondTable[unit];
+ const word64 freq = TicksPerSecond();
+
+ if (freq % unitsPerSecond == 0)
+ return t / (freq / unitsPerSecond);
+ else
+ return word64((double)t * unitsPerSecond / freq);
}
void Timer::StartTimer()
@@ -57,7 +71,7 @@ void Timer::StartTimer()
m_started = true;
}
-unsigned long Timer::ElapsedTime()
+word64 Timer::ElapsedTimeInWord64()
{
if (m_stuckAtZero)
return 0;
@@ -70,6 +84,13 @@ unsigned long Timer::ElapsedTime()
}
}
+unsigned long Timer::ElapsedTime()
+{
+ word64 elapsed = ElapsedTimeInWord64();
+ assert(elapsed <= ULONG_MAX);
+ return (unsigned long)elapsed;
+}
+
NAMESPACE_END
#endif