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
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.
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]
23 * Copyright 1996 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984 AT&T */
28 /* All Rights Reserved */
30 #pragma ident "%Z%%M% %I% %E% SMI"
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.
44 #define MASK ((unsigned)(1 << (N - 1)) + (1 << (N - 1)) - 1)
45 #define LOW(x) ((unsigned)(x) & MASK)
46 #define HIGH(x) LOW((x) >> N)
47 #define MUL(x, y, z) { long l = (long)(x) * (long)(y); \
48 (z)[0] = LOW(l); (z)[1] = HIGH(l); }
49 #define CARRY(x, y) ((long)(x) + (long)(y) > MASK)
50 #define ADDEQU(x, y, z) (z = CARRY(x, (y)), x = LOW(x + (y)))
58 #define SET3(x, x0, x1, x2) ((x)[0] = (x0), (x)[1] = (x1), (x)[2] = (x2))
59 #define SETLOW(x, y, n) SET3(x, LOW((y)[n]), LOW((y)[(n)+1]), LOW((y)[(n)+2]))
60 #define SEED(x0, x1, x2) (SET3(x, x0, x1, x2), SET3(a, A0, A1, A2), c = C)
62 for (i = 0; i < 3; i++) { \
68 #define NEST(TYPE, f, F) \
70 register unsigned short *xsubi; \
76 for (i = 0; i < 3; i++) { \
78 x[i] = LOW(xsubi[i]); \
84 static unsigned x
[3] = { X0
, X1
, X2
}, a
[3] = { A0
, A1
, A2
}, c
= C
;
85 static unsigned short lastx
[3];
91 static double two16m
= 1.0 / (1L << N
);
94 return (two16m
* (two16m
* (two16m
* x
[0] + x
[1]) + x
[2]));
97 NEST(double, erand48
, drand48
)
103 return (((long)x
[2] << (N
- 1)) + (x
[1] >> 1));
110 return (((long)x
[2] << N
) + x
[1]);
116 unsigned p
[2], q
[2], r
[2], carry0
, carry1
;
119 ADDEQU(p
[0], c
, carry0
);
120 ADDEQU(p
[1], carry0
, carry1
);
122 ADDEQU(p
[1], q
[0], carry0
);
124 x
[2] = LOW(carry0
+ carry1
+ CARRY(p
[1], r
[0]) + q
[1] + r
[1] +
125 a
[0] * x
[2] + a
[1] * x
[1] + a
[2] * x
[0]);
126 x
[1] = LOW(p
[1] + r
[0]);
134 SEED(X0
, LOW(seedval
), HIGH(seedval
));
139 unsigned short seed16v
[3];
142 SEED(LOW(seed16v
[0]), LOW(seed16v
[1]), LOW(seed16v
[2]));
148 unsigned short param
[7];
155 NEST(long, nrand48
, lrand48
)
157 NEST(long, jrand48
, mrand48
)
161 * This should print the sequences of integers in Tables 2
163 * 1623, 3442, 1447, 1829, 1305, ...
164 * 657EB7255101, D72A0C966378, 5A743C062A23, ...
172 for (i
= 0; i
< 80; i
++) {
173 printf("%4d ", (int)(4096 * drand48()));
174 printf("%.4X%.4X%.4X\n", x
[2], x
[1], x
[0]);