Project Alice
Loading...
Searching...
No Matches
MicroURNG.hpp
Go to the documentation of this file.
1/*
2Copyright 2010-2011, D. E. Shaw Research.
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are
7met:
8
9* Redistributions of source code must retain the above copyright
10 notice, this list of conditions, and the following disclaimer.
11
12* Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions, and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15
16* Neither the name of D. E. Shaw Research nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31*/
32#ifndef __MicroURNG_dot_hpp__
33#define __MicroURNG_dot_hpp__
34
35#include <stdexcept>
36#include <limits>
37
38namespace r123 {
78template<typename CBRNG>
79class MicroURNG {
80 // According to C++11, a URNG requires only a result_type,
81 // operator()(), min() and max() methods. Everything else
82 // (ctr_type, key_type, reset() method, etc.) is "value added"
83 // for the benefit of users that "know" that they're dealing with
84 // a MicroURNG.
85public:
86 typedef CBRNG cbrng_type;
87 static int const BITS = 32;
88 typedef typename cbrng_type::ctr_type ctr_type;
89 typedef typename cbrng_type::key_type key_type;
90 typedef typename cbrng_type::ukey_type ukey_type;
91 typedef typename ctr_type::value_type result_type;
92
93 R123_STATIC_ASSERT(std::numeric_limits<result_type>::digits >= BITS, "The result_type must have at least 32 bits");
94
96 if(last_elem == 0) {
97 // jam n into the high bits of c
98 const size_t W = std::numeric_limits<result_type>::digits;
99 ctr_type c = c0;
100 c[c0.size() - 1] |= n << (W - BITS);
101 rdata = b(c, k);
102 n++;
103 last_elem = rdata.size();
104 }
105 return rdata[--last_elem];
106 }
107 MicroURNG(cbrng_type _b, ctr_type _c0, ukey_type _uk) : b(_b), c0(_c0), k(_uk), n(0), last_elem(0) {
108 chkhighbits();
109 }
110 MicroURNG(ctr_type _c0, ukey_type _uk) : b(), c0(_c0), k(_uk), n(0), last_elem(0) {
111 chkhighbits();
112 }
113
114 // _Min and _Max work around a bug in the library shipped with MacOS Xcode 4.5.2.
115 // See the commment in conventional/Engine.hpp.
116 const static result_type _Min = 0;
117 const static result_type _Max = ~((result_type)0);
118
119 static R123_CONSTEXPR result_type min R123_NO_MACRO_SUBST() {
120 return _Min;
121 }
122 static R123_CONSTEXPR result_type max R123_NO_MACRO_SUBST() {
123 return _Max;
124 }
125 // extra methods:
126 ctr_type const& counter() const {
127 return c0;
128 }
129 void reset(ctr_type _c0, ukey_type _uk) {
130 c0 = _c0;
131 chkhighbits();
132 k = _uk;
133 n = 0;
134 last_elem = 0;
135 }
136
137private:
138 cbrng_type b;
139 ctr_type c0;
140 key_type k;
142 size_t last_elem;
143 ctr_type rdata;
144 void chkhighbits() {
145 result_type r = c0[c0.size() - 1];
146 result_type mask = ((uint64_t)std::numeric_limits<result_type>::max R123_NO_MACRO_SUBST()) >> BITS;
147 if((r & mask) != r)
148 throw std::runtime_error("MicroURNG: c0, does not have high bits clear");
149 }
150};
151} // namespace r123
152#endif
MicroURNG(cbrng_type _b, ctr_type _c0, ukey_type _uk)
Definition: MicroURNG.hpp:107
static const result_type _Min
Definition: MicroURNG.hpp:116
ctr_type const & counter() const
Definition: MicroURNG.hpp:126
static int const BITS
Definition: MicroURNG.hpp:87
static R123_CONSTEXPR result_type max R123_NO_MACRO_SUBST()
Definition: MicroURNG.hpp:122
static const result_type _Max
Definition: MicroURNG.hpp:117
ctr_type::value_type result_type
Definition: MicroURNG.hpp:91
cbrng_type::ctr_type ctr_type
Definition: MicroURNG.hpp:88
result_type operator()()
Definition: MicroURNG.hpp:95
cbrng_type::ukey_type ukey_type
Definition: MicroURNG.hpp:90
MicroURNG(ctr_type _c0, ukey_type _uk)
Definition: MicroURNG.hpp:110
R123_STATIC_ASSERT(std::numeric_limits< result_type >::digits >=BITS, "The result_type must have at least 32 bits")
static R123_CONSTEXPR result_type min R123_NO_MACRO_SUBST()
Definition: MicroURNG.hpp:119
void reset(ctr_type _c0, ukey_type _uk)
Definition: MicroURNG.hpp:129
cbrng_type::key_type key_type
Definition: MicroURNG.hpp:89
#define R123_ULONG_LONG
Definition: array.h:344
ulong uint64_t