xref: /minix3/lib/libc/stdlib/_rand48.c (revision 2fe8fb192fe7e8720e3e7a77f928da545e872a6a)
1*2fe8fb19SBen Gras /*	$NetBSD: _rand48.c,v 1.7 2005/06/12 05:21:27 lukem Exp $	*/
2*2fe8fb19SBen Gras 
3*2fe8fb19SBen Gras /*
4*2fe8fb19SBen Gras  * Copyright (c) 1993 Martin Birgmeier
5*2fe8fb19SBen Gras  * All rights reserved.
6*2fe8fb19SBen Gras  *
7*2fe8fb19SBen Gras  * You may redistribute unmodified or modified versions of this source
8*2fe8fb19SBen Gras  * code provided that the above copyright notice and this and the
9*2fe8fb19SBen Gras  * following conditions are retained.
10*2fe8fb19SBen Gras  *
11*2fe8fb19SBen Gras  * This software is provided ``as is'', and comes with no warranties
12*2fe8fb19SBen Gras  * of any kind. I shall in no event be liable for anything that happens
13*2fe8fb19SBen Gras  * to anyone/anything when using this software.
14*2fe8fb19SBen Gras  */
15*2fe8fb19SBen Gras 
16*2fe8fb19SBen Gras #include <sys/cdefs.h>
17*2fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
18*2fe8fb19SBen Gras __RCSID("$NetBSD: _rand48.c,v 1.7 2005/06/12 05:21:27 lukem Exp $");
19*2fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
20*2fe8fb19SBen Gras 
21*2fe8fb19SBen Gras #include <assert.h>
22*2fe8fb19SBen Gras 
23*2fe8fb19SBen Gras #include "rand48.h"
24*2fe8fb19SBen Gras 
25*2fe8fb19SBen Gras unsigned short __rand48_seed[3] = {
26*2fe8fb19SBen Gras 	RAND48_SEED_0,
27*2fe8fb19SBen Gras 	RAND48_SEED_1,
28*2fe8fb19SBen Gras 	RAND48_SEED_2
29*2fe8fb19SBen Gras };
30*2fe8fb19SBen Gras unsigned short __rand48_mult[3] = {
31*2fe8fb19SBen Gras 	RAND48_MULT_0,
32*2fe8fb19SBen Gras 	RAND48_MULT_1,
33*2fe8fb19SBen Gras 	RAND48_MULT_2
34*2fe8fb19SBen Gras };
35*2fe8fb19SBen Gras unsigned short __rand48_add = RAND48_ADD;
36*2fe8fb19SBen Gras 
37*2fe8fb19SBen Gras void
__dorand48(unsigned short xseed[3])38*2fe8fb19SBen Gras __dorand48(unsigned short xseed[3])
39*2fe8fb19SBen Gras {
40*2fe8fb19SBen Gras 	unsigned long accu;
41*2fe8fb19SBen Gras 	unsigned short temp[2];
42*2fe8fb19SBen Gras 
43*2fe8fb19SBen Gras 	_DIAGASSERT(xseed != NULL);
44*2fe8fb19SBen Gras 
45*2fe8fb19SBen Gras 	accu = (unsigned long) __rand48_mult[0] * (unsigned long) xseed[0] +
46*2fe8fb19SBen Gras 	 (unsigned long) __rand48_add;
47*2fe8fb19SBen Gras 	temp[0] = (unsigned short) accu;	/* lower 16 bits */
48*2fe8fb19SBen Gras 	accu >>= sizeof(unsigned short) * 8;
49*2fe8fb19SBen Gras 	accu += (unsigned long) __rand48_mult[0] * (unsigned long) xseed[1] +
50*2fe8fb19SBen Gras 	 (unsigned long) __rand48_mult[1] * (unsigned long) xseed[0];
51*2fe8fb19SBen Gras 	temp[1] = (unsigned short) accu;	/* middle 16 bits */
52*2fe8fb19SBen Gras 	accu >>= sizeof(unsigned short) * 8;
53*2fe8fb19SBen Gras 	accu += __rand48_mult[0] * xseed[2] + __rand48_mult[1] * xseed[1] + __rand48_mult[2] * xseed[0];
54*2fe8fb19SBen Gras 	xseed[0] = temp[0];
55*2fe8fb19SBen Gras 	xseed[1] = temp[1];
56*2fe8fb19SBen Gras 	xseed[2] = (unsigned short) accu;
57*2fe8fb19SBen Gras }
58