Remove the encoding *numbers* from the comments. They are useless, and
[PostgreSQL.git] / src / port / rand.c
blobe72b2bd50098dedeaa05e91e03f31066df77d2a6
1 /*
2 * $PostgreSQL:$
4 *-------------------------------------------------------------------------
6 * rand.c
7 * Missing rand implementations for Win32
9 *-------------------------------------------------------------------------
11 #include "c.h"
14 * Copyright (c) 1993 Martin Birgmeier
15 * All rights reserved.
17 * You may redistribute unmodified or modified versions of this source
18 * code provided that the above copyright notice and this and the
19 * following conditions are retained.
21 * This software is provided ``as is'', and comes with no warranties
22 * of any kind. I shall in no event be liable for anything that happens
23 * to anyone/anything when using this software.
25 #define RAND48_SEED_0 (0x330e)
26 #define RAND48_SEED_1 (0xabcd)
27 #define RAND48_SEED_2 (0x1234)
28 #define RAND48_MULT_0 (0xe66d)
29 #define RAND48_MULT_1 (0xdeec)
30 #define RAND48_MULT_2 (0x0005)
31 #define RAND48_ADD (0x000b)
33 unsigned short _rand48_seed[3] = {
34 RAND48_SEED_0,
35 RAND48_SEED_1,
36 RAND48_SEED_2
38 unsigned short _rand48_mult[3] = {
39 RAND48_MULT_0,
40 RAND48_MULT_1,
41 RAND48_MULT_2
43 unsigned short _rand48_add = RAND48_ADD;
45 static void
46 _dorand48(unsigned short xseed[3])
48 unsigned long accu;
49 unsigned short temp[2];
51 accu = (unsigned long) _rand48_mult[0] * (unsigned long) xseed[0] +
52 (unsigned long) _rand48_add;
53 temp[0] = (unsigned short) accu; /* lower 16 bits */
54 accu >>= sizeof(unsigned short) * 8;
55 accu += (unsigned long) _rand48_mult[0] * (unsigned long) xseed[1] +
56 (unsigned long) _rand48_mult[1] * (unsigned long) xseed[0];
57 temp[1] = (unsigned short) accu; /* middle 16 bits */
58 accu >>= sizeof(unsigned short) * 8;
59 accu += _rand48_mult[0] * xseed[2] + _rand48_mult[1] * xseed[1] + _rand48_mult[2] * xseed[0];
60 xseed[0] = temp[0];
61 xseed[1] = temp[1];
62 xseed[2] = (unsigned short) accu;
65 long
66 lrand48(void)
68 _dorand48(_rand48_seed);
69 return ((long) _rand48_seed[2] << 15) + ((long) _rand48_seed[1] >> 1);
72 void
73 srand48(long seed)
75 _rand48_seed[0] = RAND48_SEED_0;
76 _rand48_seed[1] = (unsigned short) seed;
77 _rand48_seed[2] = (unsigned short) (seed > 16);
78 _rand48_mult[0] = RAND48_MULT_0;
79 _rand48_mult[1] = RAND48_MULT_1;
80 _rand48_mult[2] = RAND48_MULT_2;
81 _rand48_add = RAND48_ADD;