Created
December 29, 2017 16:05
-
-
Save mjs3339/ed9bcda93a98b4e057a24af5215a9526 to your computer and use it in GitHub Desktop.
C++ A Collection of Low Level Functions for the CPU
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "SysInfo.h" | |
#include <intrin.h> | |
BOOL __stdcall DllMain(HINSTANCE hInst, DWORD dwReason, LPVOID lpReserved) | |
{ | |
return TRUE; | |
} | |
extern "C" __declspec(dllexport) bool __stdcall tsc_supported() | |
{ | |
int CPUInfo[4] = { -1 }; | |
__cpuid(CPUInfo, 0x80000001); | |
if (CPUInfo[3] & 0x4000000) | |
return true; | |
return false; | |
} | |
extern "C" __declspec(dllexport) unsigned int __stdcall getCPUSpeed() | |
{ | |
LARGE_INTEGER ulFreq, ulTicks, ulValue, ulStartCounter, ulEAX_EDX, ulResult; | |
QueryPerformanceFrequency(&ulFreq); | |
QueryPerformanceCounter(&ulTicks); | |
ulValue.QuadPart = ulTicks.QuadPart + ulFreq.QuadPart; | |
_asm { | |
rdtsc | |
mov ulEAX_EDX.LowPart, EAX | |
mov ulEAX_EDX.HighPart, EDX | |
} | |
ulStartCounter.QuadPart = ulEAX_EDX.QuadPart; | |
do | |
{ | |
QueryPerformanceCounter(&ulTicks); | |
} while (ulTicks.QuadPart <= ulValue.QuadPart); | |
_asm { | |
rdtsc | |
mov ulEAX_EDX.LowPart, EAX | |
mov ulEAX_EDX.HighPart, EDX | |
} | |
ulResult.QuadPart = ulEAX_EDX.QuadPart - ulStartCounter.QuadPart; | |
return (unsigned int)ulResult.QuadPart / 1000000; | |
} | |
extern "C" __declspec(dllexport) unsigned __int64 __stdcall rdtsc() | |
{ | |
return __rdtsc(); | |
} | |
extern "C" __declspec(dllexport) char* __stdcall getCPUType() | |
{ | |
static char pszCPUType[13]; | |
memset(pszCPUType, 0, 13); | |
_asm { | |
mov eax, 0 | |
cpuid | |
mov pszCPUType[0], bl | |
mov pszCPUType[1], bh | |
ror ebx, 16 | |
mov pszCPUType[2], bl | |
mov pszCPUType[3], bh | |
mov pszCPUType[4], dl | |
mov pszCPUType[5], dh | |
ror edx, 16 | |
mov pszCPUType[6], dl | |
mov pszCPUType[7], dh | |
mov pszCPUType[8], cl | |
mov pszCPUType[9], ch | |
ror ecx, 16 | |
mov pszCPUType[10], cl | |
mov pszCPUType[11], ch | |
} | |
pszCPUType[12] = '\0'; | |
return pszCPUType; | |
} | |
extern "C" __declspec(dllexport) int __stdcall getCPUFamily() | |
{ | |
int retVal; | |
_asm { | |
mov eax, 1 | |
cpuid | |
mov retVal, eax | |
} | |
return (retVal >> 8); | |
} | |
extern "C" __declspec(dllexport) int __stdcall getCPUModel() | |
{ | |
int retVal; | |
_asm { | |
mov eax, 1 | |
cpuid | |
mov retVal, eax | |
} | |
return ((retVal >> 4) & 0x0000000f); | |
} | |
extern "C" __declspec(dllexport) int __stdcall getCPUStepping() | |
{ | |
int retVal; | |
_asm { | |
mov eax, 1 | |
cpuid | |
mov retVal, eax | |
} | |
return (retVal & 0x0000000f); | |
} | |
extern "C" __declspec(dllexport) unsigned int __stdcall getCPUSpeed1() | |
{ | |
unsigned long TimerLo, TimerHi; | |
Sleep(10); | |
_asm { | |
rdtsc | |
mov TimerLo, eax | |
mov TimerHi, edx | |
} | |
Sleep(500); | |
_asm { | |
rdtsc | |
sub eax, TimerLo | |
sbb edx, TimerHi | |
mov TimerLo, eax | |
mov TimerHi, edx | |
} | |
return (unsigned int)(TimerLo / (1000 * 500)); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment