xref: /illumos-gate/usr/src/lib/libc/port/gen/drand48.c (revision cd61ae21816e53b94bc1673f3f1aa651fc3115e8)
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 "lint.h"
51 #include <mtlib.h>
52 #include <synch.h>
53 #include <thread.h>
54 
55 static mutex_t seed_lock = DEFAULTMUTEX;
56 
57 #define	EXPORT0(TYPE, fn, fnu)	TYPE fn() { \
58 	TYPE res; \
59 	lmutex_lock(&seed_lock); \
60 	res = fnu(); \
61 	lmutex_unlock(&seed_lock); \
62 	return (res); }
63 #define	EXPORT1(TYPE, fn, fnu)	TYPE fn(unsigned short xsubi[3]) { \
64 	TYPE res; \
65 	lmutex_lock(&seed_lock); \
66 	res = fnu(xsubi); \
67 	lmutex_unlock(&seed_lock); \
68 	return (res); }
69 
70 #define	N	16
71 #define	MASK	((unsigned)(1 << (N - 1)) + (1 << (N - 1)) - 1)
72 #define	LOW(x)	((unsigned)(x) & MASK)
73 #define	HIGH(x)	LOW((x) >> N)
74 #define	MUL(x, y, z)	{ int32_t l = (int32_t)(x) * (int32_t)(y); \
75 		(z)[0] = LOW(l); (z)[1] = HIGH(l); }
76 #define	CARRY(x, y)	((int32_t)(x) + (int32_t)(y) > MASK)
77 #define	ADDEQU(x, y, z)	(z = CARRY(x, (y)), x = LOW(x + (y)))
78 #define	X0	0x330E
79 #define	X1	0xABCD
80 #define	X2	0x1234
81 #define	A0	0xE66D
82 #define	A1	0xDEEC
83 #define	A2	0x5
84 #define	C	0xB
85 #define	SET3(x, x0, x1, x2)	((x)[0] = (x0), (x)[1] = (x1), (x)[2] = (x2))
86 #define	SETLOW(x, y, n) SET3(x, LOW((y)[n]), LOW((y)[(n)+1]), LOW((y)[(n)+2]))
87 #define	SEED(x0, x1, x2) (SET3(x, x0, x1, x2), SET3(a, A0, A1, A2), c = C)
88 #define	REST(v)	for (i = 0; i < 3; i++) { xsubi[i] = x[i]; x[i] = temp[i]; } \
89 		return (v)
90 #define	NEST(TYPE, f, F) static TYPE f(unsigned short *xsubi) { \
91 	int i; TYPE v; unsigned temp[3]; \
92 	for (i = 0; i < 3; i++) { temp[i] = x[i]; x[i] = LOW(xsubi[i]); }  \
93 	v = F(); REST(v); }
94 
95 /* Way ugly solution to problem names, but it works */
96 #define	x	_drand48_x
97 #define	a	_drand48_a
98 #define	c	_drand48_c
99 /* End way ugly */
100 static unsigned x[3] = { X0, X1, X2 }, a[3] = { A0, A1, A2 }, c = C;
101 static unsigned short lastx[3];
102 static void next(void);
103 
104 static double
105 _drand48_u(void)
106 {
107 	static double two16m = 1.0 / ((int32_t)1 << N);
108 
109 	next();
110 	return (two16m * (two16m * (two16m * x[0] + x[1]) + x[2]));
111 }
112 
113 NEST(double, _erand48_u, _drand48_u)
114 
115 static long
116 _lrand48_u(void)
117 {
118 	next();
119 	return ((long)((int32_t)x[2] << (N - 1)) + (x[1] >> 1));
120 }
121 
122 static long
123 _mrand48_u(void)
124 {
125 	next();
126 	return ((long)((int32_t)x[2] << N) + x[1]);
127 }
128 
129 static void
130 next(void)
131 {
132 	unsigned p[2], q[2], r[2], carry0, carry1;
133 
134 	MUL(a[0], x[0], p);
135 	ADDEQU(p[0], c, carry0);
136 	ADDEQU(p[1], carry0, carry1);
137 	MUL(a[0], x[1], q);
138 	ADDEQU(p[1], q[0], carry0);
139 	MUL(a[1], x[0], r);
140 	x[2] = LOW(carry0 + carry1 + CARRY(p[1], r[0]) + q[1] + r[1] +
141 	    a[0] * x[2] + a[1] * x[1] + a[2] * x[0]);
142 	x[1] = LOW(p[1] + r[0]);
143 	x[0] = LOW(p[0]);
144 }
145 
146 void
147 srand48(long seedval)
148 {
149 	int32_t fixseed = (int32_t)seedval;	/* limit to 32 bits */
150 
151 	lmutex_lock(&seed_lock);
152 	SEED(X0, LOW(fixseed), HIGH(fixseed));
153 	lmutex_unlock(&seed_lock);
154 }
155 
156 unsigned short *
157 seed48(unsigned short seed16v[3])
158 {
159 	lmutex_lock(&seed_lock);
160 	SETLOW(lastx, x, 0);
161 	SEED(LOW(seed16v[0]), LOW(seed16v[1]), LOW(seed16v[2]));
162 	lmutex_unlock(&seed_lock);
163 	return (lastx);
164 }
165 
166 void
167 lcong48(unsigned short param[7])
168 {
169 	lmutex_lock(&seed_lock);
170 	SETLOW(x, param, 0);
171 	SETLOW(a, param, 3);
172 	c = LOW(param[6]);
173 	lmutex_unlock(&seed_lock);
174 }
175 
176 NEST(long, _nrand48_u, _lrand48_u)
177 
178 NEST(long, _jrand48_u, _mrand48_u)
179 
180 EXPORT0(double, drand48, _drand48_u)
181 EXPORT1(double, erand48, _erand48_u)
182 
183 EXPORT0(long, lrand48, _lrand48_u)
184 EXPORT1(long, nrand48, _nrand48_u)
185 
186 EXPORT0(long, mrand48, _mrand48_u)
187 EXPORT1(long, jrand48, _jrand48_u)
188 
189 #ifdef DRIVER
190 /*
191  *	This should print the sequences of integers in Tables 2
192  *		and 1 of the TM:
193  *	1623, 3442, 1447, 1829, 1305, ...
194  *	657EB7255101, D72A0C966378, 5A743C062A23, ...
195  */
196 #include <stdio.h>
197 
198 main()
199 {
200 	int i;
201 
202 	for (i = 0; i < 80; i++) {
203 		printf("%4d ", (int)(4096 * drand48()));
204 		printf("%.4X%.4X%.4X\n", x[2], x[1], x[0]);
205 	}
206 }
207 #endif
208