aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaryll Strauss1999-12-04 19:19:48 -0600
committerDaryll Strauss1999-12-04 19:19:48 -0600
commitb3a5766992019fc5f44cc9afd01b2617b76f47a7 (patch)
tree8c7d4209618d11e15253351a0689de007f0105fe /libdrm/xf86drmRandom.c
downloadexternal-libgbm-b3a5766992019fc5f44cc9afd01b2617b76f47a7.tar.gz
external-libgbm-b3a5766992019fc5f44cc9afd01b2617b76f47a7.tar.xz
external-libgbm-b3a5766992019fc5f44cc9afd01b2617b76f47a7.zip
Initial revision
Diffstat (limited to 'libdrm/xf86drmRandom.c')
-rw-r--r--libdrm/xf86drmRandom.c219
1 files changed, 219 insertions, 0 deletions
diff --git a/libdrm/xf86drmRandom.c b/libdrm/xf86drmRandom.c
new file mode 100644
index 00000000..dad836d0
--- /dev/null
+++ b/libdrm/xf86drmRandom.c
@@ -0,0 +1,219 @@
1/* xf86drmRandom.c -- "Minimal Standard" PRNG Implementation
2 * Created: Mon Apr 19 08:28:13 1999 by faith@precisioninsight.com
3 * Revised: Thu Jun 24 14:53:45 1999 by faith@precisioninsight.com
4 *
5 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
6 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
26 *
27 * $PI: xc/programs/Xserver/hw/xfree86/os-support/linux/drm/xf86drmRandom.c,v 1.4 1999/06/24 18:54:55 faith Exp $
28 * $XFree86: xc/programs/Xserver/hw/xfree86/os-support/linux/drm/xf86drmRandom.c,v 1.2 1999/06/27 14:08:20 dawes Exp $
29 *
30 * DESCRIPTION
31 *
32 * This file contains a simple, straightforward implementation of the Park
33 * & Miller "Minimal Standard" PRNG [PM88, PMS93], which is a Lehmer
34 * multiplicative linear congruential generator (MLCG) with a period of
35 * 2^31-1.
36 *
37 * This implementation is intended to provide a reliable, portable PRNG
38 * that is suitable for testing a hash table implementation and for
39 * implementing skip lists.
40 *
41 * FUTURE ENHANCEMENTS
42 *
43 * If initial seeds are not selected randomly, two instances of the PRNG
44 * can be correlated. [Knuth81, pp. 32-33] describes a shuffling technique
45 * that can eliminate this problem.
46 *
47 * If PRNGs are used for simulation, the period of the current
48 * implementation may be too short. [LE88] discusses methods of combining
49 * MLCGs to produce much longer periods, and suggests some alternative
50 * values for A and M. [LE90 and Sch92] also provide information on
51 * long-period PRNGs.
52 *
53 * REFERENCES
54 *
55 * [Knuth81] Donald E. Knuth. The Art of Computer Programming. Volume 2:
56 * Seminumerical Algorithms. Reading, Massachusetts: Addison-Wesley, 1981.
57 *
58 * [LE88] Pierre L'Ecuyer. "Efficient and Portable Combined Random Number
59 * Generators". CACM 31(6), June 1988, pp. 742-774.
60 *
61 * [LE90] Pierre L'Ecuyer. "Random Numbers for Simulation". CACM 33(10,
62 * October 1990, pp. 85-97.
63 *
64 * [PM88] Stephen K. Park and Keith W. Miller. "Random Number Generators:
65 * Good Ones are Hard to Find". CACM 31(10), October 1988, pp. 1192-1201.
66 *
67 * [Sch92] Bruce Schneier. "Pseudo-Ransom Sequence Generator for 32-Bit
68 * CPUs". Dr. Dobb's Journal 17(2), February 1992, pp. 34, 37-38, 40.
69 *
70 * [PMS93] Stephen K. Park, Keith W. Miller, and Paul K. Stockmeyer. In
71 * "Technical Correspondence: Remarks on Choosing and Implementing Random
72 * Number Generators". CACM 36(7), July 1993, pp. 105-110.
73 *
74 */
75
76#define RANDOM_MAIN 0
77
78#if RANDOM_MAIN
79# include <stdio.h>
80# include <stdlib.h>
81#else
82# include "xf86drm.h"
83# ifdef XFree86LOADER
84# include "xf86.h"
85# include "xf86_ansic.h"
86# else
87# include <stdio.h>
88# include <stdlib.h>
89# endif
90#endif
91
92#define N(x) drm##x
93
94#define RANDOM_MAGIC 0xfeedbeef
95#define RANDOM_DEBUG 0
96
97#if RANDOM_MAIN
98#define RANDOM_ALLOC malloc
99#define RANDOM_FREE free
100#else
101#define RANDOM_ALLOC drmMalloc
102#define RANDOM_FREE drmFree
103#endif
104
105typedef struct RandomState {
106 unsigned long magic;
107 unsigned long a;
108 unsigned long m;
109 unsigned long q; /* m div a */
110 unsigned long r; /* m mod a */
111 unsigned long check;
112 long seed;
113} RandomState;
114
115#if RANDOM_MAIN
116extern void *N(RandomCreate)(unsigned long seed);
117extern int N(RandomDestroy)(void *state);
118extern unsigned long N(Random)(void *state);
119extern double N(RandomDouble)(void *state);
120#endif
121
122void *N(RandomCreate)(unsigned long seed)
123{
124 RandomState *state;
125
126 state = RANDOM_ALLOC(sizeof(*state));
127 if (!state) return NULL;
128 state->magic = RANDOM_MAGIC;
129#if 0
130 /* Park & Miller, October 1988 */
131 state->a = 16807;
132 state->m = 2147483647;
133 state->check = 1043618065; /* After 10000 iterations */
134#else
135 /* Park, Miller, and Stockmeyer, July 1993 */
136 state->a = 48271;
137 state->m = 2147483647;
138 state->check = 399268537; /* After 10000 iterations */
139#endif
140 state->q = state->m / state->a;
141 state->r = state->m % state->a;
142
143 state->seed = seed;
144 /* Check for illegal boundary conditions,
145 and choose closest legal value. */
146 if (state->seed <= 0) state->seed = 1;
147 if (state->seed >= state->m) state->seed = state->m - 1;
148
149 return state;
150}
151
152int N(RandomDestroy)(void *state)
153{
154 RANDOM_FREE(state);
155 return 0;
156}
157
158unsigned long N(Random)(void *state)
159{
160 RandomState *s = (RandomState *)state;
161 long hi;
162 long lo;
163
164 hi = s->seed / s->q;
165 lo = s->seed % s->q;
166 s->seed = s->a * lo - s->r * hi;
167 if (s->seed <= 0) s->seed += s->m;
168
169 return s->seed;
170}
171
172double N(RandomDouble)(void *state)
173{
174 RandomState *s = (RandomState *)state;
175
176 return (double)N(Random)(state)/(double)s->m;
177}
178
179#if RANDOM_MAIN
180static void check_period(long seed)
181{
182 unsigned long count = 0;
183 unsigned long initial;
184 void *state;
185
186 state = N(RandomCreate)(seed);
187 initial = N(Random)(state);
188 ++count;
189 while (initial != N(Random)(state)) {
190 if (!++count) break;
191 }
192 printf("With seed of %10ld, period = %10lu (0x%08lx)\n",
193 seed, count, count);
194 N(RandomDestroy)(state);
195}
196
197int main(void)
198{
199 RandomState *state;
200 int i;
201 unsigned long rand;
202
203 state = N(RandomCreate)(1);
204 for (i = 0; i < 10000; i++) {
205 rand = N(Random)(state);
206 }
207 printf("After 10000 iterations: %lu (%lu expected): %s\n",
208 rand, state->check,
209 rand - state->check ? "*INCORRECT*" : "CORRECT");
210 N(RandomDestroy)(state);
211
212 printf("Checking periods...\n");
213 check_period(1);
214 check_period(2);
215 check_period(31415926);
216
217 return 0;
218}
219#endif