ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/utils.C
(Generate patch)

Comparing deliantra/server/common/utils.C (file contents):
Revision 1.96 by root, Mon Apr 5 20:33:13 2010 UTC vs.
Revision 1.97 by root, Fri Jul 2 02:00:47 2010 UTC

40 40
41#include <glib.h> 41#include <glib.h>
42 42
43refcnt_base::refcnt_t refcnt_dummy; 43refcnt_base::refcnt_t refcnt_dummy;
44ssize_t slice_alloc; 44ssize_t slice_alloc;
45rand_gen rndm, rmg_rndm;
46 45
47#if !GCC_VERSION(3,4) 46#if !GCC_VERSION(3,4)
48int least_significant_bit (uint32_t x) 47int least_significant_bit (uint32_t x)
49{ 48{
50 x &= -x; // this isolates the lowest bit 49 x &= -x; // this isolates the lowest bit
58 if (x & 0xffff0000) r += 16; 57 if (x & 0xffff0000) r += 16;
59 58
60 return r; 59 return r;
61} 60}
62#endif 61#endif
63
64void
65tausworthe_random_generator::seed (uint32_t seed)
66{
67 state [0] = seed * 69069U; if (state [0] < 2U) state [0] += 2U;
68 state [1] = state [0] * 69069U; if (state [0] < 8U) state [0] += 8U;
69 state [2] = state [1] * 69069U; if (state [0] < 16U) state [0] += 16U;
70 state [3] = state [2] * 69069U; if (state [0] < 128U) state [0] += 128U;
71
72 for (int i = 11; --i; )
73 next ();
74}
75
76uint32_t
77tausworthe_random_generator::next ()
78{
79 state [0] = ((state [0] & 0xFFFFFFFEU) << 18U) ^ (((state [0] << 6U) ^ state [0]) >> 13U);
80 state [1] = ((state [1] & 0xFFFFFFF8U) << 2U) ^ (((state [1] << 2U) ^ state [1]) >> 27U);
81 state [2] = ((state [2] & 0xFFFFFFF0U) << 7U) ^ (((state [2] << 13U) ^ state [2]) >> 21U);
82 state [3] = ((state [3] & 0xFFFFFF80U) << 13U) ^ (((state [3] << 3U) ^ state [3]) >> 12U);
83
84 return state [0] ^ state [1] ^ state [2] ^ state [3];
85}
86
87template<class generator>
88uint32_t
89random_number_generator<generator>::get_range (uint32_t num)
90{
91 return (this->next () * (uint64_t)num) >> 32U;
92}
93
94// return a number within (min .. max)
95template<class generator>
96int
97random_number_generator<generator>::get_range (int r_min, int r_max)
98{
99 return r_min + get_range (max (r_max - r_min + 1, 0));
100}
101
102template struct random_number_generator<tausworthe_random_generator>;
103template struct random_number_generator<xorshift_random_generator>;
104 62
105/******************************************************************************/ 63/******************************************************************************/
106 64
107/* Checks a player-provided string which will become the msg property of 65/* Checks a player-provided string which will become the msg property of
108 * an object for dangerous input. 66 * an object for dangerous input.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines