Skip to content

Instantly share code, notes, and snippets.

@kvanbere
Last active December 24, 2015 17:38
Show Gist options
  • Save kvanbere/6836713 to your computer and use it in GitHub Desktop.
Save kvanbere/6836713 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <intrin.h>
#include <deque>
#include <numeric>
#include <thread>
using namespace std;
void test (void(*f)()) {
deque<unsigned long long> times;
for (size_t n = 10000; n; --n) {
auto a = __rdtsc();
f();
auto b = __rdtsc();
times.emplace_back(b - a);
this_thread::yield();
}
auto sum = accumulate(times.begin(), times.end(), 0LL);
auto avg = sum / times.size();
cout << "Time: " << avg << " ticks" << endl;
}
void pipelined_asm () {
char buf1[0x20];
__asm
{
push ebx
push eax
lea ebx, buf1
mov eax, dword ptr [esp]
mov[ebx], eax
mov eax, dword ptr [esp + 0x4]
mov[ebx + 0x4], eax
mov eax, dword ptr[esp + 0x8]
mov[ebx + 0x8], eax
mov eax, dword ptr[esp + 0xC]
mov[ebx + 0xC], eax
mov eax, dword ptr[esp + 0x10]
mov[ebx + 0x10], eax
mov eax, dword ptr[esp + 0x14]
mov[ebx + 0x14], eax
mov eax, dword ptr[esp + 0x18]
mov[ebx + 0x18], eax
mov eax, dword ptr[esp + 0x1C]
mov[ebx + 0x1C], eax
pop eax
pop ebx
}
}
void spilled_asm () {
char buf1[0x20];
__asm
{
push esi
push eax
push ebx
push ecx
push edx
lea esi, buf1
mov eax, dword ptr[esp]
mov[esi], eax
mov ebx, dword ptr[esp + 0x4]
mov[esi + 0x4], ebx
mov ecx, dword ptr[esp + 0x8]
mov[esi + 0x8], ecx
mov edx, dword ptr[esp + 0xC]
mov[esi + 0xC], edx
mov eax, dword ptr[esp + 0x10]
mov[esi + 0x10], eax
mov ebx, dword ptr[esp + 0x14]
mov[esi + 0x14], ebx
mov ecx, dword ptr[esp + 0x18]
mov[esi + 0x18], ecx
mov edx, dword ptr[esp + 0x1C]
mov[esi + 0x1C], edx
pop edx
pop ecx
pop ebx
pop eax
pop esi
}
}
int main (int argc, char *argv []) {
cout << "pipelined:" << endl;
test(pipelined_asm);
cout << "spilled" << endl;
test(spilled_asm);
getchar();
return 0;
}
@kvanbere
Copy link
Author

kvanbere commented Oct 5, 2013

pipelined:
Time: 98 ticks
spilled
Time: 88 ticks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment