VirtualBox

Ticket #2869: rdtsc.cpp

File rdtsc.cpp, 1.0 KB (added by Markus Laker, 16 years ago)

Demonstration program, to be compiled with G++

Line 
1#include <iostream>
2
3typedef unsigned uint32;
4typedef long long unsigned uint64;
5
6static uint64 rdtsc(void) {
7 uint32 low, high;
8
9 __asm__ __volatile__ ("rdtsc" : "=a" (low), "=d" (high));
10 return low | (uint64(high) << 32);
11}
12
13int main(int, char *[]) {
14 uint64 previous = 0, current, initial = rdtsc();
15 for (unsigned i = 0; i < 100 * 1000 * 1000; ++i) {
16 current = rdtsc();
17 if (current < previous) {
18 std::cout << "After " << i << " iterations:\n"
19 "Current: " << current << " (" << std::hex << current << std::dec << ")\n"
20 "Previous: " << previous << " (" << std::hex << previous << std::dec << ")\n";
21 return 1;
22 }
23
24 previous = current;
25 }
26
27 std::cout << "OK\nOn the final iteration:\n"
28 "Initial: " << initial << " (" << std::hex << initial << std::dec << ")\n"
29 "Current: " << current << " (" << std::hex << current << std::dec << ")\n";
30 return 0;
31}

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy