| 1 | #pragma once |
|---|
| 2 | #ifndef DAWG_DETAILS_MUTT_GEN_H |
|---|
| 3 | #define DAWG_DETAILS_MUTT_GEN_H |
|---|
| 4 | /**************************************************************************** |
|---|
| 5 | * Copyright (C) 2009-2012 Reed A. Cartwright, PhD <reed@scit.us> * |
|---|
| 6 | ****************************************************************************/ |
|---|
| 7 | |
|---|
| 8 | #ifndef __STDC_CONSTANT_MACROS |
|---|
| 9 | # define __STDC_CONSTANT_MACROS 1 |
|---|
| 10 | #endif |
|---|
| 11 | #ifndef __STDC_LIMIT_MACROS |
|---|
| 12 | # define __STDC_LIMIT_MACROS 1 |
|---|
| 13 | #endif |
|---|
| 14 | #include <boost/cstdint.hpp> |
|---|
| 15 | #include <utility> |
|---|
| 16 | |
|---|
| 17 | namespace dawg { namespace details { |
|---|
| 18 | |
|---|
| 19 | // Based on George Marsaglia's Xorshift Generator. |
|---|
| 20 | // Includes ideas from Richard Brent and Francois Panneton |
|---|
| 21 | // http://www.jstatsoft.org/v08/i14/paper |
|---|
| 22 | // http://wwwmaths.anu.edu.au/~brent/random.html |
|---|
| 23 | // http://www.iro.umontreal.ca/~panneton/These.pdf |
|---|
| 24 | // |
|---|
| 25 | // Passes the BigCrush tests |
|---|
| 26 | |
|---|
| 27 | struct xorshift_64_mutt_gen { |
|---|
| 28 | typedef boost::uint64_t native_t; |
|---|
| 29 | typedef std::pair<native_t,native_t> state_t; |
|---|
| 30 | xorshift_64_mutt_gen() { |
|---|
| 31 | seed(0); |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | inline native_t rand_native() { return rand_uint64(); } |
|---|
| 35 | |
|---|
| 36 | inline boost::uint32_t rand_uint32() { |
|---|
| 37 | return static_cast<boost::uint32_t>(rand_uint64() >> 32); |
|---|
| 38 | } |
|---|
| 39 | inline native_t rand_uint64() { |
|---|
| 40 | y ^= (y << 5); y ^= (y >> 15); y ^= (y << 27); |
|---|
| 41 | #ifndef DAWG_DISABLE_WEYL_GENERATOR |
|---|
| 42 | w += UINT64_C(0x61C8864680B583EB); |
|---|
| 43 | return y+(w^(w>>27)); |
|---|
| 44 | #else |
|---|
| 45 | return y; |
|---|
| 46 | #endif |
|---|
| 47 | } |
|---|
| 48 | // doubles with 52-bits worth of precision |
|---|
| 49 | inline double rand_real() { return to_double52(rand_uint64()); } |
|---|
| 50 | |
|---|
| 51 | inline state_t state() const { |
|---|
| 52 | return std::make_pair(y,w); |
|---|
| 53 | } |
|---|
| 54 | inline void state(const state_t x) { |
|---|
| 55 | y = x.first; |
|---|
| 56 | w = x.second; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | inline void seed(boost::uint32_t xx) { |
|---|
| 60 | y = UINT64_C(0x6A7BCC427F295846); |
|---|
| 61 | w = UINT64_C(0x61C8864680B583EB); |
|---|
| 62 | if(xx != 0) { |
|---|
| 63 | rand_native(); |
|---|
| 64 | y ^= static_cast<native_t>(xx); |
|---|
| 65 | } |
|---|
| 66 | for(int i=0;i<128;++i) |
|---|
| 67 | rand_native(); |
|---|
| 68 | } |
|---|
| 69 | template<int N> |
|---|
| 70 | inline void seed(boost::uint32_t (&xx)[N]) { |
|---|
| 71 | seed(&xx[0],&xx[N]); |
|---|
| 72 | } |
|---|
| 73 | template<typename _It> |
|---|
| 74 | inline void seed(_It first, _It last) { |
|---|
| 75 | y = UINT64_C(0x6A7BCC427F295846); |
|---|
| 76 | w = UINT64_C(0x61C8864680B583EB); |
|---|
| 77 | for(;first != last;++first) { |
|---|
| 78 | rand_native(); |
|---|
| 79 | y ^= static_cast<native_t>(*first); |
|---|
| 80 | } |
|---|
| 81 | for(int i=0;i<128;++i) |
|---|
| 82 | rand_native(); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | private: |
|---|
| 86 | native_t y,w; |
|---|
| 87 | }; |
|---|
| 88 | |
|---|
| 89 | typedef xorshift_64_mutt_gen mutt_gen_default; |
|---|
| 90 | |
|---|
| 91 | }} //namespace dawg::details |
|---|
| 92 | #endif |
|---|