xref: /illumos-gate/usr/src/uts/common/inet/ipf/drand48.c (revision 0f1702c5201310f0529cd5abb77652e5e9b241b6)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 /*
33  *	drand48, etc. pseudo-random number generator
34  *	This implementation assumes unsigned short integers of at least
35  *	16 bits, long integers of at least 32 bits, and ignores
36  *	overflows on adding or multiplying two unsigned integers.
37  *	Two's-complement representation is assumed in a few places.
38  *	Some extra masking is done if unsigneds are exactly 16 bits
39  *	or longs are exactly 32 bits, but so what?
40  *	An assembly-language implementation would run significantly faster.
41  */
42 /*
43  *	New assumptions (supercede those stated above) for 64-bit work.
44  *	Longs are now 64 bits, and we are bound by standards to return
45  *	type long, hovever all internal calculations where long was
46  *	previously used (32 bit precision) are now using the int32_t
47  *	type (32 bit precision in both ILP32 and LP64 worlds).
48  */
49 
50 #include <sys/mutex.h>
51 
52 static kmutex_t seed_lock;
53 static int	init48done = 0;
54 
55 #define	EXPORT0(TYPE, fn, fnu)	TYPE fn() { \
56 	TYPE res; \
57 	mutex_enter(&seed_lock); \
58 	res = fnu(); \
59 	mutex_exit(&seed_lock); \
60 	return (res); }
61 #define	EXPORT1(TYPE, fn, fnu)	TYPE fn(unsigned short xsubi[3]) { \
62 	TYPE res; \
63 	mutex_enter(&seed_lock); \
64 	res = fnu(xsubi); \
65 	mutex_exit(&seed_lock); \
66 	return (res); }
67 
68 #define	N	16
69 #define	MASK	((unsigned)(1 << (N - 1)) + (1 << (N - 1)) - 1)
70 #define	LOW(x)	((unsigned)(x) & MASK)
71 #define	HIGH(x)	LOW((x) >> N)
72 #define	MUL(x, y, z)	{ int32_t l = (int32_t)(x) * (int32_t)(y); \
73 		(z)[0] = LOW(l); (z)[1] = HIGH(l); }
74 #define	CARRY(x, y)	((int32_t)(x) + (int32_t)(y) > MASK)
75 #define	ADDEQU(x, y, z)	(z = CARRY(x, (y)), x = LOW(x + (y)))
76 #define	X0	0x330E
77 #define	X1	0xABCD
78 #define	X2	0x1234
79 #define	A0	0xE66D
80 #define	A1	0xDEEC
81 #define	A2	0x5
82 #define	C	0xB
83 #define	SET3(x, x0, x1, x2)	((x)[0] = (x0), (x)[1] = (x1), (x)[2] = (x2))
84 #define	SETLOW(x, y, n) SET3(x, LOW((y)[n]), LOW((y)[(n)+1]), LOW((y)[(n)+2]))
85 #define	SEED(x0, x1, x2) (SET3(x, x0, x1, x2), SET3(a, A0, A1, A2), c = C)
86 #define	REST(v)	for (i = 0; i < 3; i++) { xsubi[i] = x[i]; x[i] = temp[i]; } \
87 		return (v)
88 #define	NEST(TYPE, f, F) static TYPE f(unsigned short *xsubi) { \
89 	int i; TYPE v; unsigned temp[3]; \
90 	for (i = 0; i < 3; i++) { temp[i] = x[i]; x[i] = LOW(xsubi[i]); }  \
91 	v = F(); REST(v); }
92 
93 /* Way ugly solution to problem names, but it works */
94 #define	x	_drand48_x
95 #define	a	_drand48_a
96 #define	c	_drand48_c
97 /* End way ugly */
98 static unsigned x[3] = { X0, X1, X2 }, a[3] = { A0, A1, A2 }, c = C;
99 static unsigned short lastx[3];
100 static void next(void);
101 
102 static double
103 ipf_r_drand48_u(void)
104 {
105 	static double two16m = 1.0 / ((int32_t)1 << N);
106 
107 	next();
108 	return (two16m * (two16m * (two16m * x[0] + x[1]) + x[2]));
109 }
110 
111 NEST(double, ipf_r_erand48_u, ipf_r_drand48_u)
112 
113 static long
114 ipf_r_lrand48_u(void)
115 {
116 	next();
117 	return ((long)((int32_t)x[2] << (N - 1)) + (x[1] >> 1));
118 }
119 
120 static void
121 init48(void)
122 {
123 	mutex_init(&seed_lock, 0L, MUTEX_DRIVER, 0L);
124 	init48done = 1;
125 }
126 
127 static long
128 ipf_r_mrand48_u(void)
129 {
130 	next();
131 	return ((long)((int32_t)x[2] << N) + x[1]);
132 }
133 
134 static void
135 next(void)
136 {
137 	unsigned p[2], q[2], r[2], carry0, carry1;
138 
139 	MUL(a[0], x[0], p);
140 	ADDEQU(p[0], c, carry0);
141 	ADDEQU(p[1], carry0, carry1);
142 	MUL(a[0], x[1], q);
143 	ADDEQU(p[1], q[0], carry0);
144 	MUL(a[1], x[0], r);
145 	x[2] = LOW(carry0 + carry1 + CARRY(p[1], r[0]) + q[1] + r[1] +
146 		a[0] * x[2] + a[1] * x[1] + a[2] * x[0]);
147 	x[1] = LOW(p[1] + r[0]);
148 	x[0] = LOW(p[0]);
149 }
150 
151 void
152 ipf_r_srand48(long seedval)
153 {
154 	int32_t fixseed = (int32_t)seedval;	/* limit to 32 bits */
155 
156 	if (init48done == 0)
157 		init48();
158 	mutex_enter(&seed_lock);
159 	SEED(X0, LOW(fixseed), HIGH(fixseed));
160 	mutex_exit(&seed_lock);
161 }
162 
163 unsigned short *
164 ipf_r_seed48(unsigned short seed16v[3])
165 {
166 	if (init48done == 0)
167 		init48();
168 	mutex_enter(&seed_lock);
169 	SETLOW(lastx, x, 0);
170 	SEED(LOW(seed16v[0]), LOW(seed16v[1]), LOW(seed16v[2]));
171 	mutex_exit(&seed_lock);
172 	return (lastx);
173 }
174 
175 void
176 ipf_r_lcong48(unsigned short param[7])
177 {
178 	if (init48done == 0)
179 		init48();
180 	mutex_enter(&seed_lock);
181 	SETLOW(x, param, 0);
182 	SETLOW(a, param, 3);
183 	c = LOW(param[6]);
184 	mutex_exit(&seed_lock);
185 }
186 
187 NEST(long, ipf_r_nrand48_u, ipf_r_lrand48_u)
188 
189 NEST(long, ipf_r_jrand48_u, ipf_r_mrand48_u)
190 
191 EXPORT0(double, ipf_r_drand48, ipf_r_drand48_u)
192 EXPORT1(double, ipf_r_erand48, ipf_r_erand48_u)
193 
194 EXPORT0(long, ipf_r_lrand48, ipf_r_lrand48_u)
195 EXPORT1(long, ipf_r_nrand48, ipf_r_nrand48_u)
196 
197 EXPORT0(long, ipf_r_mrand48, ipf_r_mrand48_u)
198 EXPORT1(long, ipf_r_jrand48, ipf_r_jrand48_u)
199 
200 #ifdef DRIVER
201 /*
202 	This should print the sequences of integers in Tables 2
203 		and 1 of the TM:
204 	1623, 3442, 1447, 1829, 1305, ...
205 	657EB7255101, D72A0C966378, 5A743C062A23, ...
206  */
207 #include <stdio.h>
208 
209 main()
210 {
211 	int i;
212 
213 	for (i = 0; i < 80; i++) {
214 		printf("%4d ", (int)(4096 * ipf_r_drand48()));
215 		printf("%.4X%.4X%.4X\n", x[2], x[1], x[0]);
216 	}
217 }
218 #else
219 
220 #include <sys/random.h>
221 
222 unsigned
223 ipf_random()
224 {
225 	static int seeded = 0;
226 
227 	if (seeded == 0) {
228 		long seed;
229 
230 		/*
231 		 * Keep reseeding until some good randomness comes from the
232 		 * kernel. One of two things will happen: it will succeed or
233 		 * it will fail (with poor randomness), thus creating NAT
234 		 * sessions will be "slow" until enough randomness is gained
235 		 * to not need to get more. It isn't necessary to initialise
236 		 * seed as it will just pickup whatever random garbage has
237 		 * been left on the heap and that's good enough until we
238 		 * get some good garbage.
239 		 */
240 		if (random_get_bytes((uint8_t *)&seed, sizeof (seed)) == 0)
241 			seeded = 1;
242 		ipf_r_srand48(seed);
243 	}
244 
245 	return (unsigned)ipf_r_lrand48();
246 }
247 #endif
248