Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Define correctly variables for windows.
[simgrid.git] / src / surf / random_mgr.c
1 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5   * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include "surf/random_mgr.h"
8 #include "xbt/sysdep.h"
9
10 #ifdef _XBT_WIN32
11
12 static unsigned int _seed = 2147483647;
13
14 typedef unsigned __int64 uint64_t;
15 typedef unsigned int uint32_t;
16
17 struct drand48_data {
18   unsigned short int __x[3];    /* Current state.  */
19   unsigned short int __old_x[3];        /* Old state.  */
20   unsigned short int __c;       /* Additive const. in congruential formula.  */
21   unsigned short int __init;    /* Flag for initializing.  */
22   unsigned long long int __a;   /* Factor in congruential formula.  */
23 };
24
25 static struct drand48_data __libc_drand48_data = { 0 };
26
27 union ieee754_double {
28   double d;
29
30   /* This is the IEEE 754 double-precision format.  */
31   struct {
32     /* Together these comprise the mantissa.  */
33     unsigned int mantissa1:32;
34     unsigned int mantissa0:20;
35     unsigned int exponent:11;
36     unsigned int negative:1;
37     /* Little endian.  */
38   } ieee;
39
40   /* This format makes it easier to see if a NaN is a signalling NaN.  */
41   struct {
42     /* Together these comprise the mantissa.  */
43     unsigned int mantissa1:32;
44     unsigned int mantissa0:19;
45     unsigned int quiet_nan:1;
46     unsigned int exponent:11;
47     unsigned int negative:1;
48
49   } ieee_nan;
50 };
51
52 #define IEEE754_DOUBLE_BIAS     0x3ff   /* Added to exponent.  */
53
54 double drand48(void);
55
56 int
57 _drand48_iterate(unsigned short int xsubi[3], struct drand48_data *buffer);
58
59 int
60 _erand48_r(unsigned short int xsubi[3], struct drand48_data *buffer,
61            double *result);
62
63
64 int
65 _erand48_r(unsigned short int xsubi[3], struct drand48_data *buffer,
66            double *result)
67 {
68   union ieee754_double temp;
69
70   /* Compute next state.  */
71   if (_drand48_iterate(xsubi, buffer) < 0)
72     return -1;
73
74   /* Construct a positive double with the 48 random bits distributed over
75      its fractional part so the resulting FP number is [0.0,1.0).  */
76
77   temp.ieee.negative = 0;
78   temp.ieee.exponent = IEEE754_DOUBLE_BIAS;
79   temp.ieee.mantissa0 = (xsubi[2] << 4) | (xsubi[1] >> 12);
80   temp.ieee.mantissa1 = ((xsubi[1] & 0xfff) << 20) | (xsubi[0] << 4);
81
82   /* Please note the lower 4 bits of mantissa1 are always 0.  */
83   *result = temp.d - 1.0;
84
85   return 0;
86 }
87
88 int _drand48_iterate(unsigned short int xsubi[3], struct drand48_data *buffer)
89 {
90   uint64_t X;
91   uint64_t result;
92
93   /* Initialize buffer, if not yet done.  */
94
95   if (buffer->__init == 0) {
96     buffer->__a = 0x5deece66dull;
97     buffer->__c = 0xb;
98     buffer->__init = 1;
99   }
100
101   /* Do the real work.  We choose a data type which contains at least
102      48 bits.  Because we compute the modulus it does not care how
103      many bits really are computed.  */
104
105   X = (uint64_t) xsubi[2] << 32 | (uint32_t) xsubi[1] << 16 | xsubi[0];
106
107   result = X * buffer->__a + buffer->__c;
108
109
110   xsubi[0] = result & 0xffff;
111   xsubi[1] = (result >> 16) & 0xffff;
112   xsubi[2] = (result >> 32) & 0xffff;
113
114   return 0;
115 }
116
117
118 double _drand48(void)
119 {
120   double result;
121
122   (void) _erand48_r(__libc_drand48_data.__x, &__libc_drand48_data, &result);
123
124   return result;
125 }
126
127 void _srand(unsigned int seed)
128 {
129   _seed = seed;
130 }
131
132 int _rand(void)
133 {
134   const long a = 16807;
135   const long m = 2147483647;
136   const long q = 127773;        /* (m/a) */
137   const long r = 2836;          /* (m%a) */
138
139   long lo, k, s;
140
141   s = (long) _seed;
142
143   k = (long) (s / q);
144
145   lo = (s - q * k);
146
147   s = a * lo - r * k;
148
149   if (s <= 0)
150     s += m;
151
152   _seed = (int) (s & RAND_MAX);
153
154   return _seed;
155 }
156
157 int _rand_r(unsigned int *pseed)
158 {
159   const long a = 16807;
160   const long m = 2147483647;
161   const long q = 127773;        /* (m/a) */
162   const long r = 2836;          /* (m%a) */
163
164   long lo, k, s;
165
166   s = (long) *pseed;
167
168   k = (long) (s / q);
169
170   lo = (s - q * k);
171
172   s = a * lo - r * k;
173
174   if (s <= 0)
175     s += m;
176
177   return (int) (s & RAND_MAX);
178
179 }
180
181
182 #define rand_r _rand_r
183 #define drand48 _drand48
184
185 #endif
186
187 static double custom_random(Generator generator, long int *seed)
188 {
189   switch (generator) {
190
191   case DRAND48:
192     return drand48();
193   case RAND:
194     return (double) rand_r((unsigned int *) seed) / RAND_MAX;
195   default:
196     return drand48();
197   }
198 }
199
200 /* Generate numbers between min and max with a given mean and standard deviation */
201 double random_generate(random_data_t random)
202 {
203   double a, b;
204   double alpha, beta, gamma;
205   double U1, U2, V, W, X;
206
207   if (random == NULL)
208     return 0.0f;
209
210   if (random->std == 0)
211     return random->mean * (random->max - random->min) + random->min;
212
213   a =
214     random->mean * (random->mean * (1 - random->mean) /
215                     (random->std * random->std) - 1);
216   b =
217     (1 -
218      random->mean) * (random->mean * (1 -
219                                       random->mean) / (random->std *
220                                                        random->std) - 1);
221
222   alpha = a + b;
223   if (a <= 1. || b <= 1.)
224     beta = ((1. / a) > (1. / b)) ? (1. / a) : (1. / b);
225   else
226     beta = sqrt((alpha - 2.) / (2. * a * b - alpha));
227   gamma = a + 1. / beta;
228
229   do {
230     /* Random generation for the Beta distribution based on
231      *   R. C. H. Cheng (1978). Generating beta variates with nonintegral shape parameters. _Communications of the ACM_, *21*, 317-322.
232      *   It is good for speed because it does not call math functions many times and respect the 4 given constraints
233      */
234     U1 = custom_random(random->generator, &(random->seed));
235     U2 = custom_random(random->generator, &(random->seed));
236
237     V = beta * log(U1 / (1 - U1));
238     W = a * exp(V);
239   } while (alpha * log(alpha / (b + W)) + gamma * V - log(4) <
240            log(U1 * U1 * U2));
241
242   X = W / (b + W);
243
244   return X * (random->max - random->min) + random->min;
245 }
246
247 random_data_t random_new(Generator generator, long int seed,
248                          double min, double max, double mean, double std)
249 {
250   random_data_t random = xbt_new0(s_random_data_t, 1);
251
252   random->generator = generator;
253   random->seed = seed;
254   random->min = min;
255   random->max = max;
256
257   /* Check user stupidities */
258   if (max < min)
259     THROW2(arg_error, 0, "random->max < random->min (%f < %f)", max, min);
260   if (mean < min)
261     THROW2(arg_error, 0, "random->mean < random->min (%f < %f)", mean, min);
262   if (mean > max)
263     THROW2(arg_error, 0, "random->mean > random->max (%f > %f)", mean, max);
264
265   /* normalize the mean and standard deviation before storing */
266   random->mean = (mean - min) / (max - min);
267   random->std = std / (max - min);
268
269   if (random->mean * (1 - random->mean) < random->std * random->std)
270     THROW2(arg_error, 0, "Invalid mean and standard deviation (%f and %f)",
271            random->mean, random->std);
272
273   return random;
274 }