annotate DEPENDENCIES/generic/include/boost/random/detail/seed_impl.hpp @ 125:34e428693f5d vext

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