xref: /onnv-gate/usr/src/lib/libc/port/gen/drand48.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*6812Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  *	drand48, etc. pseudo-random number generator
340Sstevel@tonic-gate  *	This implementation assumes unsigned short integers of at least
350Sstevel@tonic-gate  *	16 bits, long integers of at least 32 bits, and ignores
360Sstevel@tonic-gate  *	overflows on adding or multiplying two unsigned integers.
370Sstevel@tonic-gate  *	Two's-complement representation is assumed in a few places.
380Sstevel@tonic-gate  *	Some extra masking is done if unsigneds are exactly 16 bits
390Sstevel@tonic-gate  *	or longs are exactly 32 bits, but so what?
400Sstevel@tonic-gate  *	An assembly-language implementation would run significantly faster.
410Sstevel@tonic-gate  */
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate  *	New assumptions (supercede those stated above) for 64-bit work.
440Sstevel@tonic-gate  *	Longs are now 64 bits, and we are bound by standards to return
450Sstevel@tonic-gate  *	type long, hovever all internal calculations where long was
460Sstevel@tonic-gate  *	previously used (32 bit precision) are now using the int32_t
470Sstevel@tonic-gate  *	type (32 bit precision in both ILP32 and LP64 worlds).
480Sstevel@tonic-gate  */
490Sstevel@tonic-gate 
50*6812Sraf #include "lint.h"
510Sstevel@tonic-gate #include <mtlib.h>
520Sstevel@tonic-gate #include <synch.h>
530Sstevel@tonic-gate #include <thread.h>
540Sstevel@tonic-gate 
550Sstevel@tonic-gate static mutex_t seed_lock = DEFAULTMUTEX;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate #define	EXPORT0(TYPE, fn, fnu)	TYPE fn() { \
580Sstevel@tonic-gate 	TYPE res; \
590Sstevel@tonic-gate 	lmutex_lock(&seed_lock); \
600Sstevel@tonic-gate 	res = fnu(); \
610Sstevel@tonic-gate 	lmutex_unlock(&seed_lock); \
620Sstevel@tonic-gate 	return (res); }
630Sstevel@tonic-gate #define	EXPORT1(TYPE, fn, fnu)	TYPE fn(unsigned short xsubi[3]) { \
640Sstevel@tonic-gate 	TYPE res; \
650Sstevel@tonic-gate 	lmutex_lock(&seed_lock); \
660Sstevel@tonic-gate 	res = fnu(xsubi); \
670Sstevel@tonic-gate 	lmutex_unlock(&seed_lock); \
680Sstevel@tonic-gate 	return (res); }
690Sstevel@tonic-gate 
700Sstevel@tonic-gate #define	N	16
710Sstevel@tonic-gate #define	MASK	((unsigned)(1 << (N - 1)) + (1 << (N - 1)) - 1)
720Sstevel@tonic-gate #define	LOW(x)	((unsigned)(x) & MASK)
730Sstevel@tonic-gate #define	HIGH(x)	LOW((x) >> N)
740Sstevel@tonic-gate #define	MUL(x, y, z)	{ int32_t l = (int32_t)(x) * (int32_t)(y); \
750Sstevel@tonic-gate 		(z)[0] = LOW(l); (z)[1] = HIGH(l); }
760Sstevel@tonic-gate #define	CARRY(x, y)	((int32_t)(x) + (int32_t)(y) > MASK)
770Sstevel@tonic-gate #define	ADDEQU(x, y, z)	(z = CARRY(x, (y)), x = LOW(x + (y)))
780Sstevel@tonic-gate #define	X0	0x330E
790Sstevel@tonic-gate #define	X1	0xABCD
800Sstevel@tonic-gate #define	X2	0x1234
810Sstevel@tonic-gate #define	A0	0xE66D
820Sstevel@tonic-gate #define	A1	0xDEEC
830Sstevel@tonic-gate #define	A2	0x5
840Sstevel@tonic-gate #define	C	0xB
850Sstevel@tonic-gate #define	SET3(x, x0, x1, x2)	((x)[0] = (x0), (x)[1] = (x1), (x)[2] = (x2))
860Sstevel@tonic-gate #define	SETLOW(x, y, n) SET3(x, LOW((y)[n]), LOW((y)[(n)+1]), LOW((y)[(n)+2]))
870Sstevel@tonic-gate #define	SEED(x0, x1, x2) (SET3(x, x0, x1, x2), SET3(a, A0, A1, A2), c = C)
880Sstevel@tonic-gate #define	REST(v)	for (i = 0; i < 3; i++) { xsubi[i] = x[i]; x[i] = temp[i]; } \
890Sstevel@tonic-gate 		return (v)
900Sstevel@tonic-gate #define	NEST(TYPE, f, F) static TYPE f(unsigned short *xsubi) { \
910Sstevel@tonic-gate 	int i; TYPE v; unsigned temp[3]; \
920Sstevel@tonic-gate 	for (i = 0; i < 3; i++) { temp[i] = x[i]; x[i] = LOW(xsubi[i]); }  \
930Sstevel@tonic-gate 	v = F(); REST(v); }
940Sstevel@tonic-gate 
950Sstevel@tonic-gate /* Way ugly solution to problem names, but it works */
960Sstevel@tonic-gate #define	x	_drand48_x
970Sstevel@tonic-gate #define	a	_drand48_a
980Sstevel@tonic-gate #define	c	_drand48_c
990Sstevel@tonic-gate /* End way ugly */
1000Sstevel@tonic-gate static unsigned x[3] = { X0, X1, X2 }, a[3] = { A0, A1, A2 }, c = C;
1010Sstevel@tonic-gate static unsigned short lastx[3];
1020Sstevel@tonic-gate static void next(void);
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate static double
_drand48_u(void)1050Sstevel@tonic-gate _drand48_u(void)
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate 	static double two16m = 1.0 / ((int32_t)1 << N);
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	next();
1100Sstevel@tonic-gate 	return (two16m * (two16m * (two16m * x[0] + x[1]) + x[2]));
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate 
NEST(double,_erand48_u,_drand48_u)1130Sstevel@tonic-gate NEST(double, _erand48_u, _drand48_u)
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate static long
1160Sstevel@tonic-gate _lrand48_u(void)
1170Sstevel@tonic-gate {
1180Sstevel@tonic-gate 	next();
1190Sstevel@tonic-gate 	return ((long)((int32_t)x[2] << (N - 1)) + (x[1] >> 1));
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate static long
_mrand48_u(void)1230Sstevel@tonic-gate _mrand48_u(void)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate 	next();
1260Sstevel@tonic-gate 	return ((long)((int32_t)x[2] << N) + x[1]);
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate static void
next(void)1300Sstevel@tonic-gate next(void)
1310Sstevel@tonic-gate {
1320Sstevel@tonic-gate 	unsigned p[2], q[2], r[2], carry0, carry1;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	MUL(a[0], x[0], p);
1350Sstevel@tonic-gate 	ADDEQU(p[0], c, carry0);
1360Sstevel@tonic-gate 	ADDEQU(p[1], carry0, carry1);
1370Sstevel@tonic-gate 	MUL(a[0], x[1], q);
1380Sstevel@tonic-gate 	ADDEQU(p[1], q[0], carry0);
1390Sstevel@tonic-gate 	MUL(a[1], x[0], r);
1400Sstevel@tonic-gate 	x[2] = LOW(carry0 + carry1 + CARRY(p[1], r[0]) + q[1] + r[1] +
141*6812Sraf 	    a[0] * x[2] + a[1] * x[1] + a[2] * x[0]);
1420Sstevel@tonic-gate 	x[1] = LOW(p[1] + r[0]);
1430Sstevel@tonic-gate 	x[0] = LOW(p[0]);
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate void
srand48(long seedval)147*6812Sraf srand48(long seedval)
1480Sstevel@tonic-gate {
1490Sstevel@tonic-gate 	int32_t fixseed = (int32_t)seedval;	/* limit to 32 bits */
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	lmutex_lock(&seed_lock);
1520Sstevel@tonic-gate 	SEED(X0, LOW(fixseed), HIGH(fixseed));
1530Sstevel@tonic-gate 	lmutex_unlock(&seed_lock);
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate unsigned short *
seed48(unsigned short seed16v[3])1570Sstevel@tonic-gate seed48(unsigned short seed16v[3])
1580Sstevel@tonic-gate {
1590Sstevel@tonic-gate 	lmutex_lock(&seed_lock);
1600Sstevel@tonic-gate 	SETLOW(lastx, x, 0);
1610Sstevel@tonic-gate 	SEED(LOW(seed16v[0]), LOW(seed16v[1]), LOW(seed16v[2]));
1620Sstevel@tonic-gate 	lmutex_unlock(&seed_lock);
1630Sstevel@tonic-gate 	return (lastx);
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate void
lcong48(unsigned short param[7])1670Sstevel@tonic-gate lcong48(unsigned short param[7])
1680Sstevel@tonic-gate {
1690Sstevel@tonic-gate 	lmutex_lock(&seed_lock);
1700Sstevel@tonic-gate 	SETLOW(x, param, 0);
1710Sstevel@tonic-gate 	SETLOW(a, param, 3);
1720Sstevel@tonic-gate 	c = LOW(param[6]);
1730Sstevel@tonic-gate 	lmutex_unlock(&seed_lock);
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate 
NEST(long,_nrand48_u,_lrand48_u)1760Sstevel@tonic-gate NEST(long, _nrand48_u, _lrand48_u)
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate NEST(long, _jrand48_u, _mrand48_u)
1790Sstevel@tonic-gate 
180*6812Sraf EXPORT0(double, drand48, _drand48_u)
181*6812Sraf EXPORT1(double, erand48, _erand48_u)
1820Sstevel@tonic-gate 
183*6812Sraf EXPORT0(long, lrand48, _lrand48_u)
184*6812Sraf EXPORT1(long, nrand48, _nrand48_u)
1850Sstevel@tonic-gate 
186*6812Sraf EXPORT0(long, mrand48, _mrand48_u)
187*6812Sraf EXPORT1(long, jrand48, _jrand48_u)
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate #ifdef DRIVER
1900Sstevel@tonic-gate /*
1910Sstevel@tonic-gate  *	This should print the sequences of integers in Tables 2
1920Sstevel@tonic-gate  *		and 1 of the TM:
1930Sstevel@tonic-gate  *	1623, 3442, 1447, 1829, 1305, ...
1940Sstevel@tonic-gate  *	657EB7255101, D72A0C966378, 5A743C062A23, ...
1950Sstevel@tonic-gate  */
1960Sstevel@tonic-gate #include <stdio.h>
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate main()
1990Sstevel@tonic-gate {
2000Sstevel@tonic-gate 	int i;
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	for (i = 0; i < 80; i++) {
2030Sstevel@tonic-gate 		printf("%4d ", (int)(4096 * drand48()));
2040Sstevel@tonic-gate 		printf("%.4X%.4X%.4X\n", x[2], x[1], x[0]);
2050Sstevel@tonic-gate 	}
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate #endif
208