ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/rng.C
Revision: 1.1
Committed: Fri Jul 2 02:00:47 2010 UTC (14 years ago) by root
Content type: text/plain
Branch: MAIN
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 /*
2     * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3     *
4     * Copyright (©) 2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5     *
6     * Deliantra is free software: you can redistribute it and/or modify it under
7     * the terms of the Affero GNU General Public License as published by the
8     * Free Software Foundation, either version 3 of the License, or (at your
9     * option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the Affero GNU General Public License
17     * and the GNU General Public License along with this program. If not, see
18     * <http://www.gnu.org/licenses/>.
19     *
20     * The authors can be reached via e-mail to <support@deliantra.net>
21     */
22    
23     #include <global.h>
24    
25     rand_gen rndm, rmg_rndm;
26    
27     void noinline
28     tausworthe_random_generator::seed (uint32_t seed)
29     {
30     state [0] = seed * 69069U; if (state [0] < 2U) state [0] += 2U;
31     state [1] = state [0] * 69069U; if (state [1] < 8U) state [1] += 8U;
32     state [2] = state [1] * 69069U; if (state [2] < 16U) state [2] += 16U;
33     state [3] = state [2] * 69069U; if (state [3] < 128U) state [3] += 128U;
34    
35     for (int i = 11; --i; )
36     next ();
37     }
38    
39     uint32_t
40     tausworthe_random_generator::next ()
41     {
42     state [0] = ((state [0] & 0xFFFFFFFEU) << 18U) ^ (((state [0] << 6U) ^ state [0]) >> 13U);
43     state [1] = ((state [1] & 0xFFFFFFF8U) << 2U) ^ (((state [1] << 2U) ^ state [1]) >> 27U);
44     state [2] = ((state [2] & 0xFFFFFFF0U) << 7U) ^ (((state [2] << 13U) ^ state [2]) >> 21U);
45     state [3] = ((state [3] & 0xFFFFFF80U) << 13U) ^ (((state [3] << 3U) ^ state [3]) >> 12U);
46    
47     return state [0] ^ state [1] ^ state [2] ^ state [3];
48     }
49    
50     void noinline
51     r250521_random_generator::seed (uint32_t seed)
52     {
53     xorshift_random_generator rng;
54    
55     rng.seed (seed);
56    
57     r250.seed_rng (rng);
58     r521.seed_rng (rng);
59     }
60    
61     uint32_t
62     r250521_random_generator::next ()
63     {
64     return r250.next () ^ r521.next ();
65     }
66    
67     template<class generator>
68     uint32_t
69     random_number_generator<generator>::get_range (uint32_t num)
70     {
71     return (this->next () * (uint64_t)num) >> 32U;
72     }
73    
74     template<class generator>
75     uint32_t noinline
76     random_number_generator<generator>::get_u32 ()
77     {
78     return generator::next ();
79     }
80    
81     template<class generator>
82     uint64_t noinline
83     random_number_generator<generator>::get_u64 ()
84     {
85     return (uint64_t (get_u32 ()) << 32U) | get_u32 ();
86     }
87    
88     // return a number within (min .. max)
89     template<class generator>
90     int
91     random_number_generator<generator>::get_range (int r_min, int r_max)
92     {
93     return r_min + get_range (max (r_max - r_min + 1, 0));
94     }
95    
96     template struct random_number_generator<tausworthe_random_generator>;
97     template struct random_number_generator<xorshift_random_generator>;
98     template struct random_number_generator<freeciv_random_generator>;
99     template struct random_number_generator<r250_random_generator>;
100     template struct random_number_generator<r521_random_generator>;
101     template struct random_number_generator<r250521_random_generator>;
102