10 #if defined(CRYPTOPP_WIN32_AVAILABLE)
11 #define WIN32_LEAN_AND_MEAN
13 # if ((WINVER >= 0x0602 ) || (_WIN32_WINNT >= 0x0602 ))
14 # include <processthreadsapi.h>
15 # if defined(WINAPI_FAMILY)
16 # if (WINAPI_FAMILY_PARTITION(WINAPI_FAMILY_PHONE_APP))
17 # include <profileapi.h>
23 #if defined(CRYPTOPP_UNIX_AVAILABLE)
25 #include <sys/times.h>
33 #if defined(CRYPTOPP_WIN32_AVAILABLE)
34 static TimerWord InitializePerformanceCounterFrequency()
36 LARGE_INTEGER freq = {0,0};
37 if (!QueryPerformanceFrequency(&freq))
42 inline TimerWord PerformanceCounterFrequency()
44 static const word64 freq = InitializePerformanceCounterFrequency();
49 #ifndef CRYPTOPP_IMPORTS
51 double TimerBase::ConvertTo(TimerWord t, Unit unit)
53 static unsigned long unitsPerSecondTable[] = {1, 1000, 1000*1000, 1000*1000*1000};
57 return static_cast<double>(t) * unitsPerSecondTable[unit] / TicksPerSecond();
60 void TimerBase::StartTimer()
62 m_last = m_start = GetCurrentTimerValue();
66 double TimerBase::ElapsedTimeAsDouble()
73 TimerWord now = GetCurrentTimerValue();
76 return ConvertTo(m_last - m_start, m_timerUnit);
83 unsigned long TimerBase::ElapsedTime()
85 double elapsed = ElapsedTimeAsDouble();
87 return (
unsigned long)elapsed;
90 TimerWord Timer::GetCurrentTimerValue()
92 #if defined(CRYPTOPP_WIN32_AVAILABLE)
94 LARGE_INTEGER now = {0,0};
95 if (!QueryPerformanceCounter(&now))
98 #elif defined(CRYPTOPP_UNIX_AVAILABLE)
100 gettimeofday(&now, NULLPTR);
101 return (TimerWord)now.tv_sec * 1000000 + now.tv_usec;
108 TimerWord Timer::TicksPerSecond()
110 #if defined(CRYPTOPP_WIN32_AVAILABLE)
111 return PerformanceCounterFrequency();
112 #elif defined(CRYPTOPP_UNIX_AVAILABLE)
115 return CLOCKS_PER_SEC;
121 TimerWord ThreadUserTimer::GetCurrentTimerValue()
123 #if defined(CRYPTOPP_WIN32_AVAILABLE) && defined(THREAD_TIMER_AVAILABLE)
124 static bool getCurrentThreadImplemented =
true;
125 if (getCurrentThreadImplemented)
127 FILETIME now, ignored;
128 if (!GetThreadTimes(GetCurrentThread(), &ignored, &ignored, &ignored, &now))
130 const DWORD lastError = GetLastError();
131 if (lastError == ERROR_CALL_NOT_IMPLEMENTED)
133 getCurrentThreadImplemented =
false;
134 goto GetCurrentThreadNotImplemented;
138 return now.dwLowDateTime + ((TimerWord)now.dwHighDateTime << 32);
140 GetCurrentThreadNotImplemented:
141 return (TimerWord)clock() * (10*1000*1000 / CLOCKS_PER_SEC);
142 #elif defined(CRYPTOPP_WIN32_AVAILABLE) && !defined(THREAD_TIMER_AVAILABLE)
144 if (!QueryPerformanceCounter(&now))
146 const DWORD lastError = GetLastError();
150 #elif defined(CRYPTOPP_UNIX_AVAILABLE)
153 return now.tms_utime;
159 TimerWord ThreadUserTimer::TicksPerSecond()
161 #if defined(CRYPTOPP_WIN32_AVAILABLE) && defined(THREAD_TIMER_AVAILABLE)
163 #elif defined(CRYPTOPP_WIN32_AVAILABLE) && !defined(THREAD_TIMER_AVAILABLE)
164 static const TimerWord ticksPerSecond = PerformanceCounterFrequency();
165 return ticksPerSecond;
166 #elif defined(CRYPTOPP_UNIX_AVAILABLE)
167 static const long ticksPerSecond = sysconf(_SC_CLK_TCK);
168 return ticksPerSecond;
170 return CLOCKS_PER_SEC;
Base class for all exceptions thrown by the library.
@ OTHER_ERROR
Some other error occurred not belonging to other categories.
Utility functions for the Crypto++ library.
#define COUNTOF(arr)
Counts elements in an array.
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Crypto++ library namespace.
Debugging and diagnostic assertions.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.