1 /* boost random/detail/const_mod.hpp header file
\r
3 * Copyright Jens Maurer 2000-2001
\r
4 * Distributed under the Boost Software License, Version 1.0. (See
\r
5 * accompanying file LICENSE_1_0.txt or copy at
\r
6 * http://www.boost.org/LICENSE_1_0.txt)
\r
8 * See http://www.boost.org for most recent version including documentation.
\r
10 * $Id: const_mod.hpp 58649 2010-01-02 21:23:17Z steven_watanabe $
\r
13 * 2001-02-18 moved to individual header files
\r
16 #ifndef BOOST_RANDOM_CONST_MOD_HPP
\r
17 #define BOOST_RANDOM_CONST_MOD_HPP
\r
20 #include <boost/static_assert.hpp>
\r
21 #include <boost/cstdint.hpp>
\r
22 #include <boost/integer_traits.hpp>
\r
23 #include <boost/detail/workaround.hpp>
\r
25 #include <boost/random/detail/disable_warnings.hpp>
\r
31 * Some random number generators require modular arithmetic. Put
\r
32 * everything we need here.
\r
33 * IntType must be an integral type.
\r
38 template<bool is_signed>
\r
45 template<class IntType>
\r
46 static IntType add(IntType m, IntType x, IntType c)
\r
56 struct do_add<false>
\r
58 template<class IntType>
\r
59 static IntType add(IntType, IntType, IntType)
\r
62 assert(!"const_mod::add with c too large");
\r
66 } // namespace detail
\r
68 #if !(defined(__BORLANDC__) && (__BORLANDC__ == 0x560))
\r
70 template<class IntType, IntType m>
\r
74 static IntType add(IntType x, IntType c)
\r
78 else if(c <= traits::const_max - m) // i.e. m+c < max
\r
79 return add_small(x, c);
\r
81 return detail::do_add<traits::is_signed>::add(m, x, c);
\r
84 static IntType mult(IntType a, IntType x)
\r
88 else if(m <= traits::const_max/a) // i.e. a*m <= max
\r
89 return mult_small(a, x);
\r
90 else if(traits::is_signed && (m%a < m/a))
\r
91 return mult_schrage(a, x);
\r
94 assert(!"const_mod::mult with a too large");
\r
99 static IntType mult_add(IntType a, IntType x, IntType c)
\r
101 if(m <= (traits::const_max-c)/a) // i.e. a*m+c <= max
\r
102 return (a*x+c) % m;
\r
104 return add(mult(a, x), c);
\r
107 static IntType invert(IntType x)
\r
108 { return x == 0 ? 0 : invert_euclidian(x); }
\r
111 typedef integer_traits<IntType> traits;
\r
113 const_mod(); // don't instantiate
\r
115 static IntType add_small(IntType x, IntType c)
\r
123 static IntType mult_small(IntType a, IntType x)
\r
128 static IntType mult_schrage(IntType a, IntType value)
\r
130 const IntType q = m / a;
\r
131 const IntType r = m % a;
\r
133 assert(r < q); // check that overflow cannot happen
\r
135 value = a*(value%q) - r*(value/q);
\r
136 // An optimizer bug in the SGI MIPSpro 7.3.1.x compiler requires this
\r
137 // convoluted formulation of the loop (Synge Todo)
\r
146 // invert c in the finite field (mod m) (m must be prime)
\r
147 static IntType invert_euclidian(IntType c)
\r
149 // we are interested in the gcd factor for c, because this is our inverse
\r
150 BOOST_STATIC_ASSERT(m > 0);
\r
151 #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
\r
152 assert(boost::integer_traits<IntType>::is_signed);
\r
153 #elif !defined(BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS)
\r
154 BOOST_STATIC_ASSERT(boost::integer_traits<IntType>::is_signed);
\r
163 l1 -= q * l2; // this requires a signed IntType!
\r
166 return (l2 < 1 ? l2 + m : l2);
\r
167 IntType q2 = n / p;
\r
171 return (l1 < 1 ? l1 + m : l1);
\r
176 // The modulus is exactly the word size: rely on machine overflow handling.
\r
177 // Due to a GCC bug, we cannot partially specialize in the presence of
\r
178 // template value parameters.
\r
180 class const_mod<unsigned int, 0>
\r
182 typedef unsigned int IntType;
\r
184 static IntType add(IntType x, IntType c) { return x+c; }
\r
185 static IntType mult(IntType a, IntType x) { return a*x; }
\r
186 static IntType mult_add(IntType a, IntType x, IntType c) { return a*x+c; }
\r
188 // m is not prime, thus invert is not useful
\r
189 private: // don't instantiate
\r
194 class const_mod<unsigned long, 0>
\r
196 typedef unsigned long IntType;
\r
198 static IntType add(IntType x, IntType c) { return x+c; }
\r
199 static IntType mult(IntType a, IntType x) { return a*x; }
\r
200 static IntType mult_add(IntType a, IntType x, IntType c) { return a*x+c; }
\r
202 // m is not prime, thus invert is not useful
\r
203 private: // don't instantiate
\r
207 // the modulus is some power of 2: rely partly on machine overflow handling
\r
208 // we only specialize for rand48 at the moment
\r
209 #ifndef BOOST_NO_INT64_T
\r
211 class const_mod<uint64_t, uint64_t(1) << 48>
\r
213 typedef uint64_t IntType;
\r
215 static IntType add(IntType x, IntType c) { return c == 0 ? x : mod(x+c); }
\r
216 static IntType mult(IntType a, IntType x) { return mod(a*x); }
\r
217 static IntType mult_add(IntType a, IntType x, IntType c)
\r
218 { return mod(a*x+c); }
\r
219 static IntType mod(IntType x) { return x &= ((uint64_t(1) << 48)-1); }
\r
221 // m is not prime, thus invert is not useful
\r
222 private: // don't instantiate
\r
225 #endif /* !BOOST_NO_INT64_T */
\r
230 // for some reason Borland C++ Builder 6 has problems with
\r
231 // the full specialisations of const_mod, define a generic version
\r
232 // instead, the compiler will optimise away the const-if statements:
\r
235 template<class IntType, IntType m>
\r
239 static IntType add(IntType x, IntType c)
\r
249 else if(c <= traits::const_max - m) // i.e. m+c < max
\r
250 return add_small(x, c);
\r
252 return detail::do_add<traits::is_signed>::add(m, x, c);
\r
256 static IntType mult(IntType a, IntType x)
\r
266 else if(m <= traits::const_max/a) // i.e. a*m <= max
\r
267 return mult_small(a, x);
\r
268 else if(traits::is_signed && (m%a < m/a))
\r
269 return mult_schrage(a, x);
\r
272 assert(!"const_mod::mult with a too large");
\r
278 static IntType mult_add(IntType a, IntType x, IntType c)
\r
286 if(m <= (traits::const_max-c)/a) // i.e. a*m+c <= max
\r
287 return (a*x+c) % m;
\r
289 return add(mult(a, x), c);
\r
293 static IntType invert(IntType x)
\r
294 { return x == 0 ? 0 : invert_euclidian(x); }
\r
297 typedef integer_traits<IntType> traits;
\r
299 const_mod(); // don't instantiate
\r
301 static IntType add_small(IntType x, IntType c)
\r
309 static IntType mult_small(IntType a, IntType x)
\r
314 static IntType mult_schrage(IntType a, IntType value)
\r
316 const IntType q = m / a;
\r
317 const IntType r = m % a;
\r
319 assert(r < q); // check that overflow cannot happen
\r
321 value = a*(value%q) - r*(value/q);
\r
327 // invert c in the finite field (mod m) (m must be prime)
\r
328 static IntType invert_euclidian(IntType c)
\r
330 // we are interested in the gcd factor for c, because this is our inverse
\r
331 BOOST_STATIC_ASSERT(m > 0);
\r
332 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
\r
333 BOOST_STATIC_ASSERT(boost::integer_traits<IntType>::is_signed);
\r
342 l1 -= q * l2; // this requires a signed IntType!
\r
345 return (l2 < 1 ? l2 + m : l2);
\r
346 IntType q2 = n / p;
\r
350 return (l1 < 1 ? l1 + m : l1);
\r
358 } // namespace random
\r
359 } // namespace boost
\r
361 #include <boost/random/detail/enable_warnings.hpp>
\r
363 #endif // BOOST_RANDOM_CONST_MOD_HPP
\r