1b8e80941Smrg/*
2b8e80941Smrg * Copyright 2017 Timothy Arceri
3b8e80941Smrg *
4b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5b8e80941Smrg * copy of this software and associated documentation files (the "Software"),
6b8e80941Smrg * to deal in the Software without restriction, including without limitation
7b8e80941Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b8e80941Smrg * and/or sell copies of the Software, and to permit persons to whom the
9b8e80941Smrg * Software is furnished to do so, subject to the following conditions:
10b8e80941Smrg *
11b8e80941Smrg * The above copyright notice and this permission notice (including the next
12b8e80941Smrg * paragraph) shall be included in all copies or substantial portions of the
13b8e80941Smrg * Software.
14b8e80941Smrg *
15b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16b8e80941Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17b8e80941Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18b8e80941Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19b8e80941Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20b8e80941Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21b8e80941Smrg * SOFTWARE.
22b8e80941Smrg *
23b8e80941Smrg */
24b8e80941Smrg
25b8e80941Smrg#if defined(__linux__)
26b8e80941Smrg#include <sys/file.h>
27b8e80941Smrg#include <unistd.h>
28b8e80941Smrg#include <fcntl.h>
29b8e80941Smrg#else
30b8e80941Smrg#include <time.h>
31b8e80941Smrg#endif
32b8e80941Smrg
33b8e80941Smrg#include "rand_xor.h"
34b8e80941Smrg
35b8e80941Smrg/* Super fast random number generator.
36b8e80941Smrg *
37b8e80941Smrg * This rand_xorshift128plus function by Sebastiano Vigna belongs
38b8e80941Smrg * to the public domain.
39b8e80941Smrg */
40b8e80941Smrguint64_t
41b8e80941Smrgrand_xorshift128plus(uint64_t *seed)
42b8e80941Smrg{
43b8e80941Smrg   uint64_t *s = seed;
44b8e80941Smrg
45b8e80941Smrg   uint64_t s1 = s[0];
46b8e80941Smrg   const uint64_t s0 = s[1];
47b8e80941Smrg   s[0] = s0;
48b8e80941Smrg   s1 ^= s1 << 23;
49b8e80941Smrg   s[1] = s1 ^ s0 ^ (s1 >> 18) ^ (s0 >> 5);
50b8e80941Smrg
51b8e80941Smrg   return s[1] + s0;
52b8e80941Smrg}
53b8e80941Smrg
54b8e80941Smrgvoid
55b8e80941Smrgs_rand_xorshift128plus(uint64_t *seed, bool randomised_seed)
56b8e80941Smrg{
57b8e80941Smrg   if (!randomised_seed)
58b8e80941Smrg      goto fixed_seed;
59b8e80941Smrg
60b8e80941Smrg#if defined(__linux__)
61b8e80941Smrg   int fd = open("/dev/urandom", O_RDONLY);
62b8e80941Smrg   if (fd < 0)
63b8e80941Smrg      goto fixed_seed;
64b8e80941Smrg
65b8e80941Smrg   size_t seed_size = sizeof(uint64_t) * 2;
66b8e80941Smrg   if (read(fd, seed, seed_size) != seed_size) {
67b8e80941Smrg      close(fd);
68b8e80941Smrg      goto fixed_seed;
69b8e80941Smrg   }
70b8e80941Smrg
71b8e80941Smrg   close(fd);
72b8e80941Smrg   return;
73b8e80941Smrg
74b8e80941Smrg#else
75b8e80941Smrg   seed[0] = 0x3bffb83978e24f88;
76b8e80941Smrg   seed[1] = time(NULL);
77b8e80941Smrg
78b8e80941Smrg   return;
79b8e80941Smrg#endif
80b8e80941Smrg
81b8e80941Smrgfixed_seed:
82b8e80941Smrg
83b8e80941Smrg   /* Fallback to a fixed seed */
84b8e80941Smrg   seed[0] = 0x3bffb83978e24f88;
85b8e80941Smrg   seed[1] = 0x9238d5d56c71cd35;
86b8e80941Smrg}
87