/* boost random/detail/seed.hpp header file * * Copyright Steven Watanabe 2009 * Distributed under the Boost Software License, Version 1.0. (See * accompanying file LICENSE_1_0.txt or copy at * http://www.boost.org/LICENSE_1_0.txt) * * See http://www.boost.org for most recent version including documentation. * * $Id$ */ #ifndef BOOST_RANDOM_DETAIL_SEED_IMPL_HPP #define BOOST_RANDOM_DETAIL_SEED_IMPL_HPP #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace boost { namespace random { namespace detail { // finds the seed type of an engine, given its // result_type. If the result_type is integral // the seed type is the same. If the result_type // is floating point, the seed type is uint32_t template struct seed_type { typedef typename boost::mpl::if_, T, boost::uint32_t >::type type; }; template struct const_pow_impl { template static T call(T arg, int n, T result) { return const_pow_impl::call(arg * arg, n / 2, n%2 == 0? result : result * arg); } }; template<> struct const_pow_impl<0> { template static T call(T, int, T result) { return result; } }; // requires N is an upper bound on n template inline T const_pow(T arg, int n) { return const_pow_impl::call(arg, n, T(1)); } template inline T pow2(int n) { typedef unsigned int_type; const int max_bits = std::numeric_limits::digits; T multiplier = T(int_type(1) << (max_bits - 1)) * 2; return (int_type(1) << (n % max_bits)) * const_pow::digits / max_bits>(multiplier, n / max_bits); } template void generate_from_real(Engine& eng, Iter begin, Iter end) { using std::fmod; typedef typename Engine::result_type RealType; const int Bits = detail::generator_bits::value(); int remaining_bits = 0; boost::uint_least32_t saved_bits = 0; RealType multiplier = pow2( Bits); RealType mult32 = RealType(4294967296.0); // 2^32 while(true) { RealType val = eng() * multiplier; int available_bits = Bits; // Make sure the compiler can optimize this out // if it isn't possible. if(Bits < 32 && available_bits < 32 - remaining_bits) { saved_bits |= boost::uint_least32_t(val) << remaining_bits; remaining_bits += Bits; } else { // If Bits < 32, then remaining_bits != 0, since // if remaining_bits == 0, available_bits < 32 - 0, // and we won't get here to begin with. if(Bits < 32 || remaining_bits != 0) { boost::uint_least32_t divisor = (boost::uint_least32_t(1) << (32 - remaining_bits)); boost::uint_least32_t extra_bits = boost::uint_least32_t(fmod(val, mult32)) & (divisor - 1); val = val / divisor; *begin++ = saved_bits | (extra_bits << remaining_bits); if(begin == end) return; available_bits -= 32 - remaining_bits; remaining_bits = 0; } // If Bits < 32 we should never enter this loop if(Bits >= 32) { for(; available_bits >= 32; available_bits -= 32) { boost::uint_least32_t word = boost::uint_least32_t(fmod(val, mult32)); val /= mult32; *begin++ = word; if(begin == end) return; } } remaining_bits = available_bits; saved_bits = static_cast(val); } } } template void generate_from_int(Engine& eng, Iter begin, Iter end) { typedef typename Engine::result_type IntType; typedef typename boost::make_unsigned::type unsigned_type; int remaining_bits = 0; boost::uint_least32_t saved_bits = 0; unsigned_type range = boost::random::detail::subtract()((eng.max)(), (eng.min)()); int bits = (range == (std::numeric_limits::max)()) ? std::numeric_limits::digits : detail::integer_log2(range + 1); { int discarded_bits = detail::integer_log2(bits); unsigned_type excess = (range + 1) >> (bits - discarded_bits); if(excess != 0) { int extra_bits = detail::integer_log2((excess - 1) ^ excess); bits = bits - discarded_bits + extra_bits; } } unsigned_type mask = (static_cast(2) << (bits - 1)) - 1; unsigned_type limit = ((range + 1) & ~mask) - 1; while(true) { unsigned_type val; do { val = boost::random::detail::subtract()(eng(), (eng.min)()); } while(limit != range && val > limit); val &= mask; int available_bits = bits; if(available_bits == 32) { *begin++ = static_cast(val) & 0xFFFFFFFFu; if(begin == end) return; } else if(available_bits % 32 == 0) { for(int i = 0; i < available_bits / 32; ++i) { boost::uint_least32_t word = boost::uint_least32_t(val) & 0xFFFFFFFFu; int suppress_warning = (bits >= 32); BOOST_ASSERT(suppress_warning == 1); val >>= (32 * suppress_warning); *begin++ = word; if(begin == end) return; } } else if(bits < 32 && available_bits < 32 - remaining_bits) { saved_bits |= boost::uint_least32_t(val) << remaining_bits; remaining_bits += bits; } else { if(bits < 32 || remaining_bits != 0) { boost::uint_least32_t extra_bits = boost::uint_least32_t(val) & ((boost::uint_least32_t(1) << (32 - remaining_bits)) - 1); val >>= 32 - remaining_bits; *begin++ = saved_bits | (extra_bits << remaining_bits); if(begin == end) return; available_bits -= 32 - remaining_bits; remaining_bits = 0; } if(bits >= 32) { for(; available_bits >= 32; available_bits -= 32) { boost::uint_least32_t word = boost::uint_least32_t(val) & 0xFFFFFFFFu; int suppress_warning = (bits >= 32); BOOST_ASSERT(suppress_warning == 1); val >>= (32 * suppress_warning); *begin++ = word; if(begin == end) return; } } remaining_bits = available_bits; saved_bits = static_cast(val); } } } template void generate_impl(Engine& eng, Iter first, Iter last, boost::mpl::true_) { return detail::generate_from_int(eng, first, last); } template void generate_impl(Engine& eng, Iter first, Iter last, boost::mpl::false_) { return detail::generate_from_real(eng, first, last); } template void generate(Engine& eng, Iter first, Iter last) { return detail::generate_impl(eng, first, last, boost::is_integral()); } template IntType seed_one_int(SeedSeq& seq) { static const int log = ::boost::mpl::if_c<(m == 0), ::boost::mpl::int_<(::std::numeric_limits::digits)>, ::boost::static_log2 >::type::value; static const int k = (log + ((~(static_cast(2) << (log - 1)) & m)? 32 : 31)) / 32; ::boost::uint_least32_t array[log / 32 + 4]; seq.generate(&array[0], &array[0] + k + 3); IntType s = 0; for(int j = 0; j < k; ++j) { IntType digit = const_mod::apply(IntType(array[j+3])); IntType mult = IntType(1) << 32*j; s = const_mod::mult_add(mult, digit, s); } return s; } template IntType get_one_int(Iter& first, Iter last) { static const int log = ::boost::mpl::if_c<(m == 0), ::boost::mpl::int_<(::std::numeric_limits::digits)>, ::boost::static_log2 >::type::value; static const int k = (log + ((~(static_cast(2) << (log - 1)) & m)? 32 : 31)) / 32; IntType s = 0; for(int j = 0; j < k; ++j) { if(first == last) { boost::throw_exception(::std::invalid_argument("Not enough elements in call to seed.")); } IntType digit = const_mod::apply(IntType(*first++)); IntType mult = IntType(1) << 32*j; s = const_mod::mult_add(mult, digit, s); } return s; } // TODO: work in-place whenever possible template void seed_array_int_impl(SeedSeq& seq, UIntType (&x)[n]) { boost::uint_least32_t storage[((w+31)/32) * n]; seq.generate(&storage[0], &storage[0] + ((w+31)/32) * n); for(std::size_t j = 0; j < n; j++) { UIntType val = 0; for(std::size_t k = 0; k < (w+31)/32; ++k) { val += static_cast(storage[(w+31)/32*j + k]) << 32*k; } x[j] = val & ::boost::low_bits_mask_t::sig_bits; } } template inline void seed_array_int_impl(SeedSeq& seq, IntType (&x)[n], boost::mpl::true_) { typedef typename boost::make_unsigned::type unsigned_array[n]; seed_array_int_impl(seq, reinterpret_cast(x)); } template inline void seed_array_int_impl(SeedSeq& seq, IntType (&x)[n], boost::mpl::false_) { seed_array_int_impl(seq, x); } template inline void seed_array_int(SeedSeq& seq, IntType (&x)[n]) { seed_array_int_impl(seq, x, boost::is_signed()); } template void fill_array_int_impl(Iter& first, Iter last, UIntType (&x)[n]) { for(std::size_t j = 0; j < n; j++) { UIntType val = 0; for(std::size_t k = 0; k < (w+31)/32; ++k) { if(first == last) { boost::throw_exception(std::invalid_argument("Not enough elements in call to seed.")); } val += static_cast(*first++) << 32*k; } x[j] = val & ::boost::low_bits_mask_t::sig_bits; } } template inline void fill_array_int_impl(Iter& first, Iter last, IntType (&x)[n], boost::mpl::true_) { typedef typename boost::make_unsigned::type unsigned_array[n]; fill_array_int_impl(first, last, reinterpret_cast(x)); } template inline void fill_array_int_impl(Iter& first, Iter last, IntType (&x)[n], boost::mpl::false_) { fill_array_int_impl(first, last, x); } template inline void fill_array_int(Iter& first, Iter last, IntType (&x)[n]) { fill_array_int_impl(first, last, x, boost::is_signed()); } template void seed_array_real_impl(const boost::uint_least32_t* storage, RealType (&x)[n]) { boost::uint_least32_t mask = ~((~boost::uint_least32_t(0)) << (w%32)); RealType two32 = 4294967296.0; const RealType divisor = RealType(1)/detail::pow2(w); unsigned int j; for(j = 0; j < n; ++j) { RealType val = RealType(0); RealType mult = divisor; for(int k = 0; k < w/32; ++k) { val += *storage++ * mult; mult *= two32; } if(mask != 0) { val += (*storage++ & mask) * mult; } BOOST_ASSERT(val >= 0); BOOST_ASSERT(val < 1); x[j] = val; } } template void seed_array_real(SeedSeq& seq, RealType (&x)[n]) { using std::pow; boost::uint_least32_t storage[((w+31)/32) * n]; seq.generate(&storage[0], &storage[0] + ((w+31)/32) * n); seed_array_real_impl(storage, x); } template void fill_array_real(Iter& first, Iter last, RealType (&x)[n]) { boost::uint_least32_t mask = ~((~boost::uint_least32_t(0)) << (w%32)); RealType two32 = 4294967296.0; const RealType divisor = RealType(1)/detail::pow2(w); unsigned int j; for(j = 0; j < n; ++j) { RealType val = RealType(0); RealType mult = divisor; for(int k = 0; k < w/32; ++k, ++first) { if(first == last) boost::throw_exception(std::invalid_argument("Not enough elements in call to seed.")); val += *first * mult; mult *= two32; } if(mask != 0) { if(first == last) boost::throw_exception(std::invalid_argument("Not enough elements in call to seed.")); val += (*first & mask) * mult; ++first; } BOOST_ASSERT(val >= 0); BOOST_ASSERT(val < 1); x[j] = val; } } } } } #include #endif