1 /* boost random/detail/seed.hpp header file
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)
8 * See http://www.boost.org for most recent version including documentation.
13 #ifndef BOOST_RANDOM_DETAIL_SEED_IMPL_HPP
14 #define BOOST_RANDOM_DETAIL_SEED_IMPL_HPP
17 #include <boost/cstdint.hpp>
18 #include <boost/throw_exception.hpp>
19 #include <boost/config/no_tr1/cmath.hpp>
20 #include <boost/integer/integer_mask.hpp>
21 #include <boost/integer/static_log2.hpp>
22 #include <boost/random/traits.hpp>
23 #include <boost/mpl/bool.hpp>
24 #include <boost/mpl/if.hpp>
25 #include <boost/mpl/int.hpp>
26 #include <boost/random/detail/const_mod.hpp>
27 #include <boost/random/detail/integer_log2.hpp>
28 #include <boost/random/detail/signed_unsigned_tools.hpp>
29 #include <boost/random/detail/generator_bits.hpp>
31 #include <boost/random/detail/disable_warnings.hpp>
37 // finds the seed type of an engine, given its
38 // result_type. If the result_type is integral
39 // the seed type is the same. If the result_type
40 // is floating point, the seed type is uint32_t
44 typedef typename boost::mpl::if_<boost::is_integral<T>,
54 static T call(T arg, int n, T result)
56 return const_pow_impl<N / 2>::call(T(arg * arg), n / 2,
57 n%2 == 0? result : T(result * arg));
62 struct const_pow_impl<0>
65 static T call(T, int, T result)
71 // requires N is an upper bound on n
72 template<int N, class T>
73 inline T const_pow(T arg, int n) { return const_pow_impl<N>::call(arg, n, T(1)); }
78 typedef unsigned int_type;
79 const int max_bits = std::numeric_limits<int_type>::digits;
80 T multiplier = T(int_type(1) << (max_bits - 1)) * 2;
81 return (int_type(1) << (n % max_bits)) *
82 const_pow<std::numeric_limits<T>::digits / max_bits>(multiplier, n / max_bits);
85 template<class Engine, class Iter>
86 void generate_from_real(Engine& eng, Iter begin, Iter end)
89 typedef typename Engine::result_type RealType;
90 const int Bits = detail::generator_bits<Engine>::value();
91 int remaining_bits = 0;
92 boost::uint_least32_t saved_bits = 0;
93 RealType multiplier = pow2<RealType>( Bits);
94 RealType mult32 = RealType(4294967296.0); // 2^32
96 RealType val = eng() * multiplier;
97 int available_bits = Bits;
98 // Make sure the compiler can optimize this out
99 // if it isn't possible.
100 if(Bits < 32 && available_bits < 32 - remaining_bits) {
101 saved_bits |= boost::uint_least32_t(val) << remaining_bits;
102 remaining_bits += Bits;
104 // If Bits < 32, then remaining_bits != 0, since
105 // if remaining_bits == 0, available_bits < 32 - 0,
106 // and we won't get here to begin with.
107 if(Bits < 32 || remaining_bits != 0) {
108 boost::uint_least32_t divisor =
109 (boost::uint_least32_t(1) << (32 - remaining_bits));
110 boost::uint_least32_t extra_bits = boost::uint_least32_t(fmod(val, mult32)) & (divisor - 1);
112 *begin++ = saved_bits | (extra_bits << remaining_bits);
113 if(begin == end) return;
114 available_bits -= 32 - remaining_bits;
117 // If Bits < 32 we should never enter this loop
119 for(; available_bits >= 32; available_bits -= 32) {
120 boost::uint_least32_t word = boost::uint_least32_t(fmod(val, mult32));
123 if(begin == end) return;
126 remaining_bits = available_bits;
127 saved_bits = static_cast<boost::uint_least32_t>(val);
132 template<class Engine, class Iter>
133 void generate_from_int(Engine& eng, Iter begin, Iter end)
135 typedef typename Engine::result_type IntType;
136 typedef typename boost::random::traits::make_unsigned<IntType>::type unsigned_type;
137 int remaining_bits = 0;
138 boost::uint_least32_t saved_bits = 0;
139 unsigned_type range = boost::random::detail::subtract<IntType>()((eng.max)(), (eng.min)());
142 (range == (std::numeric_limits<unsigned_type>::max)()) ?
143 std::numeric_limits<unsigned_type>::digits :
144 detail::integer_log2(range + 1);
147 int discarded_bits = detail::integer_log2(bits);
148 unsigned_type excess = (range + 1) >> (bits - discarded_bits);
150 int extra_bits = detail::integer_log2((excess - 1) ^ excess);
151 bits = bits - discarded_bits + extra_bits;
155 unsigned_type mask = (static_cast<unsigned_type>(2) << (bits - 1)) - 1;
156 unsigned_type limit = ((range + 1) & ~mask) - 1;
161 val = boost::random::detail::subtract<IntType>()(eng(), (eng.min)());
162 } while(limit != range && val > limit);
164 int available_bits = bits;
165 if(available_bits == 32) {
166 *begin++ = static_cast<boost::uint_least32_t>(val) & 0xFFFFFFFFu;
167 if(begin == end) return;
168 } else if(available_bits % 32 == 0) {
169 for(int i = 0; i < available_bits / 32; ++i) {
170 boost::uint_least32_t word = boost::uint_least32_t(val) & 0xFFFFFFFFu;
171 int suppress_warning = (bits >= 32);
172 BOOST_ASSERT(suppress_warning == 1);
173 val >>= (32 * suppress_warning);
175 if(begin == end) return;
177 } else if(bits < 32 && available_bits < 32 - remaining_bits) {
178 saved_bits |= boost::uint_least32_t(val) << remaining_bits;
179 remaining_bits += bits;
181 if(bits < 32 || remaining_bits != 0) {
182 boost::uint_least32_t extra_bits = boost::uint_least32_t(val) & ((boost::uint_least32_t(1) << (32 - remaining_bits)) - 1);
183 val >>= 32 - remaining_bits;
184 *begin++ = saved_bits | (extra_bits << remaining_bits);
185 if(begin == end) return;
186 available_bits -= 32 - remaining_bits;
190 for(; available_bits >= 32; available_bits -= 32) {
191 boost::uint_least32_t word = boost::uint_least32_t(val) & 0xFFFFFFFFu;
192 int suppress_warning = (bits >= 32);
193 BOOST_ASSERT(suppress_warning == 1);
194 val >>= (32 * suppress_warning);
196 if(begin == end) return;
199 remaining_bits = available_bits;
200 saved_bits = static_cast<boost::uint_least32_t>(val);
205 template<class Engine, class Iter>
206 void generate_impl(Engine& eng, Iter first, Iter last, boost::mpl::true_)
208 return detail::generate_from_int(eng, first, last);
211 template<class Engine, class Iter>
212 void generate_impl(Engine& eng, Iter first, Iter last, boost::mpl::false_)
214 return detail::generate_from_real(eng, first, last);
217 template<class Engine, class Iter>
218 void generate(Engine& eng, Iter first, Iter last)
220 return detail::generate_impl(eng, first, last, boost::random::traits::is_integral<typename Engine::result_type>());
225 template<class IntType, IntType m, class SeedSeq>
226 IntType seed_one_int(SeedSeq& seq)
228 static const int log = ::boost::mpl::if_c<(m == 0),
229 ::boost::mpl::int_<(::std::numeric_limits<IntType>::digits)>,
230 ::boost::static_log2<m> >::type::value;
232 (log + ((~(static_cast<IntType>(2) << (log - 1)) & m)? 32 : 31)) / 32;
233 ::boost::uint_least32_t array[log / 32 + 4];
234 seq.generate(&array[0], &array[0] + k + 3);
236 for(int j = 0; j < k; ++j) {
237 IntType digit = const_mod<IntType, m>::apply(IntType(array[j+3]));
238 IntType mult = IntType(1) << 32*j;
239 s = const_mod<IntType, m>::mult_add(mult, digit, s);
244 template<class IntType, IntType m, class Iter>
245 IntType get_one_int(Iter& first, Iter last)
247 static const int log = ::boost::mpl::if_c<(m == 0),
248 ::boost::mpl::int_<(::std::numeric_limits<IntType>::digits)>,
249 ::boost::static_log2<m> >::type::value;
251 (log + ((~(static_cast<IntType>(2) << (log - 1)) & m)? 32 : 31)) / 32;
253 for(int j = 0; j < k; ++j) {
255 boost::throw_exception(::std::invalid_argument("Not enough elements in call to seed."));
257 IntType digit = const_mod<IntType, m>::apply(IntType(*first++));
258 IntType mult = IntType(1) << 32*j;
259 s = const_mod<IntType, m>::mult_add(mult, digit, s);
264 // TODO: work in-place whenever possible
265 template<int w, std::size_t n, class SeedSeq, class UIntType>
266 void seed_array_int_impl(SeedSeq& seq, UIntType (&x)[n])
268 boost::uint_least32_t storage[((w+31)/32) * n];
269 seq.generate(&storage[0], &storage[0] + ((w+31)/32) * n);
270 for(std::size_t j = 0; j < n; j++) {
272 for(std::size_t k = 0; k < (w+31)/32; ++k) {
273 val += static_cast<UIntType>(storage[(w+31)/32*j + k]) << 32*k;
275 x[j] = val & ::boost::low_bits_mask_t<w>::sig_bits;
279 template<int w, std::size_t n, class SeedSeq, class IntType>
280 inline void seed_array_int_impl(SeedSeq& seq, IntType (&x)[n], boost::mpl::true_)
282 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.");
283 typedef typename boost::make_unsigned<IntType>::type unsigned_array[n];
284 seed_array_int_impl<w>(seq, reinterpret_cast<unsigned_array&>(x));
287 template<int w, std::size_t n, class SeedSeq, class IntType>
288 inline void seed_array_int_impl(SeedSeq& seq, IntType (&x)[n], boost::mpl::false_)
290 seed_array_int_impl<w>(seq, x);
293 template<int w, std::size_t n, class SeedSeq, class IntType>
294 inline void seed_array_int(SeedSeq& seq, IntType (&x)[n])
296 seed_array_int_impl<w>(seq, x, boost::random::traits::is_signed<IntType>());
299 template<int w, std::size_t n, class Iter, class UIntType>
300 void fill_array_int_impl(Iter& first, Iter last, UIntType (&x)[n])
302 for(std::size_t j = 0; j < n; j++) {
304 for(std::size_t k = 0; k < (w+31)/32; ++k) {
306 boost::throw_exception(std::invalid_argument("Not enough elements in call to seed."));
308 val += static_cast<UIntType>(*first++) << 32*k;
310 x[j] = val & ::boost::low_bits_mask_t<w>::sig_bits;
314 template<int w, std::size_t n, class Iter, class IntType>
315 inline void fill_array_int_impl(Iter& first, Iter last, IntType (&x)[n], boost::mpl::true_)
317 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.");
318 typedef typename boost::make_unsigned<IntType>::type unsigned_array[n];
319 fill_array_int_impl<w>(first, last, reinterpret_cast<unsigned_array&>(x));
322 template<int w, std::size_t n, class Iter, class IntType>
323 inline void fill_array_int_impl(Iter& first, Iter last, IntType (&x)[n], boost::mpl::false_)
325 fill_array_int_impl<w>(first, last, x);
328 template<int w, std::size_t n, class Iter, class IntType>
329 inline void fill_array_int(Iter& first, Iter last, IntType (&x)[n])
331 fill_array_int_impl<w>(first, last, x, boost::random::traits::is_signed<IntType>());
334 template<int w, std::size_t n, class RealType>
335 void seed_array_real_impl(const boost::uint_least32_t* storage, RealType (&x)[n])
337 boost::uint_least32_t mask = ~((~boost::uint_least32_t(0)) << (w%32));
338 RealType two32 = 4294967296.0;
339 const RealType divisor = RealType(1)/detail::pow2<RealType>(w);
341 for(j = 0; j < n; ++j) {
342 RealType val = RealType(0);
343 RealType mult = divisor;
344 for(int k = 0; k < w/32; ++k) {
345 val += *storage++ * mult;
349 val += (*storage++ & mask) * mult;
351 BOOST_ASSERT(val >= 0);
352 BOOST_ASSERT(val < 1);
357 template<int w, std::size_t n, class SeedSeq, class RealType>
358 void seed_array_real(SeedSeq& seq, RealType (&x)[n])
361 boost::uint_least32_t storage[((w+31)/32) * n];
362 seq.generate(&storage[0], &storage[0] + ((w+31)/32) * n);
363 seed_array_real_impl<w>(storage, x);
366 template<int w, std::size_t n, class Iter, class RealType>
367 void fill_array_real(Iter& first, Iter last, RealType (&x)[n])
369 boost::uint_least32_t mask = ~((~boost::uint_least32_t(0)) << (w%32));
370 RealType two32 = 4294967296.0;
371 const RealType divisor = RealType(1)/detail::pow2<RealType>(w);
373 for(j = 0; j < n; ++j) {
374 RealType val = RealType(0);
375 RealType mult = divisor;
376 for(int k = 0; k < w/32; ++k, ++first) {
377 if(first == last) boost::throw_exception(std::invalid_argument("Not enough elements in call to seed."));
378 val += *first * mult;
382 if(first == last) boost::throw_exception(std::invalid_argument("Not enough elements in call to seed."));
383 val += (*first & mask) * mult;
386 BOOST_ASSERT(val >= 0);
387 BOOST_ASSERT(val < 1);
396 #include <boost/random/detail/enable_warnings.hpp>