sl@0: // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // e32\nkern\win32\ncutilf.cpp sl@0: // sl@0: // sl@0: sl@0: #include "nk_priv.h" sl@0: sl@0: // Win32 fast counter implementation sl@0: sl@0: static TBool FastCounterSupported = FALSE; sl@0: static TInt FastCounterShift; sl@0: static TInt FastCounterAdjustedFreq; sl@0: sl@0: /** Initialise the fast counter. sl@0: sl@0: Check whether a high performance counter is available and determine its sl@0: frequency. Work out a scaling that allows a 32 bit counter value that wraps sl@0: at most every 16 seconds. sl@0: */ sl@0: void FastCounterInit() sl@0: { sl@0: // Test support by getting count - QueryPerformanceFrequency can succeed sl@0: // even if not supported sl@0: LARGE_INTEGER count; sl@0: count.QuadPart = 0; sl@0: if (!QueryPerformanceCounter(&count) || count.QuadPart == 0) sl@0: return; // not supported sl@0: sl@0: LARGE_INTEGER freq; sl@0: freq.QuadPart = 0; sl@0: if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) sl@0: return; // not supported sl@0: sl@0: TInt mso = __e32_find_ms1_32(freq.LowPart); sl@0: FastCounterShift = (mso > 27) ? mso - 27 : 0; sl@0: sl@0: FastCounterSupported = TRUE; sl@0: FastCounterAdjustedFreq = freq.LowPart >> FastCounterShift; sl@0: } sl@0: sl@0: /** Get the current value of the high performance counter. sl@0: sl@0: If a high performance counter is not available, this uses the millisecond sl@0: tick count instead. sl@0: */ sl@0: EXPORT_C TUint32 NKern::FastCounter() sl@0: { sl@0: if (FastCounterSupported) sl@0: { sl@0: LARGE_INTEGER count; sl@0: count.QuadPart = 0; sl@0: QueryPerformanceCounter(&count); sl@0: return (TUint32)(count.QuadPart >> FastCounterShift); sl@0: } sl@0: else sl@0: return NTickCount(); sl@0: } sl@0: sl@0: sl@0: /** Get the frequency of counter queried by NKern::FastCounter(). sl@0: */ sl@0: EXPORT_C TInt NKern::FastCounterFrequency() sl@0: { sl@0: if (FastCounterSupported) sl@0: return FastCounterAdjustedFreq; sl@0: else sl@0: return 1000000 / TickPeriod(); sl@0: }