seed_impl.hpp 14 KB

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