xref: /freebsd/lib/libc/gen/_rand48.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 /*
2  * Copyright (c) 1993 Martin Birgmeier
3  * All rights reserved.
4  *
5  * You may redistribute unmodified or modified versions of this source
6  * code provided that the above copyright notice and this and the
7  * following conditions are retained.
8  *
9  * This software is provided ``as is'', and comes with no warranties
10  * of any kind. I shall in no event be liable for anything that happens
11  * to anyone/anything when using this software.
12  */
13 
14 #include <sys/cdefs.h>
15 #include "rand48.h"
16 
17 unsigned short _rand48_seed[3] = {
18 	RAND48_SEED_0,
19 	RAND48_SEED_1,
20 	RAND48_SEED_2
21 };
22 unsigned short _rand48_mult[3] = {
23 	RAND48_MULT_0,
24 	RAND48_MULT_1,
25 	RAND48_MULT_2
26 };
27 unsigned short _rand48_add = RAND48_ADD;
28 
29 void
30 _dorand48(unsigned short xseed[3])
31 {
32 	unsigned long accu;
33 	unsigned short temp[2];
34 
35 	accu = (unsigned long) _rand48_mult[0] * (unsigned long) xseed[0] +
36 	 (unsigned long) _rand48_add;
37 	temp[0] = (unsigned short) accu;	/* lower 16 bits */
38 	accu >>= sizeof(unsigned short) * 8;
39 	accu += (unsigned long) _rand48_mult[0] * (unsigned long) xseed[1] +
40 	 (unsigned long) _rand48_mult[1] * (unsigned long) xseed[0];
41 	temp[1] = (unsigned short) accu;	/* middle 16 bits */
42 	accu >>= sizeof(unsigned short) * 8;
43 	accu += _rand48_mult[0] * xseed[2] + _rand48_mult[1] * xseed[1] + _rand48_mult[2] * xseed[0];
44 	xseed[0] = temp[0];
45 	xseed[1] = temp[1];
46 	xseed[2] = (unsigned short) accu;
47 }
48