]> de.git.xonotic.org Git - xonotic/gmqcc.git/commitdiff
Some cleanups
authorDale Weiler <killfieldengine@gmail.com>
Fri, 11 Oct 2013 13:32:46 +0000 (09:32 -0400)
committerDale Weiler <killfieldengine@gmail.com>
Fri, 11 Oct 2013 13:32:46 +0000 (09:32 -0400)
ftepp.c
main.c
util.c

diff --git a/ftepp.c b/ftepp.c
index 36bb1a7b59faeca6390ef3ce31fac56e8de5127f..fe63fbf656cb9c172ee060abf7f68b3d4a8aaa01 100644 (file)
--- a/ftepp.c
+++ b/ftepp.c
@@ -23,7 +23,6 @@
  */
 #include <string.h>
 #include <stdlib.h>
-#include <time.h>
 #include <sys/stat.h>
 
 #include "gmqcc.h"
diff --git a/main.c b/main.c
index 74d927daf98187ef4bff8cff4983d20ee341d0e9..f9a71e26240e93cf1513f6d9bd2fafed4a1c05c8 100644 (file)
--- 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 <time.h>
-#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
diff --git a/util.c b/util.c
index baff4a435c0c2268624a6982c4d5cbe0371644a6..12cfb3e06863bce3090fb1900d3f7af47c6584c6 100644 (file)
--- a/util.c
+++ b/util.c
  * SOFTWARE.
  */
 #define GMQCC_PLATFORM_HEADER
-#include <string.h>
-#include <stdlib.h>
-#include <stdarg.h>
-
 #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();
 }
+