Skip to content

Instantly share code, notes, and snippets.

@xtaci
Last active December 20, 2015 20:28
Show Gist options
  • Save xtaci/6190416 to your computer and use it in GitHub Desktop.
Save xtaci/6190416 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <stdint.h>
#ifdef __MACH__
#define CLOCK_REALTIME 0
#include <sys/time.h>
//clock_gettime is not implemented on OSX
int clock_gettime(int clk_id, struct timespec* t) {
struct timeval now;
int rv = gettimeofday(&now, NULL);
if (rv) return rv;
t->tv_sec = now.tv_sec;
t->tv_nsec = now.tv_usec * 1000;
return 0;
}
#endif
#define LEN 512
typedef struct __attribute__ ((__packed__)) {
int32_t N;
char _padding[60];
} padded;
typedef struct __attribute__ ((__packed__)) {
padded X[LEN];
padded Y[LEN];
} EVILXY;
typedef struct __attribute__ ((__packed__)) {
int32_t X;
int32_t Y;
} XY;
int main(void) {
EVILXY *evil;
XY *good;
int i, loop;
struct timespec begin;
struct timespec end;
posix_memalign((void**)&evil, 4096, sizeof(EVILXY));
posix_memalign((void**)&good, 4096, LEN*sizeof(XY));
clock_gettime(CLOCK_REALTIME, &begin);
for (i=0;i<LEN;i++) {
evil->X[i].N^=evil->Y[i].N;
evil->Y[i].N^=evil->X[i].N;
evil->X[i].N^=evil->Y[i].N;
}
clock_gettime(CLOCK_REALTIME, &end);
printf("nanoseconds of evil prog:%ld\n", end.tv_nsec - begin.tv_nsec);
clock_gettime(CLOCK_REALTIME, &begin);
for (i=0;i<LEN;i++) {
good[i].X^=good[i].Y;
good[i].Y^=good[i].X;
good[i].X^=good[i].Y;
}
clock_gettime(CLOCK_REALTIME, &end);
printf("nanoseconds of good prog:%ld\n", end.tv_nsec - begin.tv_nsec);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment