mirror of
https://gitlab.com/Nils_Kasulke/spectre_project.git
synced 2026-04-04 04:57:26 +00:00
test for rdtsc
This commit is contained in:
49
measure/clock_measure.c
Normal file
49
measure/clock_measure.c
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
/*
|
||||||
|
* not usable clock() isn't precise enough
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
#include<stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include<time.h>
|
||||||
|
#include<stdlib.h>
|
||||||
|
|
||||||
|
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] == 'd') {
|
||||||
|
double out;
|
||||||
|
t0 = (uint64_t) clock();
|
||||||
|
out = (double) a / b;
|
||||||
|
t1 = (uint64_t) clock();
|
||||||
|
printf("out = %lf\n", out);
|
||||||
|
} else if(argv[1][0] == 'f') {
|
||||||
|
float out;
|
||||||
|
t0 = (uint64_t) clock();
|
||||||
|
out = (float) a / b;
|
||||||
|
t1 = (uint64_t) clock();
|
||||||
|
printf("out = %f\n", out);
|
||||||
|
} else if(argv[1][0] == 'i') {
|
||||||
|
int out;
|
||||||
|
t0 = (uint64_t) clock();
|
||||||
|
out = a / b;
|
||||||
|
t1 = (uint64_t) clock();
|
||||||
|
printf("out = %d\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);
|
||||||
|
}
|
||||||
67
measure/measure.c
Normal file
67
measure/measure.c
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
#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);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user