From f008cc257d57d28fa291b064c25ef9eac6a2be16 Mon Sep 17 00:00:00 2001 From: Dale Weiler Date: Fri, 11 Oct 2013 09:32:46 -0400 Subject: [PATCH] Some cleanups --- ftepp.c | 1 - main.c | 2 -- util.c | 43 +++++++++++++++++++++++++++++++++++-------- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/ftepp.c b/ftepp.c index 36bb1a7..fe63fbf 100644 --- a/ftepp.c +++ b/ftepp.c @@ -23,7 +23,6 @@ */ #include #include -#include #include #include "gmqcc.h" diff --git a/main.c b/main.c index 74d927d..f9a71e2 100644 --- a/main.c +++ b/main.c @@ -21,8 +21,6 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#include -#include #include #include diff --git a/util.c b/util.c index baff4a4..12cfb3e 100644 --- a/util.c +++ b/util.c @@ -22,10 +22,6 @@ * SOFTWARE. */ #define GMQCC_PLATFORM_HEADER -#include -#include -#include - #include "gmqcc.h" #include "platform.h" @@ -286,9 +282,40 @@ bool util_isatty(fs_file_t *file) { return false; } -void util_seed(uint32_t value) { - srand((int)value); -} +/* + * A small noncryptographic PRNG based on: + * http://burtleburtle.net/bob/rand/smallprng.html + */ +static uint32_t util_rand_state[4] = { + 0xF1EA5EED, 0x00000000, + 0x00000000, 0x00000000 +}; + +#define util_rand_rot(X, Y) (((X)<<(Y))|((X)>>(32-(Y)))) + uint32_t util_rand() { - return rand(); + uint32_t last; + + last = util_rand_state[0] - util_rand_rot(util_rand_state[1], 27); + util_rand_state[0] = util_rand_state[1] ^ util_rand_rot(util_rand_state[2], 17); + util_rand_state[1] = util_rand_state[2] + util_rand_state[3]; + util_rand_state[2] = util_rand_state[3] + last; + util_rand_state[3] = util_rand_state[0] + last; + + return util_rand_state[3]; +} + +#undef util_rand_rot + +void util_seed(uint32_t value) { + size_t i; + + util_rand_state[0] = 0xF1EA5EED; + util_rand_state[1] = value; + util_rand_state[2] = value; + util_rand_state[3] = value; + + for (i = 0; i < 20; ++i) + (void)util_rand(); } + -- 2.39.2