Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Copyright (c) 2024 Michael Niedermayer <michael-ffmpeg@niedermayer.cc> | ||
3 | * | ||
4 | * This file is part of FFmpeg. | ||
5 | * | ||
6 | * FFmpeg is free software; you can redistribute it and/or | ||
7 | * modify it under the terms of the GNU Lesser General Public | ||
8 | * License as published by the Free Software Foundation; either | ||
9 | * version 2.1 of the License, or (at your option) any later version. | ||
10 | * | ||
11 | * FFmpeg is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
14 | * Lesser General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU Lesser General Public | ||
17 | * License along with FFmpeg; if not, write to the Free Software | ||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
19 | * | ||
20 | */ | ||
21 | |||
22 | /** | ||
23 | * @file | ||
24 | * simple Pseudo Random Number Generator | ||
25 | * | ||
26 | * This is a implementation of SFC64, a 64-bit PRNG by Chris Doty-Humphrey. | ||
27 | * | ||
28 | * This Generator is much faster (0m1.872s) than 64bit KISS (0m3.823s) and PCG-XSH-RR-64/32 (0m2.700s) | ||
29 | * And passes testu01 and practrand test suits. | ||
30 | */ | ||
31 | |||
32 | #ifndef AVUTIL_SFC64_H | ||
33 | #define AVUTIL_SFC64_H | ||
34 | |||
35 | #include <inttypes.h> | ||
36 | |||
37 | typedef struct FFSFC64 { | ||
38 | uint64_t a,b,c,counter; | ||
39 | } FFSFC64; | ||
40 | |||
41 | 265561 | static inline uint64_t ff_sfc64_get(FFSFC64 *s) { | |
42 | 265561 | uint64_t tmp = s->a + s->b + s->counter++; | |
43 | 265561 | s->a = s->b ^ (s->b >> 11); | |
44 | 265561 | s->b = s->c + (s->c << 3); // This is a multiply by 9 | |
45 | 265561 | s->c = (s->c << 24 | s->c >> 40) + tmp; | |
46 | 265561 | return tmp; | |
47 | } | ||
48 | |||
49 | /** | ||
50 | * Return the previous random value, and step the generator backward. | ||
51 | * | ||
52 | * It is safe to take values before the first, but such values can be highly | ||
53 | * correlated to the seeds. | ||
54 | */ | ||
55 | static inline uint64_t ff_sfc64_reverse_get(FFSFC64 *s) { | ||
56 | uint64_t prev_c = s->b * 0x8E38E38E38E38E39; | ||
57 | uint64_t tmp = s->c - (prev_c << 24 | prev_c >> 40); | ||
58 | s->b = s->a ^ (s->a >> 11); | ||
59 | s->b ^= s->b >> 22; | ||
60 | s->b ^= s->b >> 44; | ||
61 | |||
62 | s->a = tmp - s->b - --s->counter; | ||
63 | s->c = prev_c; | ||
64 | |||
65 | return tmp; | ||
66 | } | ||
67 | |||
68 | /** | ||
69 | * Initialize sfc64 with up to 3 seeds. | ||
70 | * | ||
71 | * @param rounds number of rounds mixing up state during init. Generally 8-18, larger numbers will help with bad quality seeds. | ||
72 | * 12 is a good choice if all 3 seeds are equal | ||
73 | * | ||
74 | */ | ||
75 | 54 | static inline void ff_sfc64_init(FFSFC64 *s, uint64_t seeda, uint64_t seedb, uint64_t seedc, int rounds) { | |
76 | 54 | s->a = seeda; | |
77 | 54 | s->b = seedb; | |
78 | 54 | s->c = seedc; | |
79 | 54 | s->counter = 1; | |
80 |
2/2✓ Branch 0 taken 648 times.
✓ Branch 1 taken 54 times.
|
702 | while (rounds--) |
81 | 648 | ff_sfc64_get(s); | |
82 | 54 | } | |
83 | |||
84 | #endif // AVUTIL_SFC64_H | ||
85 |