Created
July 6, 2019 22:26
-
-
Save Marc-B-Reynolds/9eed37676589d84b313d501976eb5b15 to your computer and use it in GitHub Desktop.
testu01 rabbit driver for https://lemire.me/blog/2019/07/03/a-fast-16-bit-random-number-generator/
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 <inttypes.h> | |
#include <stdio.h> | |
#include <stdint.h> | |
#include "gdef.h" | |
#include "swrite.h" | |
#include "bbattery.h" | |
#include "scomp.h" | |
#include "swalk.h" | |
#include "svaria.h" | |
uint16_t state; | |
#define A 0xfc15 | |
#if 0 | |
#define M 0x2ab | |
#define SHIFT 16 | |
#else | |
#define M 1365 | |
#define SHIFT 17 | |
#endif | |
uint32_t hash16_(uint32_t input, uint32_t key) | |
{ | |
uint32_t hash = input * key; | |
return ((hash >> SHIFT) ^ hash) & 0xFFFF; | |
} | |
uint16_t wyhash16() { | |
uint16_t oldstate = state; | |
state += A; | |
//return hash16_(state, M); | |
return hash16_(state, M)+oldstate; | |
} | |
// hacky testu01 driver stuff below here | |
#define NAME "hack" | |
#include "util.h" | |
#include "unif01.h" | |
#include "swrite.h" | |
#include <float.h> | |
unif01_Gen* gen; | |
static uint32_t next_u32(void* p, void* s) | |
{ | |
uint32_t r = wyhash16() << 16; | |
return r | wyhash16(); | |
} | |
// for double tests | |
static double next_f64(void* p, void* s) | |
{ | |
#if 0 | |
uint32_t l = next_u32(0,0) >> 6; | |
uint32_t h = next_u32(0,0) >> 6; | |
uint64_t i = l; | |
i |= (h << (32-6)); | |
#else | |
// sparse fill: upper 32-bits only | |
uint32_t i = next_u32(0,0); | |
#endif | |
return (double)i*DBL_EPSILON; | |
} | |
static void print_state(void* s) | |
{ | |
printf(" S = 0x%04x\n", state); | |
} | |
unif01_Gen* createGenerator() | |
{ | |
unif01_Gen* gen = util_Malloc(sizeof(unif01_Gen)); | |
state = 0; | |
gen->state = 0; | |
gen->param = 0; | |
gen->name = NAME; | |
gen->GetU01 = (void*)&next_f64; | |
gen->GetBits = (void*)&next_u32; | |
gen->Write = &print_state; | |
return gen; | |
} | |
void deleteGenerator(unif01_Gen* gen) | |
{ | |
if (gen != NULL) { | |
//util_Free(gen->param); | |
//util_Free(gen->state); | |
util_Free(gen); | |
} | |
} | |
int main(void) | |
{ | |
gen = createGenerator(); | |
swrite_Basic = FALSE; // only print summary | |
//bbattery_Rabbit(gen, 512.0*16.0); | |
bbattery_Rabbit(gen, 32768.0); | |
//bbattery_Rabbit(gen, 262144.0/*32768.0*/); | |
deleteGenerator(gen); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment