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.
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.
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]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1988 AT&T */
28 /* All Rights Reserved */
30 #pragma ident "%Z%%M% %I% %E% SMI"
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.
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).
55 static mutex_t seed_lock
= DEFAULTMUTEX
;
57 #define EXPORT0(TYPE, fn, fnu) TYPE fn() { \
59 lmutex_lock(&seed_lock); \
61 lmutex_unlock(&seed_lock); \
63 #define EXPORT1(TYPE, fn, fnu) TYPE fn(unsigned short xsubi[3]) { \
65 lmutex_lock(&seed_lock); \
67 lmutex_unlock(&seed_lock); \
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)))
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]; } \
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]); } \
95 /* Way ugly solution to problem names, but it works */
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);
107 static double two16m
= 1.0 / ((int32_t)1 << N
);
110 return (two16m
* (two16m
* (two16m
* x
[0] + x
[1]) + x
[2]));
113 NEST(double, _erand48_u
, _drand48_u
)
119 return ((long)((int32_t)x
[2] << (N
- 1)) + (x
[1] >> 1));
126 return ((long)((int32_t)x
[2] << N
) + x
[1]);
132 unsigned p
[2], q
[2], r
[2], carry0
, carry1
;
135 ADDEQU(p
[0], c
, carry0
);
136 ADDEQU(p
[1], carry0
, carry1
);
138 ADDEQU(p
[1], q
[0], carry0
);
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]);
147 srand48(long seedval
)
149 int32_t fixseed
= (int32_t)seedval
; /* limit to 32 bits */
151 lmutex_lock(&seed_lock
);
152 SEED(X0
, LOW(fixseed
), HIGH(fixseed
));
153 lmutex_unlock(&seed_lock
);
157 seed48(unsigned short seed16v
[3])
159 lmutex_lock(&seed_lock
);
161 SEED(LOW(seed16v
[0]), LOW(seed16v
[1]), LOW(seed16v
[2]));
162 lmutex_unlock(&seed_lock
);
167 lcong48(unsigned short param
[7])
169 lmutex_lock(&seed_lock
);
173 lmutex_unlock(&seed_lock
);
176 NEST(long, _nrand48_u
, _lrand48_u
)
178 NEST(long, _jrand48_u
, _mrand48_u
)
180 EXPORT0(double, drand48
, _drand48_u
)
181 EXPORT1(double, erand48
, _erand48_u
)
183 EXPORT0(long, lrand48
, _lrand48_u
)
184 EXPORT1(long, nrand48
, _nrand48_u
)
186 EXPORT0(long, mrand48
, _mrand48_u
)
187 EXPORT1(long, jrand48
, _jrand48_u
)
191 * This should print the sequences of integers in Tables 2
193 * 1623, 3442, 1447, 1829, 1305, ...
194 * 657EB7255101, D72A0C966378, 5A743C062A23, ...
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]);