Mercurial > libavutil.hg
annotate random.c @ 567:bd4052d9050c libavutil
Globally rename the header inclusion guard names.
Consistently apply this rule: the guard name is obtained from the
filename by stripping the leading "lib", converting '/' and '.' to
'_' and uppercasing the resulting name. Guard names in the root
directory have to be prefixed by "FFMPEG_".
author | stefano |
---|---|
date | Sun, 31 Aug 2008 07:39:47 +0000 |
parents | db7f6a006d4f |
children | 70367460f323 |
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 |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
24 |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
25 /** |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
26 see http://en.wikipedia.org/wiki/Mersenne_twister for an explanation of this algorithm. |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
27 */ |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
28 #include <stdio.h> |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
29 #include "random.h" |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
30 |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
31 |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
32 /* Period parameters */ |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
33 #define M 397 |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
34 #define A 0x9908b0df /* constant vector a */ |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
35 #define UPPER_MASK 0x80000000 /* most significant w-r bits */ |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
36 #define LOWER_MASK 0x7fffffff /* least significant r bits */ |
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 /** initializes mt[AV_RANDOM_N] with a seed */ |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
39 void av_init_random(unsigned int seed, 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 int index; |
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 /* |
540 | 44 This differs from the wikipedia article. Source is from the |
239
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
45 Makoto Matsumoto and Takuji Nishimura code, with the following comment: |
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 /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
48 /* In the previous versions, MSBs of the seed affect */ |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
49 /* only MSBs of the array mt[]. */ |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
50 state->mt[0] = seed & 0xffffffff; |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
51 for (index = 1; index < AV_RANDOM_N; index++) { |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
52 unsigned int prev= state->mt[index - 1]; |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
53 state->mt[index] = (1812433253UL * (prev ^ (prev>>30)) + index) & 0xffffffff; |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
54 } |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
55 state->index= index; // will cause it to generate untempered numbers the first iteration |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
56 } |
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 /** generate AV_RANDOM_N words at one time (which will then be tempered later) (av_random calls this; you shouldn't) */ |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
59 void av_random_generate_untempered_numbers(AVRandomState *state) |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
60 { |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
61 int kk; |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
62 unsigned int y; |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
63 |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
64 for (kk = 0; kk < AV_RANDOM_N - M; kk++) { |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
65 y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK); |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
66 state->mt[kk] = state->mt[kk + M] ^ (y >> 1) ^ ((y&1)*A); |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
67 } |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
68 for (; kk < AV_RANDOM_N - 1; kk++) { |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
69 y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK); |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
70 state->mt[kk] = state->mt[kk + (M - AV_RANDOM_N)] ^ (y >> 1) ^ ((y&1)*A); |
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 y = (state->mt[AV_RANDOM_N - 1] & UPPER_MASK) | (state->mt[0] & LOWER_MASK); |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
73 state->mt[AV_RANDOM_N - 1] = state->mt[M - 1] ^ (y >> 1) ^ ((y&1)*A); |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
74 state->index = 0; |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
75 } |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
76 |
528 | 77 #ifdef TEST |
530
f8a5cb24c164
Move #includes, which are only used in the test program, below the #ifdef
diego
parents:
529
diff
changeset
|
78 #include "common.h" |
f8a5cb24c164
Move #includes, which are only used in the test program, below the #ifdef
diego
parents:
529
diff
changeset
|
79 #include "log.h" |
531
eb47924a8b25
Change return type of main function to int to avoid a warning.
diego
parents:
530
diff
changeset
|
80 int main(void) |
239
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
81 { |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
82 int x=0; |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
83 int i, j; |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
84 AVRandomState state; |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
85 |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
86 av_init_random(0xdeadbeef, &state); |
529
98f03aca378b
Change benchmarking code so it returns comparable values.
michael
parents:
528
diff
changeset
|
87 for (j = 0; j < 10000; j++) { |
98f03aca378b
Change benchmarking code so it returns comparable values.
michael
parents:
528
diff
changeset
|
88 START_TIMER |
98f03aca378b
Change benchmarking code so it returns comparable values.
michael
parents:
528
diff
changeset
|
89 for (i = 0; i < 624; i++) { |
239
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
90 x+= av_random(&state); |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
91 } |
529
98f03aca378b
Change benchmarking code so it returns comparable values.
michael
parents:
528
diff
changeset
|
92 STOP_TIMER("624 calls of av_random"); |
239
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
93 } |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
94 av_log(NULL, AV_LOG_ERROR, "final value:%X\n", x); |
531
eb47924a8b25
Change return type of main function to int to avoid a warning.
diego
parents:
530
diff
changeset
|
95 return 0; |
239
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
96 } |
06fd8e5c5a04
av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff
changeset
|
97 #endif |