Chris@16
|
1 /* boost random/detail/const_mod.hpp header file
|
Chris@16
|
2 *
|
Chris@16
|
3 * Copyright Jens Maurer 2000-2001
|
Chris@16
|
4 * Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
5 * accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
6 * http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7 *
|
Chris@16
|
8 * See http://www.boost.org for most recent version including documentation.
|
Chris@16
|
9 *
|
Chris@101
|
10 * $Id$
|
Chris@16
|
11 *
|
Chris@16
|
12 * Revision history
|
Chris@16
|
13 * 2001-02-18 moved to individual header files
|
Chris@16
|
14 */
|
Chris@16
|
15
|
Chris@16
|
16 #ifndef BOOST_RANDOM_CONST_MOD_HPP
|
Chris@16
|
17 #define BOOST_RANDOM_CONST_MOD_HPP
|
Chris@16
|
18
|
Chris@16
|
19 #include <boost/assert.hpp>
|
Chris@16
|
20 #include <boost/static_assert.hpp>
|
Chris@16
|
21 #include <boost/integer_traits.hpp>
|
Chris@16
|
22 #include <boost/type_traits/make_unsigned.hpp>
|
Chris@16
|
23 #include <boost/random/detail/large_arithmetic.hpp>
|
Chris@16
|
24
|
Chris@16
|
25 #include <boost/random/detail/disable_warnings.hpp>
|
Chris@16
|
26
|
Chris@16
|
27 namespace boost {
|
Chris@16
|
28 namespace random {
|
Chris@16
|
29
|
Chris@16
|
30 template<class IntType, IntType m>
|
Chris@16
|
31 class const_mod
|
Chris@16
|
32 {
|
Chris@16
|
33 public:
|
Chris@16
|
34 static IntType apply(IntType x)
|
Chris@16
|
35 {
|
Chris@16
|
36 if(((unsigned_m() - 1) & unsigned_m()) == 0)
|
Chris@16
|
37 return (unsigned_type(x)) & (unsigned_m() - 1);
|
Chris@16
|
38 else {
|
Chris@101
|
39 IntType suppress_warnings = (m == 0);
|
Chris@101
|
40 BOOST_ASSERT(suppress_warnings == 0);
|
Chris@101
|
41 return x % (m + suppress_warnings);
|
Chris@16
|
42 }
|
Chris@16
|
43 }
|
Chris@16
|
44
|
Chris@16
|
45 static IntType add(IntType x, IntType c)
|
Chris@16
|
46 {
|
Chris@16
|
47 if(((unsigned_m() - 1) & unsigned_m()) == 0)
|
Chris@16
|
48 return (unsigned_type(x) + unsigned_type(c)) & (unsigned_m() - 1);
|
Chris@16
|
49 else if(c == 0)
|
Chris@16
|
50 return x;
|
Chris@16
|
51 else if(x < m - c)
|
Chris@16
|
52 return x + c;
|
Chris@16
|
53 else
|
Chris@16
|
54 return x - (m - c);
|
Chris@16
|
55 }
|
Chris@16
|
56
|
Chris@16
|
57 static IntType mult(IntType a, IntType x)
|
Chris@16
|
58 {
|
Chris@16
|
59 if(((unsigned_m() - 1) & unsigned_m()) == 0)
|
Chris@16
|
60 return unsigned_type(a) * unsigned_type(x) & (unsigned_m() - 1);
|
Chris@16
|
61 else if(a == 0)
|
Chris@16
|
62 return 0;
|
Chris@16
|
63 else if(a == 1)
|
Chris@16
|
64 return x;
|
Chris@16
|
65 else if(m <= traits::const_max/a) // i.e. a*m <= max
|
Chris@16
|
66 return mult_small(a, x);
|
Chris@16
|
67 else if(traits::is_signed && (m%a < m/a))
|
Chris@16
|
68 return mult_schrage(a, x);
|
Chris@16
|
69 else
|
Chris@16
|
70 return mult_general(a, x);
|
Chris@16
|
71 }
|
Chris@16
|
72
|
Chris@16
|
73 static IntType mult_add(IntType a, IntType x, IntType c)
|
Chris@16
|
74 {
|
Chris@16
|
75 if(((unsigned_m() - 1) & unsigned_m()) == 0)
|
Chris@16
|
76 return (unsigned_type(a) * unsigned_type(x) + unsigned_type(c)) & (unsigned_m() - 1);
|
Chris@16
|
77 else if(a == 0)
|
Chris@16
|
78 return c;
|
Chris@16
|
79 else if(m <= (traits::const_max-c)/a) { // i.e. a*m+c <= max
|
Chris@101
|
80 IntType suppress_warnings = (m == 0);
|
Chris@101
|
81 BOOST_ASSERT(suppress_warnings == 0);
|
Chris@101
|
82 return (a*x+c) % (m + suppress_warnings);
|
Chris@16
|
83 } else
|
Chris@16
|
84 return add(mult(a, x), c);
|
Chris@16
|
85 }
|
Chris@16
|
86
|
Chris@16
|
87 static IntType pow(IntType a, boost::uintmax_t exponent)
|
Chris@16
|
88 {
|
Chris@16
|
89 IntType result = 1;
|
Chris@16
|
90 while(exponent != 0) {
|
Chris@16
|
91 if(exponent % 2 == 1) {
|
Chris@16
|
92 result = mult(result, a);
|
Chris@16
|
93 }
|
Chris@16
|
94 a = mult(a, a);
|
Chris@16
|
95 exponent /= 2;
|
Chris@16
|
96 }
|
Chris@16
|
97 return result;
|
Chris@16
|
98 }
|
Chris@16
|
99
|
Chris@16
|
100 static IntType invert(IntType x)
|
Chris@16
|
101 { return x == 0 ? 0 : (m == 0? invert_euclidian0(x) : invert_euclidian(x)); }
|
Chris@16
|
102
|
Chris@16
|
103 private:
|
Chris@16
|
104 typedef integer_traits<IntType> traits;
|
Chris@16
|
105 typedef typename make_unsigned<IntType>::type unsigned_type;
|
Chris@16
|
106
|
Chris@16
|
107 const_mod(); // don't instantiate
|
Chris@16
|
108
|
Chris@16
|
109 static IntType mult_small(IntType a, IntType x)
|
Chris@16
|
110 {
|
Chris@101
|
111 IntType suppress_warnings = (m == 0);
|
Chris@101
|
112 BOOST_ASSERT(suppress_warnings == 0);
|
Chris@101
|
113 return a*x % (m + suppress_warnings);
|
Chris@16
|
114 }
|
Chris@16
|
115
|
Chris@16
|
116 static IntType mult_schrage(IntType a, IntType value)
|
Chris@16
|
117 {
|
Chris@16
|
118 const IntType q = m / a;
|
Chris@16
|
119 const IntType r = m % a;
|
Chris@16
|
120
|
Chris@16
|
121 BOOST_ASSERT(r < q); // check that overflow cannot happen
|
Chris@16
|
122
|
Chris@16
|
123 return sub(a*(value%q), r*(value/q));
|
Chris@16
|
124 }
|
Chris@16
|
125
|
Chris@16
|
126 static IntType mult_general(IntType a, IntType b)
|
Chris@16
|
127 {
|
Chris@16
|
128 IntType suppress_warnings = (m == 0);
|
Chris@16
|
129 BOOST_ASSERT(suppress_warnings == 0);
|
Chris@16
|
130 IntType modulus = m + suppress_warnings;
|
Chris@16
|
131 BOOST_ASSERT(modulus == m);
|
Chris@16
|
132 if(::boost::uintmax_t(modulus) <=
|
Chris@16
|
133 (::std::numeric_limits< ::boost::uintmax_t>::max)() / modulus)
|
Chris@16
|
134 {
|
Chris@16
|
135 return static_cast<IntType>(boost::uintmax_t(a) * b % modulus);
|
Chris@16
|
136 } else {
|
Chris@16
|
137 return static_cast<IntType>(detail::mulmod(a, b, modulus));
|
Chris@16
|
138 }
|
Chris@16
|
139 }
|
Chris@16
|
140
|
Chris@16
|
141 static IntType sub(IntType a, IntType b)
|
Chris@16
|
142 {
|
Chris@16
|
143 if(a < b)
|
Chris@16
|
144 return m - (b - a);
|
Chris@16
|
145 else
|
Chris@16
|
146 return a - b;
|
Chris@16
|
147 }
|
Chris@16
|
148
|
Chris@16
|
149 static unsigned_type unsigned_m()
|
Chris@16
|
150 {
|
Chris@16
|
151 if(m == 0) {
|
Chris@16
|
152 return unsigned_type((std::numeric_limits<IntType>::max)()) + 1;
|
Chris@16
|
153 } else {
|
Chris@16
|
154 return unsigned_type(m);
|
Chris@16
|
155 }
|
Chris@16
|
156 }
|
Chris@16
|
157
|
Chris@16
|
158 // invert c in the finite field (mod m) (m must be prime)
|
Chris@16
|
159 static IntType invert_euclidian(IntType c)
|
Chris@16
|
160 {
|
Chris@16
|
161 // we are interested in the gcd factor for c, because this is our inverse
|
Chris@16
|
162 BOOST_ASSERT(c > 0);
|
Chris@16
|
163 IntType l1 = 0;
|
Chris@16
|
164 IntType l2 = 1;
|
Chris@16
|
165 IntType n = c;
|
Chris@16
|
166 IntType p = m;
|
Chris@16
|
167 for(;;) {
|
Chris@16
|
168 IntType q = p / n;
|
Chris@16
|
169 l1 += q * l2;
|
Chris@16
|
170 p -= q * n;
|
Chris@16
|
171 if(p == 0)
|
Chris@16
|
172 return l2;
|
Chris@16
|
173 IntType q2 = n / p;
|
Chris@16
|
174 l2 += q2 * l1;
|
Chris@16
|
175 n -= q2 * p;
|
Chris@16
|
176 if(n == 0)
|
Chris@16
|
177 return m - l1;
|
Chris@16
|
178 }
|
Chris@16
|
179 }
|
Chris@16
|
180
|
Chris@16
|
181 // invert c in the finite field (mod m) (c must be relatively prime to m)
|
Chris@16
|
182 static IntType invert_euclidian0(IntType c)
|
Chris@16
|
183 {
|
Chris@16
|
184 // we are interested in the gcd factor for c, because this is our inverse
|
Chris@16
|
185 BOOST_ASSERT(c > 0);
|
Chris@16
|
186 if(c == 1) return 1;
|
Chris@16
|
187 IntType l1 = 0;
|
Chris@16
|
188 IntType l2 = 1;
|
Chris@16
|
189 IntType n = c;
|
Chris@16
|
190 IntType p = m;
|
Chris@16
|
191 IntType max = (std::numeric_limits<IntType>::max)();
|
Chris@16
|
192 IntType q = max / n;
|
Chris@16
|
193 BOOST_ASSERT(max % n != n - 1 && "c must be relatively prime to m.");
|
Chris@16
|
194 l1 += q * l2;
|
Chris@16
|
195 p = max - q * n + 1;
|
Chris@16
|
196 for(;;) {
|
Chris@16
|
197 if(p == 0)
|
Chris@16
|
198 return l2;
|
Chris@16
|
199 IntType q2 = n / p;
|
Chris@16
|
200 l2 += q2 * l1;
|
Chris@16
|
201 n -= q2 * p;
|
Chris@16
|
202 if(n == 0)
|
Chris@16
|
203 return m - l1;
|
Chris@16
|
204 q = p / n;
|
Chris@16
|
205 l1 += q * l2;
|
Chris@16
|
206 p -= q * n;
|
Chris@16
|
207 }
|
Chris@16
|
208 }
|
Chris@16
|
209 };
|
Chris@16
|
210
|
Chris@16
|
211 } // namespace random
|
Chris@16
|
212 } // namespace boost
|
Chris@16
|
213
|
Chris@16
|
214 #include <boost/random/detail/enable_warnings.hpp>
|
Chris@16
|
215
|
Chris@16
|
216 #endif // BOOST_RANDOM_CONST_MOD_HPP
|