Mercurial > libavutil.hg
annotate random.h @ 496:b55c2d61403d libavutil
cosmetics: __asm__ __volatile__ --> asm volatile
author | diego |
---|---|
date | Thu, 17 Apr 2008 21:57:52 +0000 |
parents | d0f3bb6e367e |
children | a5c3bc3dcd3e |
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 | 24 #ifndef FFMPEG_RANDOM_H |
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 |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
31 int index; ///< current untempered value we use as the base. |
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 |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
35 void av_init_random(unsigned int seed, AVRandomState *state); ///< to be inlined, the struct must be visible, so it doesn't make sense to try and keep it opaque with malloc/free like calls |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
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) |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
37 |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
38 /** generates a random number on [0,0xffffffff]-interval */ |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
39 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
|
40 { |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
41 unsigned int y; |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
42 |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
43 // regenerate the untempered numbers if we should... |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
44 if (state->index >= AV_RANDOM_N) |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
45 av_random_generate_untempered_numbers(state); |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
46 |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
47 // grab one... |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
48 y = state->mt[state->index++]; |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
49 |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
50 /* Now temper (Mersenne Twister coefficients) The coefficients for MT19937 are.. */ |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
51 y ^= (y >> 11); |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
52 y ^= (y << 7) & 0x9d2c5680; |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
53 y ^= (y << 15) & 0xefc60000; |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
54 y ^= (y >> 18); |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
55 |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
56 return y; |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
57 } |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
58 |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
59 /** return random in range [0-1] as double */ |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
60 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
|
61 { |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
62 /* divided by 2^32-1 */ |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
63 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
|
64 } |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
65 |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
66 // only available if DEBUG is defined in the .c file |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
67 void av_benchmark_random(void); |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
68 |
392 | 69 #endif /* FFMPEG_RANDOM_H */ |