xref: /illumos-gate/usr/src/lib/libc/port/gen/drand48.c (revision 1da57d55)
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 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  *	drand48, etc. pseudo-random number generator
327c478bd9Sstevel@tonic-gate  *	This implementation assumes unsigned short integers of at least
337c478bd9Sstevel@tonic-gate  *	16 bits, long integers of at least 32 bits, and ignores
347c478bd9Sstevel@tonic-gate  *	overflows on adding or multiplying two unsigned integers.
357c478bd9Sstevel@tonic-gate  *	Two's-complement representation is assumed in a few places.
367c478bd9Sstevel@tonic-gate  *	Some extra masking is done if unsigneds are exactly 16 bits
377c478bd9Sstevel@tonic-gate  *	or longs are exactly 32 bits, but so what?
387c478bd9Sstevel@tonic-gate  *	An assembly-language implementation would run significantly faster.
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  *	New assumptions (supercede those stated above) for 64-bit work.
427c478bd9Sstevel@tonic-gate  *	Longs are now 64 bits, and we are bound by standards to return
437c478bd9Sstevel@tonic-gate  *	type long, hovever all internal calculations where long was
447c478bd9Sstevel@tonic-gate  *	previously used (32 bit precision) are now using the int32_t
457c478bd9Sstevel@tonic-gate  *	type (32 bit precision in both ILP32 and LP64 worlds).
467c478bd9Sstevel@tonic-gate  */
47*7257d1b4Sraf 
48*7257d1b4Sraf #include "lint.h"
497c478bd9Sstevel@tonic-gate #include <mtlib.h>
507c478bd9Sstevel@tonic-gate #include <synch.h>
517c478bd9Sstevel@tonic-gate #include <thread.h>
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate static mutex_t seed_lock = DEFAULTMUTEX;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #define	EXPORT0(TYPE, fn, fnu)	TYPE fn() { \
567c478bd9Sstevel@tonic-gate 	TYPE res; \
577c478bd9Sstevel@tonic-gate 	lmutex_lock(&seed_lock); \
587c478bd9Sstevel@tonic-gate 	res = fnu(); \
597c478bd9Sstevel@tonic-gate 	lmutex_unlock(&seed_lock); \
607c478bd9Sstevel@tonic-gate 	return (res); }
617c478bd9Sstevel@tonic-gate #define	EXPORT1(TYPE, fn, fnu)	TYPE fn(unsigned short xsubi[3]) { \
627c478bd9Sstevel@tonic-gate 	TYPE res; \
637c478bd9Sstevel@tonic-gate 	lmutex_lock(&seed_lock); \
647c478bd9Sstevel@tonic-gate 	res = fnu(xsubi); \
657c478bd9Sstevel@tonic-gate 	lmutex_unlock(&seed_lock); \
667c478bd9Sstevel@tonic-gate 	return (res); }
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #define	N	16
697c478bd9Sstevel@tonic-gate #define	MASK	((unsigned)(1 << (N - 1)) + (1 << (N - 1)) - 1)
707c478bd9Sstevel@tonic-gate #define	LOW(x)	((unsigned)(x) & MASK)
717c478bd9Sstevel@tonic-gate #define	HIGH(x)	LOW((x) >> N)
727c478bd9Sstevel@tonic-gate #define	MUL(x, y, z)	{ int32_t l = (int32_t)(x) * (int32_t)(y); \
737c478bd9Sstevel@tonic-gate 		(z)[0] = LOW(l); (z)[1] = HIGH(l); }
747c478bd9Sstevel@tonic-gate #define	CARRY(x, y)	((int32_t)(x) + (int32_t)(y) > MASK)
757c478bd9Sstevel@tonic-gate #define	ADDEQU(x, y, z)	(z = CARRY(x, (y)), x = LOW(x + (y)))
767c478bd9Sstevel@tonic-gate #define	X0	0x330E
777c478bd9Sstevel@tonic-gate #define	X1	0xABCD
787c478bd9Sstevel@tonic-gate #define	X2	0x1234
797c478bd9Sstevel@tonic-gate #define	A0	0xE66D
807c478bd9Sstevel@tonic-gate #define	A1	0xDEEC
817c478bd9Sstevel@tonic-gate #define	A2	0x5
827c478bd9Sstevel@tonic-gate #define	C	0xB
837c478bd9Sstevel@tonic-gate #define	SET3(x, x0, x1, x2)	((x)[0] = (x0), (x)[1] = (x1), (x)[2] = (x2))
847c478bd9Sstevel@tonic-gate #define	SETLOW(x, y, n) SET3(x, LOW((y)[n]), LOW((y)[(n)+1]), LOW((y)[(n)+2]))
857c478bd9Sstevel@tonic-gate #define	SEED(x0, x1, x2) (SET3(x, x0, x1, x2), SET3(a, A0, A1, A2), c = C)
867c478bd9Sstevel@tonic-gate #define	REST(v)	for (i = 0; i < 3; i++) { xsubi[i] = x[i]; x[i] = temp[i]; } \
877c478bd9Sstevel@tonic-gate 		return (v)
887c478bd9Sstevel@tonic-gate #define	NEST(TYPE, f, F) static TYPE f(unsigned short *xsubi) { \
897c478bd9Sstevel@tonic-gate 	int i; TYPE v; unsigned temp[3]; \
907c478bd9Sstevel@tonic-gate 	for (i = 0; i < 3; i++) { temp[i] = x[i]; x[i] = LOW(xsubi[i]); }  \
917c478bd9Sstevel@tonic-gate 	v = F(); REST(v); }
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /* Way ugly solution to problem names, but it works */
947c478bd9Sstevel@tonic-gate #define	x	_drand48_x
957c478bd9Sstevel@tonic-gate #define	a	_drand48_a
967c478bd9Sstevel@tonic-gate #define	c	_drand48_c
977c478bd9Sstevel@tonic-gate /* End way ugly */
987c478bd9Sstevel@tonic-gate static unsigned x[3] = { X0, X1, X2 }, a[3] = { A0, A1, A2 }, c = C;
997c478bd9Sstevel@tonic-gate static unsigned short lastx[3];
1007c478bd9Sstevel@tonic-gate static void next(void);
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate static double
_drand48_u(void)1037c478bd9Sstevel@tonic-gate _drand48_u(void)
1047c478bd9Sstevel@tonic-gate {
1057c478bd9Sstevel@tonic-gate 	static double two16m = 1.0 / ((int32_t)1 << N);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	next();
1087c478bd9Sstevel@tonic-gate 	return (two16m * (two16m * (two16m * x[0] + x[1]) + x[2]));
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate 
NEST(double,_erand48_u,_drand48_u)1117c478bd9Sstevel@tonic-gate NEST(double, _erand48_u, _drand48_u)
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate static long
1147c478bd9Sstevel@tonic-gate _lrand48_u(void)
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate 	next();
1177c478bd9Sstevel@tonic-gate 	return ((long)((int32_t)x[2] << (N - 1)) + (x[1] >> 1));
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate static long
_mrand48_u(void)1217c478bd9Sstevel@tonic-gate _mrand48_u(void)
1227c478bd9Sstevel@tonic-gate {
1237c478bd9Sstevel@tonic-gate 	next();
1247c478bd9Sstevel@tonic-gate 	return ((long)((int32_t)x[2] << N) + x[1]);
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate static void
next(void)1287c478bd9Sstevel@tonic-gate next(void)
1297c478bd9Sstevel@tonic-gate {
1307c478bd9Sstevel@tonic-gate 	unsigned p[2], q[2], r[2], carry0, carry1;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	MUL(a[0], x[0], p);
1337c478bd9Sstevel@tonic-gate 	ADDEQU(p[0], c, carry0);
1347c478bd9Sstevel@tonic-gate 	ADDEQU(p[1], carry0, carry1);
1357c478bd9Sstevel@tonic-gate 	MUL(a[0], x[1], q);
1367c478bd9Sstevel@tonic-gate 	ADDEQU(p[1], q[0], carry0);
1377c478bd9Sstevel@tonic-gate 	MUL(a[1], x[0], r);
1387c478bd9Sstevel@tonic-gate 	x[2] = LOW(carry0 + carry1 + CARRY(p[1], r[0]) + q[1] + r[1] +
139*7257d1b4Sraf 	    a[0] * x[2] + a[1] * x[1] + a[2] * x[0]);
1407c478bd9Sstevel@tonic-gate 	x[1] = LOW(p[1] + r[0]);
1417c478bd9Sstevel@tonic-gate 	x[0] = LOW(p[0]);
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate void
srand48(long seedval)145*7257d1b4Sraf srand48(long seedval)
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate 	int32_t fixseed = (int32_t)seedval;	/* limit to 32 bits */
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	lmutex_lock(&seed_lock);
1507c478bd9Sstevel@tonic-gate 	SEED(X0, LOW(fixseed), HIGH(fixseed));
1517c478bd9Sstevel@tonic-gate 	lmutex_unlock(&seed_lock);
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate unsigned short *
seed48(unsigned short seed16v[3])1557c478bd9Sstevel@tonic-gate seed48(unsigned short seed16v[3])
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate 	lmutex_lock(&seed_lock);
1587c478bd9Sstevel@tonic-gate 	SETLOW(lastx, x, 0);
1597c478bd9Sstevel@tonic-gate 	SEED(LOW(seed16v[0]), LOW(seed16v[1]), LOW(seed16v[2]));
1607c478bd9Sstevel@tonic-gate 	lmutex_unlock(&seed_lock);
1617c478bd9Sstevel@tonic-gate 	return (lastx);
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate void
lcong48(unsigned short param[7])1657c478bd9Sstevel@tonic-gate lcong48(unsigned short param[7])
1667c478bd9Sstevel@tonic-gate {
1677c478bd9Sstevel@tonic-gate 	lmutex_lock(&seed_lock);
1687c478bd9Sstevel@tonic-gate 	SETLOW(x, param, 0);
1697c478bd9Sstevel@tonic-gate 	SETLOW(a, param, 3);
1707c478bd9Sstevel@tonic-gate 	c = LOW(param[6]);
1717c478bd9Sstevel@tonic-gate 	lmutex_unlock(&seed_lock);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate 
NEST(long,_nrand48_u,_lrand48_u)1747c478bd9Sstevel@tonic-gate NEST(long, _nrand48_u, _lrand48_u)
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate NEST(long, _jrand48_u, _mrand48_u)
1777c478bd9Sstevel@tonic-gate 
178*7257d1b4Sraf EXPORT0(double, drand48, _drand48_u)
179*7257d1b4Sraf EXPORT1(double, erand48, _erand48_u)
1807c478bd9Sstevel@tonic-gate 
181*7257d1b4Sraf EXPORT0(long, lrand48, _lrand48_u)
182*7257d1b4Sraf EXPORT1(long, nrand48, _nrand48_u)
1837c478bd9Sstevel@tonic-gate 
184*7257d1b4Sraf EXPORT0(long, mrand48, _mrand48_u)
185*7257d1b4Sraf EXPORT1(long, jrand48, _jrand48_u)
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate #ifdef DRIVER
1887c478bd9Sstevel@tonic-gate /*
1897c478bd9Sstevel@tonic-gate  *	This should print the sequences of integers in Tables 2
1907c478bd9Sstevel@tonic-gate  *		and 1 of the TM:
1917c478bd9Sstevel@tonic-gate  *	1623, 3442, 1447, 1829, 1305, ...
1927c478bd9Sstevel@tonic-gate  *	657EB7255101, D72A0C966378, 5A743C062A23, ...
1937c478bd9Sstevel@tonic-gate  */
1947c478bd9Sstevel@tonic-gate #include <stdio.h>
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate main()
1977c478bd9Sstevel@tonic-gate {
1987c478bd9Sstevel@tonic-gate 	int i;
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	for (i = 0; i < 80; i++) {
2017c478bd9Sstevel@tonic-gate 		printf("%4d ", (int)(4096 * drand48()));
2027c478bd9Sstevel@tonic-gate 		printf("%.4X%.4X%.4X\n", x[2], x[1], x[0]);
2037c478bd9Sstevel@tonic-gate 	}
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate #endif
206