BOSS 7.1.1
BESIII Offline Software System
Loading...
Searching...
No Matches
BesEvtGen/BesEvtGen-00-04-28/src/phokhara/PHOKHARA/ranlxd.c
Go to the documentation of this file.
1
2/*******************************************************************************
3*
4* file ranlxd.c
5*
6* Random number generator "ranlxd". See the notes
7*
8* "User's guide for ranlxs and ranlxd v3.0" (May 2001)
9*
10* "Algorithms used in ranlux v3.0" (May 2001)
11*
12* for a detailed description
13*
14* The externally accessible functions are
15*
16* void ranlxd(double r[],int n)
17* Computes the next n single-precision random numbers and
18* assigns them to the elements r[0],...,r[n-1] of the array r[]
19*
20* void rlxd_init(int level,int seed)
21* Initialization of the generator
22*
23* int rlxd_size(void)
24* Returns the number of integers required to save the state of
25* the generator
26*
27* void rlxd_get(int state[])
28* Extracts the current state of the generator and stores the
29* information in the array state[N] where N>=rlxd_size()
30*
31* void rlxd_reset(int state[])
32* Resets the generator to the state defined by the array state[N]
33*
34* Version: 3.0
35* Author: Martin Luescher <[email protected]>
36* Date: 06.05.2001
37*
38*******************************************************************************/
39
40#include <limits.h>
41#include <float.h>
42#include <stdlib.h>
43#include <stdio.h>
44#include <math.h>
45//#include "EvtGenBase/EvtRandom.hh"
46
47#if ((defined SSE)||(defined SSE2))
48
49typedef struct
50{
51 float c1,c2,c3,c4;
52} vec_t __attribute__ ((aligned (16)));
53
54typedef struct
55{
56 vec_t c1,c2;
57} dble_vec_t __attribute__ ((aligned (16)));
58
59static int init=0,pr,prm,ir,jr,is,is_old,next[96];
60static vec_t one,one_bit,carry;
61
62static union
63{
64 dble_vec_t vec[12];
65 float num[96];
66} x __attribute__ ((aligned (16)));
67
68#define STEP(pi,pj) \
69 __asm__ __volatile__ ("movaps %2, %%xmm4 \n\t" \
70 "movaps %%xmm2, %%xmm3 \n\t" \
71 "subps %0, %%xmm4 \n\t" \
72 "movaps %%xmm1, %%xmm5 \n\t" \
73 "cmpps $0x6, %%xmm4, %%xmm2 \n\t" \
74 "andps %%xmm2, %%xmm5 \n\t" \
75 "subps %%xmm3, %%xmm4 \n\t" \
76 "andps %%xmm0, %%xmm2 \n\t" \
77 "addps %%xmm4, %%xmm5 \n\t" \
78 "movaps %%xmm5, %0 \n\t" \
79 "movaps %3, %%xmm6 \n\t" \
80 "movaps %%xmm2, %%xmm3 \n\t" \
81 "subps %1, %%xmm6 \n\t" \
82 "movaps %%xmm1, %%xmm7 \n\t" \
83 "cmpps $0x6, %%xmm6, %%xmm2 \n\t" \
84 "andps %%xmm2, %%xmm7 \n\t" \
85 "subps %%xmm3, %%xmm6 \n\t" \
86 "andps %%xmm0, %%xmm2 \n\t" \
87 "addps %%xmm6, %%xmm7 \n\t" \
88 "movaps %%xmm7, %1" \
89 : \
90 "+m" ((*pi).c1), \
91 "+m" ((*pi).c2) \
92 : \
93 "m" ((*pj).c1), \
94 "m" ((*pj).c2))
95
96
97static void error(int no)
98{
99 switch(no)
100 {
101 case 1:
102 printf("Error in subroutine rlxd_init\n");
103 printf("Bad choice of luxury level (should be 1 or 2)\n");
104 break;
105 case 2:
106 printf("Error in subroutine rlxd_init\n");
107 printf("Bad choice of seed (should be between 1 and 2^31-1)\n");
108 break;
109 case 3:
110 printf("Error in rlxd_get\n");
111 printf("Undefined state (ranlxd is not initialized\n");
112 break;
113 case 5:
114 printf("Error in rlxd_reset\n");
115 printf("Unexpected input data\n");
116 break;
117 }
118 printf("Program aborted\n");
119 exit(0);
120}
121
122
123static void update()
124{
125 int k,kmax;
126 dble_vec_t *pmin,*pmax,*pi,*pj;
127
128 kmax=pr;
129 pmin=&x.vec[0];
130 pmax=pmin+12;
131 pi=&x.vec[ir];
132 pj=&x.vec[jr];
133
134 __asm__ __volatile__ ("movaps %0, %%xmm0 \n\t"
135 "movaps %1, %%xmm1 \n\t"
136 "movaps %2, %%xmm2"
137 :
138 :
139 "m" (one_bit),
140 "m" (one),
141 "m" (carry));
142
143 for (k=0;k<kmax;k++)
144 {
145 STEP(pi,pj);
146 pi+=1;
147 pj+=1;
148 if (pi==pmax)
149 pi=pmin;
150 if (pj==pmax)
151 pj=pmin;
152 }
153
154 __asm__ __volatile__ ("movaps %%xmm2, %0"
155 :
156 :
157 "m" (carry));
158
159 ir+=prm;
160 jr+=prm;
161 if (ir>=12)
162 ir-=12;
163 if (jr>=12)
164 jr-=12;
165 is=8*ir;
166 is_old=is;
167}
168
169
170static void define_constants()
171{
172 int k;
173 float b;
174
175 one.c1=1.0f;
176 one.c2=1.0f;
177 one.c3=1.0f;
178 one.c4=1.0f;
179
180 b=(float)(ldexp(1.0,-24));
181 one_bit.c1=b;
182 one_bit.c2=b;
183 one_bit.c3=b;
184 one_bit.c4=b;
185
186 for (k=0;k<96;k++)
187 {
188 next[k]=(k+1)%96;
189 if ((k%4)==3)
190 next[k]=(k+5)%96;
191 }
192}
193
194
195void rlxd_init(int level,int seed)
196{
197 int i,k,l;
198 int ibit,jbit,xbit[31];
199 int ix,iy;
200
201 define_constants();
202
203 if (level==1)
204 pr=202;
205 else if (level==2)
206 pr=397;
207 else
208 error(1);
209
210 i=seed;
211
212 for (k=0;k<31;k++)
213 {
214 xbit[k]=i%2;
215 i/=2;
216 }
217
218 if ((seed<=0)||(i!=0))
219 error(2);
220
221 ibit=0;
222 jbit=18;
223
224 for (i=0;i<4;i++)
225 {
226 for (k=0;k<24;k++)
227 {
228 ix=0;
229
230 for (l=0;l<24;l++)
231 {
232 iy=xbit[ibit];
233 ix=2*ix+iy;
234
235 xbit[ibit]=(xbit[ibit]+xbit[jbit])%2;
236 ibit=(ibit+1)%31;
237 jbit=(jbit+1)%31;
238 }
239
240 if ((k%4)!=i)
241 ix=16777215-ix;
242
243 x.num[4*k+i]=(float)(ldexp((double)(ix),-24));
244 }
245 }
246
247 carry.c1=0.0f;
248 carry.c2=0.0f;
249 carry.c3=0.0f;
250 carry.c4=0.0f;
251
252 ir=0;
253 jr=7;
254 is=91;
255 is_old=0;
256 prm=pr%12;
257 init=1;
258}
259
260
261//void ranlxd(double r[],int n)
262//{
263// int k;
264//
265// if (init==0)
266// rlxd_init(1,1);
267//
268// for (k=0;k<n;k++)
269// {
270// is=next[is];
271// if (is==is_old)
272// update();
273// r[k]=(double)(x.num[is+4])+(double)(one_bit.c1*x.num[is]);
274// }
275//}
276void ranlxd(double r[],int n)
277{
278 int k;
279 printf("ranlxd works!");
280 for (k=0;k<n;k++)
281 {
282 r[k]=EvtRandom::Flat();
283 }
284}
285
286
287int rlxd_size(void)
288{
289 return(105);
290}
291
292
293void rlxd_get(int state[])
294{
295 int k;
296 float base;
297
298 if (init==0)
299 error(3);
300
301 base=(float)(ldexp(1.0,24));
302 state[0]=rlxd_size();
303
304 for (k=0;k<96;k++)
305 state[k+1]=(int)(base*x.num[k]);
306
307 state[97]=(int)(base*carry.c1);
308 state[98]=(int)(base*carry.c2);
309 state[99]=(int)(base*carry.c3);
310 state[100]=(int)(base*carry.c4);
311
312 state[101]=pr;
313 state[102]=ir;
314 state[103]=jr;
315 state[104]=is;
316}
317
318
319void rlxd_reset(int state[])
320{
321 int k;
322
323 define_constants();
324
325 if (state[0]!=rlxd_size())
326 error(5);
327
328 for (k=0;k<96;k++)
329 {
330 if ((state[k+1]<0)||(state[k+1]>=167777216))
331 error(5);
332
333 x.num[k]=(float)(ldexp((double)(state[k+1]),-24));
334 }
335
336 if (((state[97]!=0)&&(state[97]!=1))||
337 ((state[98]!=0)&&(state[98]!=1))||
338 ((state[99]!=0)&&(state[99]!=1))||
339 ((state[100]!=0)&&(state[100]!=1)))
340 error(5);
341
342 carry.c1=(float)(ldexp((double)(state[97]),-24));
343 carry.c2=(float)(ldexp((double)(state[98]),-24));
344 carry.c3=(float)(ldexp((double)(state[99]),-24));
345 carry.c4=(float)(ldexp((double)(state[100]),-24));
346
347 pr=state[101];
348 ir=state[102];
349 jr=state[103];
350 is=state[104];
351 is_old=8*ir;
352 prm=pr%12;
353 init=1;
354
355 if (((pr!=202)&&(pr!=397))||
356 (ir<0)||(ir>11)||(jr<0)||(jr>11)||(jr!=((ir+7)%12))||
357 (is<0)||(is>91))
358 error(5);
359}
360
361#else
362
363#define BASE 0x1000000
364#define MASK 0xffffff
365
366typedef struct
367{
368 int c1,c2,c3,c4;
369} vec_t;
370
371typedef struct
372{
374} dble_vec_t;
375
376static int init=0,pr,prm,ir,jr,is,is_old,next[96];
377static double one_bit;
378static vec_t carry;
379
380static union
381{
383 int num[96];
384} x;
385
386#define STEP(pi,pj) \
387 d=(*pj).c1.c1-(*pi).c1.c1-carry.c1; \
388 (*pi).c2.c1+=(d<0); \
389 d+=BASE; \
390 (*pi).c1.c1=d&MASK; \
391 d=(*pj).c1.c2-(*pi).c1.c2-carry.c2; \
392 (*pi).c2.c2+=(d<0); \
393 d+=BASE; \
394 (*pi).c1.c2=d&MASK; \
395 d=(*pj).c1.c3-(*pi).c1.c3-carry.c3; \
396 (*pi).c2.c3+=(d<0); \
397 d+=BASE; \
398 (*pi).c1.c3=d&MASK; \
399 d=(*pj).c1.c4-(*pi).c1.c4-carry.c4; \
400 (*pi).c2.c4+=(d<0); \
401 d+=BASE; \
402 (*pi).c1.c4=d&MASK; \
403 d=(*pj).c2.c1-(*pi).c2.c1; \
404 carry.c1=(d<0); \
405 d+=BASE; \
406 (*pi).c2.c1=d&MASK; \
407 d=(*pj).c2.c2-(*pi).c2.c2; \
408 carry.c2=(d<0); \
409 d+=BASE; \
410 (*pi).c2.c2=d&MASK; \
411 d=(*pj).c2.c3-(*pi).c2.c3; \
412 carry.c3=(d<0); \
413 d+=BASE; \
414 (*pi).c2.c3=d&MASK; \
415 d=(*pj).c2.c4-(*pi).c2.c4; \
416 carry.c4=(d<0); \
417 d+=BASE; \
418 (*pi).c2.c4=d&MASK
419
420
421static void error(int no)
422{
423 switch(no)
424 {
425 case 0:
426 printf("Error in rlxd_init\n");
427 printf("Arithmetic on this machine is not suitable for ranlxd\n");
428 break;
429 case 1:
430 printf("Error in subroutine rlxd_init\n");
431 printf("Bad choice of luxury level (should be 1 or 2)\n");
432 break;
433 case 2:
434 printf("Error in subroutine rlxd_init\n");
435 printf("Bad choice of seed (should be between 1 and 2^31-1)\n");
436 break;
437 case 3:
438 printf("Error in rlxd_get\n");
439 printf("Undefined state (ranlxd is not initialized)\n");
440 break;
441 case 4:
442 printf("Error in rlxd_reset\n");
443 printf("Arithmetic on this machine is not suitable for ranlxd\n");
444 break;
445 case 5:
446 printf("Error in rlxd_reset\n");
447 printf("Unexpected input data\n");
448 break;
449 }
450 printf("Program aborted\n");
451 exit(0);
452}
453
454
455static void update()
456{
457 int k,kmax,d;
458 dble_vec_t *pmin,*pmax,*pi,*pj;
459
460 kmax=pr;
461 pmin=&x.vec[0];
462 pmax=pmin+12;
463 pi=&x.vec[ir];
464 pj=&x.vec[jr];
465
466 for (k=0;k<kmax;k++)
467 {
468 STEP(pi,pj);
469 pi+=1;
470 pj+=1;
471 if (pi==pmax)
472 pi=pmin;
473 if (pj==pmax)
474 pj=pmin;
475 }
476
477 ir+=prm;
478 jr+=prm;
479 if (ir>=12)
480 ir-=12;
481 if (jr>=12)
482 jr-=12;
483 is=8*ir;
484 is_old=is;
485}
486
487
488static void define_constants()
489{
490 int k;
491
492 one_bit=ldexp(1.0,-24);
493
494 for (k=0;k<96;k++)
495 {
496 next[k]=(k+1)%96;
497 if ((k%4)==3)
498 next[k]=(k+5)%96;
499 }
500}
501
502
503void rlxd_init(int level,int seed)
504{
505 int i,k,l;
506 int ibit,jbit,xbit[31];
507 int ix,iy;
508
509 if ((INT_MAX<2147483647)||(FLT_RADIX!=2)||(FLT_MANT_DIG<24)||
510 (DBL_MANT_DIG<48))
511 error(0);
512
513 define_constants();
514
515 if (level==1)
516 pr=202;
517 else if (level==2)
518 pr=397;
519 else
520 error(1);
521
522 i=seed;
523
524 for (k=0;k<31;k++)
525 {
526 xbit[k]=i%2;
527 i/=2;
528 }
529
530 if ((seed<=0)||(i!=0))
531 error(2);
532
533 ibit=0;
534 jbit=18;
535
536 for (i=0;i<4;i++)
537 {
538 for (k=0;k<24;k++)
539 {
540 ix=0;
541
542 for (l=0;l<24;l++)
543 {
544 iy=xbit[ibit];
545 ix=2*ix+iy;
546
547 xbit[ibit]=(xbit[ibit]+xbit[jbit])%2;
548 ibit=(ibit+1)%31;
549 jbit=(jbit+1)%31;
550 }
551
552 if ((k%4)!=i)
553 ix=16777215-ix;
554
555 x.num[4*k+i]=ix;
556 }
557 }
558
559 carry.c1=0;
560 carry.c2=0;
561 carry.c3=0;
562 carry.c4=0;
563
564 ir=0;
565 jr=7;
566 is=91;
567 is_old=0;
568 prm=pr%12;
569 init=1;
570}
571
572
573void ranlxd(double r[],int n)
574{
575 int k;
576
577 if (init==0)
578 rlxd_init(0,1);
579
580 for (k=0;k<n;k++)
581 {
582 is=next[is];
583 if (is==is_old)
584 update();
585 r[k]=one_bit*((double)(x.num[is+4])+one_bit*(double)(x.num[is]));
586 }
587}
588//void ranlxd(double r[],int n)
589//{
590// int k;
591// printf("ranlxd works!");
592// for (k=0;k<n;k++)
593// {
594// r[k]=EvtRandom::Flat();
595// }
596//}
597
598
599int rlxd_size(void)
600{
601 return(105);
602}
603
604
605void rlxd_get(int state[])
606{
607 int k;
608
609 if (init==0)
610 error(3);
611
612 state[0]=rlxd_size();
613
614 for (k=0;k<96;k++)
615 state[k+1]=x.num[k];
616
617 state[97]=carry.c1;
618 state[98]=carry.c2;
619 state[99]=carry.c3;
620 state[100]=carry.c4;
621
622 state[101]=pr;
623 state[102]=ir;
624 state[103]=jr;
625 state[104]=is;
626}
627
628
629void rlxd_reset(int state[])
630{
631 int k;
632
633 if ((INT_MAX<2147483647)||(FLT_RADIX!=2)||(FLT_MANT_DIG<24)||
634 (DBL_MANT_DIG<48))
635 error(4);
636
637 define_constants();
638
639 if (state[0]!=rlxd_size())
640 error(5);
641
642 for (k=0;k<96;k++)
643 {
644 if ((state[k+1]<0)||(state[k+1]>=167777216))
645 error(5);
646
647 x.num[k]=state[k+1];
648 }
649
650 if (((state[97]!=0)&&(state[97]!=1))||
651 ((state[98]!=0)&&(state[98]!=1))||
652 ((state[99]!=0)&&(state[99]!=1))||
653 ((state[100]!=0)&&(state[100]!=1)))
654 error(5);
655
656 carry.c1=state[97];
657 carry.c2=state[98];
658 carry.c3=state[99];
659 carry.c4=state[100];
660
661 pr=state[101];
662 ir=state[102];
663 jr=state[103];
664 is=state[104];
665 is_old=8*ir;
666 prm=pr%12;
667 init=1;
668
669 if (((pr!=202)&&(pr!=397))||
670 (ir<0)||(ir>11)||(jr<0)||(jr>11)||(jr!=((ir+7)%12))||
671 (is<0)||(is>91))
672 error(5);
673}
674
675#endif
676
const int no
const Int_t n
static double Flat()
Definition EvtRandom.cc:74
@ error
Definition Core.h:24
double precision pisqo6 one
Definition qlconstants.h:4
const double b
Definition slope.cxx:9
const float pi
Definition vector3.h:133