6811333 Remove prom_printf() message in emlxs driver
[opensolaris.git] / usr / src / lib / libc / port / gen / drand48.c
blob897a3b65bdd6a1dc45bd941389012b00d0258872
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
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).
50 #include "lint.h"
51 #include <mtlib.h>
52 #include <synch.h>
53 #include <thread.h>
55 static mutex_t seed_lock = DEFAULTMUTEX;
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); }
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); }
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);
104 static double
105 _drand48_u(void)
107 static double two16m = 1.0 / ((int32_t)1 << N);
109 next();
110 return (two16m * (two16m * (two16m * x[0] + x[1]) + x[2]));
113 NEST(double, _erand48_u, _drand48_u)
115 static long
116 _lrand48_u(void)
118 next();
119 return ((long)((int32_t)x[2] << (N - 1)) + (x[1] >> 1));
122 static long
123 _mrand48_u(void)
125 next();
126 return ((long)((int32_t)x[2] << N) + x[1]);
129 static void
130 next(void)
132 unsigned p[2], q[2], r[2], carry0, carry1;
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]);
146 void
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);
156 unsigned short *
157 seed48(unsigned short seed16v[3])
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);
166 void
167 lcong48(unsigned short param[7])
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);
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)
189 #ifdef DRIVER
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, ...
196 #include <stdio.h>
198 main()
200 int i;
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]);
207 #endif