Created
December 20, 2021 08:51
-
-
Save louisswarren/cbc5d2b7ac7ac892b3b2b5e898a74f69 to your computer and use it in GitHub Desktop.
Integer logs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdint.h> | |
#include <string.h> | |
#define PRIME ((1ul << 31) - 1) | |
#define GEN ((1ul << 11) - 1) | |
unsigned | |
fastlog(uint64_t x) | |
{ | |
float xf = x; | |
uint32_t xf_bin; | |
memcpy(&xf_bin, &xf, sizeof(xf_bin)); | |
return ((xf_bin >> 23) & 0xFF) - 127; | |
} | |
unsigned | |
cpulog(uint64_t x) | |
{ | |
return 63 - __builtin_clzll(x); | |
} | |
uint32_t | |
cycle(uint32_t prev) | |
{ | |
uint64_t x = prev; | |
x *= (uint64_t) GEN; | |
return x % PRIME; | |
} | |
uint32_t | |
myrandom(uint32_t *state) | |
{ | |
uint32_t n = (*state = cycle(*state)); | |
uint32_t s = (*state = cycle(*state)); | |
return n >> (s % 31); | |
} | |
void | |
analyse(unsigned (*logfunc)(uint64_t)) | |
{ | |
uint32_t state = GEN; | |
int logctr[31] = {0}; | |
long long i; | |
for (i = 0; i < (1ull << 22); ++i) { | |
logctr[(*logfunc)(myrandom(&state))]++; | |
} | |
for (i = 0; i < 31; ++i) { | |
printf("%lld:\t%d\n", i, logctr[i]); | |
} | |
} | |
int | |
main(void) | |
{ | |
analyse(&cpulog); | |
analyse(&fastlog); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment