annotate random.c @ 627:d4b82de2ce47 libavutil

Disambiguate the macros used to disable fprintf/printf/puts functions.
author diego
date Sun, 25 Jan 2009 22:05:26 +0000
parents ed203c477511
children 8c48a1b999a3
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
613
ed203c477511 cosmetics: Remove pointless period after copyright statement non-sentences.
diego
parents: 612
diff changeset
3 * Copyright (c) 2006 Ryan Martell
239
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 */
609
70367460f323 Deprecate av_init_random() in favour of av_random_init(), with a more natural
stefano
parents: 540
diff changeset
39 void av_random_init(AVRandomState *state, unsigned int seed)
239
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
db7f6a006d4f misc spelling/wording fixes
diego
parents: 531
diff changeset
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
612
84d82b1dcd99 Drop the deprecated av_init_random() at the next libavutil major bump.
stefano
parents: 611
diff changeset
58 #if LIBAVUTIL_VERSION_MAJOR < 50
609
70367460f323 Deprecate av_init_random() in favour of av_random_init(), with a more natural
stefano
parents: 540
diff changeset
59 void av_init_random(unsigned int seed, AVRandomState *state)
70367460f323 Deprecate av_init_random() in favour of av_random_init(), with a more natural
stefano
parents: 540
diff changeset
60 {
70367460f323 Deprecate av_init_random() in favour of av_random_init(), with a more natural
stefano
parents: 540
diff changeset
61 av_random_init(state, seed);
70367460f323 Deprecate av_init_random() in favour of av_random_init(), with a more natural
stefano
parents: 540
diff changeset
62 }
612
84d82b1dcd99 Drop the deprecated av_init_random() at the next libavutil major bump.
stefano
parents: 611
diff changeset
63 #endif
609
70367460f323 Deprecate av_init_random() in favour of av_random_init(), with a more natural
stefano
parents: 540
diff changeset
64
239
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
65 /** 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
66 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
67 {
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
68 int kk;
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
69 unsigned int y;
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
70
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
71 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
72 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
73 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
74 }
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
75 for (; kk < AV_RANDOM_N - 1; kk++) {
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
76 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
77 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
78 }
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
79 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
80 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
81 state->index = 0;
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
82 }
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
83
528
a5c3bc3dcd3e Make the selftesting code use and compileable.
michael
parents: 239
diff changeset
84 #ifdef TEST
530
f8a5cb24c164 Move #includes, which are only used in the test program, below the #ifdef
diego
parents: 529
diff changeset
85 #include "common.h"
f8a5cb24c164 Move #includes, which are only used in the test program, below the #ifdef
diego
parents: 529
diff changeset
86 #include "log.h"
531
eb47924a8b25 Change return type of main function to int to avoid a warning.
diego
parents: 530
diff changeset
87 int main(void)
239
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
88 {
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
89 int x=0;
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
90 int i, j;
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
91 AVRandomState state;
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
92
611
a837b715e7d6 Replace calls to the deprecated function av_init_random() with
stefano
parents: 609
diff changeset
93 av_random_init(&state, 0xdeadbeef);
529
98f03aca378b Change benchmarking code so it returns comparable values.
michael
parents: 528
diff changeset
94 for (j = 0; j < 10000; j++) {
98f03aca378b Change benchmarking code so it returns comparable values.
michael
parents: 528
diff changeset
95 START_TIMER
98f03aca378b Change benchmarking code so it returns comparable values.
michael
parents: 528
diff changeset
96 for (i = 0; i < 624; i++) {
239
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
97 x+= av_random(&state);
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
98 }
529
98f03aca378b Change benchmarking code so it returns comparable values.
michael
parents: 528
diff changeset
99 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
100 }
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
101 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
102 return 0;
239
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
103 }
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
104 #endif