forked from QW-Group/ktx
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master'
- Loading branch information
Showing
16 changed files
with
220 additions
and
65 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// rng.h -- High-quality random number generation. | ||
|
||
#ifndef __RNG_H__ | ||
#define __RNG_H__ | ||
|
||
#include "rng_gen_state.h" | ||
|
||
void rng_seed(RngState*, int seed); | ||
uint32_t rng_next(RngState*); | ||
|
||
void rng_seed_global(int seed); | ||
uint32_t rng_next_global(void); | ||
|
||
#endif |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// rng_gen_impl.h -- Xoroshiro128** implementation. | ||
|
||
#ifndef __RNG_GEN_IMPL_H__ | ||
#define __RNG_GEN_IMPL_H__ | ||
|
||
#include <stdint.h> | ||
|
||
#include "rng_gen_state.h" | ||
|
||
RngState rng_gen_impl_initial_state(int seed); | ||
uint32_t rng_gen_impl_next(RngState* state); | ||
|
||
#endif |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#ifndef __RNG_STATE_H__ | ||
#define __RNG_STATE_H__ | ||
|
||
// You must initialize this with rng_seed/rng_seed_global! | ||
typedef struct RngState { uint32_t s[4]; } RngState; | ||
|
||
#endif |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// rng_seed_impl.h -- SplitMix64 implementation. | ||
|
||
#ifndef __RNG_SEED_IMPL_H__ | ||
#define __RNG_SEED_IMPL_H__ | ||
|
||
#include <stdint.h> | ||
|
||
uint64_t rng_seed_impl_next(uint64_t*); | ||
|
||
#endif |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <stdint.h> | ||
|
||
#include "rng.h" | ||
|
||
#include "rng_gen_impl.h" | ||
#include "rng_seed_impl.h" | ||
|
||
/* Seed the PRNG. | ||
Since the main game loop only provides 4 bytes of entropy, but the | ||
xoroshiro128** PRNG expects 16 bytes of entropy, this function uses the | ||
splitmix64 algorithm to expand the seed. This approach is recommended by | ||
the authors of xoroshiro128**, as described here: https://prng.di.unimi.it . | ||
You must initialize your RngStates with rng_seed/rng_seed_global! | ||
(The all-zeros state has bad statistics.) | ||
*/ | ||
void rng_seed(RngState* s, int seed) { | ||
uint64_t x = (uint64_t)(seed); | ||
|
||
s->s[0] = (uint32_t)rng_seed_impl_next(&x); | ||
s->s[1] = (uint32_t)rng_seed_impl_next(&x); | ||
s->s[2] = (uint32_t)rng_seed_impl_next(&x); | ||
s->s[3] = (uint32_t)rng_seed_impl_next(&x); | ||
} | ||
|
||
uint32_t rng_next(RngState* state) { | ||
return rng_gen_impl_next(state); | ||
} | ||
|
||
// You must initialize this with rng_seed/rng_seed_global! | ||
static RngState global_rng = { 0 }; | ||
|
||
void rng_seed_global(int seed) { | ||
return rng_seed(&global_rng, seed); | ||
} | ||
|
||
uint32_t rng_next_global(void) { | ||
return rng_next(&global_rng); | ||
} | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include "rng_gen_impl.h" | ||
|
||
//// Start imported code. | ||
// Code in this section copied from | ||
// https://prng.di.unimi.it/xoshiro128starstar.c | ||
// It was lightly edited to change a global variable to a local variable. | ||
|
||
/* Written in 2018 by David Blackman and Sebastiano Vigna (vigna@acm.org) | ||
To the extent possible under law, the author has dedicated all copyright | ||
and related and neighboring rights to this software to the public domain | ||
worldwide. This software is distributed without any warranty. | ||
See <http://creativecommons.org/publicdomain/zero/1.0/>. */ | ||
|
||
#include <stdint.h> | ||
|
||
/* This is xoshiro128** 1.1, one of our 32-bit all-purpose, rock-solid | ||
generators. It has excellent speed, a state size (128 bits) that is | ||
large enough for mild parallelism, and it passes all tests we are aware | ||
of. | ||
Note that version 1.0 had mistakenly s[0] instead of s[1] as state | ||
word passed to the scrambler. | ||
For generating just single-precision (i.e., 32-bit) floating-point | ||
numbers, xoshiro128+ is even faster. | ||
The state must be seeded so that it is not everywhere zero. */ | ||
|
||
|
||
static inline uint32_t rotl(const uint32_t x, int k) { | ||
return (x << k) | (x >> (32 - k)); | ||
} | ||
|
||
|
||
static inline uint32_t next(RngState* s) { | ||
const uint32_t result = rotl(s->s[1] * 5, 7) * 9; | ||
|
||
const uint32_t t = s->s[1] << 9; | ||
|
||
s->s[2] ^= s->s[0]; | ||
s->s[3] ^= s->s[1]; | ||
s->s[1] ^= s->s[2]; | ||
s->s[0] ^= s->s[3]; | ||
|
||
s->s[2] ^= t; | ||
|
||
s->s[3] = rotl(s->s[3], 11); | ||
|
||
return result; | ||
} | ||
//// End imported code. | ||
|
||
// Code in this section wraps the PRNG implementation. | ||
uint32_t rng_gen_impl_next(RngState* state) { | ||
return next(state); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
//// Start imported code. | ||
// Code in this section was copied from | ||
// https://prng.di.unimi.it/splitmix64.c | ||
// then lightly edited to remove a global variable and rename a function. | ||
|
||
/* Written in 2015 by Sebastiano Vigna (vigna@acm.org) | ||
To the extent possible under law, the author has dedicated all copyright | ||
and related and neighboring rights to this software to the public domain | ||
worldwide. This software is distributed without any warranty. | ||
See <http://creativecommons.org/publicdomain/zero/1.0/>. */ | ||
|
||
#include <stdint.h> | ||
|
||
/* This is a fixed-increment version of Java 8's SplittableRandom generator | ||
See http://dx.doi.org/10.1145/2714064.2660195 and | ||
http://docs.oracle.com/javase/8/docs/api/java/util/SplittableRandom.html | ||
It is a very fast generator passing BigCrush, and it can be useful if | ||
for some reason you absolutely want 64 bits of state. */ | ||
|
||
uint64_t rng_seed_impl_next(uint64_t* x) { | ||
uint64_t z = (*x += 0x9e3779b97f4a7c15); | ||
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9; | ||
z = (z ^ (z >> 27)) * 0x94d049bb133111eb; | ||
return z ^ (z >> 31); | ||
} | ||
//// End imported code. |
This file contains 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
Oops, something went wrong.