Coverage Report

Created: 2025-12-27 07:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mpv/misc/random.c
Line
Count
Source
1
/*
2
 * Implementation of non-cryptographic pseudo-random number
3
 * generator algorithm known as xoshiro.
4
 *
5
 * This file is part of mpv.
6
 *
7
 * mpv is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * mpv is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with mpv.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
#include <stdint.h>
22
23
#include <libavutil/random_seed.h>
24
25
#include "common/common.h"
26
#include "misc/mp_assert.h"
27
#include "osdep/timer.h"
28
#include "random.h"
29
30
31
static inline uint64_t rotl_u64(const uint64_t x, const int k)
32
34.6k
{
33
34.6k
    return (x << k) | (x >> (64 - k));
34
34.6k
}
35
36
static inline uint64_t splitmix64(uint64_t *const x)
37
378
{
38
378
    uint64_t z = (*x += UINT64_C(0x9e3779b97f4a7c15));
39
378
    z = (z ^ (z >> 30)) * UINT64_C(0xbf58476d1ce4e5b9);
40
378
    z = (z ^ (z >> 27)) * UINT64_C(0x94d049bb133111eb);
41
378
    return z ^ (z >> 31);
42
378
}
43
44
mp_rand_state mp_rand_seed(uint64_t seed)
45
126
{
46
126
    mp_rand_state ret;
47
48
126
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
49
126
    seed = 42;
50
126
#endif
51
52
126
    if (seed == 0) {
53
0
        if (av_random_bytes((void *)ret.v, sizeof(ret.v)) == 0) {
54
0
            return ret;
55
0
        }
56
0
        seed = mp_raw_time_ns();
57
0
        seed ^= (uintptr_t)&mp_rand_seed; // ASLR, hopefully
58
0
        seed += (uintptr_t)&ret; // stack position
59
0
    }
60
61
126
    ret.v[0] = seed;
62
504
    for (int i = 1; i < 4; i++)
63
378
        ret.v[i] = splitmix64(&seed);
64
126
    return ret;
65
126
}
66
67
uint64_t mp_rand_next(mp_rand_state *s)
68
17.3k
{
69
17.3k
    uint64_t result, t;
70
17.3k
    uint64_t *state = s->v;
71
72
17.3k
    result = rotl_u64(state[1] * 5, 7) * 9;
73
17.3k
    t = state[1] << 17;
74
75
17.3k
    state[2] ^= state[0];
76
17.3k
    state[3] ^= state[1];
77
17.3k
    state[1] ^= state[2];
78
17.3k
    state[0] ^= state[3];
79
17.3k
    state[2] ^= t;
80
17.3k
    state[3] = rotl_u64(state[3], 45);
81
82
17.3k
    return result;
83
17.3k
}
84
85
double mp_rand_next_double(mp_rand_state *s)
86
0
{
87
0
    return (mp_rand_next(s) >> 11) * 0x1.0p-53;
88
0
}
89
90
// <https://web.archive.org/web/20250321082025/
91
// https://www.pcg-random.org/posts/bounded-rands.html#bitmask-with-rejection-unbiased-apples-method>
92
uint32_t mp_rand_in_range32(mp_rand_state *s, uint32_t min, uint32_t max)
93
12.4k
{
94
12.4k
    mp_assert(min < max);
95
12.4k
    uint32_t range = max - min;
96
12.4k
    uint32_t mask = mp_round_next_power_of_2(range) - 1;
97
12.4k
    uint32_t ret;
98
17.3k
    do {
99
17.3k
        ret = mp_rand_next(s) & mask;
100
17.3k
    } while (ret >= range);
101
12.4k
    return min + ret;
102
12.4k
}