xref: /illumos-gate/usr/src/lib/libc/port/gen/drand48.c (revision 8119dad84d6416f13557b0ba8e2aaf9064cbcfd3)
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 /*
31  *	drand48, etc. pseudo-random number generator
32  *	This implementation assumes unsigned short integers of at least
33  *	16 bits, long integers of at least 32 bits, and ignores
34  *	overflows on adding or multiplying two unsigned integers.
35  *	Two's-complement representation is assumed in a few places.
36  *	Some extra masking is done if unsigneds are exactly 16 bits
37  *	or longs are exactly 32 bits, but so what?
38  *	An assembly-language implementation would run significantly faster.
39  */
40 /*
41  *	New assumptions (supercede those stated above) for 64-bit work.
42  *	Longs are now 64 bits, and we are bound by standards to return
43  *	type long, hovever all internal calculations where long was
44  *	previously used (32 bit precision) are now using the int32_t
45  *	type (32 bit precision in both ILP32 and LP64 worlds).
46  */
47 
48 #include "lint.h"
49 #include <mtlib.h>
50 #include <synch.h>
51 #include <thread.h>
52 
53 static mutex_t seed_lock = DEFAULTMUTEX;
54 
55 #define	EXPORT0(TYPE, fn, fnu)	TYPE fn() { \
56 	TYPE res; \
57 	lmutex_lock(&seed_lock); \
58 	res = fnu(); \
59 	lmutex_unlock(&seed_lock); \
60 	return (res); }
61 #define	EXPORT1(TYPE, fn, fnu)	TYPE fn(unsigned short xsubi[3]) { \
62 	TYPE res; \
63 	lmutex_lock(&seed_lock); \
64 	res = fnu(xsubi); \
65 	lmutex_unlock(&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 _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, _erand48_u, _drand48_u)
112 
113 static long
114 _lrand48_u(void)
115 {
116 	next();
117 	return ((long)((int32_t)x[2] << (N - 1)) + (x[1] >> 1));
118 }
119 
120 static long
121 _mrand48_u(void)
122 {
123 	next();
124 	return ((long)((int32_t)x[2] << N) + x[1]);
125 }
126 
127 static void
128 next(void)
129 {
130 	unsigned p[2], q[2], r[2], carry0, carry1;
131 
132 	MUL(a[0], x[0], p);
133 	ADDEQU(p[0], c, carry0);
134 	ADDEQU(p[1], carry0, carry1);
135 	MUL(a[0], x[1], q);
136 	ADDEQU(p[1], q[0], carry0);
137 	MUL(a[1], x[0], r);
138 	x[2] = LOW(carry0 + carry1 + CARRY(p[1], r[0]) + q[1] + r[1] +
139 	    a[0] * x[2] + a[1] * x[1] + a[2] * x[0]);
140 	x[1] = LOW(p[1] + r[0]);
141 	x[0] = LOW(p[0]);
142 }
143 
144 void
145 srand48(long seedval)
146 {
147 	int32_t fixseed = (int32_t)seedval;	/* limit to 32 bits */
148 
149 	lmutex_lock(&seed_lock);
150 	SEED(X0, LOW(fixseed), HIGH(fixseed));
151 	lmutex_unlock(&seed_lock);
152 }
153 
154 unsigned short *
155 seed48(unsigned short seed16v[3])
156 {
157 	lmutex_lock(&seed_lock);
158 	SETLOW(lastx, x, 0);
159 	SEED(LOW(seed16v[0]), LOW(seed16v[1]), LOW(seed16v[2]));
160 	lmutex_unlock(&seed_lock);
161 	return (lastx);
162 }
163 
164 void
165 lcong48(unsigned short param[7])
166 {
167 	lmutex_lock(&seed_lock);
168 	SETLOW(x, param, 0);
169 	SETLOW(a, param, 3);
170 	c = LOW(param[6]);
171 	lmutex_unlock(&seed_lock);
172 }
173 
174 NEST(long, _nrand48_u, _lrand48_u)
175 
176 NEST(long, _jrand48_u, _mrand48_u)
177 
178 EXPORT0(double, drand48, _drand48_u)
179 EXPORT1(double, erand48, _erand48_u)
180 
181 EXPORT0(long, lrand48, _lrand48_u)
182 EXPORT1(long, nrand48, _nrand48_u)
183 
184 EXPORT0(long, mrand48, _mrand48_u)
185 EXPORT1(long, jrand48, _jrand48_u)
186 
187 #ifdef DRIVER
188 /*
189  *	This should print the sequences of integers in Tables 2
190  *		and 1 of the TM:
191  *	1623, 3442, 1447, 1829, 1305, ...
192  *	657EB7255101, D72A0C966378, 5A743C062A23, ...
193  */
194 #include <stdio.h>
195 
196 main()
197 {
198 	int i;
199 
200 	for (i = 0; i < 80; i++) {
201 		printf("%4d ", (int)(4096 * drand48()));
202 		printf("%.4X%.4X%.4X\n", x[2], x[1], x[0]);
203 	}
204 }
205 #endif
206