annotate random.h @ 546:b4e91071297f libavutil

Cosmetics: fix weird indent.
author stefano
date Fri, 08 Aug 2008 19:50:35 +0000
parents db7f6a006d4f
children bd4052d9050c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
239
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
1 /*
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
2 * Mersenne Twister Random Algorithm
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
3 * Copyright (c) 2006 Ryan Martell.
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
4 * Based on A C-program for MT19937, with initialization improved 2002/1/26. Coded by
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
5 * Takuji Nishimura and Makoto Matsumoto.
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
6 *
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
7 * This file is part of FFmpeg.
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
8 *
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
9 * FFmpeg is free software; you can redistribute it and/or
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
10 * modify it under the terms of the GNU Lesser General Public
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
11 * License as published by the Free Software Foundation; either
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
12 * version 2.1 of the License, or (at your option) any later version.
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
13 *
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
14 * FFmpeg is distributed in the hope that it will be useful,
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
17 * Lesser General Public License for more details.
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
18 *
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
19 * You should have received a copy of the GNU Lesser General Public
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
20 * License along with FFmpeg; if not, write to the Free Software
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
22 */
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
23
392
d0f3bb6e367e Add FFMPEG_ prefix to all multiple inclusion guards.
diego
parents: 239
diff changeset
24 #ifndef FFMPEG_RANDOM_H
d0f3bb6e367e Add FFMPEG_ prefix to all multiple inclusion guards.
diego
parents: 239
diff changeset
25 #define FFMPEG_RANDOM_H
239
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
26
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
27 #define AV_RANDOM_N 624
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
28
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
29 typedef struct {
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
30 unsigned int mt[AV_RANDOM_N]; ///< the array for the state vector
540
db7f6a006d4f misc spelling/wording fixes
diego
parents: 534
diff changeset
31 int index; ///< Current untempered value we use as the base.
239
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
32 } AVRandomState;
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
33
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
34
540
db7f6a006d4f misc spelling/wording fixes
diego
parents: 534
diff changeset
35 void av_init_random(unsigned int seed, AVRandomState *state); ///< 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.
db7f6a006d4f misc spelling/wording fixes
diego
parents: 534
diff changeset
36 void av_random_generate_untempered_numbers(AVRandomState *state); ///< Regenerate the untempered numbers (must be done every 624 iterations, or it will loop).
239
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
37
534
0f5a73051b25 Warning about MTs speed.
michael
parents: 528
diff changeset
38 /**
540
db7f6a006d4f misc spelling/wording fixes
diego
parents: 534
diff changeset
39 * Generates a random number from the interval [0,0xffffffff].
534
0f5a73051b25 Warning about MTs speed.
michael
parents: 528
diff changeset
40 *
540
db7f6a006d4f misc spelling/wording fixes
diego
parents: 534
diff changeset
41 * Please do NOT use the Mersenne Twister, it is slow. Use the random generator
534
0f5a73051b25 Warning about MTs speed.
michael
parents: 528
diff changeset
42 * from lfg.c/h or a simple LCG like state= state*1664525+1013904223.
0f5a73051b25 Warning about MTs speed.
michael
parents: 528
diff changeset
43 * If you still choose to use MT, expect that you will have to provide
0f5a73051b25 Warning about MTs speed.
michael
parents: 528
diff changeset
44 * some evidence that it makes a difference for the case where you use it.
0f5a73051b25 Warning about MTs speed.
michael
parents: 528
diff changeset
45 */
239
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
46 static inline unsigned int av_random(AVRandomState *state)
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
47 {
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
48 unsigned int y;
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
49
540
db7f6a006d4f misc spelling/wording fixes
diego
parents: 534
diff changeset
50 // Regenerate the untempered numbers if we should...
239
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
51 if (state->index >= AV_RANDOM_N)
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
52 av_random_generate_untempered_numbers(state);
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
53
540
db7f6a006d4f misc spelling/wording fixes
diego
parents: 534
diff changeset
54 // Grab one...
239
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
55 y = state->mt[state->index++];
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
56
540
db7f6a006d4f misc spelling/wording fixes
diego
parents: 534
diff changeset
57 /* Now temper (Mersenne Twister coefficients). The coefficients for MT19937 are.. */
239
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
58 y ^= (y >> 11);
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
59 y ^= (y << 7) & 0x9d2c5680;
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
60 y ^= (y << 15) & 0xefc60000;
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
61 y ^= (y >> 18);
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
62
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
63 return y;
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
64 }
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
65
540
db7f6a006d4f misc spelling/wording fixes
diego
parents: 534
diff changeset
66 /** Return random in range [0-1] as double. */
239
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
67 static inline double av_random_real1(AVRandomState *state)
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
68 {
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
69 /* divided by 2^32-1 */
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
70 return av_random(state) * (1.0 / 4294967296.0);
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
71 }
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
72
392
d0f3bb6e367e Add FFMPEG_ prefix to all multiple inclusion guards.
diego
parents: 239
diff changeset
73 #endif /* FFMPEG_RANDOM_H */