const_mod.hpp 5.4 KB

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