Skip to content

Upgrade Random123 to 1.09 #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions src/include/clRNG/private/Random123/MicroURNG.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
Copyright 2010-2011, D. E. Shaw Research.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions, and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions, and the following disclaimer in the
documentation and/or other materials provided with the distribution.

* Neither the name of D. E. Shaw Research nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __MicroURNG_dot_hpp__
#define __MicroURNG_dot_hpp__

#include <stdexcept>
#include <limits>

namespace r123{
/**
Given a CBRNG whose ctr_type has an unsigned integral value_type,
MicroURNG<CBRNG>(c, k) is a type that satisfies the
requirements of a C++0x Uniform Random Number Generator.

The intended purpose is for a MicroURNG to be passed
as an argument to a C++0x Distribution, e.g.,
std::normal_distribution. See examples/MicroURNG.cpp.

The MicroURNG functor has a period of "only"

ctr_type.size()*2^32,

after which it will silently repeat.

The high 32 bits of the highest word in the counter c, passed to
the constructor must be zero. MicroURNG uses these bits to
"count".

Older versions of the library permitted a second template
parameter by which the caller could control the number of
bits devoted to the URNG's internal counter. This flexibility
has been disabled because URNGs created with different
numbers of counter bits could, conceivably "collide".

\code
typedef ?someCBRNG? RNG;
RNG::ctr_type c = ...; // under application control
RNG::key_type k = ...; //
std::normal_distribution<float> nd;
MicroURNG<RNG> urng(c, k);
for(???){
...
nd(urng); // may be called several hundred times with BITS=10
...
}
\endcode
*/

template<typename CBRNG>
class MicroURNG{
// According to C++0x, a URNG requires only a result_type,
// operator()(), min() and max() methods. Everything else
// (ctr_type, key_type, reset() method, etc.) is "value added"
// for the benefit of users that "know" that they're dealing with
// a MicroURNG.
public:
typedef CBRNG cbrng_type;
static const int BITS = 32;
typedef typename cbrng_type::ctr_type ctr_type;
typedef typename cbrng_type::key_type key_type;
typedef typename cbrng_type::ukey_type ukey_type;
typedef typename ctr_type::value_type result_type;

R123_STATIC_ASSERT( std::numeric_limits<result_type>::digits >= BITS, "The result_type must have at least 32 bits" );

result_type operator()(){
if(last_elem == 0){
// jam n into the high bits of c
const size_t W = std::numeric_limits<result_type>::digits;
ctr_type c = c0;
c[c0.size()-1] |= n<<(W-BITS);
rdata = b(c,k);
n++;
last_elem = rdata.size();
}
return rdata[--last_elem];
}
MicroURNG(cbrng_type _b, ctr_type _c0, ukey_type _uk) : b(_b), c0(_c0), k(_uk), n(0), last_elem(0) {
chkhighbits();
}
MicroURNG(ctr_type _c0, ukey_type _uk) : b(), c0(_c0), k(_uk), n(0), last_elem(0) {
chkhighbits();
}

// _Min and _Max work around a bug in the library shipped with MacOS Xcode 4.5.2.
// See the commment in conventional/Engine.hpp.
const static result_type _Min = 0;
const static result_type _Max = ~((result_type)0);

static R123_CONSTEXPR result_type min R123_NO_MACRO_SUBST () { return _Min; }
static R123_CONSTEXPR result_type max R123_NO_MACRO_SUBST () { return _Max; }
// extra methods:
const ctr_type& counter() const{ return c0; }
void reset(ctr_type _c0, ukey_type _uk){
c0 = _c0;
chkhighbits();
k = _uk;
n = 0;
last_elem = 0;
}

private:
cbrng_type b;
ctr_type c0;
key_type k;
R123_ULONG_LONG n;
size_t last_elem;
ctr_type rdata;
void chkhighbits(){
result_type r = c0[c0.size()-1];
result_type mask = ((uint64_t)std::numeric_limits<result_type>::max R123_NO_MACRO_SUBST ())>>BITS;
if((r&mask) != r)
throw std::runtime_error("MicroURNG: c0, does not have high bits clear");
}
};
} // namespace r123
#endif
88 changes: 88 additions & 0 deletions src/include/clRNG/private/Random123/ReinterpretCtr.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
Copyright 2010-2011, D. E. Shaw Research.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions, and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions, and the following disclaimer in the
documentation and/or other materials provided with the distribution.

* Neither the name of D. E. Shaw Research nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __ReinterpretCtr_dot_hpp__
#define __ReinterpretCtr_dot_hpp__

#include "features/compilerfeatures.h"
#include <cstring>

namespace r123{
/*!
ReinterpretCtr uses memcpy to map back and forth
between a CBRNG's ctr_type and the specified ToType. For example,
after:

typedef ReinterpretCtr<r123array4x32, Philox2x64> G;

G is a bona fide CBRNG with ctr_type r123array4x32.

WARNING: ReinterpretCtr is endian dependent. The
values returned by G, declared as above,
will depend on the endianness of the machine on which it runs.
*/

template <typename ToType, typename CBRNG>
struct ReinterpretCtr{
typedef ToType ctr_type;
typedef typename CBRNG::key_type key_type;
typedef typename CBRNG::ctr_type bctype;
typedef typename CBRNG::ukey_type ukey_type;
R123_STATIC_ASSERT(sizeof(ToType) == sizeof(bctype) && sizeof(typename bctype::value_type) != 16,
"ReinterpretCtr: sizeof(ToType) is not the same as sizeof(CBRNG::ctr_type) or CBRNG::ctr_type::value_type looks like it might be __m128i");
// It's amazingly difficult to safely do conversions with __m128i.
// If we use the operator() implementation below with a CBRNG
// whose ctr_type is r123array1xm128i, gcc4.6 optimizes away the
// memcpys, inlines the operator()(c,k), and produces assembly
// language that ends with an aesenclast instruction with a
// destination operand pointing to an unaligned memory address ...
// Segfault! See: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=50444
// MSVC also produces code that crashes. We suspect a
// similar mechanism but haven't done the debugging necessary to
// be sure. We were able to 'fix' gcc4.6 by making bc a mutable
// data member rather than declaring it in the scope of
// operator(). That didn't fix the MSVC problems, though.
//
// Conclusion - don't touch __m128i, at least for now. The
// easiest (but highly imprecise) way to do that is the static
// assertion above that rejects bctype::value_types of size 16. -
// Sep 2011.
ctr_type operator()(ctr_type c, key_type k){
bctype bc;
std::memcpy(&bc, &c, sizeof(c));
CBRNG b;
bc = b(bc, k);
std::memcpy(&c, &bc, sizeof(bc));
return c;
}
};
} // namespace r123
#endif
Loading