Update to 2.0.0 tree from current Fremantle build
[opencv] / 3rdparty / include / ffmpeg_ / random.h
1 /*
2  * Mersenne Twister PRNG algorithm
3  * Copyright (c) 2006 Ryan Martell
4  * Based on a C program for MT19937, with initialization improved 2002/1/26.
5  * Coded by Takuji Nishimura and Makoto Matsumoto.
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23
24 #ifndef AVUTIL_RANDOM_H
25 #define AVUTIL_RANDOM_H
26
27 #define AV_RANDOM_N 624
28
29 #include "avutil.h"
30 #include "common.h"
31
32 typedef struct {
33     unsigned int mt[AV_RANDOM_N]; ///< the array for the state vector
34     int index; ///< Current untempered value we use as the base.
35 } AVRandomState;
36
37
38 #if LIBAVUTIL_VERSION_MAJOR < 50
39 attribute_deprecated void av_init_random(unsigned int seed, AVRandomState *state);
40 #endif
41 void av_random_init(AVRandomState *state, unsigned int seed); ///< To be inlined, the struct must be visible. So it does not make sense to try and keep it opaque with malloc/free-like calls.
42 void av_random_generate_untempered_numbers(AVRandomState *state); ///< Regenerate the untempered numbers (must be done every 624 iterations, or it will loop).
43
44 /**
45  * Generates a random number from the interval [0,0xffffffff].
46  *
47  * Please do NOT use the Mersenne Twister, it is slow. Use the random number
48  * generator from lfg.c/h or a simple LCG like state = state*1664525+1013904223.
49  * If you still choose to use MT, expect that you will have to provide
50  * some evidence that it makes a difference for the case where you use it.
51  */
52 static inline unsigned int av_random(AVRandomState *state)
53 {
54     unsigned int y;
55
56     // Regenerate the untempered numbers if we should...
57     if (state->index >= AV_RANDOM_N)
58         av_random_generate_untempered_numbers(state);
59
60     // Grab one...
61     y = state->mt[state->index++];
62
63     /* Now temper (Mersenne Twister coefficients). The coefficients for MT19937 are.. */
64     y ^= (y >> 11);
65     y ^= (y << 7) & 0x9d2c5680;
66     y ^= (y << 15) & 0xefc60000;
67     y ^= (y >> 18);
68
69     return y;
70 }
71
72 /** Returns a random number in the range [0-1] as double. */
73 static inline double av_random_real1(AVRandomState *state)
74 {
75     /* divided by 2^32-1 */
76     return av_random(state) * (1.0 / 4294967296.0);
77 }
78
79 #endif /* AVUTIL_RANDOM_H */