/* Copyright (c) 2007 dean gaudet Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include #include #include #include #define xx(x) x x x x x x x x #define N_ITER (50000) #define DIVISOR (8.*50000) static inline uint64_t rdtsc(void) { #ifdef __x86_64__ uint64_t lo, hi; __asm volatile("rdtsc" : "=&a" (lo), "=&d" (hi)); return ((uint64_t)hi << 32) | lo; #else uint64_t result; __asm volatile("rdtsc" : "=A" (result)); return result; #endif } // i want cpu family, model, stepping but i'm too lazy to decode // all the cpuid bits and special extended family/etc.. static void print_fms(void) { unsigned matched = 0; unsigned family; unsigned model; unsigned stepping; char vendor[13]; char buf[1024]; FILE *f; f = fopen("/proc/cpuinfo", "r"); if (f == NULL) { printf("error opening /proc/cpuinfo: %s\n", strerror(errno)); return; } while (fgets(buf, sizeof(buf), f)) { if (sscanf(buf, "cpu family : %u", &family) == 1) { matched |= 1; } else if (sscanf(buf, "model : %u", &model) == 1) { matched |= 2; } else if (sscanf(buf, "stepping : %u", &stepping) == 1) { matched |= 4; } else if (sscanf(buf, "vendor_id : %12s", vendor) == 1) { matched |= 8; } if (matched == 0xf) { fclose(f); printf("%-12s %02x%02x%02x", vendor, family, model, stepping); return; } } printf("didn't find (one of) cpu family, model, and stepping in /proc/cpuinfo!\n"); } unsigned dead_code_me_harder; int main(int argc, char **argv) { unsigned long tmp_r; struct { //char pad[6]; uint16_t limit; uint64_t base; } tmp_desc; uint64_t a, b; unsigned i; // in each case i measure the cost of actually calculating the // cpu number -- and then accumulate the bogus cpu number into // this sum variable so the compiler doesn't get rid of it. unsigned sum = 0; print_fms(); #ifdef __x86_64__ printf(" 64"); #else printf(" 32"); #endif a = rdtsc(); for (i = 0; i < N_ITER; ++i) { xx( asm volatile("sgdt %0": "=m" (tmp_desc.limit)); sum += 0x10000 - tmp_desc.limit; ) } b = rdtsc(); printf(" sgdt %5.1f", (b - a)/DIVISOR); a = rdtsc(); for (i = 0; i < N_ITER; ++i) { xx( asm volatile("sidt %0": "=m" (tmp_desc.limit)); sum += tmp_desc.limit & 0xfff; ) } b = rdtsc(); printf(" sidt %5.1f", (b - a)/DIVISOR); a = rdtsc(); for (i = 0; i < N_ITER; ++i) { xx( asm volatile("sldt %0": "=r" (tmp_r)); sum += 0x10000 - tmp_r; ) } b = rdtsc(); printf(" sldt %5.1f", (b - a)/DIVISOR); a = rdtsc(); for (i = 0; i < N_ITER; ++i) { xx( asm volatile("lsl %1,%0" : "=r" (tmp_r) : "r" (UINT64_C(123))); sum += tmp_r; ) } b = rdtsc(); printf(" lsl %5.1f", (b - a)/DIVISOR); dead_code_me_harder = sum; printf(" (all in cycles)\n"); exit(0); }