Mesa (master): util/rand_xor: fallback Linux to time-based instead of fixed seed

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Sat May 16 12:25:18 UTC 2020


Module: Mesa
Branch: master
Commit: d76abe98cf15226f25d93e76e383715061ada6f4
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=d76abe98cf15226f25d93e76e383715061ada6f4

Author: Eric Engestrom <eric.engestrom at intel.com>
Date:   Sun Oct 13 16:09:22 2019 +0100

util/rand_xor: fallback Linux to time-based instead of fixed seed

When the caller asked for a randomised_seed, we should fall back to the
time-based seed instead of the fixed seed.

Signed-off-by: Eric Engestrom <eric.engestrom at intel.com>
Reviewed-by: Emmanuel Gil Peyrot <linkmauve at linkmauve.fr>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/2316>

---

 src/util/rand_xor.c | 36 ++++++++++++++----------------------
 1 file changed, 14 insertions(+), 22 deletions(-)

diff --git a/src/util/rand_xor.c b/src/util/rand_xor.c
index b486a53343c..3668ac9b84a 100644
--- a/src/util/rand_xor.c
+++ b/src/util/rand_xor.c
@@ -28,10 +28,10 @@
 #endif
 #include <unistd.h>
 #include <fcntl.h>
-#else
-#include <time.h>
 #endif
 
+#include <time.h>
+
 #include "rand_xor.h"
 
 /* Super fast random number generator.
@@ -56,8 +56,12 @@ rand_xorshift128plus(uint64_t seed[2])
 void
 s_rand_xorshift128plus(uint64_t seed[2], bool randomised_seed)
 {
-   if (!randomised_seed)
-      goto fixed_seed;
+   if (!randomised_seed) {
+      /* Use a fixed seed */
+      seed[0] = 0x3bffb83978e24f88;
+      seed[1] = 0x9238d5d56c71cd35;
+      return;
+   }
 
 #if defined(__linux__)
    size_t seed_size = sizeof(uint64_t) * 2;
@@ -69,27 +73,15 @@ s_rand_xorshift128plus(uint64_t seed[2], bool randomised_seed)
 #endif
 
    int fd = open("/dev/urandom", O_RDONLY);
-   if (fd < 0)
-      goto fixed_seed;
-
-   if (read(fd, seed, seed_size) != seed_size) {
+   if (fd >= 0) {
+      if (read(fd, seed, seed_size) == seed_size) {
+         close(fd);
+         return;
+      }
       close(fd);
-      goto fixed_seed;
    }
-
-   close(fd);
-   return;
-
-#else
-   seed[0] = 0x3bffb83978e24f88;
-   seed[1] = time(NULL);
-
-   return;
 #endif
 
-fixed_seed:
-
-   /* Fallback to a fixed seed */
    seed[0] = 0x3bffb83978e24f88;
-   seed[1] = 0x9238d5d56c71cd35;
+   seed[1] = time(NULL);
 }



More information about the mesa-commit mailing list