epoc32/include/stdapis/boost/random/mersenne_twister.hpp
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:33:34 +0100
branchSymbian3
changeset 4 837f303aceeb
permissions -rw-r--r--
Current Symbian^3 public API header files (from PDK 3.0.h)
This is the epoc32/include tree with the "platform" subtrees removed, and
all but a selected few mbg and rsg files removed.
williamr@2
     1
/* boost random/mersenne_twister.hpp header file
williamr@2
     2
 *
williamr@2
     3
 * Copyright Jens Maurer 2000-2001
williamr@2
     4
 * Distributed under the Boost Software License, Version 1.0. (See
williamr@2
     5
 * accompanying file LICENSE_1_0.txt or copy at
williamr@2
     6
 * http://www.boost.org/LICENSE_1_0.txt)
williamr@2
     7
 *
williamr@2
     8
 * See http://www.boost.org for most recent version including documentation.
williamr@2
     9
 *
williamr@2
    10
 * $Id: mersenne_twister.hpp,v 1.20 2005/07/21 22:04:31 jmaurer Exp $
williamr@2
    11
 *
williamr@2
    12
 * Revision history
williamr@2
    13
 *  2001-02-18  moved to individual header files
williamr@2
    14
 */
williamr@2
    15
williamr@2
    16
#ifndef BOOST_RANDOM_MERSENNE_TWISTER_HPP
williamr@2
    17
#define BOOST_RANDOM_MERSENNE_TWISTER_HPP
williamr@2
    18
williamr@2
    19
#include <iostream>
williamr@2
    20
#include <algorithm>     // std::copy
williamr@2
    21
#include <stdexcept>
williamr@2
    22
#include <boost/config.hpp>
williamr@2
    23
#include <boost/limits.hpp>
williamr@2
    24
#include <boost/static_assert.hpp>
williamr@2
    25
#include <boost/integer_traits.hpp>
williamr@2
    26
#include <boost/cstdint.hpp>
williamr@2
    27
#include <boost/random/linear_congruential.hpp>
williamr@2
    28
#include <boost/detail/workaround.hpp>
williamr@2
    29
#include <boost/random/detail/ptr_helper.hpp>
williamr@2
    30
williamr@2
    31
namespace boost {
williamr@2
    32
namespace random {
williamr@2
    33
williamr@2
    34
// http://www.math.keio.ac.jp/matumoto/emt.html
williamr@2
    35
template<class UIntType, int w, int n, int m, int r, UIntType a, int u,
williamr@2
    36
  int s, UIntType b, int t, UIntType c, int l, UIntType val>
williamr@2
    37
class mersenne_twister
williamr@2
    38
{
williamr@2
    39
public:
williamr@2
    40
  typedef UIntType result_type;
williamr@2
    41
  BOOST_STATIC_CONSTANT(int, word_size = w);
williamr@2
    42
  BOOST_STATIC_CONSTANT(int, state_size = n);
williamr@2
    43
  BOOST_STATIC_CONSTANT(int, shift_size = m);
williamr@2
    44
  BOOST_STATIC_CONSTANT(int, mask_bits = r);
williamr@2
    45
  BOOST_STATIC_CONSTANT(UIntType, parameter_a = a);
williamr@2
    46
  BOOST_STATIC_CONSTANT(int, output_u = u);
williamr@2
    47
  BOOST_STATIC_CONSTANT(int, output_s = s);
williamr@2
    48
  BOOST_STATIC_CONSTANT(UIntType, output_b = b);
williamr@2
    49
  BOOST_STATIC_CONSTANT(int, output_t = t);
williamr@2
    50
  BOOST_STATIC_CONSTANT(UIntType, output_c = c);
williamr@2
    51
  BOOST_STATIC_CONSTANT(int, output_l = l);
williamr@2
    52
williamr@2
    53
  BOOST_STATIC_CONSTANT(bool, has_fixed_range = false);
williamr@2
    54
  
williamr@2
    55
  mersenne_twister() { seed(); }
williamr@2
    56
williamr@2
    57
#if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x520)
williamr@2
    58
  // Work around overload resolution problem (Gennadiy E. Rozental)
williamr@2
    59
  explicit mersenne_twister(const UIntType& value)
williamr@2
    60
#else
williamr@2
    61
  explicit mersenne_twister(UIntType value)
williamr@2
    62
#endif
williamr@2
    63
  { seed(value); }
williamr@2
    64
  template<class It> mersenne_twister(It& first, It last) { seed(first,last); }
williamr@2
    65
williamr@2
    66
  template<class Generator>
williamr@2
    67
  explicit mersenne_twister(Generator & gen) { seed(gen); }
williamr@2
    68
williamr@2
    69
  // compiler-generated copy ctor and assignment operator are fine
williamr@2
    70
williamr@2
    71
  void seed() { seed(UIntType(5489)); }
williamr@2
    72
williamr@2
    73
#if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x520)
williamr@2
    74
  // Work around overload resolution problem (Gennadiy E. Rozental)
williamr@2
    75
  void seed(const UIntType& value)
williamr@2
    76
#else
williamr@2
    77
  void seed(UIntType value)
williamr@2
    78
#endif
williamr@2
    79
  {
williamr@2
    80
    // New seeding algorithm from 
williamr@2
    81
    // http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/emt19937ar.html
williamr@2
    82
    // In the previous versions, MSBs of the seed affected only MSBs of the
williamr@2
    83
    // state x[].
williamr@2
    84
    const UIntType mask = ~0u;
williamr@2
    85
    x[0] = value & mask;
williamr@2
    86
    for (i = 1; i < n; i++) {
williamr@2
    87
      // See Knuth "The Art of Computer Programming" Vol. 2, 3rd ed., page 106
williamr@2
    88
      x[i] = (1812433253UL * (x[i-1] ^ (x[i-1] >> (w-2))) + i) & mask;
williamr@2
    89
    }
williamr@2
    90
  }
williamr@2
    91
williamr@2
    92
  // For GCC, moving this function out-of-line prevents inlining, which may
williamr@2
    93
  // reduce overall object code size.  However, MSVC does not grok
williamr@2
    94
  // out-of-line definitions of member function templates.
williamr@2
    95
  template<class Generator>
williamr@2
    96
  void seed(Generator & gen)
williamr@2
    97
  {
williamr@2
    98
#ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
williamr@2
    99
    BOOST_STATIC_ASSERT(!std::numeric_limits<result_type>::is_signed);
williamr@2
   100
#endif
williamr@2
   101
    // I could have used std::generate_n, but it takes "gen" by value
williamr@2
   102
    for(int j = 0; j < n; j++)
williamr@2
   103
      x[j] = gen();
williamr@2
   104
    i = n;
williamr@2
   105
  }
williamr@2
   106
williamr@2
   107
  template<class It>
williamr@2
   108
  void seed(It& first, It last)
williamr@2
   109
  {
williamr@2
   110
    int j;
williamr@2
   111
    for(j = 0; j < n && first != last; ++j, ++first)
williamr@2
   112
      x[j] = *first;
williamr@2
   113
    i = n;
williamr@2
   114
    if(first == last && j < n)
williamr@2
   115
      throw std::invalid_argument("mersenne_twister::seed");
williamr@2
   116
  }
williamr@2
   117
  
williamr@2
   118
  result_type min BOOST_PREVENT_MACRO_SUBSTITUTION () const { return 0; }
williamr@2
   119
  result_type max BOOST_PREVENT_MACRO_SUBSTITUTION () const
williamr@2
   120
  {
williamr@2
   121
    // avoid "left shift count >= with of type" warning
williamr@2
   122
    result_type res = 0;
williamr@2
   123
    for(int i = 0; i < w; ++i)
williamr@2
   124
      res |= (1u << i);
williamr@2
   125
    return res;
williamr@2
   126
  }
williamr@2
   127
williamr@2
   128
  result_type operator()();
williamr@2
   129
  static bool validation(result_type v) { return val == v; }
williamr@2
   130
williamr@2
   131
#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
williamr@2
   132
williamr@2
   133
#ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
williamr@2
   134
  template<class CharT, class Traits>
williamr@2
   135
  friend std::basic_ostream<CharT,Traits>&
williamr@2
   136
  operator<<(std::basic_ostream<CharT,Traits>& os, const mersenne_twister& mt)
williamr@2
   137
  {
williamr@2
   138
    for(int j = 0; j < mt.state_size; ++j)
williamr@2
   139
      os << mt.compute(j) << " ";
williamr@2
   140
    return os;
williamr@2
   141
  }
williamr@2
   142
williamr@2
   143
  template<class CharT, class Traits>
williamr@2
   144
  friend std::basic_istream<CharT,Traits>&
williamr@2
   145
  operator>>(std::basic_istream<CharT,Traits>& is, mersenne_twister& mt)
williamr@2
   146
  {
williamr@2
   147
    for(int j = 0; j < mt.state_size; ++j)
williamr@2
   148
      is >> mt.x[j] >> std::ws;
williamr@2
   149
    // MSVC (up to 7.1) and Borland (up to 5.64) don't handle the template
williamr@2
   150
    // value parameter "n" available from the class template scope, so use
williamr@2
   151
    // the static constant with the same value
williamr@2
   152
    mt.i = mt.state_size;
williamr@2
   153
    return is;
williamr@2
   154
  }
williamr@2
   155
#endif
williamr@2
   156
williamr@2
   157
  friend bool operator==(const mersenne_twister& x, const mersenne_twister& y)
williamr@2
   158
  {
williamr@2
   159
    for(int j = 0; j < state_size; ++j)
williamr@2
   160
      if(x.compute(j) != y.compute(j))
williamr@2
   161
        return false;
williamr@2
   162
    return true;
williamr@2
   163
  }
williamr@2
   164
williamr@2
   165
  friend bool operator!=(const mersenne_twister& x, const mersenne_twister& y)
williamr@2
   166
  { return !(x == y); }
williamr@2
   167
#else
williamr@2
   168
  // Use a member function; Streamable concept not supported.
williamr@2
   169
  bool operator==(const mersenne_twister& rhs) const
williamr@2
   170
  {
williamr@2
   171
    for(int j = 0; j < state_size; ++j)
williamr@2
   172
      if(compute(j) != rhs.compute(j))
williamr@2
   173
        return false;
williamr@2
   174
    return true;
williamr@2
   175
  }
williamr@2
   176
williamr@2
   177
  bool operator!=(const mersenne_twister& rhs) const
williamr@2
   178
  { return !(*this == rhs); }
williamr@2
   179
#endif
williamr@2
   180
williamr@2
   181
private:
williamr@2
   182
  // returns x(i-n+index), where index is in 0..n-1
williamr@2
   183
  UIntType compute(unsigned int index) const
williamr@2
   184
  {
williamr@2
   185
    // equivalent to (i-n+index) % 2n, but doesn't produce negative numbers
williamr@2
   186
    return x[ (i + n + index) % (2*n) ];
williamr@2
   187
  }
williamr@2
   188
  void twist(int block);
williamr@2
   189
williamr@2
   190
  // state representation: next output is o(x(i))
williamr@2
   191
  //   x[0]  ... x[k] x[k+1] ... x[n-1]     x[n]     ... x[2*n-1]   represents
williamr@2
   192
  //  x(i-k) ... x(i) x(i+1) ... x(i-k+n-1) x(i-k-n) ... x[i(i-k-1)]
williamr@2
   193
  // The goal is to always have x(i-n) ... x(i-1) available for
williamr@2
   194
  // operator== and save/restore.
williamr@2
   195
williamr@2
   196
  UIntType x[2*n]; 
williamr@2
   197
  int i;
williamr@2
   198
};
williamr@2
   199
williamr@2
   200
#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
williamr@2
   201
//  A definition is required even for integral static constants
williamr@2
   202
template<class UIntType, int w, int n, int m, int r, UIntType a, int u,
williamr@2
   203
  int s, UIntType b, int t, UIntType c, int l, UIntType val>
williamr@2
   204
const bool mersenne_twister<UIntType,w,n,m,r,a,u,s,b,t,c,l,val>::has_fixed_range;
williamr@2
   205
template<class UIntType, int w, int n, int m, int r, UIntType a, int u,
williamr@2
   206
  int s, UIntType b, int t, UIntType c, int l, UIntType val>
williamr@2
   207
const int mersenne_twister<UIntType,w,n,m,r,a,u,s,b,t,c,l,val>::state_size;
williamr@2
   208
template<class UIntType, int w, int n, int m, int r, UIntType a, int u,
williamr@2
   209
  int s, UIntType b, int t, UIntType c, int l, UIntType val>
williamr@2
   210
const int mersenne_twister<UIntType,w,n,m,r,a,u,s,b,t,c,l,val>::shift_size;
williamr@2
   211
template<class UIntType, int w, int n, int m, int r, UIntType a, int u,
williamr@2
   212
  int s, UIntType b, int t, UIntType c, int l, UIntType val>
williamr@2
   213
const int mersenne_twister<UIntType,w,n,m,r,a,u,s,b,t,c,l,val>::mask_bits;
williamr@2
   214
template<class UIntType, int w, int n, int m, int r, UIntType a, int u,
williamr@2
   215
  int s, UIntType b, int t, UIntType c, int l, UIntType val>
williamr@2
   216
const UIntType mersenne_twister<UIntType,w,n,m,r,a,u,s,b,t,c,l,val>::parameter_a;
williamr@2
   217
template<class UIntType, int w, int n, int m, int r, UIntType a, int u,
williamr@2
   218
  int s, UIntType b, int t, UIntType c, int l, UIntType val>
williamr@2
   219
const int mersenne_twister<UIntType,w,n,m,r,a,u,s,b,t,c,l,val>::output_u;
williamr@2
   220
template<class UIntType, int w, int n, int m, int r, UIntType a, int u,
williamr@2
   221
  int s, UIntType b, int t, UIntType c, int l, UIntType val>
williamr@2
   222
const int mersenne_twister<UIntType,w,n,m,r,a,u,s,b,t,c,l,val>::output_s;
williamr@2
   223
template<class UIntType, int w, int n, int m, int r, UIntType a, int u,
williamr@2
   224
  int s, UIntType b, int t, UIntType c, int l, UIntType val>
williamr@2
   225
const UIntType mersenne_twister<UIntType,w,n,m,r,a,u,s,b,t,c,l,val>::output_b;
williamr@2
   226
template<class UIntType, int w, int n, int m, int r, UIntType a, int u,
williamr@2
   227
  int s, UIntType b, int t, UIntType c, int l, UIntType val>
williamr@2
   228
const int mersenne_twister<UIntType,w,n,m,r,a,u,s,b,t,c,l,val>::output_t;
williamr@2
   229
template<class UIntType, int w, int n, int m, int r, UIntType a, int u,
williamr@2
   230
  int s, UIntType b, int t, UIntType c, int l, UIntType val>
williamr@2
   231
const UIntType mersenne_twister<UIntType,w,n,m,r,a,u,s,b,t,c,l,val>::output_c;
williamr@2
   232
template<class UIntType, int w, int n, int m, int r, UIntType a, int u,
williamr@2
   233
  int s, UIntType b, int t, UIntType c, int l, UIntType val>
williamr@2
   234
const int mersenne_twister<UIntType,w,n,m,r,a,u,s,b,t,c,l,val>::output_l;
williamr@2
   235
#endif
williamr@2
   236
williamr@2
   237
template<class UIntType, int w, int n, int m, int r, UIntType a, int u,
williamr@2
   238
  int s, UIntType b, int t, UIntType c, int l, UIntType val>
williamr@2
   239
void mersenne_twister<UIntType,w,n,m,r,a,u,s,b,t,c,l,val>::twist(int block)
williamr@2
   240
{
williamr@2
   241
  const UIntType upper_mask = (~0u) << r;
williamr@2
   242
  const UIntType lower_mask = ~upper_mask;
williamr@2
   243
williamr@2
   244
  if(block == 0) {
williamr@2
   245
    for(int j = n; j < 2*n; j++) {
williamr@2
   246
      UIntType y = (x[j-n] & upper_mask) | (x[j-(n-1)] & lower_mask);
williamr@2
   247
      x[j] = x[j-(n-m)] ^ (y >> 1) ^ (y&1 ? a : 0);
williamr@2
   248
    }
williamr@2
   249
  } else if (block == 1) {
williamr@2
   250
    // split loop to avoid costly modulo operations
williamr@2
   251
    {  // extra scope for MSVC brokenness w.r.t. for scope
williamr@2
   252
      for(int j = 0; j < n-m; j++) {
williamr@2
   253
        UIntType y = (x[j+n] & upper_mask) | (x[j+n+1] & lower_mask);
williamr@2
   254
        x[j] = x[j+n+m] ^ (y >> 1) ^ (y&1 ? a : 0);
williamr@2
   255
      }
williamr@2
   256
    }
williamr@2
   257
    
williamr@2
   258
    for(int j = n-m; j < n-1; j++) {
williamr@2
   259
      UIntType y = (x[j+n] & upper_mask) | (x[j+n+1] & lower_mask);
williamr@2
   260
      x[j] = x[j-(n-m)] ^ (y >> 1) ^ (y&1 ? a : 0);
williamr@2
   261
    }
williamr@2
   262
    // last iteration
williamr@2
   263
    UIntType y = (x[2*n-1] & upper_mask) | (x[0] & lower_mask);
williamr@2
   264
    x[n-1] = x[m-1] ^ (y >> 1) ^ (y&1 ? a : 0);
williamr@2
   265
    i = 0;
williamr@2
   266
  }
williamr@2
   267
}
williamr@2
   268
williamr@2
   269
template<class UIntType, int w, int n, int m, int r, UIntType a, int u,
williamr@2
   270
  int s, UIntType b, int t, UIntType c, int l, UIntType val>
williamr@2
   271
inline typename mersenne_twister<UIntType,w,n,m,r,a,u,s,b,t,c,l,val>::result_type
williamr@2
   272
mersenne_twister<UIntType,w,n,m,r,a,u,s,b,t,c,l,val>::operator()()
williamr@2
   273
{
williamr@2
   274
  if(i == n)
williamr@2
   275
    twist(0);
williamr@2
   276
  else if(i >= 2*n)
williamr@2
   277
    twist(1);
williamr@2
   278
  // Step 4
williamr@2
   279
  UIntType z = x[i];
williamr@2
   280
  ++i;
williamr@2
   281
  z ^= (z >> u);
williamr@2
   282
  z ^= ((z << s) & b);
williamr@2
   283
  z ^= ((z << t) & c);
williamr@2
   284
  z ^= (z >> l);
williamr@2
   285
  return z;
williamr@2
   286
}
williamr@2
   287
williamr@2
   288
} // namespace random
williamr@2
   289
williamr@2
   290
williamr@2
   291
typedef random::mersenne_twister<uint32_t,32,351,175,19,0xccab8ee7,11,
williamr@2
   292
  7,0x31b6ab00,15,0xffe50000,17, 0xa37d3c92> mt11213b;
williamr@2
   293
williamr@2
   294
// validation by experiment from mt19937.c
williamr@2
   295
typedef random::mersenne_twister<uint32_t,32,624,397,31,0x9908b0df,11,
williamr@2
   296
  7,0x9d2c5680,15,0xefc60000,18, 3346425566U> mt19937;
williamr@2
   297
williamr@2
   298
} // namespace boost
williamr@2
   299
williamr@2
   300
BOOST_RANDOM_PTR_HELPER_SPEC(boost::mt19937)
williamr@2
   301
williamr@2
   302
#endif // BOOST_RANDOM_MERSENNE_TWISTER_HPP