mirror of
https://gitlab.com/Nils_Kasulke/spectre_project.git
synced 2026-04-04 07:47:24 +00:00
68 lines
1.2 KiB
C
68 lines
1.2 KiB
C
|
|
#include<stdio.h>
|
||
|
|
#ifdef _WIN32
|
||
|
|
#include <intrin.h>
|
||
|
|
#else
|
||
|
|
#include <x86intrin.h>
|
||
|
|
#endif
|
||
|
|
|
||
|
|
#include <stdint.h>
|
||
|
|
|
||
|
|
// Windows
|
||
|
|
#ifdef _WIN32
|
||
|
|
|
||
|
|
#include <intrin.h>
|
||
|
|
uint64_t rdtsc(){
|
||
|
|
return __rdtsc();
|
||
|
|
}
|
||
|
|
// Linux/GCC
|
||
|
|
#else
|
||
|
|
|
||
|
|
uint64_t rdtsc(){
|
||
|
|
unsigned int lo,hi;
|
||
|
|
__asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
|
||
|
|
return ((uint64_t)hi << 32) | lo;
|
||
|
|
}
|
||
|
|
#endif
|
||
|
|
|
||
|
|
void usage_and_exit();
|
||
|
|
|
||
|
|
int main(int argc, char* argv[]) {
|
||
|
|
if(argc != 4) usage_and_exit();
|
||
|
|
|
||
|
|
uint64_t t0, t1;
|
||
|
|
int a, b;
|
||
|
|
sscanf(argv[2], "%d", &a);
|
||
|
|
sscanf(argv[3], "%d", &b);
|
||
|
|
if(argv[1][0] == 'x') {
|
||
|
|
printf("filler found\n");
|
||
|
|
} else if(argv[1][0] == 'i') {
|
||
|
|
int out;
|
||
|
|
t0 = rdtsc();
|
||
|
|
out = a + b;
|
||
|
|
t1 = rdtsc();
|
||
|
|
printf("out = %d\n", out);
|
||
|
|
} else if(argv[1][0] == 'f') {
|
||
|
|
float out;
|
||
|
|
t0 = rdtsc();
|
||
|
|
out = (float) a + b;
|
||
|
|
t1 = rdtsc();
|
||
|
|
printf("out = %f\n", out);
|
||
|
|
} else if(argv[1][0] == 'd') {
|
||
|
|
double out;
|
||
|
|
t0 = rdtsc();
|
||
|
|
out = (double) a + b;
|
||
|
|
t1 = rdtsc();
|
||
|
|
printf("out = %lf\n", out);
|
||
|
|
} else {
|
||
|
|
usage_and_exit();
|
||
|
|
}
|
||
|
|
printf("clocks = %lu\n", t1 - t0);
|
||
|
|
}
|
||
|
|
void usage_and_exit() {
|
||
|
|
printf("<prog> <i|f|d> <a> <b>\n");
|
||
|
|
printf("i = integer\n");
|
||
|
|
printf("f = float\n");
|
||
|
|
printf("d = double\n");
|
||
|
|
exit(1);
|
||
|
|
}
|