47 #include "DRand48Engine.h"
54 # include <sys/types.h>
59 struct drand48_data libc_drand48_data;
61 int drand48_iterate (
unsigned short int xsubi[3],
struct drand48_data *buffer)
63 ULONGLONG X, a, result;
68 #if (USHRT_MAX == 0xffffU)
70 buffer->a[1] = 0xdeec;
71 buffer->a[0] = 0xe66d;
73 buffer->a[2] = 0x5deecUL;
74 buffer->a[1] = 0xe66d0000UL;
85 if (
sizeof (
unsigned short int) == 2)
87 X = (ULONGLONG)xsubi[2] << 32 | (ULONGLONG)xsubi[1] << 16 | xsubi[0];
88 a = ((ULONGLONG)buffer->a[2] << 32 | (ULONGLONG)buffer->a[1] << 16
91 result = X * a + buffer->c;
93 xsubi[0] = (
unsigned short int) (result & 0xffff);
94 xsubi[1] = (
unsigned short int) ((result >> 16) & 0xffff);
95 xsubi[2] = (
unsigned short int) ((result >> 32) & 0xffff);
99 X = (ULONGLONG)xsubi[2] << 16 | xsubi[1] >> 16;
100 a = (ULONGLONG)buffer->a[2] << 16 | buffer->a[1] >> 16;
102 result = X * a + buffer->c;
104 xsubi[0] = (
unsigned short int) (result >> 16 & 0xffffffffl);
105 xsubi[1] = (
unsigned short int) (result << 16 & 0xffff0000l);
111 int seed48_r (
unsigned short int seed16v[3],
struct drand48_data *buffer)
114 memcpy (buffer->old_X, buffer->X, sizeof (buffer->X));
117 memcpy (buffer->X, seed16v, sizeof (buffer->X));
122 unsigned short int * seed48 (
unsigned short int seed16v[3])
124 (void) seed48_r (seed16v, &libc_drand48_data);
126 return libc_drand48_data.old_X;
129 int srand48_r (
long seedval,
struct drand48_data *buffer)
132 if (
sizeof (
long) > 4)
133 seedval &= 0xffffffffl;
135 #if (USHRT_MAX == 0xffffU)
136 buffer->X[2] = (
unsigned short int) (seedval >> 16);
137 buffer->X[1] = (
unsigned short int) (seedval & 0xffffl);
138 buffer->X[0] = 0x330e;
140 buffer->X[2] = seedval;
141 buffer->X[1] = 0x330e0000UL;
148 void srand48 (
long seedval)
150 (void) srand48_r (seedval, &libc_drand48_data);
153 int erand48_r (
unsigned short int xsubi[3],
struct drand48_data *buffer,
double *result)
156 if (drand48_iterate (xsubi, buffer) < 0)
162 #if USHRT_MAX == 65535
163 *result = ((double) xsubi[2] / ((ULONGLONG)1 << 48) +
164 (
double) xsubi[1] / ((ULONGLONG)1 << 32) +
165 (double) xsubi[0] / ((ULONGLONG)1 << 16));
167 # error Unsupported size of short int
177 (void) erand48_r (libc_drand48_data.X, &libc_drand48_data, &result);
186 DRand48Engine::DRand48Engine(
long seed)
189 setSeeds(&theSeed,0);
192 DRand48Engine::~DRand48Engine() {}
200 if ((
this != &p) && (&p)) {
203 setSeeds(&theSeed,0);
213 if ((
this != &p) && (&p)) {
216 setSeeds(&theSeed,0);
221 void DRand48Engine::setSeed(
long seed, HepInt)
227 void DRand48Engine::setSeeds(
const long* seeds, HepInt)
229 setSeed(seeds ? *seeds : 19780503, 0);
233 void DRand48Engine::saveStatus()
const
235 ofstream outFile(
"DRand48.conf", std::ios::out ) ;
237 unsigned short dummy[] = { 0, 0, 0 };
238 unsigned short* cseed = seed48(dummy);
240 if (!outFile.bad()) {
241 outFile << theSeed << endl;
242 for (HepInt i=0; i<3; ++i) {
243 outFile << cseed[i] << endl;
250 void DRand48Engine::restoreStatus()
252 ifstream inFile(
"DRand48.conf", std::ios::in);
253 unsigned short cseed[3];
255 if (!inFile.bad() && !inFile.eof()) {
257 for (HepInt i=0; i<3; ++i)
263 void DRand48Engine::showStatus()
const
265 unsigned short dummy[] = { 0, 0, 0 };
266 unsigned short* cseed = seed48(dummy);
268 cout <<
"-------- DRand48 engine status ---------" << endl;
269 cout <<
" Initial seed = " << theSeed << endl;
270 cout <<
" Current seeds = " << cseed[0] <<
", ";
271 cout << cseed[1] <<
", ";
272 cout << cseed[2] << endl;
273 cout <<
"----------------------------------------" << endl;
274 for (HepInt i=0; i<3; ++i)
279 HepDouble DRand48Engine::flat()
288 void DRand48Engine::flatArray(
const HepInt size, HepDouble* vect)
292 for (i=0; i<size; ++i)
297 #ifndef ST_NO_TEMPLATE_DEF_ARGS
298 DRand48Engine::flatArray(vector<HepDouble>& vec)
300 DRand48Engine::flatArray(vector<HepDouble,allocator<HepDouble> >& vec)
303 for (
unsigned int i=0; i<vec.size(); ++i)