Coverage Report

Created: 2025-12-11 06:55

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
32.0k
{
33
32.0k
    return (x << k) | (x >> (64 - k));
34
32.0k
}
35
36
static inline uint64_t splitmix64(uint64_t *const x)
37
270
{
38
270
    uint64_t z = (*x += UINT64_C(0x9e3779b97f4a7c15));
39
270
    z = (z ^ (z >> 30)) * UINT64_C(0xbf58476d1ce4e5b9);
40
270
    z = (z ^ (z >> 27)) * UINT64_C(0x94d049bb133111eb);
41
270
    return z ^ (z >> 31);
42
270
}
43
44
mp_rand_state mp_rand_seed(uint64_t seed)
45
90
{
46
90
    mp_rand_state ret;
47
48
90
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
49
90
    seed = 42;
50
90
#endif
51
52
90
    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
90
    ret.v[0] = seed;
62
360
    for (int i = 1; i < 4; i++)
63
270
        ret.v[i] = splitmix64(&seed);
64
90
    return ret;
65
90
}
66
67
uint64_t mp_rand_next(mp_rand_state *s)
68
16.0k
{
69
16.0k
    uint64_t result, t;
70
16.0k
    uint64_t *state = s->v;
71
72
16.0k
    result = rotl_u64(state[1] * 5, 7) * 9;
73
16.0k
    t = state[1] << 17;
74
75
16.0k
    state[2] ^= state[0];
76
16.0k
    state[3] ^= state[1];
77
16.0k
    state[1] ^= state[2];
78
16.0k
    state[0] ^= state[3];
79
16.0k
    state[2] ^= t;
80
16.0k
    state[3] = rotl_u64(state[3], 45);
81
82
16.0k
    return result;
83
16.0k
}
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
11.5k
{
94
11.5k
    mp_assert(min < max);
95
11.5k
    uint32_t range = max - min;
96
11.5k
    uint32_t mask = mp_round_next_power_of_2(range) - 1;
97
11.5k
    uint32_t ret;
98
16.0k
    do {
99
16.0k
        ret = mp_rand_next(s) & mask;
100
16.0k
    } while (ret >= range);
101
11.5k
    return min + ret;
102
11.5k
}