37#include "CLHEP/Random/defs.h"
38#include "CLHEP/Random/RanshiEngine.h"
39#include "CLHEP/Random/engineIDulong.h"
40#include "CLHEP/Utility/atomic_int.h"
50 #if __GNUC__ > 3 && __GNUC_MINOR__ > 8
51 #pragma GCC diagnostic push
52 #pragma GCC diagnostic ignored "-Waggressive-loop-optimizations"
63static const int MarkerLen = 64;
69 halfBuff(0), numFlats(0)
71 int numEngines = numberOfEngines++;
74 buffer[i] = (
unsigned int)((numEngines+19780503L*(i+1))& 0xffffffff);
77 theSeed = numEngines+19780503L*++i;
78 redSpin = (
unsigned int)(
theSeed & 0xffffffff);
80 for( i = 0; i < 10000; ++i)
flat();
85 halfBuff(0), numFlats(0)
92 halfBuff(0), numFlats(0)
94 for (
int i = 0; i < numBuff; ++i) {
95 buffer[i] = (
unsigned int)seed&0xffffffff;
98 redSpin = (
unsigned int)(
theSeed & 0xffffffff);
100 for (j = 0; j < numBuff*20; ++j) {
107 halfBuff(0), numFlats(0)
110 while( i < numBuff ) {
111 buffer[i] = (
unsigned int)((rowIndex + (i+1)*(colIndex+8))&0xffffffff);
115 redSpin = colIndex & 0xffffffff;
116 for( i = 0; i < 100; ++i)
flat();
122 unsigned int redAngle = (((numBuff/2) - 1) & redSpin) + halfBuff;
123 unsigned int blkSpin = buffer[redAngle] & 0xffffffff;
124 unsigned int boostResult = blkSpin ^ redSpin;
126 buffer[redAngle] = ((blkSpin << 17) | (blkSpin >> (32-17))) ^ redSpin;
128 redSpin = (blkSpin + numFlats++) & 0xffffffff;
129 halfBuff = numBuff/2 - halfBuff;
137 for (
int i = 0; i < size; ++i) {
149 while (seeds[i] && i < numBuff) {
150 buffer[i] = (
unsigned int)seeds[i];
153 while (i < numBuff) {
154 buffer[i] = buffer[i-1];
158 redSpin = (
unsigned int)
theSeed;
164 std::ofstream outFile(filename, std::ios::out);
165 if (!outFile.bad()) {
167 std::vector<unsigned long> v =
put();
169 std::cout <<
"Result of v = put() is:\n";
171 for (
unsigned int i=0; i<v.size(); ++i) {
172 outFile << v[i] <<
"\n";
174 std::cout << v[i] <<
" ";
175 if (i%6==0) std::cout <<
"\n";
183 if (!outFile.bad()) {
184 outFile << std::setprecision(20) <<
theSeed << std::endl;
185 for (
int i = 0; i < numBuff; ++i) {
186 outFile << buffer[i] <<
" ";
188 outFile << redSpin <<
" " << numFlats <<
" " << halfBuff << std::endl;
194 std::ifstream inFile(filename, std::ios::in);
196 std::cerr <<
" -- Engine state remains unchanged\n";
200 std::vector<unsigned long> v;
202 for (
unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
205 std::cout <<
"ivec = " << ivec <<
" xin = " << xin <<
" ";
206 if (ivec%3 == 0) std::cout <<
"\n";
209 inFile.clear(std::ios::badbit | inFile.rdstate());
210 std::cerr <<
"\nRanshiEngine state (vector) description improper."
211 <<
"\nrestoreStatus has failed."
212 <<
"\nInput stream is probably mispositioned now." << std::endl;
223 for (
int i = 0; i < numBuff; ++i) {
226 inFile >> redSpin >> numFlats >> halfBuff;
231 std::cout << std::setprecision(20) << std::endl;
232 std::cout <<
"----------- Ranshi engine status ----------" << std::endl;
233 std::cout <<
"Initial seed = " <<
theSeed << std::endl;
234 std::cout <<
"Current red spin = " << redSpin << std::endl;
235 std::cout <<
"Values produced = " << numFlats << std::endl;
236 std::cout <<
"Side of buffer = " << (halfBuff ?
"upper" :
"lower")
238 std::cout <<
"Current buffer = " << std::endl;
239 for (
int i = 0; i < numBuff; i+=4) {
240 std::cout << std::setw(10) << std::setiosflags(std::ios::right)
241 << buffer[i] << std::setw(11) << buffer[i+1] << std::setw(11)
242 << buffer[i+2] << std::setw(11) << buffer[i+3] << std::endl;
244 std::cout <<
"-------------------------------------------" << std::endl;
251RanshiEngine::operator float() {
252 unsigned int redAngle = (((numBuff/2) - 1) & redSpin) + halfBuff;
253 unsigned int blkSpin = buffer[redAngle] & 0xffffffff;
255 buffer[redAngle] = ((blkSpin << 17) | (blkSpin >> (32-17))) ^ redSpin;
257 redSpin = (blkSpin + numFlats++) & 0xffffffff;
258 halfBuff = numBuff/2 - halfBuff;
260 return float(blkSpin * twoToMinus_32());
263RanshiEngine::operator
unsigned int() {
264 unsigned int redAngle = (((numBuff/2) - 1) & redSpin) + halfBuff;
265 unsigned int blkSpin = buffer[redAngle] & 0xffffffff;
267 buffer[redAngle] = ((blkSpin << 17) | (blkSpin >> (32-17))) ^ redSpin;
269 redSpin = (blkSpin + numFlats++) & 0xffffffff;
270 halfBuff = numBuff/2 - halfBuff;
276 char beginMarker[] =
"RanshiEngine-begin";
277 os << beginMarker <<
"\nUvec\n";
278 std::vector<unsigned long> v =
put();
279 for (
unsigned int i=0; i<v.size(); ++i) {
284 char endMarker[] =
"RanshiEngine-end";
285 long pr=os.precision(20);
286 os <<
" " << beginMarker <<
" ";
289 for (
int i = 0; i < numBuff; ++i) {
290 os << buffer[i] <<
"\n";
292 os << redSpin <<
" " << numFlats <<
"\n" << halfBuff;
294 os <<
" " << endMarker <<
"\n";
301 std::vector<unsigned long> v;
302 v.push_back (engineIDulong<RanshiEngine>());
303 for (
int i = 0; i < numBuff; ++i) {
304 v.push_back(
static_cast<unsigned long>(buffer[i]));
306 v.push_back(
static_cast<unsigned long>(redSpin));
307 v.push_back(
static_cast<unsigned long>(numFlats));
308 v.push_back(
static_cast<unsigned long>(halfBuff));
313 char beginMarker [MarkerLen];
319 if (strcmp(beginMarker,
"RanshiEngine-begin")) {
320 is.clear(std::ios::badbit | is.rdstate());
321 std::cerr <<
"\nInput mispositioned or"
322 <<
"\nRanshiEngine state description missing or"
323 <<
"\nwrong engine type found." << std::endl;
330 return "RanshiEngine-begin";
335 std::vector<unsigned long> v;
337 for (
unsigned int ivec=0; ivec < VECTOR_STATE_SIZE; ++ivec) {
340 is.clear(std::ios::badbit | is.rdstate());
341 std::cerr <<
"\nRanshiEngine state (vector) description improper."
342 <<
"\ngetState() has failed."
343 <<
"\nInput stream is probably mispositioned now." << std::endl;
354 char endMarker [MarkerLen];
355 for (
int i = 0; i < numBuff; ++i) {
358 is >> redSpin >> numFlats >> halfBuff;
362 if (strcmp(endMarker,
"RanshiEngine-end")) {
363 is.clear(std::ios::badbit | is.rdstate());
364 std::cerr <<
"\nRanshiEngine state description incomplete."
365 <<
"\nInput stream is probably mispositioned now." << std::endl;
372 if ((v[0] & 0xffffffffUL) != engineIDulong<RanshiEngine>()) {
374 "\nRanshiEngine get:state vector has wrong ID word - state unchanged\n";
381 if (v.size() != VECTOR_STATE_SIZE ) {
383 "\nRanshiEngine get:state vector has wrong length - state unchanged\n";
386 for (
int i = 0; i < numBuff; ++i) {
387 buffer[i] = (
unsigned int)v[i+1];
389 redSpin = (
unsigned int)v[numBuff+1];
390 numFlats = (
unsigned int)v[numBuff+2];
391 halfBuff = (
unsigned int)v[numBuff+3];
398 #if __GNUC__ > 3 && __GNUC_MINOR__ > 8
399 #pragma GCC diagnostic pop
#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)
virtual std::istream & getState(std::istream &is)
static std::string beginTag()
void flatArray(const int size, double *vect)
virtual std::istream & get(std::istream &is)
void saveStatus(const char filename[]="RanshiEngine.conf") const
std::vector< unsigned long > put() const
static std::string engineName()
void restoreStatus(const char filename[]="RanshiEngine.conf")
void setSeeds(const long *seeds, int)
void setSeed(long seed, int)
bool possibleKeywordInput(IS &is, const std::string &key, T &t)