31#include "CLHEP/Random/defs.h"
32#include "CLHEP/Random/Random.h"
33#include "CLHEP/Random/Hurd160Engine.h"
34#include "CLHEP/Random/engineIDulong.h"
35#include "CLHEP/Utility/atomic_int.h"
52 const int maxIndex = 215;
55static const int MarkerLen = 64;
59static inline unsigned int f160(
unsigned int a,
unsigned int b,
unsigned int c)
61 return ( ((b<<2) & 0x7c) | ((a<<2) & ~0x7c) | (a>>30) ) ^ ( (c<<1)|(c>>31) );
67 int numEngines = numberOfEngines++;
68 int cycle = std::abs(
int(numEngines/maxIndex));
69 int curIndex = std::abs(
int(numEngines%maxIndex));
70 long mask = ((cycle & 0x007fffff) << 8);
74 words[0] ^= 0x1324abcd;
75 if (words[0]==0) words[0] = 1;
77 for(
int i=0; i < 100; ++i )
flat();
91 words[0] ^= 0xa5482134;
92 if (words[0]==0) words[0] = 1;
93 for(
int i=0; i < 100; ++i )
flat();
99 int cycle = std::abs(
int(rowIndex/maxIndex));
100 int row = std::abs(
int(rowIndex%maxIndex));
101 int col = colIndex & 0x1;
102 long mask = (( cycle & 0x000007ff ) << 20 );
105 seeds_[0] = seeds_[col]^mask;
107 for(
int i=0; i < 100; ++i )
flat();
112void Hurd160Engine::advance() {
113 unsigned int W0, W1, W2, W3, W4;
120 W1 ^= W0; W0 = f160(W4, W3, W0);
121 W2 ^= W1; W1 = f160(W0, W4, W1);
122 W3 ^= W2; W2 = f160(W1, W0, W2);
123 W4 ^= W3; W3 = f160(W2, W1, W3);
124 W0 ^= W4; W4 = f160(W3, W2, W4);
125 words[0] = W0 & 0xffffffff;
126 words[1] = W1 & 0xffffffff;
127 words[2] = W2 & 0xffffffff;
128 words[3] = W3 & 0xffffffff;
129 words[4] = W4 & 0xffffffff;
136 if( wordIndex <= 2 ) {
150 for (
int i = 0; i < size; ++i) {
159 words[0] = (
unsigned int)seed;
160 for (wordIndex = 1; wordIndex < 5; ++wordIndex) {
161 words[wordIndex] = 69607 * words[wordIndex-1] + 54329;
167 setSeed( *seeds ? *seeds : 32767, 0 );
171 std::ofstream outFile(filename, std::ios::out);
172 if( !outFile.bad() ) {
174 std::vector<unsigned long> v =
put();
176 std::cout <<
"Result of v = put() is:\n";
178 for (
unsigned int i=0; i<v.size(); ++i) {
179 outFile << v[i] <<
"\n";
181 std::cout << v[i] <<
" ";
182 if (i%6==0) std::cout <<
"\n";
190 outFile << std::setprecision(20) <<
theSeed <<
" ";
191 outFile << wordIndex <<
" ";
192 for(
int i = 0; i < 5; ++i ) {
193 outFile << words[i] <<
" ";
195 outFile << std::endl;
200 std::ifstream inFile(filename, std::ios::in);
202 std::cerr <<
" -- Engine state remains unchanged\n";
206 std::vector<unsigned long> v;
211 std::cout <<
"ivec = " << ivec <<
" xin = " << xin <<
" ";
212 if (ivec%3 == 0) std::cout <<
"\n";
215 inFile.clear(std::ios::badbit | inFile.rdstate());
216 std::cerr <<
"\nHurd160Engine state (vector) description improper."
217 <<
"\nrestoreStatus has failed."
218 <<
"\nInput stream is probably mispositioned now." << std::endl;
227 if( !inFile.bad() ) {
230 for(
int i = 0; i < 5; ++i ) {
237 long pr = std::cout.precision(20);
238 std::cout << std::endl;
239 std::cout <<
"----------- Hurd engine status ----------" << std::endl;
240 std::cout <<
"Initial seed = " <<
theSeed << std::endl;
241 std::cout <<
"Current index = " << wordIndex << std::endl;
242 std::cout <<
"Current words = " << std::endl;
243 for(
int i = 0; i < 5 ; ++i ) {
244 std::cout <<
" " << words[i] << std::endl;
246 std::cout <<
"------------------------------------------" << std::endl;
247 std::cout.precision(pr);
254Hurd160Engine::operator float() {
255 if( wordIndex <= 1 ) {
258 return words[--wordIndex ] * twoToMinus_32();
261Hurd160Engine::operator
unsigned int() {
262 if( wordIndex <= 1 ) {
265 return words[--wordIndex];
269 char beginMarker[] =
"Hurd160Engine-begin";
270 os << beginMarker <<
"\nUvec\n";
271 std::vector<unsigned long> v =
put();
272 for (
unsigned int i=0; i<v.size(); ++i) {
277 char endMarker[] =
"Hurd160Engine-end";
278 long pr = os.precision(20);
279 os <<
" " << beginMarker <<
" ";
281 os << wordIndex <<
" ";
282 for (
int i = 0; i < 5; ++i) {
283 os << words[i] <<
"\n";
285 os << endMarker <<
"\n ";
292 std::vector<unsigned long> v;
293 v.push_back (engineIDulong<Hurd160Engine>());
294 v.push_back(
static_cast<unsigned long>(wordIndex));
295 for (
int i = 0; i < 5; ++i) {
296 v.push_back(
static_cast<unsigned long>(words[i]));
303 char beginMarker [MarkerLen];
309 if (strcmp(beginMarker,
"Hurd160Engine-begin")) {
310 is.clear(std::ios::badbit | is.rdstate());
311 std::cerr <<
"\nInput mispositioned or"
312 <<
"\nHurd160Engine state description missing or"
313 <<
"\nwrong engine type found." << std::endl;
320 return "Hurd160Engine-begin";
325 std::vector<unsigned long> v;
330 is.clear(std::ios::badbit | is.rdstate());
331 std::cerr <<
"\nHurd160Engine state (vector) description improper."
332 <<
"\ngetState() has failed."
333 <<
"\nInput stream is probably mispositioned now." << std::endl;
344 char endMarker [MarkerLen];
346 for (
int i = 0; i < 5; ++i) {
352 if (strcmp(endMarker,
"Hurd160Engine-end")) {
353 is.clear(std::ios::badbit | is.rdstate());
354 std::cerr <<
"\nHurd160Engine state description incomplete."
355 <<
"\nInput stream is probably mispositioned now." << std::endl;
363 if ((v[0] & 0xffffffffUL) != engineIDulong<Hurd160Engine>()) {
365 "\nHurd160Engine get:state vector has wrong ID word - state unchanged\n";
374 "\nHurd160Engine get:state vector has wrong length - state unchanged\n";
377 wordIndex = (int)v[1];
378 for (
int i = 0; i < 5; ++i) {
379 words[i] = (
unsigned int)v[i+2];
#define CLHEP_ATOMIC_INT_TYPE
static double twoToMinus_32()
static double twoToMinus_53()
static double nearlyTwoToMinus_54()
static bool checkFile(std::istream &file, const std::string &filename, const std::string &classname, const std::string &methodname)
static void getTheTableSeeds(long *seeds, int index)
static const unsigned int VECTOR_STATE_SIZE
virtual std::istream & get(std::istream &is)
void setSeeds(const long *seeds, int)
void restoreStatus(const char filename[]="Hurd160Engine.conf")
virtual std::istream & getState(std::istream &is)
std::vector< unsigned long > put() const
static std::string engineName()
void flatArray(const int size, double *vect)
void setSeed(long seed, int)
static std::string beginTag()
void saveStatus(const char filename[]="Hurd160Engine.conf") const
bool possibleKeywordInput(IS &is, const std::string &key, T &t)