xref: /trafficserver/src/tscore/ink_rand.cc (revision 4cfd5a73)
1 /** @file
2 
3    A C-program for MT19937-64 (2004/9/29 version).
4    Coded by Takuji Nishimura and Makoto Matsumoto.
5 
6    This is a 64-bit version of Mersenne Twister pseudorandom number
7    generator.
8 
9    Before using, initialize the state by using init_genrand64(seed)
10    or init_by_array64(init_key, key_length).
11 
12    Copyright (C) 2004, Makoto Matsumoto and Takuji Nishimura,
13    All rights reserved.
14 
15    Redistribution and use in source and binary forms, with or without
16    modification, are permitted provided that the following conditions
17    are met:
18 
19      1. Redistributions of source code must retain the above copyright
20         notice, this list of conditions and the following disclaimer.
21 
22      2. Redistributions in binary form must reproduce the above copyright
23         notice, this list of conditions and the following disclaimer in the
24         documentation and/or other materials provided with the distribution.
25 
26      3. The names of its contributors may not be used to endorse or promote
27         products derived from this software without specific prior written
28         permission.
29 
30    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
35    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
36    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
37    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
38    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
39    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
40    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 
42    References:
43    T. Nishimura, ``Tables of 64-bit Mersenne Twisters''
44      ACM Transactions on Modeling and
45      Computer Simulation 10. (2000) 348--357.
46    M. Matsumoto and T. Nishimura,
47      ``Mersenne Twister: a 623-dimensionally equidistributed
48        uniform pseudorandom number generator''
49      ACM Transactions on Modeling and
50      Computer Simulation 8. (Jan. 1998) 3--30.
51 
52    Any feedback is very welcome.
53    http://www.math.hiroshima-u.ac.jp/~m-mat/MT/emt.html
54    email: m-mat @ math.sci.hiroshima-u.ac.jp (remove spaces)
55 */
56 
57 #include "tscore/ink_platform.h"
58 #include "tscore/ink_rand.h"
59 
60 #define NN 312
61 #define MM 156
62 #define MATRIX_A 0xB5026F5AA96619E9ULL
63 #define UM 0xFFFFFFFF80000000ULL /* Most significant 33 bits */
64 #define LM 0x7FFFFFFFULL         /* Least significant 31 bits */
65 
66 static uint64_t mag01[2] = {0ULL, MATRIX_A};
67 
InkRand(uint64_t d)68 InkRand::InkRand(uint64_t d)
69 {
70   seed(d);
71 }
72 
73 void
seed(uint64_t seed)74 InkRand::seed(uint64_t seed)
75 {
76   mt[0] = seed;
77   for (mti = 1; mti < NN; mti++) {
78     mt[mti] = (6364136223846793005ULL * (mt[mti - 1] ^ (mt[mti - 1] >> 62)) + mti);
79   }
80 }
81 
82 uint64_t
random()83 InkRand::random()
84 {
85   int i;
86   uint64_t x;
87 
88   if (mti >= NN) { /* generate NN words at one time */
89     for (i = 0; i < NN - MM; i++) {
90       x     = (mt[i] & UM) | (mt[i + 1] & LM);
91       mt[i] = mt[i + MM] ^ (x >> 1) ^ mag01[static_cast<int>(x & 1ULL)];
92     }
93     for (; i < NN - 1; i++) {
94       x     = (mt[i] & UM) | (mt[i + 1] & LM);
95       mt[i] = mt[i + (MM - NN)] ^ (x >> 1) ^ mag01[static_cast<int>(x & 1ULL)];
96     }
97     x          = (mt[NN - 1] & UM) | (mt[0] & LM);
98     mt[NN - 1] = mt[MM - 1] ^ (x >> 1) ^ mag01[static_cast<int>(x & 1ULL)];
99 
100     mti = 0;
101   }
102 
103   x = mt[mti++];
104 
105   x ^= (x >> 29) & 0x5555555555555555ULL;
106   x ^= (x << 17) & 0x71D67FFFEDA60000ULL;
107   x ^= (x << 37) & 0xFFF7EEE000000000ULL;
108   x ^= (x >> 43);
109 
110   return x;
111 }
112 
113 double
drandom()114 InkRand::drandom()
115 {
116   return (random() >> 11) * (1.0 / 9007199254740991.0);
117 }
118