1 /*        $NetBSD: _rand48.c,v 1.10 2020/02/23 09:53:42 kamil 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 <sys/cdefs.h>
17 #if defined(LIBC_SCCS) && !defined(lint)
18 __RCSID("$NetBSD: _rand48.c,v 1.10 2020/02/23 09:53:42 kamil Exp $");
19 #endif /* LIBC_SCCS and not lint */
20 
21 #include <assert.h>
22 
23 #include "rand48.h"
24 
25 unsigned short __rand48_seed[3] = {
26           RAND48_SEED_0,
27           RAND48_SEED_1,
28           RAND48_SEED_2
29 };
30 unsigned short __rand48_mult[3] = {
31           RAND48_MULT_0,
32           RAND48_MULT_1,
33           RAND48_MULT_2
34 };
35 unsigned short __rand48_add = RAND48_ADD;
36 
37 void
__dorand48(unsigned short xseed[3])38 __dorand48(unsigned short xseed[3])
39 {
40           unsigned long accu;
41           unsigned short temp[2];
42 
43           _DIAGASSERT(xseed != NULL);
44 
45           accu = (unsigned long) __rand48_mult[0] * (unsigned long) xseed[0];
46           accu += (unsigned long) __rand48_add;
47           temp[0] = (unsigned short) accu;        /* lower 16 bits */
48           accu >>= sizeof(unsigned short) * 8;
49           accu += (unsigned long) __rand48_mult[0] * (unsigned long) xseed[1];
50           accu += (unsigned long) __rand48_mult[1] * (unsigned long) xseed[0];
51           temp[1] = (unsigned short) accu;        /* middle 16 bits */
52           accu >>= sizeof(unsigned short) * 8;
53           accu += (unsigned long) __rand48_mult[0] * (unsigned long) xseed[2];
54           accu += (unsigned long) __rand48_mult[1] * (unsigned long) xseed[1];
55           accu += (unsigned long) __rand48_mult[2] * (unsigned long) xseed[0];
56           xseed[0] = temp[0];
57           xseed[1] = temp[1];
58           xseed[2] = (unsigned short) accu;
59 }
60