| 1 | // |
| 2 | // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com) |
| 3 | // |
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | // |
| 7 | // Official repository: https://github.com/boostorg/beast |
| 8 | // |
| 9 | |
| 10 | #ifndef BOOST_BEAST_CORE_DETAIL_PCG_HPP |
| 11 | #define BOOST_BEAST_CORE_DETAIL_PCG_HPP |
| 12 | |
| 13 | #include <boost/core/ignore_unused.hpp> |
| 14 | #include <cstdint> |
| 15 | #include <random> |
| 16 | |
| 17 | namespace boost { |
| 18 | namespace beast { |
| 19 | namespace detail { |
| 20 | |
| 21 | class pcg |
| 22 | { |
| 23 | std::uint64_t state_ = 0; |
| 24 | std::uint64_t increment_; |
| 25 | |
| 26 | public: |
| 27 | using result_type = std::uint32_t; |
| 28 | |
| 29 | // Initialize the generator. |
| 30 | // There are no restrictions on the input values. |
| 31 | pcg( |
| 32 | std::uint64_t seed, |
| 33 | std::uint64_t stream) |
| 34 | { |
| 35 | // increment must be odd |
| 36 | increment_ = 2 * stream + 1; |
| 37 | boost::ignore_unused((*this)()); |
| 38 | state_ += seed; |
| 39 | boost::ignore_unused((*this)()); |
| 40 | } |
| 41 | |
| 42 | std::uint32_t |
| 43 | operator()() |
| 44 | { |
| 45 | std::uint64_t const p = state_; |
| 46 | state_ = p * |
| 47 | 6364136223846793005ULL + |
| 48 | increment_; |
| 49 | std::uint32_t const x = |
| 50 | static_cast<std::uint32_t>( |
| 51 | ((p >> 18) ^ p) >> 27); |
| 52 | std::uint32_t const r = p >> 59; |
| 53 | #ifdef BOOST_MSVC |
| 54 | return _rotr(x, r); |
| 55 | #else |
| 56 | return (x >> r) | (x << ((1 + ~r) & 31)); |
| 57 | #endif |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | } // detail |
| 62 | } // beast |
| 63 | } // boost |
| 64 | |
| 65 | #endif |
| 66 | |