xref: /titanic_41/usr/src/lib/libbc/libc/gen/common/drand48.c (revision 80e2ca8596e3435bc3b76f3c597833ea0a87f85e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1996 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*      Copyright (c) 1984 AT&T */
28 /*        All Rights Reserved   */
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 /*LINTLIBRARY*/
33 /*
34  *	drand48, etc. pseudo-random number generator
35  *	This implementation assumes unsigned short integers of at least
36  *	16 bits, long integers of at least 32 bits, and ignores
37  *	overflows on adding or multiplying two unsigned integers.
38  *	Two's-complement representation is assumed in a few places.
39  *	Some extra masking is done if unsigneds are exactly 16 bits
40  *	or longs are exactly 32 bits, but so what?
41  *	An assembly-language implementation would run significantly faster.
42  */
43 #define	N	16
44 #define	MASK	((unsigned)(1 << (N - 1)) + (1 << (N - 1)) - 1)
45 #define	LOW(x)	((unsigned)(x) & MASK)
46 #define	HIGH(x)	LOW((x) >> N)
47 #define	MUL(x, y, z)	{ long l = (long)(x) * (long)(y); \
48 		(z)[0] = LOW(l); (z)[1] = HIGH(l); }
49 #define	CARRY(x, y)	((long)(x) + (long)(y) > MASK)
50 #define	ADDEQU(x, y, z)	(z = CARRY(x, (y)), x = LOW(x + (y)))
51 #define	X0	0x330E
52 #define	X1	0xABCD
53 #define	X2	0x1234
54 #define	A0	0xE66D
55 #define	A1	0xDEEC
56 #define	A2	0x5
57 #define	C	0xB
58 #define	SET3(x, x0, x1, x2)	((x)[0] = (x0), (x)[1] = (x1), (x)[2] = (x2))
59 #define	SETLOW(x, y, n) SET3(x, LOW((y)[n]), LOW((y)[(n)+1]), LOW((y)[(n)+2]))
60 #define	SEED(x0, x1, x2) (SET3(x, x0, x1, x2), SET3(a, A0, A1, A2), c = C)
61 #define	REST(v)							\
62 	for (i = 0; i < 3; i++) {				\
63 		xsubi[i] = x[i];				\
64 		x[i] = temp[i];					\
65 	}							\
66 	return (v)
67 
68 #define	NEST(TYPE, f, F)					\
69 	TYPE f(xsubi)						\
70 	register unsigned short *xsubi;				\
71 	{							\
72 		register int i;					\
73 		register TYPE v;				\
74 		unsigned temp[3];				\
75 								\
76 		for (i = 0; i < 3; i++) {			\
77 			temp[i] = x[i];				\
78 			x[i] = LOW(xsubi[i]);			\
79 		}						\
80 		v = F();					\
81 		REST(v);					\
82 	}
83 
84 static unsigned x[3] = { X0, X1, X2 }, a[3] = { A0, A1, A2 }, c = C;
85 static unsigned short lastx[3];
86 static void next();
87 
88 double
89 drand48()
90 {
91 	static double two16m = 1.0 / (1L << N);
92 
93 	next();
94 	return (two16m * (two16m * (two16m * x[0] + x[1]) + x[2]));
95 }
96 
97 NEST(double, erand48, drand48)
98 
99 long
100 lrand48()
101 {
102 	next();
103 	return (((long)x[2] << (N - 1)) + (x[1] >> 1));
104 }
105 
106 long
107 mrand48()
108 {
109 	next();
110 	return (((long)x[2] << N) + x[1]);
111 }
112 
113 static void
114 next()
115 {
116 	unsigned p[2], q[2], r[2], carry0, carry1;
117 
118 	MUL(a[0], x[0], p);
119 	ADDEQU(p[0], c, carry0);
120 	ADDEQU(p[1], carry0, carry1);
121 	MUL(a[0], x[1], q);
122 	ADDEQU(p[1], q[0], carry0);
123 	MUL(a[1], x[0], r);
124 	x[2] = LOW(carry0 + carry1 + CARRY(p[1], r[0]) + q[1] + r[1] +
125 		a[0] * x[2] + a[1] * x[1] + a[2] * x[0]);
126 	x[1] = LOW(p[1] + r[0]);
127 	x[0] = LOW(p[0]);
128 }
129 
130 void
131 srand48(seedval)
132 long seedval;
133 {
134 	SEED(X0, LOW(seedval), HIGH(seedval));
135 }
136 
137 unsigned short *
138 seed48(seed16v)
139 unsigned short seed16v[3];
140 {
141 	SETLOW(lastx, x, 0);
142 	SEED(LOW(seed16v[0]), LOW(seed16v[1]), LOW(seed16v[2]));
143 	return (lastx);
144 }
145 
146 void
147 lcong48(param)
148 unsigned short param[7];
149 {
150 	SETLOW(x, param, 0);
151 	SETLOW(a, param, 3);
152 	c = LOW(param[6]);
153 }
154 
155 NEST(long, nrand48, lrand48)
156 
157 NEST(long, jrand48, mrand48)
158 
159 #ifdef DRIVER
160 /*
161  *	This should print the sequences of integers in Tables 2
162  *		and 1 of the TM:
163  *	1623, 3442, 1447, 1829, 1305, ...
164  *	657EB7255101, D72A0C966378, 5A743C062A23, ...
165  */
166 #include <stdio.h>
167 
168 main()
169 {
170 	int i;
171 
172 	for (i = 0; i < 80; i++) {
173 		printf("%4d ", (int)(4096 * drand48()));
174 		printf("%.4X%.4X%.4X\n", x[2], x[1], x[0]);
175 	}
176 }
177 #endif
178