seed_impl.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /* boost random/detail/seed.hpp header file
  2. *
  3. * Copyright Steven Watanabe 2009
  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: seed_impl.hpp 72951 2011-07-07 04:57:37Z steven_watanabe $
  11. */
  12. #ifndef BOOST_RANDOM_DETAIL_SEED_IMPL_HPP
  13. #define BOOST_RANDOM_DETAIL_SEED_IMPL_HPP
  14. #include <stdexcept>
  15. #include <boost/cstdint.hpp>
  16. #include <boost/config/no_tr1/cmath.hpp>
  17. #include <boost/integer/integer_mask.hpp>
  18. #include <boost/integer/static_log2.hpp>
  19. #include <boost/type_traits/is_signed.hpp>
  20. #include <boost/type_traits/is_integral.hpp>
  21. #include <boost/type_traits/make_unsigned.hpp>
  22. #include <boost/mpl/bool.hpp>
  23. #include <boost/mpl/if.hpp>
  24. #include <boost/mpl/int.hpp>
  25. #include <boost/random/detail/const_mod.hpp>
  26. #include <boost/random/detail/integer_log2.hpp>
  27. #include <boost/random/detail/signed_unsigned_tools.hpp>
  28. #include <boost/random/detail/generator_bits.hpp>
  29. #include <boost/random/detail/disable_warnings.hpp>
  30. namespace boost {
  31. namespace random {
  32. namespace detail {
  33. // finds the seed type of an engine, given its
  34. // result_type. If the result_type is integral
  35. // the seed type is the same. If the result_type
  36. // is floating point, the seed type is uint32_t
  37. template<class T>
  38. struct seed_type
  39. {
  40. typedef typename boost::mpl::if_<boost::is_integral<T>,
  41. T,
  42. boost::uint32_t
  43. >::type type;
  44. };
  45. template<int N>
  46. struct const_pow_impl
  47. {
  48. template<class T>
  49. static T call(T arg, int n, T result)
  50. {
  51. return const_pow_impl<N / 2>::call(arg * arg, n / 2,
  52. n%2 == 0? result : result * arg);
  53. }
  54. };
  55. template<>
  56. struct const_pow_impl<0>
  57. {
  58. template<class T>
  59. static T call(T, int, T result)
  60. {
  61. return result;
  62. }
  63. };
  64. // requires N is an upper bound on n
  65. template<int N, class T>
  66. inline T const_pow(T arg, int n) { return const_pow_impl<N>::call(arg, n, T(1)); }
  67. template<class T>
  68. inline T pow2(int n)
  69. {
  70. typedef unsigned int_type;
  71. const int max_bits = std::numeric_limits<int_type>::digits;
  72. T multiplier = T(int_type(1) << (max_bits - 1)) * 2;
  73. return (int_type(1) << (n % max_bits)) *
  74. const_pow<std::numeric_limits<T>::digits / max_bits>(multiplier, n / max_bits);
  75. }
  76. template<class Engine, class Iter>
  77. void generate_from_real(Engine& eng, Iter begin, Iter end)
  78. {
  79. using std::fmod;
  80. typedef typename Engine::result_type RealType;
  81. const int Bits = detail::generator_bits<Engine>::value();
  82. int remaining_bits = 0;
  83. boost::uint_least32_t saved_bits = 0;
  84. RealType multiplier = pow2<RealType>( Bits);
  85. RealType mult32 = RealType(4294967296.0); // 2^32
  86. while(true) {
  87. RealType val = eng() * multiplier;
  88. int available_bits = Bits;
  89. // Make sure the compiler can optimize this out
  90. // if it isn't possible.
  91. if(Bits < 32 && available_bits < 32 - remaining_bits) {
  92. saved_bits |= boost::uint_least32_t(val) << remaining_bits;
  93. remaining_bits += Bits;
  94. } else {
  95. // If Bits < 32, then remaining_bits != 0, since
  96. // if remaining_bits == 0, available_bits < 32 - 0,
  97. // and we won't get here to begin with.
  98. if(Bits < 32 || remaining_bits != 0) {
  99. boost::uint_least32_t divisor =
  100. (boost::uint_least32_t(1) << (32 - remaining_bits));
  101. boost::uint_least32_t extra_bits = boost::uint_least32_t(fmod(val, mult32)) & (divisor - 1);
  102. val = val / divisor;
  103. *begin++ = saved_bits | (extra_bits << remaining_bits);
  104. if(begin == end) return;
  105. available_bits -= 32 - remaining_bits;
  106. remaining_bits = 0;
  107. }
  108. // If Bits < 32 we should never enter this loop
  109. if(Bits >= 32) {
  110. for(; available_bits >= 32; available_bits -= 32) {
  111. boost::uint_least32_t word = boost::uint_least32_t(fmod(val, mult32));
  112. val /= mult32;
  113. *begin++ = word;
  114. if(begin == end) return;
  115. }
  116. }
  117. remaining_bits = available_bits;
  118. saved_bits = static_cast<boost::uint_least32_t>(val);
  119. }
  120. }
  121. }
  122. template<class Engine, class Iter>
  123. void generate_from_int(Engine& eng, Iter begin, Iter end)
  124. {
  125. typedef typename Engine::result_type IntType;
  126. typedef typename boost::make_unsigned<IntType>::type unsigned_type;
  127. int remaining_bits = 0;
  128. boost::uint_least32_t saved_bits = 0;
  129. unsigned_type range = boost::random::detail::subtract<IntType>()((eng.max)(), (eng.min)());
  130. int bits =
  131. (range == (std::numeric_limits<unsigned_type>::max)()) ?
  132. std::numeric_limits<unsigned_type>::digits :
  133. detail::integer_log2(range + 1);
  134. {
  135. int discarded_bits = detail::integer_log2(bits);
  136. unsigned_type excess = (range + 1) >> (bits - discarded_bits);
  137. if(excess != 0) {
  138. int extra_bits = detail::integer_log2((excess - 1) ^ excess);
  139. bits = bits - discarded_bits + extra_bits;
  140. }
  141. }
  142. unsigned_type mask = (static_cast<unsigned_type>(2) << (bits - 1)) - 1;
  143. unsigned_type limit = ((range + 1) & ~mask) - 1;
  144. while(true) {
  145. unsigned_type val;
  146. do {
  147. val = boost::random::detail::subtract<IntType>()(eng(), (eng.min)());
  148. } while(limit != range && val > limit);
  149. val &= mask;
  150. int available_bits = bits;
  151. if(available_bits == 32) {
  152. *begin++ = static_cast<boost::uint_least32_t>(val) & 0xFFFFFFFFu;
  153. if(begin == end) return;
  154. } else if(available_bits % 32 == 0) {
  155. for(int i = 0; i < available_bits / 32; ++i) {
  156. boost::uint_least32_t word = boost::uint_least32_t(val) & 0xFFFFFFFFu;
  157. int supress_warning = (bits >= 32);
  158. BOOST_ASSERT(supress_warning == 1);
  159. val >>= (32 * supress_warning);
  160. *begin++ = word;
  161. if(begin == end) return;
  162. }
  163. } else if(bits < 32 && available_bits < 32 - remaining_bits) {
  164. saved_bits |= boost::uint_least32_t(val) << remaining_bits;
  165. remaining_bits += bits;
  166. } else {
  167. if(bits < 32 || remaining_bits != 0) {
  168. boost::uint_least32_t extra_bits = boost::uint_least32_t(val) & ((boost::uint_least32_t(1) << (32 - remaining_bits)) - 1);
  169. val >>= 32 - remaining_bits;
  170. *begin++ = saved_bits | (extra_bits << remaining_bits);
  171. if(begin == end) return;
  172. available_bits -= 32 - remaining_bits;
  173. remaining_bits = 0;
  174. }
  175. if(bits >= 32) {
  176. for(; available_bits >= 32; available_bits -= 32) {
  177. boost::uint_least32_t word = boost::uint_least32_t(val) & 0xFFFFFFFFu;
  178. int supress_warning = (bits >= 32);
  179. BOOST_ASSERT(supress_warning == 1);
  180. val >>= (32 * supress_warning);
  181. *begin++ = word;
  182. if(begin == end) return;
  183. }
  184. }
  185. remaining_bits = available_bits;
  186. saved_bits = static_cast<boost::uint_least32_t>(val);
  187. }
  188. }
  189. }
  190. template<class Engine, class Iter>
  191. void generate_impl(Engine& eng, Iter first, Iter last, boost::mpl::true_)
  192. {
  193. return detail::generate_from_int(eng, first, last);
  194. }
  195. template<class Engine, class Iter>
  196. void generate_impl(Engine& eng, Iter first, Iter last, boost::mpl::false_)
  197. {
  198. return detail::generate_from_real(eng, first, last);
  199. }
  200. template<class Engine, class Iter>
  201. void generate(Engine& eng, Iter first, Iter last)
  202. {
  203. return detail::generate_impl(eng, first, last, boost::is_integral<typename Engine::result_type>());
  204. }
  205. template<class IntType, IntType m, class SeedSeq>
  206. IntType seed_one_int(SeedSeq& seq)
  207. {
  208. static const int log = ::boost::mpl::if_c<(m == 0),
  209. ::boost::mpl::int_<(::std::numeric_limits<IntType>::digits)>,
  210. ::boost::static_log2<m> >::type::value;
  211. static const int k =
  212. (log + ((~(static_cast<IntType>(2) << (log - 1)) & m)? 32 : 31)) / 32;
  213. ::boost::uint_least32_t array[log / 32 + 4];
  214. seq.generate(&array[0], &array[0] + k + 3);
  215. IntType s = 0;
  216. for(int j = 0; j < k; ++j) {
  217. IntType digit = const_mod<IntType, m>::apply(IntType(array[j+3]));
  218. IntType mult = IntType(1) << 32*j;
  219. s = const_mod<IntType, m>::mult_add(mult, digit, s);
  220. }
  221. return s;
  222. }
  223. template<class IntType, IntType m, class Iter>
  224. IntType get_one_int(Iter& first, Iter last)
  225. {
  226. static const int log = ::boost::mpl::if_c<(m == 0),
  227. ::boost::mpl::int_<(::std::numeric_limits<IntType>::digits)>,
  228. ::boost::static_log2<m> >::type::value;
  229. static const int k =
  230. (log + ((~(static_cast<IntType>(2) << (log - 1)) & m)? 32 : 31)) / 32;
  231. IntType s = 0;
  232. for(int j = 0; j < k; ++j) {
  233. if(first == last) {
  234. throw ::std::invalid_argument("Not enough elements in call to seed.");
  235. }
  236. IntType digit = const_mod<IntType, m>::apply(IntType(*first++));
  237. IntType mult = IntType(1) << 32*j;
  238. s = const_mod<IntType, m>::mult_add(mult, digit, s);
  239. }
  240. return s;
  241. }
  242. // TODO: work in-place whenever possible
  243. template<int w, std::size_t n, class SeedSeq, class UIntType>
  244. void seed_array_int_impl(SeedSeq& seq, UIntType (&x)[n])
  245. {
  246. boost::uint_least32_t storage[((w+31)/32) * n];
  247. seq.generate(&storage[0], &storage[0] + ((w+31)/32) * n);
  248. for(std::size_t j = 0; j < n; j++) {
  249. UIntType val = 0;
  250. for(std::size_t k = 0; k < (w+31)/32; ++k) {
  251. val += static_cast<UIntType>(storage[(w+31)/32*j + k]) << 32*k;
  252. }
  253. x[j] = val & ::boost::low_bits_mask_t<w>::sig_bits;
  254. }
  255. }
  256. template<int w, std::size_t n, class SeedSeq, class IntType>
  257. inline void seed_array_int_impl(SeedSeq& seq, IntType (&x)[n], boost::mpl::true_)
  258. {
  259. typedef typename boost::make_unsigned<IntType>::type unsigned_array[n];
  260. seed_array_int_impl<w>(seq, reinterpret_cast<unsigned_array&>(x));
  261. }
  262. template<int w, std::size_t n, class SeedSeq, class IntType>
  263. inline void seed_array_int_impl(SeedSeq& seq, IntType (&x)[n], boost::mpl::false_)
  264. {
  265. seed_array_int_impl<w>(seq, x);
  266. }
  267. template<int w, std::size_t n, class SeedSeq, class IntType>
  268. inline void seed_array_int(SeedSeq& seq, IntType (&x)[n])
  269. {
  270. seed_array_int_impl<w>(seq, x, boost::is_signed<IntType>());
  271. }
  272. template<int w, std::size_t n, class Iter, class UIntType>
  273. void fill_array_int_impl(Iter& first, Iter last, UIntType (&x)[n])
  274. {
  275. for(std::size_t j = 0; j < n; j++) {
  276. UIntType val = 0;
  277. for(std::size_t k = 0; k < (w+31)/32; ++k) {
  278. if(first == last) {
  279. throw std::invalid_argument("Not enough elements in call to seed.");
  280. }
  281. val += static_cast<UIntType>(*first++) << 32*k;
  282. }
  283. x[j] = val & ::boost::low_bits_mask_t<w>::sig_bits;
  284. }
  285. }
  286. template<int w, std::size_t n, class Iter, class IntType>
  287. inline void fill_array_int_impl(Iter& first, Iter last, IntType (&x)[n], boost::mpl::true_)
  288. {
  289. typedef typename boost::make_unsigned<IntType>::type unsigned_array[n];
  290. fill_array_int_impl<w>(first, last, reinterpret_cast<unsigned_array&>(x));
  291. }
  292. template<int w, std::size_t n, class Iter, class IntType>
  293. inline void fill_array_int_impl(Iter& first, Iter last, IntType (&x)[n], boost::mpl::false_)
  294. {
  295. fill_array_int_impl<w>(first, last, x);
  296. }
  297. template<int w, std::size_t n, class Iter, class IntType>
  298. inline void fill_array_int(Iter& first, Iter last, IntType (&x)[n])
  299. {
  300. fill_array_int_impl<w>(first, last, x, boost::is_signed<IntType>());
  301. }
  302. template<int w, std::size_t n, class RealType>
  303. void seed_array_real_impl(const boost::uint_least32_t* storage, RealType (&x)[n])
  304. {
  305. boost::uint_least32_t mask = ~((~boost::uint_least32_t(0)) << (w%32));
  306. RealType two32 = 4294967296.0;
  307. const RealType divisor = RealType(1)/detail::pow2<RealType>(w);
  308. unsigned int j;
  309. for(j = 0; j < n; ++j) {
  310. RealType val = RealType(0);
  311. RealType mult = divisor;
  312. for(int k = 0; k < w/32; ++k) {
  313. val += *storage++ * mult;
  314. mult *= two32;
  315. }
  316. if(mask != 0) {
  317. val += (*storage++ & mask) * mult;
  318. }
  319. BOOST_ASSERT(val >= 0);
  320. BOOST_ASSERT(val < 1);
  321. x[j] = val;
  322. }
  323. }
  324. template<int w, std::size_t n, class SeedSeq, class RealType>
  325. void seed_array_real(SeedSeq& seq, RealType (&x)[n])
  326. {
  327. using std::pow;
  328. boost::uint_least32_t storage[((w+31)/32) * n];
  329. seq.generate(&storage[0], &storage[0] + ((w+31)/32) * n);
  330. seed_array_real_impl<w>(storage, x);
  331. }
  332. template<int w, std::size_t n, class Iter, class RealType>
  333. void fill_array_real(Iter& first, Iter last, RealType (&x)[n])
  334. {
  335. boost::uint_least32_t mask = ~((~boost::uint_least32_t(0)) << (w%32));
  336. RealType two32 = 4294967296.0;
  337. const RealType divisor = RealType(1)/detail::pow2<RealType>(w);
  338. unsigned int j;
  339. for(j = 0; j < n; ++j) {
  340. RealType val = RealType(0);
  341. RealType mult = divisor;
  342. for(int k = 0; k < w/32; ++k, ++first) {
  343. if(first == last) throw std::invalid_argument("Not enough elements in call to seed.");
  344. val += *first * mult;
  345. mult *= two32;
  346. }
  347. if(mask != 0) {
  348. if(first == last) throw std::invalid_argument("Not enough elements in call to seed.");
  349. val += (*first & mask) * mult;
  350. ++first;
  351. }
  352. BOOST_ASSERT(val >= 0);
  353. BOOST_ASSERT(val < 1);
  354. x[j] = val;
  355. }
  356. }
  357. }
  358. }
  359. }
  360. #include <boost/random/detail/enable_warnings.hpp>
  361. #endif