Skip to content

Instantly share code, notes, and snippets.

@pervognsen
Last active July 10, 2021 13:32
Show Gist options
  • Save pervognsen/d118566bb8df70794c3f22e524204fcc to your computer and use it in GitHub Desktop.
Save pervognsen/d118566bb8df70794c3f22e524204fcc to your computer and use it in GitHub Desktop.
#define NOP ({ int x; asm("nop" : "=r"(x)); asm("" : : "r"(x)); })
NOINLINE
double calc_mispredict_penalty(void) {
i64 n = 1ull << 28;
u64 rnglen = (n + 63) / 64;
u64 *rng = malloc(rnglen * sizeof(u64));
for (u64 i = 0; i < rnglen; i++)
rng[i] = random_u64();
u64 *next = rng;
i64 i = n;
start_timer();
do {
u64 bits = *next++;
#pragma unroll(64)
for (int k = 0; k < 64; k++) {
if (bits & (1ull << k)) {
RW(i);
NOP;
}
}
RW(i);
i -= 64;
} while (i >= 0);
double secs = stop_timer();
double cycles = secs * cpufreq;
double penalty = 2.0 * cycles / n;
free(rng);
return penalty;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment