xref: /illumos-gate/usr/src/lib/libc/port/gen/drand48.c (revision 03831d35)
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 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*	Copyright (c) 1988 AT&T	*/
30 /*	  All Rights Reserved  	*/
31 
32 
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 /*
44  *	New assumptions (supercede those stated above) for 64-bit work.
45  *	Longs are now 64 bits, and we are bound by standards to return
46  *	type long, hovever all internal calculations where long was
47  *	previously used (32 bit precision) are now using the int32_t
48  *	type (32 bit precision in both ILP32 and LP64 worlds).
49  */
50 #pragma weak drand48 = _drand48
51 #pragma weak erand48 = _erand48
52 #pragma weak lrand48 = _lrand48
53 #pragma weak mrand48 = _mrand48
54 #pragma weak srand48 = _srand48
55 #pragma weak seed48 = _seed48
56 #pragma weak lcong48 = _lcong48
57 #pragma weak nrand48 = _nrand48
58 #pragma weak jrand48 = _jrand48
59 
60 #include "synonyms.h"
61 #include <mtlib.h>
62 #include <synch.h>
63 #include <thread.h>
64 
65 static mutex_t seed_lock = DEFAULTMUTEX;
66 
67 #define	EXPORT0(TYPE, fn, fnu)	TYPE fn() { \
68 	TYPE res; \
69 	lmutex_lock(&seed_lock); \
70 	res = fnu(); \
71 	lmutex_unlock(&seed_lock); \
72 	return (res); }
73 #define	EXPORT1(TYPE, fn, fnu)	TYPE fn(unsigned short xsubi[3]) { \
74 	TYPE res; \
75 	lmutex_lock(&seed_lock); \
76 	res = fnu(xsubi); \
77 	lmutex_unlock(&seed_lock); \
78 	return (res); }
79 
80 #define	N	16
81 #define	MASK	((unsigned)(1 << (N - 1)) + (1 << (N - 1)) - 1)
82 #define	LOW(x)	((unsigned)(x) & MASK)
83 #define	HIGH(x)	LOW((x) >> N)
84 #define	MUL(x, y, z)	{ int32_t l = (int32_t)(x) * (int32_t)(y); \
85 		(z)[0] = LOW(l); (z)[1] = HIGH(l); }
86 #define	CARRY(x, y)	((int32_t)(x) + (int32_t)(y) > MASK)
87 #define	ADDEQU(x, y, z)	(z = CARRY(x, (y)), x = LOW(x + (y)))
88 #define	X0	0x330E
89 #define	X1	0xABCD
90 #define	X2	0x1234
91 #define	A0	0xE66D
92 #define	A1	0xDEEC
93 #define	A2	0x5
94 #define	C	0xB
95 #define	SET3(x, x0, x1, x2)	((x)[0] = (x0), (x)[1] = (x1), (x)[2] = (x2))
96 #define	SETLOW(x, y, n) SET3(x, LOW((y)[n]), LOW((y)[(n)+1]), LOW((y)[(n)+2]))
97 #define	SEED(x0, x1, x2) (SET3(x, x0, x1, x2), SET3(a, A0, A1, A2), c = C)
98 #define	REST(v)	for (i = 0; i < 3; i++) { xsubi[i] = x[i]; x[i] = temp[i]; } \
99 		return (v)
100 #define	NEST(TYPE, f, F) static TYPE f(unsigned short *xsubi) { \
101 	int i; TYPE v; unsigned temp[3]; \
102 	for (i = 0; i < 3; i++) { temp[i] = x[i]; x[i] = LOW(xsubi[i]); }  \
103 	v = F(); REST(v); }
104 
105 /* Way ugly solution to problem names, but it works */
106 #define	x	_drand48_x
107 #define	a	_drand48_a
108 #define	c	_drand48_c
109 /* End way ugly */
110 static unsigned x[3] = { X0, X1, X2 }, a[3] = { A0, A1, A2 }, c = C;
111 static unsigned short lastx[3];
112 static void next(void);
113 
114 static double
115 _drand48_u(void)
116 {
117 	static double two16m = 1.0 / ((int32_t)1 << N);
118 
119 	next();
120 	return (two16m * (two16m * (two16m * x[0] + x[1]) + x[2]));
121 }
122 
123 NEST(double, _erand48_u, _drand48_u)
124 
125 static long
126 _lrand48_u(void)
127 {
128 	next();
129 	return ((long)((int32_t)x[2] << (N - 1)) + (x[1] >> 1));
130 }
131 
132 static long
133 _mrand48_u(void)
134 {
135 	next();
136 	return ((long)((int32_t)x[2] << N) + x[1]);
137 }
138 
139 static void
140 next(void)
141 {
142 	unsigned p[2], q[2], r[2], carry0, carry1;
143 
144 	MUL(a[0], x[0], p);
145 	ADDEQU(p[0], c, carry0);
146 	ADDEQU(p[1], carry0, carry1);
147 	MUL(a[0], x[1], q);
148 	ADDEQU(p[1], q[0], carry0);
149 	MUL(a[1], x[0], r);
150 	x[2] = LOW(carry0 + carry1 + CARRY(p[1], r[0]) + q[1] + r[1] +
151 		a[0] * x[2] + a[1] * x[1] + a[2] * x[0]);
152 	x[1] = LOW(p[1] + r[0]);
153 	x[0] = LOW(p[0]);
154 }
155 
156 void
157 _srand48(long seedval)
158 {
159 	int32_t fixseed = (int32_t)seedval;	/* limit to 32 bits */
160 
161 	lmutex_lock(&seed_lock);
162 	SEED(X0, LOW(fixseed), HIGH(fixseed));
163 	lmutex_unlock(&seed_lock);
164 }
165 
166 unsigned short *
167 seed48(unsigned short seed16v[3])
168 {
169 	lmutex_lock(&seed_lock);
170 	SETLOW(lastx, x, 0);
171 	SEED(LOW(seed16v[0]), LOW(seed16v[1]), LOW(seed16v[2]));
172 	lmutex_unlock(&seed_lock);
173 	return (lastx);
174 }
175 
176 void
177 lcong48(unsigned short param[7])
178 {
179 	lmutex_lock(&seed_lock);
180 	SETLOW(x, param, 0);
181 	SETLOW(a, param, 3);
182 	c = LOW(param[6]);
183 	lmutex_unlock(&seed_lock);
184 }
185 
186 NEST(long, _nrand48_u, _lrand48_u)
187 
188 NEST(long, _jrand48_u, _mrand48_u)
189 
190 EXPORT0(double, _drand48, _drand48_u)
191 EXPORT1(double, _erand48, _erand48_u)
192 
193 EXPORT0(long, _lrand48, _lrand48_u)
194 EXPORT1(long, _nrand48, _nrand48_u)
195 
196 EXPORT0(long, _mrand48, _mrand48_u)
197 EXPORT1(long, _jrand48, _jrand48_u)
198 
199 #ifdef DRIVER
200 /*
201  *	This should print the sequences of integers in Tables 2
202  *		and 1 of the TM:
203  *	1623, 3442, 1447, 1829, 1305, ...
204  *	657EB7255101, D72A0C966378, 5A743C062A23, ...
205  */
206 #include <stdio.h>
207 
208 main()
209 {
210 	int i;
211 
212 	for (i = 0; i < 80; i++) {
213 		printf("%4d ", (int)(4096 * drand48()));
214 		printf("%.4X%.4X%.4X\n", x[2], x[1], x[0]);
215 	}
216 }
217 #endif
218