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