17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7257d1b4Sraf * Common Development and Distribution License (the "License").
6*7257d1b4Sraf * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21*7257d1b4Sraf
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
30*7257d1b4Sraf #pragma ident "%Z%%M% %I% %E% SMI"
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate * drand48, etc. pseudo-random number generator
347c478bd9Sstevel@tonic-gate * This implementation assumes unsigned short integers of at least
357c478bd9Sstevel@tonic-gate * 16 bits, long integers of at least 32 bits, and ignores
367c478bd9Sstevel@tonic-gate * overflows on adding or multiplying two unsigned integers.
377c478bd9Sstevel@tonic-gate * Two's-complement representation is assumed in a few places.
387c478bd9Sstevel@tonic-gate * Some extra masking is done if unsigneds are exactly 16 bits
397c478bd9Sstevel@tonic-gate * or longs are exactly 32 bits, but so what?
407c478bd9Sstevel@tonic-gate * An assembly-language implementation would run significantly faster.
417c478bd9Sstevel@tonic-gate */
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate * New assumptions (supercede those stated above) for 64-bit work.
447c478bd9Sstevel@tonic-gate * Longs are now 64 bits, and we are bound by standards to return
457c478bd9Sstevel@tonic-gate * type long, hovever all internal calculations where long was
467c478bd9Sstevel@tonic-gate * previously used (32 bit precision) are now using the int32_t
477c478bd9Sstevel@tonic-gate * type (32 bit precision in both ILP32 and LP64 worlds).
487c478bd9Sstevel@tonic-gate */
497c478bd9Sstevel@tonic-gate
50*7257d1b4Sraf #include "lint.h"
517c478bd9Sstevel@tonic-gate #include <mtlib.h>
527c478bd9Sstevel@tonic-gate #include <synch.h>
537c478bd9Sstevel@tonic-gate #include <thread.h>
547c478bd9Sstevel@tonic-gate
557c478bd9Sstevel@tonic-gate static mutex_t seed_lock = DEFAULTMUTEX;
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate #define EXPORT0(TYPE, fn, fnu) TYPE fn() { \
587c478bd9Sstevel@tonic-gate TYPE res; \
597c478bd9Sstevel@tonic-gate lmutex_lock(&seed_lock); \
607c478bd9Sstevel@tonic-gate res = fnu(); \
617c478bd9Sstevel@tonic-gate lmutex_unlock(&seed_lock); \
627c478bd9Sstevel@tonic-gate return (res); }
637c478bd9Sstevel@tonic-gate #define EXPORT1(TYPE, fn, fnu) TYPE fn(unsigned short xsubi[3]) { \
647c478bd9Sstevel@tonic-gate TYPE res; \
657c478bd9Sstevel@tonic-gate lmutex_lock(&seed_lock); \
667c478bd9Sstevel@tonic-gate res = fnu(xsubi); \
677c478bd9Sstevel@tonic-gate lmutex_unlock(&seed_lock); \
687c478bd9Sstevel@tonic-gate return (res); }
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate #define N 16
717c478bd9Sstevel@tonic-gate #define MASK ((unsigned)(1 << (N - 1)) + (1 << (N - 1)) - 1)
727c478bd9Sstevel@tonic-gate #define LOW(x) ((unsigned)(x) & MASK)
737c478bd9Sstevel@tonic-gate #define HIGH(x) LOW((x) >> N)
747c478bd9Sstevel@tonic-gate #define MUL(x, y, z) { int32_t l = (int32_t)(x) * (int32_t)(y); \
757c478bd9Sstevel@tonic-gate (z)[0] = LOW(l); (z)[1] = HIGH(l); }
767c478bd9Sstevel@tonic-gate #define CARRY(x, y) ((int32_t)(x) + (int32_t)(y) > MASK)
777c478bd9Sstevel@tonic-gate #define ADDEQU(x, y, z) (z = CARRY(x, (y)), x = LOW(x + (y)))
787c478bd9Sstevel@tonic-gate #define X0 0x330E
797c478bd9Sstevel@tonic-gate #define X1 0xABCD
807c478bd9Sstevel@tonic-gate #define X2 0x1234
817c478bd9Sstevel@tonic-gate #define A0 0xE66D
827c478bd9Sstevel@tonic-gate #define A1 0xDEEC
837c478bd9Sstevel@tonic-gate #define A2 0x5
847c478bd9Sstevel@tonic-gate #define C 0xB
857c478bd9Sstevel@tonic-gate #define SET3(x, x0, x1, x2) ((x)[0] = (x0), (x)[1] = (x1), (x)[2] = (x2))
867c478bd9Sstevel@tonic-gate #define SETLOW(x, y, n) SET3(x, LOW((y)[n]), LOW((y)[(n)+1]), LOW((y)[(n)+2]))
877c478bd9Sstevel@tonic-gate #define SEED(x0, x1, x2) (SET3(x, x0, x1, x2), SET3(a, A0, A1, A2), c = C)
887c478bd9Sstevel@tonic-gate #define REST(v) for (i = 0; i < 3; i++) { xsubi[i] = x[i]; x[i] = temp[i]; } \
897c478bd9Sstevel@tonic-gate return (v)
907c478bd9Sstevel@tonic-gate #define NEST(TYPE, f, F) static TYPE f(unsigned short *xsubi) { \
917c478bd9Sstevel@tonic-gate int i; TYPE v; unsigned temp[3]; \
927c478bd9Sstevel@tonic-gate for (i = 0; i < 3; i++) { temp[i] = x[i]; x[i] = LOW(xsubi[i]); } \
937c478bd9Sstevel@tonic-gate v = F(); REST(v); }
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate /* Way ugly solution to problem names, but it works */
967c478bd9Sstevel@tonic-gate #define x _drand48_x
977c478bd9Sstevel@tonic-gate #define a _drand48_a
987c478bd9Sstevel@tonic-gate #define c _drand48_c
997c478bd9Sstevel@tonic-gate /* End way ugly */
1007c478bd9Sstevel@tonic-gate static unsigned x[3] = { X0, X1, X2 }, a[3] = { A0, A1, A2 }, c = C;
1017c478bd9Sstevel@tonic-gate static unsigned short lastx[3];
1027c478bd9Sstevel@tonic-gate static void next(void);
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate static double
_drand48_u(void)1057c478bd9Sstevel@tonic-gate _drand48_u(void)
1067c478bd9Sstevel@tonic-gate {
1077c478bd9Sstevel@tonic-gate static double two16m = 1.0 / ((int32_t)1 << N);
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate next();
1107c478bd9Sstevel@tonic-gate return (two16m * (two16m * (two16m * x[0] + x[1]) + x[2]));
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate
NEST(double,_erand48_u,_drand48_u)1137c478bd9Sstevel@tonic-gate NEST(double, _erand48_u, _drand48_u)
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate static long
1167c478bd9Sstevel@tonic-gate _lrand48_u(void)
1177c478bd9Sstevel@tonic-gate {
1187c478bd9Sstevel@tonic-gate next();
1197c478bd9Sstevel@tonic-gate return ((long)((int32_t)x[2] << (N - 1)) + (x[1] >> 1));
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate
1227c478bd9Sstevel@tonic-gate static long
_mrand48_u(void)1237c478bd9Sstevel@tonic-gate _mrand48_u(void)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate next();
1267c478bd9Sstevel@tonic-gate return ((long)((int32_t)x[2] << N) + x[1]);
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate static void
next(void)1307c478bd9Sstevel@tonic-gate next(void)
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate unsigned p[2], q[2], r[2], carry0, carry1;
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate MUL(a[0], x[0], p);
1357c478bd9Sstevel@tonic-gate ADDEQU(p[0], c, carry0);
1367c478bd9Sstevel@tonic-gate ADDEQU(p[1], carry0, carry1);
1377c478bd9Sstevel@tonic-gate MUL(a[0], x[1], q);
1387c478bd9Sstevel@tonic-gate ADDEQU(p[1], q[0], carry0);
1397c478bd9Sstevel@tonic-gate MUL(a[1], x[0], r);
1407c478bd9Sstevel@tonic-gate x[2] = LOW(carry0 + carry1 + CARRY(p[1], r[0]) + q[1] + r[1] +
1417c478bd9Sstevel@tonic-gate a[0] * x[2] + a[1] * x[1] + a[2] * x[0]);
1427c478bd9Sstevel@tonic-gate x[1] = LOW(p[1] + r[0]);
1437c478bd9Sstevel@tonic-gate x[0] = LOW(p[0]);
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate void
srand48(long seedval)147*7257d1b4Sraf srand48(long seedval)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate int32_t fixseed = (int32_t)seedval; /* limit to 32 bits */
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate lmutex_lock(&seed_lock);
1527c478bd9Sstevel@tonic-gate SEED(X0, LOW(fixseed), HIGH(fixseed));
1537c478bd9Sstevel@tonic-gate lmutex_unlock(&seed_lock);
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate unsigned short *
seed48(unsigned short seed16v[3])1577c478bd9Sstevel@tonic-gate seed48(unsigned short seed16v[3])
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate lmutex_lock(&seed_lock);
1607c478bd9Sstevel@tonic-gate SETLOW(lastx, x, 0);
1617c478bd9Sstevel@tonic-gate SEED(LOW(seed16v[0]), LOW(seed16v[1]), LOW(seed16v[2]));
1627c478bd9Sstevel@tonic-gate lmutex_unlock(&seed_lock);
1637c478bd9Sstevel@tonic-gate return (lastx);
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate
1667c478bd9Sstevel@tonic-gate void
lcong48(unsigned short param[7])1677c478bd9Sstevel@tonic-gate lcong48(unsigned short param[7])
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate lmutex_lock(&seed_lock);
1707c478bd9Sstevel@tonic-gate SETLOW(x, param, 0);
1717c478bd9Sstevel@tonic-gate SETLOW(a, param, 3);
1727c478bd9Sstevel@tonic-gate c = LOW(param[6]);
1737c478bd9Sstevel@tonic-gate lmutex_unlock(&seed_lock);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate
NEST(long,_nrand48_u,_lrand48_u)1767c478bd9Sstevel@tonic-gate NEST(long, _nrand48_u, _lrand48_u)
1777c478bd9Sstevel@tonic-gate
1787c478bd9Sstevel@tonic-gate NEST(long, _jrand48_u, _mrand48_u)
1797c478bd9Sstevel@tonic-gate
180*7257d1b4Sraf EXPORT0(double, drand48, _drand48_u)
181*7257d1b4Sraf EXPORT1(double, erand48, _erand48_u)
1827c478bd9Sstevel@tonic-gate
183*7257d1b4Sraf EXPORT0(long, lrand48, _lrand48_u)
184*7257d1b4Sraf EXPORT1(long, nrand48, _nrand48_u)
1857c478bd9Sstevel@tonic-gate
186*7257d1b4Sraf EXPORT0(long, mrand48, _mrand48_u)
187*7257d1b4Sraf EXPORT1(long, jrand48, _jrand48_u)
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate #ifdef DRIVER
1907c478bd9Sstevel@tonic-gate /*
1917c478bd9Sstevel@tonic-gate * This should print the sequences of integers in Tables 2
1927c478bd9Sstevel@tonic-gate * and 1 of the TM:
1937c478bd9Sstevel@tonic-gate * 1623, 3442, 1447, 1829, 1305, ...
1947c478bd9Sstevel@tonic-gate * 657EB7255101, D72A0C966378, 5A743C062A23, ...
1957c478bd9Sstevel@tonic-gate */
1967c478bd9Sstevel@tonic-gate #include <stdio.h>
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate main()
1997c478bd9Sstevel@tonic-gate {
2007c478bd9Sstevel@tonic-gate int i;
2017c478bd9Sstevel@tonic-gate
2027c478bd9Sstevel@tonic-gate for (i = 0; i < 80; i++) {
2037c478bd9Sstevel@tonic-gate printf("%4d ", (int)(4096 * drand48()));
2047c478bd9Sstevel@tonic-gate printf("%.4X%.4X%.4X\n", x[2], x[1], x[0]);
2057c478bd9Sstevel@tonic-gate }
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate #endif
208