xref: /petsc/src/sys/classes/random/impls/rander48/rander48.c (revision 6a5217c03994f2d95bb2e6dbd8bed42381aeb015)
1 #include <petsc/private/randomimpl.h>
2 
3 typedef struct {
4   unsigned short seed[3];
5   unsigned short mult[3];
6   unsigned short add;
7 } PetscRandom_Rander48;
8 
9 #define RANDER48_SEED_0 (0x330e)
10 #define RANDER48_SEED_1 (0xabcd)
11 #define RANDER48_SEED_2 (0x1234)
12 #define RANDER48_MULT_0 (0xe66d)
13 #define RANDER48_MULT_1 (0xdeec)
14 #define RANDER48_MULT_2 (0x0005)
15 #define RANDER48_ADD    (0x000b)
16 
17 static double _dorander48(PetscRandom_Rander48 *r48)
18 {
19   unsigned long accu;
20   unsigned short temp[2];
21 
22   accu     = (unsigned long) r48->mult[0] * (unsigned long) r48->seed[0] + (unsigned long)r48->add;
23   temp[0]  = (unsigned short) accu;        /* lower 16 bits */
24   accu   >>= sizeof(unsigned short) * 8;
25   accu    += (unsigned long) r48->mult[0] * (unsigned long) r48->seed[1] + (unsigned long) r48->mult[1] * (unsigned long) r48->seed[0];
26   temp[1]  = (unsigned short)accu;        /* middle 16 bits */
27   accu   >>= sizeof(unsigned short) * 8;
28   accu    += r48->mult[0] * r48->seed[2] + r48->mult[1] * r48->seed[1] + r48->mult[2] * r48->seed[0];
29   r48->seed[0] = temp[0];
30   r48->seed[1] = temp[1];
31   r48->seed[2] = (unsigned short) accu;
32   return ldexp((double) r48->seed[0], -48) + ldexp((double) r48->seed[1], -32) + ldexp((double) r48->seed[2], -16);
33 }
34 
35 static PetscErrorCode  PetscRandomSeed_Rander48(PetscRandom r)
36 {
37   PetscRandom_Rander48 *r48 = (PetscRandom_Rander48*)r->data;
38 
39   PetscFunctionBegin;
40   r48->seed[0] = RANDER48_SEED_0;
41   r48->seed[1] = (unsigned short) r->seed;
42   r48->seed[2] = (unsigned short) (r->seed >> 16);
43   r48->mult[0] = RANDER48_MULT_0;
44   r48->mult[1] = RANDER48_MULT_1;
45   r48->mult[2] = RANDER48_MULT_2;
46   r48->add     = RANDER48_ADD;
47   PetscFunctionReturn(0);
48 }
49 
50 static PetscErrorCode  PetscRandomGetValue_Rander48(PetscRandom r, PetscScalar *val)
51 {
52   PetscRandom_Rander48 *r48 = (PetscRandom_Rander48*)r->data;
53 
54   PetscFunctionBegin;
55 #if defined(PETSC_USE_COMPLEX)
56   if (r->iset) {
57     *val = PetscRealPart(r->low) + PetscImaginaryPart(r->low) * PETSC_i;
58     if (PetscRealPart(r->width)) {
59       *val += PetscRealPart(r->width)* _dorander48(r48);
60     }
61     if (PetscImaginaryPart(r->width)) {
62       *val += PetscImaginaryPart(r->width)* _dorander48(r48) * PETSC_i;
63     }
64   } else {
65     *val = _dorander48(r48) +  _dorander48(r48)*PETSC_i;
66   }
67 #else
68   if (r->iset) *val = r->width * _dorander48(r48) + r->low;
69   else         *val = _dorander48(r48);
70 #endif
71   PetscFunctionReturn(0);
72 }
73 
74 static PetscErrorCode  PetscRandomGetValueReal_Rander48(PetscRandom r, PetscReal *val)
75 {
76   PetscRandom_Rander48 *r48 = (PetscRandom_Rander48*)r->data;
77 
78   PetscFunctionBegin;
79 #if defined(PETSC_USE_COMPLEX)
80   if (r->iset) *val = PetscRealPart(r->width)*_dorander48(r48) + PetscRealPart(r->low);
81   else         *val = _dorander48(r48);
82 #else
83   if (r->iset) *val = r->width * _dorander48(r48) + r->low;
84   else         *val = _dorander48(r48);
85 #endif
86   PetscFunctionReturn(0);
87 }
88 
89 static PetscErrorCode  PetscRandomDestroy_Rander48(PetscRandom r)
90 {
91   PetscFunctionBegin;
92   PetscCall(PetscFree(r->data));
93   PetscFunctionReturn(0);
94 }
95 
96 static struct _PetscRandomOps PetscRandomOps_Values = {
97   PetscDesignatedInitializer(seed,PetscRandomSeed_Rander48),
98   PetscDesignatedInitializer(getvalue,PetscRandomGetValue_Rander48),
99   PetscDesignatedInitializer(getvaluereal,PetscRandomGetValueReal_Rander48),
100   PetscDesignatedInitializer(getvalues,NULL),
101   PetscDesignatedInitializer(getvaluesreal,NULL),
102   PetscDesignatedInitializer(destroy,PetscRandomDestroy_Rander48),
103 };
104 
105 /*MC
106    PETSCRANDER48 - simple portable reimplementation of basic Unix drand48() random number generator that should generate the
107         exact same random numbers on any system.
108 
109    Options Database Keys:
110 . -random_type <rand,rand48,rander48,sprng>
111 
112   Notes:
113     This is the default random number generate provided by PetscRandomCreate() if you do not set a particular implementation.
114 
115   Each PetscRandom object created with this type has its own seed and its own history, so multiple PetscRandom objects of this type
116   will not interfer with random numbers generated by other objects. Each PETSc object of this type will produce the exact same set of
117   random numbers so if you wish different PetscObjects of this type set different seeds for each one after you create them with
118   PetscRandomSetSeed() followed by PetscRandomSeed().
119 
120   Level: beginner
121 
122 .seealso: PetscRandomCreate(), PetscRandomSetType(), PETSCRAND, PETSCRAND48, PETSCRANDER48, PETSCSPRNG, PetscRandomSetSeed(), PetscRandomSeed()
123 M*/
124 
125 PETSC_EXTERN PetscErrorCode PetscRandomCreate_Rander48(PetscRandom r)
126 {
127   PetscRandom_Rander48 *r48;
128 
129   PetscFunctionBegin;
130   PetscCall(PetscNewLog(r,&r48));
131   /* r48 does not need to be initialized because PetscRandomSeed() is always called before use and sets the needed values */
132   r->data = r48;
133   PetscCall(PetscMemcpy(r->ops, &PetscRandomOps_Values, sizeof(PetscRandomOps_Values)));
134   PetscCall(PetscObjectChangeTypeName((PetscObject) r, PETSCRANDER48));
135   PetscFunctionReturn(0);
136 }
137