2022-08-15 01:28:54 +00:00
|
|
|
/*
|
|
|
|
* Implementation of non-cryptographic pseudo-random number
|
|
|
|
* generator algorithm known as xoshiro.
|
|
|
|
*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* mpv is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2024-09-27 17:35:37 +00:00
|
|
|
#include <libavutil/random_seed.h>
|
|
|
|
|
2023-10-21 02:55:41 +00:00
|
|
|
#include "osdep/threads.h"
|
2024-09-27 17:35:37 +00:00
|
|
|
#include "osdep/timer.h"
|
2022-08-15 01:28:54 +00:00
|
|
|
#include "random.h"
|
|
|
|
|
|
|
|
static uint64_t state[4];
|
2023-10-21 02:55:41 +00:00
|
|
|
static mp_static_mutex state_mutex = MP_STATIC_MUTEX_INITIALIZER;
|
2022-08-15 01:28:54 +00:00
|
|
|
|
|
|
|
static inline uint64_t rotl_u64(const uint64_t x, const int k)
|
|
|
|
{
|
|
|
|
return (x << k) | (x >> (64 - k));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint64_t splitmix64(uint64_t *const x)
|
|
|
|
{
|
|
|
|
uint64_t z = (*x += UINT64_C(0x9e3779b97f4a7c15));
|
|
|
|
z = (z ^ (z >> 30)) * UINT64_C(0xbf58476d1ce4e5b9);
|
|
|
|
z = (z ^ (z >> 27)) * UINT64_C(0x94d049bb133111eb);
|
|
|
|
return z ^ (z >> 31);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mp_rand_seed(uint64_t seed)
|
|
|
|
{
|
2024-06-23 14:45:51 +00:00
|
|
|
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
|
|
|
|
seed = 42;
|
|
|
|
#endif
|
2024-09-27 17:35:37 +00:00
|
|
|
|
|
|
|
if (seed == 0) {
|
|
|
|
uint8_t buf[sizeof(seed)];
|
|
|
|
if (av_random_bytes(buf, sizeof(buf)) < 0) {
|
|
|
|
seed = mp_raw_time_ns();
|
|
|
|
} else {
|
|
|
|
memcpy(&seed, buf, sizeof(seed));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-21 02:55:41 +00:00
|
|
|
mp_mutex_lock(&state_mutex);
|
2022-08-15 01:28:54 +00:00
|
|
|
state[0] = seed;
|
|
|
|
for (int i = 1; i < 4; i++)
|
|
|
|
state[i] = splitmix64(&seed);
|
2023-10-21 02:55:41 +00:00
|
|
|
mp_mutex_unlock(&state_mutex);
|
2022-08-15 01:28:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t mp_rand_next(void)
|
|
|
|
{
|
|
|
|
uint64_t result, t;
|
|
|
|
|
2023-10-21 02:55:41 +00:00
|
|
|
mp_mutex_lock(&state_mutex);
|
2022-08-15 01:28:54 +00:00
|
|
|
|
|
|
|
result = rotl_u64(state[1] * 5, 7) * 9;
|
|
|
|
t = state[1] << 17;
|
|
|
|
|
|
|
|
state[2] ^= state[0];
|
|
|
|
state[3] ^= state[1];
|
|
|
|
state[1] ^= state[2];
|
|
|
|
state[0] ^= state[3];
|
|
|
|
state[2] ^= t;
|
|
|
|
state[3] = rotl_u64(state[3], 45);
|
|
|
|
|
2023-10-21 02:55:41 +00:00
|
|
|
mp_mutex_unlock(&state_mutex);
|
2022-08-15 01:28:54 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
double mp_rand_next_double(void)
|
|
|
|
{
|
|
|
|
return (mp_rand_next() >> 11) * 0x1.0p-53;
|
|
|
|
}
|