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