annotate random.c @ 460:9e9e727a9238 libavutil

simplify lzo-test rule
author mru
date Wed, 27 Feb 2008 21:38:36 +0000
parents 06fd8e5c5a04
children a5c3bc3dcd3e
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
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 //#define DEBUG
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
32
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
33 #ifdef DEBUG
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
34 #include "common.h"
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
35 #include "log.h"
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
36 #endif
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
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
39 /* Period parameters */
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
40 #define M 397
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
41 #define A 0x9908b0df /* constant vector a */
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
42 #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
43 #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
44
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
45 /** 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
46 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
47 {
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
48 int 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 /*
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
51 This differs from the wikipedia article. Source is from the Makoto
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
52 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
53 */
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
54 /* 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
55 /* 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
56 /* only MSBs of the array mt[]. */
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
57 state->mt[0] = seed & 0xffffffff;
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
58 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
59 unsigned int prev= state->mt[index - 1];
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
60 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
61 }
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
62 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
63 }
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 /** 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
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
84 #ifdef DEBUG
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
85 void av_benchmark_random(void)
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
86 {
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
87 int x=0;
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
88 int i, j;
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
89 AVRandomState state;
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
90
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
91 av_init_random(0xdeadbeef, &state);
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
92 for (j = 0; j < 100; j++) {
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
93 START_TIMER;
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
94 x+= av_random(&state);
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
95 STOP_TIMER("first call to av_random");
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
96 for (i = 1; i < AV_RANDOM_N; i++) {
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
97 START_TIMER;
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
98 x+= av_random(&state);
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
99 STOP_TIMER("AV_RANDOM_N calls of av_random");
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 }
06fd8e5c5a04 av_random() by Ryan Martell rdm4 name-server-seperator martellventures d0t com
michael
parents:
diff changeset
102 av_log(NULL, AV_LOG_ERROR, "final value:%X\n", x);
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