Add split_mix_64 utility
This commit is contained in:
parent
05b9a22174
commit
e4893c150b
25
xorshift.c
25
xorshift.c
@ -4,6 +4,10 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
u64 seed;
|
||||||
|
} SplitMix64State;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
u64 x;
|
u64 x;
|
||||||
u64 y;
|
u64 y;
|
||||||
@ -22,6 +26,7 @@ u64 xoshiro_256p_generate(void *s);
|
|||||||
u64 rol64(u64 x, u64 bits);
|
u64 rol64(u64 x, u64 bits);
|
||||||
XOR256State init_xor_256_state();
|
XOR256State init_xor_256_state();
|
||||||
void write_image_data(Image *img, RandGenFunc *gen_rand, void *s);
|
void write_image_data(Image *img, RandGenFunc *gen_rand, void *s);
|
||||||
|
u64 split_mix_64(SplitMix64State *state);
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
struct timespec ts = {0};
|
struct timespec ts = {0};
|
||||||
@ -115,11 +120,13 @@ u64 xoshiro_256p_generate(void *s) {
|
|||||||
u64 rol64(u64 x, u64 bits) { return (x << bits) | (x >> (64 - bits)); }
|
u64 rol64(u64 x, u64 bits) { return (x << bits) | (x >> (64 - bits)); }
|
||||||
|
|
||||||
XOR256State init_xor_256_state() {
|
XOR256State init_xor_256_state() {
|
||||||
|
SplitMix64State sm64 = {.seed = lrand48()};
|
||||||
|
|
||||||
return (XOR256State){
|
return (XOR256State){
|
||||||
.x = lrand48(),
|
.x = split_mix_64(&sm64),
|
||||||
.y = lrand48(),
|
.y = split_mix_64(&sm64),
|
||||||
.z = lrand48(),
|
.z = split_mix_64(&sm64),
|
||||||
.w = lrand48(),
|
.w = split_mix_64(&sm64),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,3 +144,13 @@ void write_image_data(Image *img, RandGenFunc *gen_rand, void *s) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
u64 split_mix_64(SplitMix64State *state) {
|
||||||
|
state->seed += 0x9E3779B97f4A7C15;
|
||||||
|
|
||||||
|
u64 result = state->seed;
|
||||||
|
result = (result ^ (result >> 30)) * 0xBF58476D1CE4E5B9;
|
||||||
|
result = (result ^ (result >> 27)) * 0x94D049BB133111EB;
|
||||||
|
|
||||||
|
return result ^ (result >> 31);
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user