fix up radeon span functions using latest r200 code from Brian,
[mesa.git] / src / mesa / drivers / dri / dri_client / xf86drmRandom.c
1 /* xf86drmRandom.c -- "Minimal Standard" PRNG Implementation
2 * Created: Mon Apr 19 08:28:13 1999 by faith@precisioninsight.com
3 *
4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 *
26 * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
27 *
28 * $XFree86: xc/programs/Xserver/hw/xfree86/os-support/linux/drm/xf86drmRandom.c,v 1.4 2000/06/17 00:03:34 martin 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 RANDOM_MAGIC 0xfeedbeef
93 #define RANDOM_DEBUG 0
94
95 #if RANDOM_MAIN
96 #define RANDOM_ALLOC malloc
97 #define RANDOM_FREE free
98 #else
99 #define RANDOM_ALLOC drmMalloc
100 #define RANDOM_FREE drmFree
101 #endif
102
103 typedef struct RandomState {
104 unsigned long magic;
105 unsigned long a;
106 unsigned long m;
107 unsigned long q; /* m div a */
108 unsigned long r; /* m mod a */
109 unsigned long check;
110 long seed;
111 } RandomState;
112
113 #if RANDOM_MAIN
114 extern void *drmRandomCreate(unsigned long seed);
115 extern int drmRandomDestroy(void *state);
116 extern unsigned long drmRandom(void *state);
117 extern double drmRandomDouble(void *state);
118 #endif
119
120 void *drmRandomCreate(unsigned long seed)
121 {
122 RandomState *state;
123
124 state = RANDOM_ALLOC(sizeof(*state));
125 if (!state) return NULL;
126 state->magic = RANDOM_MAGIC;
127 #if 0
128 /* Park & Miller, October 1988 */
129 state->a = 16807;
130 state->m = 2147483647;
131 state->check = 1043618065; /* After 10000 iterations */
132 #else
133 /* Park, Miller, and Stockmeyer, July 1993 */
134 state->a = 48271;
135 state->m = 2147483647;
136 state->check = 399268537; /* After 10000 iterations */
137 #endif
138 state->q = state->m / state->a;
139 state->r = state->m % state->a;
140
141 state->seed = seed;
142 /* Check for illegal boundary conditions,
143 and choose closest legal value. */
144 if (state->seed <= 0) state->seed = 1;
145 if (state->seed >= state->m) state->seed = state->m - 1;
146
147 return state;
148 }
149
150 int drmRandomDestroy(void *state)
151 {
152 RANDOM_FREE(state);
153 return 0;
154 }
155
156 unsigned long drmRandom(void *state)
157 {
158 RandomState *s = (RandomState *)state;
159 long hi;
160 long lo;
161
162 hi = s->seed / s->q;
163 lo = s->seed % s->q;
164 s->seed = s->a * lo - s->r * hi;
165 if (s->seed <= 0) s->seed += s->m;
166
167 return s->seed;
168 }
169
170 double drmRandomDouble(void *state)
171 {
172 RandomState *s = (RandomState *)state;
173
174 return (double)drmRandom(state)/(double)s->m;
175 }
176
177 #if RANDOM_MAIN
178 static void check_period(long seed)
179 {
180 unsigned long count = 0;
181 unsigned long initial;
182 void *state;
183
184 state = drmRandomCreate(seed);
185 initial = drmRandom(state);
186 ++count;
187 while (initial != drmRandom(state)) {
188 if (!++count) break;
189 }
190 printf("With seed of %10ld, period = %10lu (0x%08lx)\n",
191 seed, count, count);
192 drmRandomDestroy(state);
193 }
194
195 int main(void)
196 {
197 RandomState *state;
198 int i;
199 unsigned long rand;
200
201 state = drmRandomCreate(1);
202 for (i = 0; i < 10000; i++) {
203 rand = drmRandom(state);
204 }
205 printf("After 10000 iterations: %lu (%lu expected): %s\n",
206 rand, state->check,
207 rand - state->check ? "*INCORRECT*" : "CORRECT");
208 drmRandomDestroy(state);
209
210 printf("Checking periods...\n");
211 check_period(1);
212 check_period(2);
213 check_period(31415926);
214
215 return 0;
216 }
217 #endif