FFmpeg
sfc64.h
Go to the documentation of this file.
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 static inline uint64_t ff_sfc64_get(FFSFC64 *s) {
42  uint64_t tmp = s->a + s->b + s->counter++;
43  s->a = s->b ^ (s->b >> 11);
44  s->b = s->c + (s->c << 3); // This is a multiply by 9
45  s->c = (s->c << 24 | s->c >> 40) + tmp;
46  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 static inline void ff_sfc64_init(FFSFC64 *s, uint64_t seeda, uint64_t seedb, uint64_t seedc, int rounds) {
76  s->a = seeda;
77  s->b = seedb;
78  s->c = seedc;
79  s->counter = 1;
80  while (rounds--)
81  ff_sfc64_get(s);
82 }
83 
84 #endif // AVUTIL_SFC64_H
FFSFC64::b
uint64_t b
Definition: sfc64.h:38
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
ff_sfc64_init
static void ff_sfc64_init(FFSFC64 *s, uint64_t seeda, uint64_t seedb, uint64_t seedc, int rounds)
Initialize sfc64 with up to 3 seeds.
Definition: sfc64.h:75
FFSFC64
Definition: sfc64.h:37
FFSFC64::counter
uint64_t counter
Definition: sfc64.h:38
s
#define s(width, name)
Definition: cbs_vp9.c:198
ff_sfc64_reverse_get
static uint64_t ff_sfc64_reverse_get(FFSFC64 *s)
Return the previous random value, and step the generator backward.
Definition: sfc64.h:55
FFSFC64::c
uint64_t c
Definition: sfc64.h:38
FFSFC64::a
uint64_t a
Definition: sfc64.h:38
ff_sfc64_get
static uint64_t ff_sfc64_get(FFSFC64 *s)
Definition: sfc64.h:41