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