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$
  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/random/detail/const_mod.hpp>
  22. #include <boost/random/detail/integer_log2.hpp>
  23. #include <boost/random/detail/signed_unsigned_tools.hpp>
  24. #include <boost/random/detail/generator_bits.hpp>
  25. #include <boost/type_traits/conditional.hpp>
  26. #include <boost/type_traits/integral_constant.hpp>
  27. #include <boost/random/detail/disable_warnings.hpp>
  28. namespace boost {
  29. namespace random {
  30. namespace detail {
  31. // finds the seed type of an engine, given its
  32. // result_type. If the result_type is integral
  33. // the seed type is the same. If the result_type
  34. // is floating point, the seed type is uint32_t
  35. template<class T>
  36. struct seed_type
  37. {
  38. typedef typename boost::conditional<boost::is_integral<T>::value,
  39. T,
  40. boost::uint32_t
  41. >::type type;
  42. };
  43. template<int N>
  44. struct const_pow_impl
  45. {
  46. template<class T>
  47. static T call(T arg, int n, T result)
  48. {
  49. return const_pow_impl<N / 2>::call(T(arg * arg), n / 2,
  50. n%2 == 0? result : T(result * arg));
  51. }
  52. };
  53. template<>
  54. struct const_pow_impl<0>
  55. {
  56. template<class T>
  57. static T call(T, int, T result)
  58. {
  59. return result;
  60. }
  61. };
  62. // requires N is an upper bound on n
  63. template<int N, class T>
  64. inline T const_pow(T arg, int n) { return const_pow_impl<N>::call(arg, n, T(1)); }
  65. template<class T>
  66. inline T pow2(int n)
  67. {
  68. typedef unsigned int_type;
  69. const int max_bits = std::numeric_limits<int_type>::digits;
  70. T multiplier = T(int_type(1) << (max_bits - 1)) * 2;
  71. return (int_type(1) << (n % max_bits)) *
  72. const_pow<std::numeric_limits<T>::digits / max_bits>(multiplier, n / max_bits);
  73. }
  74. template<class Engine, class Iter>
  75. void generate_from_real(Engine& eng, Iter begin, Iter end)
  76. {
  77. using std::fmod;
  78. typedef typename Engine::result_type RealType;
  79. const int Bits = detail::generator_bits<Engine>::value();
  80. int remaining_bits = 0;
  81. boost::uint_least32_t saved_bits = 0;
  82. RealType multiplier = pow2<RealType>( Bits);
  83. RealType mult32 = RealType(4294967296.0); // 2^32
  84. while(true) {
  85. RealType val = eng() * multiplier;
  86. int available_bits = Bits;
  87. // Make sure the compiler can optimize this out
  88. // if it isn't possible.
  89. if(Bits < 32 && available_bits < 32 - remaining_bits) {
  90. saved_bits |= boost::uint_least32_t(val) << remaining_bits;
  91. remaining_bits += Bits;
  92. } else {
  93. // If Bits < 32, then remaining_bits != 0, since
  94. // if remaining_bits == 0, available_bits < 32 - 0,
  95. // and we won't get here to begin with.
  96. if(Bits < 32 || remaining_bits != 0) {
  97. boost::uint_least32_t divisor =
  98. (boost::uint_least32_t(1) << (32 - remaining_bits));
  99. boost::uint_least32_t extra_bits = boost::uint_least32_t(fmod(val, mult32)) & (divisor - 1);
  100. val = val / divisor;
  101. *begin++ = saved_bits | (extra_bits << remaining_bits);
  102. if(begin == end) return;
  103. available_bits -= 32 - remaining_bits;
  104. remaining_bits = 0;
  105. }
  106. // If Bits < 32 we should never enter this loop
  107. if(Bits >= 32) {
  108. for(; available_bits >= 32; available_bits -= 32) {
  109. boost::uint_least32_t word = boost::uint_least32_t(fmod(val, mult32));
  110. val /= mult32;
  111. *begin++ = word;
  112. if(begin == end) return;
  113. }
  114. }
  115. remaining_bits = available_bits;
  116. saved_bits = static_cast<boost::uint_least32_t>(val);
  117. }
  118. }
  119. }
  120. template<class Engine, class Iter>
  121. void generate_from_int(Engine& eng, Iter begin, Iter end)
  122. {
  123. typedef typename Engine::result_type IntType;
  124. typedef typename boost::random::traits::make_unsigned<IntType>::type unsigned_type;
  125. int remaining_bits = 0;
  126. boost::uint_least32_t saved_bits = 0;
  127. unsigned_type range = boost::random::detail::subtract<IntType>()((eng.max)(), (eng.min)());
  128. int bits =
  129. (range == (std::numeric_limits<unsigned_type>::max)()) ?
  130. std::numeric_limits<unsigned_type>::digits :
  131. detail::integer_log2(range + 1);
  132. {
  133. int discarded_bits = detail::integer_log2(bits);
  134. unsigned_type excess = (range + 1) >> (bits - discarded_bits);
  135. if(excess != 0) {
  136. int extra_bits = detail::integer_log2((excess - 1) ^ excess);
  137. bits = bits - discarded_bits + extra_bits;
  138. }
  139. }
  140. unsigned_type mask = (static_cast<unsigned_type>(2) << (bits - 1)) - 1;
  141. unsigned_type limit = ((range + 1) & ~mask) - 1;
  142. while(true) {
  143. unsigned_type val;
  144. do {
  145. val = boost::random::detail::subtract<IntType>()(eng(), (eng.min)());
  146. } while(limit != range && val > limit);
  147. val &= mask;
  148. int available_bits = bits;
  149. if(available_bits == 32) {
  150. *begin++ = static_cast<boost::uint_least32_t>(val) & 0xFFFFFFFFu;
  151. if(begin == end) return;
  152. } else if(available_bits % 32 == 0) {
  153. for(int i = 0; i < available_bits / 32; ++i) {
  154. boost::uint_least32_t word = boost::uint_least32_t(val) & 0xFFFFFFFFu;
  155. int suppress_warning = (bits >= 32);
  156. BOOST_ASSERT(suppress_warning == 1);
  157. val >>= (32 * suppress_warning);
  158. *begin++ = word;
  159. if(begin == end) return;
  160. }
  161. } else if(bits < 32 && available_bits < 32 - remaining_bits) {
  162. saved_bits |= boost::uint_least32_t(val) << remaining_bits;
  163. remaining_bits += bits;
  164. } else {
  165. if(bits < 32 || remaining_bits != 0) {
  166. boost::uint_least32_t extra_bits = boost::uint_least32_t(val) & ((boost::uint_least32_t(1) << (32 - remaining_bits)) - 1);
  167. val >>= 32 - remaining_bits;
  168. *begin++ = saved_bits | (extra_bits << remaining_bits);
  169. if(begin == end) return;
  170. available_bits -= 32 - remaining_bits;
  171. remaining_bits = 0;
  172. }
  173. if(bits >= 32) {
  174. for(; available_bits >= 32; available_bits -= 32) {
  175. boost::uint_least32_t word = boost::uint_least32_t(val) & 0xFFFFFFFFu;
  176. int suppress_warning = (bits >= 32);
  177. BOOST_ASSERT(suppress_warning == 1);
  178. val >>= (32 * suppress_warning);
  179. *begin++ = word;
  180. if(begin == end) return;
  181. }
  182. }
  183. remaining_bits = available_bits;
  184. saved_bits = static_cast<boost::uint_least32_t>(val);
  185. }
  186. }
  187. }
  188. template<class Engine, class Iter>
  189. void generate_impl(Engine& eng, Iter first, Iter last, boost::true_type)
  190. {
  191. return detail::generate_from_int(eng, first, last);
  192. }
  193. template<class Engine, class Iter>
  194. void generate_impl(Engine& eng, Iter first, Iter last, boost::false_type)
  195. {
  196. return detail::generate_from_real(eng, first, last);
  197. }
  198. template<class Engine, class Iter>
  199. void generate(Engine& eng, Iter first, Iter last)
  200. {
  201. return detail::generate_impl(eng, first, last, boost::random::traits::is_integral<typename Engine::result_type>());
  202. }
  203. template<class IntType, IntType m, class SeedSeq>
  204. IntType seed_one_int(SeedSeq& seq)
  205. {
  206. static const int log = ::boost::conditional<(m == 0),
  207. ::boost::integral_constant<int, (::std::numeric_limits<IntType>::digits)>,
  208. ::boost::static_log2<m> >::type::value;
  209. static const int k =
  210. (log + ((~(static_cast<IntType>(2) << (log - 1)) & m)? 32 : 31)) / 32;
  211. ::boost::uint_least32_t array[log / 32 + 4];
  212. seq.generate(&array[0], &array[0] + k + 3);
  213. IntType s = 0;
  214. for(int j = 0; j < k; ++j) {
  215. IntType digit = const_mod<IntType, m>::apply(IntType(array[j+3]));
  216. IntType mult = IntType(1) << 32*j;
  217. s = const_mod<IntType, m>::mult_add(mult, digit, s);
  218. }
  219. return s;
  220. }
  221. template<class IntType, IntType m, class Iter>
  222. IntType get_one_int(Iter& first, Iter last)
  223. {
  224. static const int log = ::boost::conditional<(m == 0),
  225. ::boost::integral_constant<int, (::std::numeric_limits<IntType>::digits)>,
  226. ::boost::static_log2<m> >::type::value;
  227. static const int k =
  228. (log + ((~(static_cast<IntType>(2) << (log - 1)) & m)? 32 : 31)) / 32;
  229. IntType s = 0;
  230. for(int j = 0; j < k; ++j) {
  231. if(first == last) {
  232. boost::throw_exception(::std::invalid_argument("Not enough elements in call to seed."));
  233. }
  234. IntType digit = const_mod<IntType, m>::apply(IntType(*first++));
  235. IntType mult = IntType(1) << 32*j;
  236. s = const_mod<IntType, m>::mult_add(mult, digit, s);
  237. }
  238. return s;
  239. }
  240. // TODO: work in-place whenever possible
  241. template<int w, std::size_t n, class SeedSeq, class UIntType>
  242. void seed_array_int_impl(SeedSeq& seq, UIntType (&x)[n])
  243. {
  244. boost::uint_least32_t storage[((w+31)/32) * n];
  245. seq.generate(&storage[0], &storage[0] + ((w+31)/32) * n);
  246. for(std::size_t j = 0; j < n; j++) {
  247. UIntType val = 0;
  248. for(std::size_t k = 0; k < (w+31)/32; ++k) {
  249. val += static_cast<UIntType>(storage[(w+31)/32*j + k]) << 32*k;
  250. }
  251. x[j] = val & ::boost::low_bits_mask_t<w>::sig_bits;
  252. }
  253. }
  254. template<int w, std::size_t n, class SeedSeq, class IntType>
  255. inline void seed_array_int_impl(SeedSeq& seq, IntType (&x)[n], boost::true_type)
  256. {
  257. 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.");
  258. typedef typename boost::make_unsigned<IntType>::type unsigned_array[n];
  259. seed_array_int_impl<w>(seq, reinterpret_cast<unsigned_array&>(x));
  260. }
  261. template<int w, std::size_t n, class SeedSeq, class IntType>
  262. inline void seed_array_int_impl(SeedSeq& seq, IntType (&x)[n], boost::false_type)
  263. {
  264. seed_array_int_impl<w>(seq, x);
  265. }
  266. template<int w, std::size_t n, class SeedSeq, class IntType>
  267. inline void seed_array_int(SeedSeq& seq, IntType (&x)[n])
  268. {
  269. seed_array_int_impl<w>(seq, x, boost::random::traits::is_signed<IntType>());
  270. }
  271. template<int w, std::size_t n, class Iter, class UIntType>
  272. void fill_array_int_impl(Iter& first, Iter last, UIntType (&x)[n])
  273. {
  274. for(std::size_t j = 0; j < n; j++) {
  275. UIntType val = 0;
  276. for(std::size_t k = 0; k < (w+31)/32; ++k) {
  277. if(first == last) {
  278. boost::throw_exception(std::invalid_argument("Not enough elements in call to seed."));
  279. }
  280. val += static_cast<UIntType>(*first++) << 32*k;
  281. }
  282. x[j] = val & ::boost::low_bits_mask_t<w>::sig_bits;
  283. }
  284. }
  285. template<int w, std::size_t n, class Iter, class IntType>
  286. inline void fill_array_int_impl(Iter& first, Iter last, IntType (&x)[n], boost::true_type)
  287. {
  288. 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.");
  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::false_type)
  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::random::traits::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) boost::throw_exception(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) boost::throw_exception(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