xref: /titanic_41/usr/src/lib/libast/common/uwin/rand48.c (revision 3e14f97f673e8a630f076077de35afdd43dc1587)
1da2e3ebdSchin /***********************************************************************
2da2e3ebdSchin *                                                                      *
3da2e3ebdSchin *               This software is part of the ast package               *
4*3e14f97fSRoger A. Faulkner *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
5da2e3ebdSchin *                      and is licensed under the                       *
6da2e3ebdSchin *                  Common Public License, Version 1.0                  *
77c2fbfb3SApril Chin *                    by AT&T Intellectual Property                     *
8da2e3ebdSchin *                                                                      *
9da2e3ebdSchin *                A copy of the License is available at                 *
10da2e3ebdSchin *            http://www.opensource.org/licenses/cpl1.0.txt             *
11da2e3ebdSchin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12da2e3ebdSchin *                                                                      *
13da2e3ebdSchin *              Information and Software Systems Research               *
14da2e3ebdSchin *                            AT&T Research                             *
15da2e3ebdSchin *                           Florham Park NJ                            *
16da2e3ebdSchin *                                                                      *
17da2e3ebdSchin *                 Glenn Fowler <gsf@research.att.com>                  *
18da2e3ebdSchin *                  David Korn <dgk@research.att.com>                   *
19da2e3ebdSchin *                   Phong Vo <kpv@research.att.com>                    *
20da2e3ebdSchin *                                                                      *
21da2e3ebdSchin ***********************************************************************/
22da2e3ebdSchin #include "FEATURE/uwin"
23da2e3ebdSchin 
24da2e3ebdSchin #if !_UWIN || _lib_srand48
25da2e3ebdSchin 
_STUB_srand48()26da2e3ebdSchin void _STUB_srand48(){}
27da2e3ebdSchin 
28da2e3ebdSchin #else
29da2e3ebdSchin 
30da2e3ebdSchin #define drand48	______drand48
31da2e3ebdSchin #define erand48	______erand48
32da2e3ebdSchin #define jrand48	______jrand48
33da2e3ebdSchin #define lcong48	______lcong48
34da2e3ebdSchin #define lrand48	______lrand48
35da2e3ebdSchin #define mrand48	______mrand48
36da2e3ebdSchin #define nrand48	______nrand48
37da2e3ebdSchin #define seed48	______seed48
38da2e3ebdSchin #define srand48	______srand48
39da2e3ebdSchin 
40da2e3ebdSchin #include	<stdlib.h>
41da2e3ebdSchin 
42da2e3ebdSchin #undef	drand48
43da2e3ebdSchin #undef	erand48
44da2e3ebdSchin #undef	jrand48
45da2e3ebdSchin #undef	lcong48
46da2e3ebdSchin #undef	lrand48
47da2e3ebdSchin #undef	mrand48
48da2e3ebdSchin #undef	nrand48
49da2e3ebdSchin #undef	seed48
50da2e3ebdSchin #undef	srand48
51da2e3ebdSchin 
52da2e3ebdSchin #if defined(__EXPORT__)
53da2e3ebdSchin #define extern		__EXPORT__
54da2e3ebdSchin #endif
55da2e3ebdSchin 
56da2e3ebdSchin #define A	0x5DEECE66D
57da2e3ebdSchin #define A0	0X5
58da2e3ebdSchin #define A1	0xDEEC
59da2e3ebdSchin #define A2	0xE66D
60da2e3ebdSchin #define C	0xB
61da2e3ebdSchin #define XINIT	0x330E
62da2e3ebdSchin #define SCALE	3.55271e-15
63da2e3ebdSchin 
64da2e3ebdSchin static unsigned short oldval[3];
65da2e3ebdSchin static unsigned short X[3] = { 0, 0, XINIT};
66da2e3ebdSchin static unsigned short a[3] = { A0, A1, A2};
67da2e3ebdSchin static unsigned short c = C;
68da2e3ebdSchin 
multadd(unsigned short x[3],unsigned short a[3],unsigned short c)69da2e3ebdSchin static void multadd(unsigned short x[3], unsigned short a[3], unsigned short c)
70da2e3ebdSchin {
71da2e3ebdSchin 	register unsigned long r = c;
72da2e3ebdSchin 	unsigned short x2 = x[2];
73da2e3ebdSchin 	unsigned short x1 = x[1];
74da2e3ebdSchin 	r += a[2]*x2;
75da2e3ebdSchin 	x[2] = (unsigned short)r;
76da2e3ebdSchin 	r >>= 16;
77da2e3ebdSchin 	r += a[1]*x2;
78da2e3ebdSchin 	r += a[2]*x1;
79da2e3ebdSchin 	x[1] = (unsigned short)r;
80da2e3ebdSchin 	r >>= 16;
81da2e3ebdSchin 	r += a[2]*x[0];
82da2e3ebdSchin 	r += a[1]*x1;
83da2e3ebdSchin 	r += a[0]*x2;
84da2e3ebdSchin 	x[0] = (unsigned short)r;
85da2e3ebdSchin }
86da2e3ebdSchin 
drand48(void)87da2e3ebdSchin extern double drand48(void)
88da2e3ebdSchin {
89da2e3ebdSchin 	double d;
90da2e3ebdSchin 	unsigned long u;
91da2e3ebdSchin 	multadd(X,a,c);
92da2e3ebdSchin 	u = (X[0]<<16) + X[1];
93da2e3ebdSchin 	d = (u*65536.) + X[2];
94da2e3ebdSchin 	return(d*SCALE);
95da2e3ebdSchin }
96da2e3ebdSchin 
erand48(unsigned short xsubi[3])97da2e3ebdSchin extern double erand48(unsigned short xsubi[3])
98da2e3ebdSchin {
99da2e3ebdSchin 	double d;
100da2e3ebdSchin 	unsigned long u;
101da2e3ebdSchin 	multadd(xsubi,a,c);
102da2e3ebdSchin 	u = (xsubi[0]<<16) + xsubi[1];
103da2e3ebdSchin 	d = (u*65536.) + xsubi[2];
104da2e3ebdSchin 	return(d*SCALE);
105da2e3ebdSchin }
106da2e3ebdSchin 
jrand48(unsigned short xsubi[3])107da2e3ebdSchin extern long jrand48(unsigned short xsubi[3])
108da2e3ebdSchin {
109da2e3ebdSchin 	long u;
110da2e3ebdSchin 	multadd(xsubi,a,c);
111da2e3ebdSchin 	u = (xsubi[0]<<16) | xsubi[1];
112da2e3ebdSchin 	return((long)u);
113da2e3ebdSchin }
114da2e3ebdSchin 
lcong48(unsigned short param[7])115da2e3ebdSchin extern void lcong48(unsigned short param[7])
116da2e3ebdSchin {
117da2e3ebdSchin 	X[0] = param[0];
118da2e3ebdSchin 	X[1] = param[1];
119da2e3ebdSchin 	X[2] = param[2];
120da2e3ebdSchin 	a[0] = param[3];
121da2e3ebdSchin 	a[1] = param[4];
122da2e3ebdSchin 	a[2] = param[5];
123da2e3ebdSchin 	c = param[6];
124da2e3ebdSchin }
125da2e3ebdSchin 
lrand48(void)126da2e3ebdSchin extern long lrand48(void)
127da2e3ebdSchin {
128da2e3ebdSchin 	long l;
129da2e3ebdSchin 	multadd(X,a,c);
130da2e3ebdSchin 	l = (X[0]<<15)|(X[1]>>1);
131da2e3ebdSchin 	return(l);
132da2e3ebdSchin }
133da2e3ebdSchin 
mrand48(void)134da2e3ebdSchin extern long mrand48(void)
135da2e3ebdSchin {
136da2e3ebdSchin 	unsigned long u;
137da2e3ebdSchin 	multadd(X,a,c);
138da2e3ebdSchin 	u = (X[0]<<16) | X[1];
139da2e3ebdSchin 	return((long)u);
140da2e3ebdSchin }
141da2e3ebdSchin 
nrand48(unsigned short xsubi[3])142da2e3ebdSchin extern long nrand48(unsigned short xsubi[3])
143da2e3ebdSchin {
144da2e3ebdSchin 	long l;
145da2e3ebdSchin 	multadd(xsubi,a,c);
146da2e3ebdSchin 	l = (xsubi[0]<<15)|(xsubi[1]>>1);
147da2e3ebdSchin 	return(l);
148da2e3ebdSchin }
149da2e3ebdSchin 
seed48(unsigned short seed[3])150da2e3ebdSchin extern unsigned short *seed48(unsigned short seed[3])
151da2e3ebdSchin {
152da2e3ebdSchin 	unsigned short *sp = (unsigned short*)&X;
153da2e3ebdSchin 	a[0] = A0;
154da2e3ebdSchin 	a[1] = A1;
155da2e3ebdSchin 	a[2] = A2;
156da2e3ebdSchin 	c = C;
157da2e3ebdSchin 	oldval[0] = X[2];
158da2e3ebdSchin 	oldval[1] = X[1];
159da2e3ebdSchin 	oldval[2] = X[0];
160da2e3ebdSchin 	X[0] = seed[2];
161da2e3ebdSchin 	X[1] = seed[1];
162da2e3ebdSchin 	X[2] = seed[0];
163da2e3ebdSchin 	return(oldval);
164da2e3ebdSchin }
165da2e3ebdSchin 
srand48(long seedval)166da2e3ebdSchin extern void srand48(long seedval)
167da2e3ebdSchin {
168da2e3ebdSchin 	a[0] = A0;
169da2e3ebdSchin 	a[1] = A1;
170da2e3ebdSchin 	a[2] = A2;
171da2e3ebdSchin 	c = C;
172da2e3ebdSchin 	X[0] = (unsigned short)(((unsigned long)seedval) >> 16);
173da2e3ebdSchin 	X[1] = (unsigned short)seedval;
174da2e3ebdSchin 	X[2] = XINIT;
175da2e3ebdSchin }
176da2e3ebdSchin 
177da2e3ebdSchin #endif
178